-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab6.cpp
122 lines (97 loc) · 1.99 KB
/
Lab6.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include<iostream>
#include<Windows.h>
#include<conio.h>
using namespace std;
void setcursor(bool visible) {
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO lpCursor;
lpCursor.bVisible = visible;
lpCursor.dwSize = 20;
SetConsoleCursorInfo(console, &lpCursor);
}
void setcolor(int fg, int bg) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, (bg * 16) + fg);
}
void gotoxy(int x, int y) {
COORD c = { x,y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void drawShip(int x, int y) {
gotoxy(x, y);
setcolor(2, 4);
cout << "<-0->";
}
void eraseShip(int x, int y) {
gotoxy(x, y);
setcolor(0, 0);
cout << " ";
}
void drawBullet(int x,int y) {
gotoxy(x, y);
setcolor(2, 0);
cout << " ^ ";
}
void eraseBullet(int x ,int y) {
gotoxy(x, y);
setcolor(2, 0);
cout << " ";
}
int main() {
setcursor(0);
char ch = ' ';
int x = 38, y = 20;
bool isLpress = 0, isRpress = 0;
int Bullet[5] = {0,0,0,0,0};
int Posx[5];
int Posy[5];
do {
drawShip(x, y);
if (_kbhit()) {
ch = _getch();
if (ch == 'a') {
isLpress = 1;
isRpress = 0;
}
if (ch == 'd') {
isRpress = 1;
isLpress = 0;
}
if (ch == 's') {
isLpress = 0;
isRpress = 0;
}
if (ch == ' ' ) {
for (int i = 0; i < 5; i++) {
if (Bullet[i] == 0) {
Bullet[i] = 1;
Posx[i] = x;
Posy[i] = y - 1;
break;
}
}
}
fflush(stdin);
}
for (int i = 0; i < 5; i++) {
if (Bullet[i] == 1) {
eraseBullet(Posx[i], Posy[i]);
drawBullet(Posx[i], --Posy[i]);
}
if (Posy[i] == 0) {
Bullet[i] = 0;
eraseBullet(Posx[i], Posy[i]);
}
}
if (isLpress && x>=1) {
eraseShip(x, y);
drawShip(--x, y);
}
if (isRpress && x<=79) {
eraseShip(x, y);
drawShip(++x, y);
}
Sleep(100);
} while (ch != 'x');
return 0;
}