Skip to content

Commit 8a20732

Browse files
authored
Add lab8
1 parent a872f65 commit 8a20732

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

Lab8.cpp

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#include <iostream>
2+
#include <windows.h>
3+
#include <time.h>
4+
5+
#define scount 80
6+
#define screen_x 80
7+
#define screen_y 25
8+
9+
using namespace std;
10+
11+
HANDLE wHnd;
12+
HANDLE rHnd;
13+
HANDLE Hnd;
14+
CHAR_INFO consoleBuffer[screen_x * screen_y];
15+
COORD bufferSize = { screen_x,screen_y };
16+
COORD characterPos = { 0,0 };
17+
SMALL_RECT windowSize = { 0,0,screen_x - 1,screen_y - 1 };
18+
COORD star[scount];
19+
DWORD fdwMode;
20+
21+
22+
void setcursor(bool visible) {
23+
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
24+
CONSOLE_CURSOR_INFO lpCursor;
25+
lpCursor.bVisible = visible;
26+
lpCursor.dwSize = 20;
27+
SetConsoleCursorInfo(console, &lpCursor);
28+
}
29+
30+
void gotoxy(int x, int y) {
31+
COORD c = { x,y };
32+
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
33+
}
34+
int setConsole(int x, int y) {
35+
wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
36+
SetConsoleWindowInfo(wHnd, TRUE, &windowSize);
37+
SetConsoleScreenBufferSize(wHnd, bufferSize);
38+
return 0;
39+
}
40+
41+
int setMode() {
42+
rHnd = GetStdHandle(STD_INPUT_HANDLE);
43+
fdwMode = ENABLE_EXTENDED_FLAGS | ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;
44+
SetConsoleMode(rHnd,fdwMode);
45+
return 0;
46+
}
47+
48+
49+
void fill_data_to_buffer(char ch,int color) {
50+
for (int y = 0; y < screen_y; ++y) {
51+
for (int x = 0; x < screen_x; ++x) {
52+
consoleBuffer[x + screen_x * y].Char.AsciiChar = ch;
53+
consoleBuffer[x + screen_x * y].Attributes = color;
54+
}
55+
}
56+
}
57+
58+
void fill_buffer_to_console() {
59+
WriteConsoleOutputA(wHnd, consoleBuffer, bufferSize, characterPos, &windowSize);
60+
}
61+
62+
void init_star() {
63+
for (int i = 0; i < scount; i++) {
64+
star[i].X = rand() % screen_x;
65+
star[i].Y = rand() % screen_y;
66+
}
67+
}
68+
69+
void clear_buffer() {
70+
for (int y = 0; y < screen_y; ++y) {
71+
for (int x = 0; x < screen_x; ++x) {
72+
consoleBuffer[x + screen_x * y].Char.AsciiChar = ' ';
73+
consoleBuffer[x + screen_x * y].Attributes = 0;
74+
}
75+
}
76+
}
77+
78+
void fill_star_to_buffer() {
79+
for (int i = 0; i < scount; i++) {
80+
consoleBuffer[star[i].X + screen_x * star[i].Y].Char.AsciiChar = '*';
81+
consoleBuffer[star[i].X + screen_x * star[i].Y].Attributes = 7;
82+
}
83+
}
84+
85+
void star_fall() {
86+
for (int i = 0; i < scount; i++) {
87+
if (star[i].Y >= screen_y - 1) {
88+
star[i] = { (rand() % screen_x),1 };
89+
}
90+
else {
91+
star[i] = { star[i].X,star[i].Y + 1 };
92+
}
93+
}
94+
}
95+
96+
void draw(char strChar[], int posx,int posy, unsigned short color) {
97+
int x = posx;
98+
int y = posy;
99+
for (size_t i = 0; i < strlen(strChar); i++){
100+
consoleBuffer[x + screen_x * y].Char.AsciiChar = strChar[i];
101+
consoleBuffer[x + screen_x * y].Attributes = color;
102+
x++;
103+
}
104+
}
105+
106+
107+
int main() {
108+
srand(time(NULL));
109+
setConsole(screen_x, screen_y);
110+
setMode();
111+
init_star();
112+
setcursor(0);
113+
char strChar[6] = "<-0->";
114+
bool play = true;
115+
DWORD numEvents = 0;
116+
DWORD numEventsRead = 0;
117+
int posx = 0;
118+
int posy = 0;
119+
int collision = 10;
120+
unsigned int randomcolor = 7;
121+
while (play && collision != 0)
122+
{
123+
124+
GetNumberOfConsoleInputEvents(rHnd, &numEvents);
125+
if (numEvents != 0) {
126+
INPUT_RECORD* eventBuffer = new INPUT_RECORD[numEvents];
127+
ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);
128+
129+
for (DWORD i = 0; i < numEventsRead; ++i) {
130+
131+
if (eventBuffer[i].EventType == KEY_EVENT && eventBuffer[i].Event.KeyEvent.bKeyDown == true) {
132+
if (eventBuffer[i].Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE) {
133+
play = false;
134+
}
135+
if (eventBuffer[i].Event.KeyEvent.uChar.AsciiChar == 'c') {
136+
randomcolor = rand() % 255;
137+
}
138+
}
139+
else if (eventBuffer[i].EventType == MOUSE_EVENT) {
140+
posx = eventBuffer[i].Event.MouseEvent.dwMousePosition.X;
141+
posy = eventBuffer[i].Event.MouseEvent.dwMousePosition.Y;
142+
if (eventBuffer[i].Event.MouseEvent.dwButtonState &
143+
FROM_LEFT_1ST_BUTTON_PRESSED) {
144+
randomcolor = rand() % 255;
145+
}
146+
else if (eventBuffer[i].Event.MouseEvent.dwButtonState & RIGHTMOST_BUTTON_PRESSED) {
147+
//printf("right click\n");
148+
}
149+
150+
else if (eventBuffer[i].Event.MouseEvent.dwEventFlags & MOUSE_MOVED) {
151+
if (posx <= 0) posx = 0;
152+
if (posx >= 75) posx = 75;
153+
if (posy <= 0) posy = 0;
154+
if (posy >= 25) posy = 25;
155+
}
156+
}
157+
}
158+
159+
delete[] eventBuffer;
160+
}
161+
for (int i = 0; i < scount; i++) {
162+
if (star[i].X >= posx && star[i].X < posx + 5 && star[i].Y == posy) {
163+
collision--;
164+
star[i].X = rand() % 80;
165+
star[i].Y = rand() % 25;
166+
}
167+
}
168+
169+
star_fall();
170+
clear_buffer();
171+
draw(strChar, posx, posy, randomcolor);
172+
fill_star_to_buffer();
173+
fill_buffer_to_console();
174+
Sleep(200);
175+
}
176+
177+
return 0;
178+
}

0 commit comments

Comments
 (0)