-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.c
314 lines (240 loc) · 5.58 KB
/
pong.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// snake_game.C
// made by rikko
#include <stdio.h>
#include <conio.h>
#include <ctime>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#include <windows.h>
#define HEIGHT 20
#define WIDTH 40
#define BOX_SIZE 2
//TO DO: for testing purposes , define fps formula later
#define FPS 50
#define PADDLE_HEIGHT (HEIGHT - 1)
#define PADDLE_SIZE 6
//maybe the boxes will be dynamic i can remove and add more?
//typedef struct box {
// int width = BOX_SIZE;
// bool active = false;
// int* coord = NULL;
// char symbol = 251; // ░
//}Box;
struct {
int* left_ptr, * right_ptr, * middle_ptr;
int left, right, middle;
int size, symbol_int;
char symbol_ch;
} paddle;
enum hit_direction { left, right, none };
struct {
int x, y;
int velocity, speed_multiplier, symbol_int;
char symbol_ch;
hit_direction direction;
} ball;
//general purpose variables
bool game_status = false;
int var;
//2D environment
int Field[HEIGHT][WIDTH] = { 0 };
void setup_ball() {
//setup ball properties
ball.symbol_int = -1;
ball.symbol_ch = 15;//☼
ball.x = WIDTH / 2;
ball.y = PADDLE_HEIGHT - 2;
ball.speed_multiplier = 1;
ball.direction = none;
// Position = initial position + speed * multiplier
ball.velocity = 1 * ball.speed_multiplier;
}
void setup_paddle() {
//setup basic properties
paddle.size = PADDLE_SIZE;
paddle.symbol_int = 1;
paddle.symbol_ch = (char)178;// ▓
//setup paddle positions
paddle.left_ptr = Field[HEIGHT - 1] + ((WIDTH / 2) - 1);
paddle.right_ptr = paddle.left_ptr + paddle.size;
}
void init() {
setup_paddle();
//Build paddle in the array
int* ch_placement_index = paddle.left_ptr;
for (int size_index = 0; ch_placement_index != paddle.right_ptr; ch_placement_index++, size_index++) {
if (size_index == paddle.size / 2)
paddle.middle_ptr = ch_placement_index;
*ch_placement_index = paddle.symbol_int;
}
setup_ball();
//TO DO: BOXES, i dont feel like doing it
}
void print() {
int curr_ch = 0;
const int CHAR_EMPTY = ' ';
for (int render_y_pos = 0; render_y_pos < HEIGHT; render_y_pos++) {
for (int render_x_pos = 0; render_x_pos < WIDTH; render_x_pos++) {
int* curr_pixel = &Field[render_y_pos][render_x_pos];
//box render code ( -2 )
//if (*curr_pixel == -2)
// curr_ch = box.;
//Visual limits for the user
if (render_x_pos == WIDTH)
printf("%c", '|');
if (!(*curr_pixel))
curr_ch = CHAR_EMPTY;
//Paddle render properties
if (*curr_pixel == paddle.symbol_int)
curr_ch = paddle.symbol_ch;
if (*curr_pixel == ball.symbol_int)
curr_ch = ball.symbol_ch;
/*
* DEBUG PADDLE TEXT*/
if (curr_pixel == paddle.left_ptr)
curr_ch = 'L';
else if (curr_pixel == paddle.middle_ptr)
curr_ch = 'M';
else if (curr_pixel == paddle.right_ptr)
curr_ch = 'R';
printf("%c", curr_ch);
}
putc('\n', stdout);
}
}
void resetscreen() {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD Position = { 0,0 };
SetConsoleCursorPosition(hOut, Position);
}
//Simple function to control the user input
int handle_input() {
if (_kbhit())
return _getch();
return -1;
}
void movement() {
var = handle_input();
var = tolower(var);
switch (var) {
case 'a':
if (paddle.left_ptr <= Field[PADDLE_HEIGHT])
return;
//delete old position
*paddle.right_ptr = 0;
//change cordinate
paddle.left_ptr -= 1;
paddle.middle_ptr -= 1;
paddle.right_ptr -= 1;
//place block at new position
*paddle.left_ptr = 1;
break;
case 'd':
if (paddle.right_ptr >= Field[PADDLE_HEIGHT] + (WIDTH - 1))
return;
// delete old position
*paddle.left_ptr = 0;
//change cordinate
paddle.left_ptr += 1;
paddle.middle_ptr += 1;
paddle.right_ptr += 1;
//place block at new position
*paddle.right_ptr = 1;
break;
}
}
void gameover() {
//end game loop
game_status = true;
//clear screen and show message
system("cls");
printf("\tGAME OVER ! ! 1! !");
}
void check_colision() {
//collision with the paddle
paddle.left = (int)(paddle.left_ptr - Field[PADDLE_HEIGHT]);
paddle.middle = (int)(paddle.middle_ptr - Field[PADDLE_HEIGHT]);
paddle.right = (int)(paddle.right_ptr - Field[PADDLE_HEIGHT]);
if (ball.y >= (PADDLE_HEIGHT - 1)) {
if (ball.x == paddle.middle) {
ball.velocity = -ball.velocity;
ball.direction = none;
}
if ((ball.x < paddle.middle) && (ball.x >= paddle.left)) {
ball.direction = left;
ball.velocity = -ball.velocity;
}
if ((ball.x > paddle.middle) && (ball.x <= paddle.right)) {
ball.direction = right;
ball.velocity = -ball.velocity;
}
}
//put the ball running
ball.y -= ball.velocity;
//repeat direction until colision
switch (ball.direction) {
case left:
ball.x -= ball.velocity;
break;
case right:
ball.x += ball.velocity;
break;
}
}
inline void delete_ball() {
Field[ball.y][ball.x] = 0;
}
inline void create_new_ball() {
//top limit
if (ball.y >= 1) {
ball.velocity = -ball.velocity;
}
//bottom limit / game over
if (ball.y > HEIGHT) {
gameover();
}
//left limit
if (ball.x >= 1)ball.velocity = -ball.velocity;
//top limit
//right limit
if (ball.x >= WIDTH) {
ball.velocity = -ball.velocity;
ball.direction = ball.direction == left ? right : left;
}
//corner collision check
if (ball.x <= -1 || ball.y <= -1)
{
ball.y += 1;
ball.x += 1;
ball.velocity = -ball.velocity;
}
Field[ball.y][ball.x] = ball.symbol_int;
}
void update_ball() {
delete_ball();
check_colision();
create_new_ball();
}
void draw() {
print();
Sleep(FPS);
resetscreen();
}
void input() {
movement();
}
void logic() {
update_ball();
}
int
main()
{
init();
while (game_status == false) {
draw();
input();
logic();
}
return 0;
}