-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
229 lines (182 loc) · 3.66 KB
/
main.c
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <tchar.h>
#include <windows.h>
#define ROW 30
#define COLUMN 20
enum
{
BALL_DIR_UP = 0,
BALL_DIR_DOWN = 1,
BALL_DIR_LEFT = 2,
BALL_DIR_RIGHT = 3,
};
int BD_X = BALL_DIR_RIGHT;
int BD_Y = BALL_DIR_DOWN;
void clrscr (void);
void draw_screen (void);
void move_ball (void);
void choose_multp (void);
DWORD WINAPI keyboard_rt (void *);
int BALL_X = 1;
int BALL_Y = 1;
int GAME_OVER = 0;
int GAME_ESCAPED = 0;
int MULTP = 1;
int BARRIER_Y = 0;
int SCORE = 0;
HANDLE stdIn;
int
main (int argc, char **argv)
{
stdIn = GetStdHandle (STD_INPUT_HANDLE);
srand (time (NULL));
DWORD thId;
HANDLE bg = CreateThread (0, 0, keyboard_rt, NULL, 0, &thId);
while (!GAME_OVER)
{
clrscr ();
draw_screen ();
move_ball ();
Sleep (100);
}
CloseHandle (bg);
CloseHandle (stdIn);
clrscr ();
draw_screen ();
if (!GAME_ESCAPED)
puts ("Game Over");
return 0;
}
void
clrscr (void)
{
system ("cls");
}
void
move_ball (void)
{
switch (BD_X)
{
case BALL_DIR_RIGHT:
{
if (BALL_X >= COLUMN - 1)
{
BD_X = BALL_DIR_LEFT;
BALL_X -= MULTP;
}
else
BALL_X += MULTP;
}
break;
case BALL_DIR_LEFT:
{
if (BALL_X <= 1)
{
if (BALL_Y >= BARRIER_Y - 1 && BALL_Y <= BARRIER_Y + 1)
{
BD_X = BALL_DIR_RIGHT;
BALL_X += MULTP;
SCORE++;
}
else
GAME_OVER = 1;
}
else
BALL_X -= MULTP;
}
break;
default:
break;
}
switch (BD_Y)
{
case BALL_DIR_UP:
{
if (BALL_Y <= 1)
{
BD_Y = BALL_DIR_DOWN;
BALL_Y += MULTP;
}
else
BALL_Y -= MULTP;
}
break;
case BALL_DIR_DOWN:
{
if (BALL_Y >= ROW - 1)
{
BD_Y = BALL_DIR_UP;
BALL_Y -= MULTP;
}
else
BALL_Y += MULTP;
}
break;
default:
break;
}
}
void
draw_screen (void)
{
char LINE[COLUMN + 5];
*LINE = '\0';
for (int i = 0; i < ROW; i++)
{
if (i >= BARRIER_Y - 1 && i <= BARRIER_Y + 1)
strcat (LINE, "||");
else
strcat (LINE, " ");
for (int j = 0; j < COLUMN; j++)
{
if (i == BALL_Y && j == BALL_X)
strcat (LINE, "o");
else
strcat (LINE, " ");
}
if (i >= BALL_Y - 1 && i <= BALL_Y + 1)
strcat (LINE, "||");
puts (LINE);
*LINE = '\0';
}
printf ("SCORE: %d\n", SCORE);
}
DWORD WINAPI
keyboard_rt (void *args)
{
while (!GAME_OVER)
{
DWORD ev;
GetNumberOfConsoleInputEvents (stdIn, &ev);
if (ev != 0)
{
INPUT_RECORD inpRec[ev];
DWORD ev_count;
ReadConsoleInput (stdIn, inpRec, ev, &ev_count);
for (size_t i = 0; i < ev_count; i++)
{
if (inpRec[i].EventType == KEY_EVENT
&& inpRec[i].Event.KeyEvent.bKeyDown)
{
WORD w = inpRec[i].Event.KeyEvent.wVirtualKeyCode;
if (w == VK_DOWN)
BARRIER_Y += 1;
else if (w == VK_UP)
BARRIER_Y -= 1;
else if (w == VK_ESCAPE)
{
GAME_ESCAPED = 1;
GAME_OVER = 1;
goto end;
}
}
}
}
}
end:
return 0;
}