-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabAssignment8.c
More file actions
177 lines (166 loc) · 4.18 KB
/
LabAssignment8.c
File metadata and controls
177 lines (166 loc) · 4.18 KB
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
#include <stdio.h>
#include <windows.h>
#include <time.h>
#define scount 80
#define screen_x 80
#define screen_y 25
HANDLE wHnd;
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];
HANDLE rHnd;
DWORD fdwMode;
int x , y ;
int color=7;
int hp = 10;
int setConsole(int x, int y)
{
wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleWindowInfo(wHnd, TRUE, &windowSize);
SetConsoleScreenBufferSize(wHnd, bufferSize);
return 0;
}
void draw_ship(int posx,int posy)
{
consoleBuffer[posx + screen_x * posy].Char.AsciiChar = '<';
consoleBuffer[posx + screen_x * posy].Attributes = color;
consoleBuffer[posx+1 + screen_x * posy].Char.AsciiChar = '-';
consoleBuffer[posx+1 + screen_x * posy].Attributes = color;
consoleBuffer[posx+2 + screen_x * posy].Char.AsciiChar = '0';
consoleBuffer[posx+2 + screen_x * posy].Attributes = color;
consoleBuffer[posx + 3 + screen_x * posy].Char.AsciiChar = '-';
consoleBuffer[posx + 3 + screen_x * posy].Attributes = color;
consoleBuffer[posx + 4 + screen_x * posy].Char.AsciiChar = '>';
consoleBuffer[posx + 4 + screen_x * posy].Attributes = color;
}
//void draw(const char* strChar, COORD coord, unsigned short color = 7)
//{
// int x = coord.X;
// int y = coord.Y;
// for (size_t i = 0; i < strlen(strChar); i++)
// {
// if (strChar[i] == '\n')
// {
// x = coord.X;
// y++;
// continue;
// }
// if (0 <= x && x < screen_x && 0 <= y && y < screen_y)
// {
// consoleBuffer[x + screen_x * y].Char.AsciiChar = strChar[i];
// consoleBuffer[x + screen_x * y].Attributes = color;
// }
// x++;
// }
//}
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 = 1 ;
}
}
}
void display_console()
{
WriteConsoleOutputA(wHnd, consoleBuffer, bufferSize, characterPos,
&windowSize);
}
void init_star()
{
for (int i = 0; i < 80; i++)
{
star[i].X = rand() % 80;
star[i].Y = rand() % 25;
}
}
void star_fall()
{
int i;
for (i = 0; i < scount; i++) {
if (star[i].Y >= screen_y - 1) {
star[i] = { short(rand() % screen_x),1 };
}
else {
star[i].Y++;
if (star[i].X >= x && star[i].X <= x + 4 && star[i].Y == y)
{
hp--;
star[i] = { short(rand() % screen_x),1 };
}
}
}
}
void fill_star_to_buffer()
{
for (int i = 0; i < 80; i++)
{
consoleBuffer[star[i].X + screen_x * star[i].Y].Char.AsciiChar = '*';
consoleBuffer[star[i].X + screen_x * star[i].Y].Attributes = 7;
}
}
int setMode()
{
rHnd = GetStdHandle(STD_INPUT_HANDLE);
fdwMode = ENABLE_EXTENDED_FLAGS | ENABLE_WINDOW_INPUT |
ENABLE_MOUSE_INPUT;
SetConsoleMode(rHnd, fdwMode);
return 0;
}
int main()
{
srand(time(NULL));
bool play = true;
DWORD numEvents = 0;
DWORD numEventsRead = 0;
setConsole(screen_x, screen_y);
SetConsoleTitleW(L"oot");
setMode();
init_star();
while (play && hp>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')
{
color = rand() % 15 + 1;
}
}
else if (eventBuffer[i].EventType == MOUSE_EVENT) {
int posx = eventBuffer[i].Event.MouseEvent.dwMousePosition.X;
int posy = eventBuffer[i].Event.MouseEvent.dwMousePosition.Y;
if (eventBuffer[i].Event.MouseEvent.dwButtonState &
FROM_LEFT_1ST_BUTTON_PRESSED) {
color = rand() % 15 + 1;
}
else if (eventBuffer[i].Event.MouseEvent.dwEventFlags & MOUSE_MOVED) {
x = posx-2;
y = posy;
}
}
}
delete[] eventBuffer;
}
star_fall();
clear_buffer();
fill_star_to_buffer();
draw_ship(x,y);
/*draw("<-0->\n<-0->", {short(x),short(y) }, color);*/
display_console();
Sleep(100);
}
return 0;
}