Skip to content

Commit a872f65

Browse files
authored
Add lab 7
1 parent 541b572 commit a872f65

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

Lab7.cpp

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#include<iostream>
2+
#include<Windows.h>
3+
#include<conio.h>
4+
#include <time.h>
5+
6+
using namespace std;
7+
8+
9+
void setcursor(bool visible) {
10+
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
11+
CONSOLE_CURSOR_INFO lpCursor;
12+
lpCursor.bVisible = visible;
13+
lpCursor.dwSize = 20;
14+
SetConsoleCursorInfo(console, &lpCursor);
15+
}
16+
17+
void setcolor(int fg, int bg) {
18+
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
19+
SetConsoleTextAttribute(hConsole, (bg * 16) + fg);
20+
}
21+
void gotoxy(int x, int y) {
22+
23+
COORD c = { x,y };
24+
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
25+
26+
}
27+
28+
char cursor(int x, int y) {
29+
HANDLE hStd = GetStdHandle(STD_OUTPUT_HANDLE);
30+
char buf[2];
31+
COORD c = { x,y };
32+
DWORD num_read;
33+
if (!ReadConsoleOutputCharacter(hStd,(LPTSTR)buf,1,c,(LPDWORD)&num_read)) {
34+
return '\0';
35+
}
36+
else {
37+
return buf[0];
38+
}
39+
}
40+
void drawShip(int x, int y) {
41+
42+
gotoxy(x, y);
43+
setcolor(2, 4);
44+
cout << "<-0->";
45+
}
46+
void eraseShip(int x, int y) {
47+
48+
gotoxy(x, y);
49+
setcolor(0, 0);
50+
cout << " ";
51+
52+
}
53+
54+
void eraseStar(int x,int y) {
55+
gotoxy(x, y);
56+
cout << " ";
57+
}
58+
59+
void drawBullet(int x, int y) {
60+
gotoxy(x, y);
61+
setcolor(2, 0);
62+
cout << "^";
63+
}
64+
65+
void eraseBullet(int x, int y) {
66+
gotoxy(x, y);
67+
setcolor(2, 0);
68+
cout << " ";
69+
}
70+
71+
void drawStar(int x, int y) {
72+
gotoxy(x, y);
73+
setcolor(7, 0);
74+
cout << "*";
75+
}
76+
77+
struct Star {
78+
int x;
79+
int y;
80+
};
81+
82+
83+
int main() {
84+
85+
setcursor(0);
86+
srand(time(NULL));
87+
Star star[20];
88+
int count_star = 0;
89+
char ch = ' ';
90+
int x = 38, y = 20;
91+
bool isLpress = 0, isRpress = 0;
92+
int Bullet[5] = { 0,0,0,0,0 };
93+
int Posx[5];
94+
int Posy[5];
95+
int score = 0;
96+
97+
for (int i = 0; i < 20; i++) {
98+
star[i].x = rand() % (70+1-10)+10;
99+
star[i].y = rand() % (5+1-2)+2;
100+
}
101+
102+
103+
do {
104+
105+
drawShip(x, y);
106+
if (_kbhit()) {
107+
ch = _getch();
108+
if (ch == 'a') {
109+
isLpress = 1;
110+
isRpress = 0;
111+
}
112+
if (ch == 'd') {
113+
isRpress = 1;
114+
isLpress = 0;
115+
116+
}
117+
if (ch == 's') {
118+
isLpress = 0;
119+
isRpress = 0;
120+
}
121+
if (ch == ' ') {
122+
for (int i = 0; i < 5; i++) {
123+
if (Bullet[i] == 0) {
124+
Bullet[i] = 1;
125+
Posx[i] = x;
126+
Posy[i] = y - 1;
127+
Beep(700, 100);
128+
break;
129+
}
130+
}
131+
}
132+
fflush(stdin);
133+
}
134+
setcolor(7, 0);
135+
gotoxy(75, 0); cout << "Score: " << score;
136+
137+
for (int i = 0; i < 20;i++) {
138+
if(star[i].x != 0 && star[i].y != 0) drawStar(star[i].x,star[i].y);
139+
}
140+
for (int i = 0; i < 5; i++) {
141+
if (Bullet[i] == 1) {
142+
eraseBullet(Posx[i]+2, Posy[i]);
143+
drawBullet(Posx[i]+2, --Posy[i]);
144+
}
145+
if (Posy[i] == 0) {
146+
Bullet[i] = 0;
147+
eraseBullet(Posx[i]+2, Posy[i]);
148+
}
149+
150+
}
151+
152+
for (int i = 0; i < 20; i++) {
153+
for (int j = 0; j < 5; j++) {
154+
if (star[i].x == Posx[j]+2 && star[i].y == Posy[j]) {
155+
eraseStar(star[i].x, star[i].y);
156+
star[i].x = rand() % (70 + 1 - 10) + 10;
157+
star[i].y = rand() % (5 + 1 - 2) + 2;
158+
score += 10;
159+
Beep(700, 100);
160+
}
161+
}
162+
}
163+
164+
if (isLpress && x >= 1) {
165+
eraseShip(x, y);
166+
drawShip(--x, y);
167+
}
168+
169+
if (isRpress && x <= 79) {
170+
eraseShip(x, y);
171+
drawShip(++x, y);
172+
}
173+
setcolor(7, 0);
174+
Sleep(100);
175+
} while (ch != 'x');
176+
177+
return 0;
178+
}

0 commit comments

Comments
 (0)