-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab8.cpp
178 lines (154 loc) · 4.48 KB
/
Lab8.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <windows.h>
#include <time.h>
#define scount 80
#define screen_x 80
#define screen_y 25
using namespace std;
HANDLE wHnd;
HANDLE rHnd;
HANDLE Hnd;
CHAR_INFO consoleBuffer[screen_x * screen_y];
COORD bufferSize = { screen_x,screen_y };
COORD characterPos = { 0,0 };
SMALL_RECT windowSize = { 0,0,screen_x - 1,screen_y - 1 };
COORD star[scount];
DWORD fdwMode;
void setcursor(bool visible) {
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO lpCursor;
lpCursor.bVisible = visible;
lpCursor.dwSize = 20;
SetConsoleCursorInfo(console, &lpCursor);
}
void gotoxy(int x, int y) {
COORD c = { x,y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
int setConsole(int x, int y) {
wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleWindowInfo(wHnd, TRUE, &windowSize);
SetConsoleScreenBufferSize(wHnd, bufferSize);
return 0;
}
int setMode() {
rHnd = GetStdHandle(STD_INPUT_HANDLE);
fdwMode = ENABLE_EXTENDED_FLAGS | ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
SetConsoleMode(rHnd,fdwMode);
return 0;
}
void fill_data_to_buffer(char ch,int color) {
for (int y = 0; y < screen_y; ++y) {
for (int x = 0; x < screen_x; ++x) {
consoleBuffer[x + screen_x * y].Char.AsciiChar = ch;
consoleBuffer[x + screen_x * y].Attributes = color;
}
}
}
void fill_buffer_to_console() {
WriteConsoleOutputA(wHnd, consoleBuffer, bufferSize, characterPos, &windowSize);
}
void init_star() {
for (int i = 0; i < scount; i++) {
star[i].X = rand() % screen_x;
star[i].Y = rand() % screen_y;
}
}
void clear_buffer() {
for (int y = 0; y < screen_y; ++y) {
for (int x = 0; x < screen_x; ++x) {
consoleBuffer[x + screen_x * y].Char.AsciiChar = ' ';
consoleBuffer[x + screen_x * y].Attributes = 0;
}
}
}
void fill_star_to_buffer() {
for (int i = 0; i < scount; i++) {
consoleBuffer[star[i].X + screen_x * star[i].Y].Char.AsciiChar = '*';
consoleBuffer[star[i].X + screen_x * star[i].Y].Attributes = 7;
}
}
void star_fall() {
for (int i = 0; i < scount; i++) {
if (star[i].Y >= screen_y - 1) {
star[i] = { (rand() % screen_x),1 };
}
else {
star[i] = { star[i].X,star[i].Y + 1 };
}
}
}
void draw(char strChar[], int posx,int posy, unsigned short color) {
int x = posx;
int y = posy;
for (size_t i = 0; i < strlen(strChar); i++){
consoleBuffer[x + screen_x * y].Char.AsciiChar = strChar[i];
consoleBuffer[x + screen_x * y].Attributes = color;
x++;
}
}
int main() {
srand(time(NULL));
setConsole(screen_x, screen_y);
setMode();
init_star();
setcursor(0);
char strChar[6] = "<-0->";
bool play = true;
DWORD numEvents = 0;
DWORD numEventsRead = 0;
int posx = 0;
int posy = 0;
int collision = 10;
unsigned int randomcolor = 7;
while (play && collision != 0)
{
GetNumberOfConsoleInputEvents(rHnd, &numEvents);
if (numEvents != 0) {
INPUT_RECORD* eventBuffer = new INPUT_RECORD[numEvents];
ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);
for (DWORD i = 0; i < numEventsRead; ++i) {
if (eventBuffer[i].EventType == KEY_EVENT && eventBuffer[i].Event.KeyEvent.bKeyDown == true) {
if (eventBuffer[i].Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE) {
play = false;
}
if (eventBuffer[i].Event.KeyEvent.uChar.AsciiChar == 'c') {
randomcolor = rand() % 255;
}
}
else if (eventBuffer[i].EventType == MOUSE_EVENT) {
posx = eventBuffer[i].Event.MouseEvent.dwMousePosition.X;
posy = eventBuffer[i].Event.MouseEvent.dwMousePosition.Y;
if (eventBuffer[i].Event.MouseEvent.dwButtonState &
FROM_LEFT_1ST_BUTTON_PRESSED) {
randomcolor = rand() % 255;
}
else if (eventBuffer[i].Event.MouseEvent.dwButtonState & RIGHTMOST_BUTTON_PRESSED) {
//printf("right click\n");
}
else if (eventBuffer[i].Event.MouseEvent.dwEventFlags & MOUSE_MOVED) {
if (posx <= 0) posx = 0;
if (posx >= 75) posx = 75;
if (posy <= 0) posy = 0;
if (posy >= 25) posy = 25;
}
}
}
delete[] eventBuffer;
}
for (int i = 0; i < scount; i++) {
if (star[i].X >= posx && star[i].X < posx + 5 && star[i].Y == posy) {
collision--;
star[i].X = rand() % 80;
star[i].Y = rand() % 25;
}
}
star_fall();
clear_buffer();
draw(strChar, posx, posy, randomcolor);
fill_star_to_buffer();
fill_buffer_to_console();
Sleep(200);
}
return 0;
}