-
Notifications
You must be signed in to change notification settings - Fork 0
/
helloworld.c
402 lines (322 loc) · 6.2 KB
/
helloworld.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#include <random.h>
#include <stdbool.h>
//Ascii codes
#define KEY_ESC 27
#define KEY_LEFT 75
#define KEY_RIGHT 77
#define KEY_UP 72
#define KEY_DOWN 80
//Road length
#define LENGTH 48
//Minimum and maximum values of the center of the road at screen that
//guaranties at least one grass unit at both sides of the screen
//MIN = -123........o = 1+3+8+1 = 13
//MAX = o........321- = 80-3-8-1 = 68
#define MIN 13
#define MAX 68
//Basic structure functions
void Initialize(void);
void Loop(void);
void Finalize(void);
void Input(void);
void Update(void);
void Draw(void);
//Auxiliar functions
int Random(void);
void DrawRoadLine(int i);
//Player variables
int x,x_prev, //current and previous player position
speed,
lifes,
meters;
//Road structure: an array of lines, where the integer value corresponds to
// the offset from left screen limit (road has a fixed width)
int road[LENGTH];
//Control variables
bool begin,
end;
//------------------------------------------
//---------------- MAIN --------------------
//------------------------------------------
int main()
{
//-- TODO:
Initialize();
Loop();
//Input();
//Update();
//Draw(); //Render();
Finalize();
//--
return 0;
}
//Returns a random value {-1,0,1}
int Random()
{
return (genrand_int32()%3)-1;
}
//Setup game: resources, variables and data structure
void Initialize()
{
int i;
//Initialize random numbers
mt_init_genrand(13);
//Text mode 80x50
textmode(C4350);
//Hide cursor
_setcursortype(_NOCURSOR);
// Player initial position at screen = (80/2=40,42)
//
// - 1st line
// o 2nd -> c[0]
// o
// ...
// o 42th -> c[40]
// ...
// o 49th
// - 50th line
x = 40;
x_prev = x;
road[40] = 40;
meters = 0;
speed = 1;
lifes = 3;
//Setup road
for(i=39;i>=0;i--)
{
road[i] = road[i+1] + Random();
if(road[i]<MIN) road[i] = MIN;
else if(road[i]>MAX) road[i] = MAX;
}
//-- TODO:BOTTOM PART OF the road
for(i=49;i>40;i--)
{
road[i] = road[i+1]+ Random();
if(road[i]<MIN) road[i] = MIN;
else if(road[i]>MAX) road[i] = MAX;
}
//--
begin = true;
}
//Game loop with frame rate synchronization based on player speed
//speed 1 => 10 fps, speed 9 =>
void Loop()
{
int t1,t2;
while(!end)
{
t1 = GetTickCount();
//-- TODO:
//x=x+speed;
Input();
Update();
//DrawRoadLine(x);
Draw();
//--
do {
t2 = GetTickCount();
} while((t2-t1)<(100/speed));
}
}
//Release
void Finalize()
{
//Setup initial text mode
textmode(LASTMODE);
}
//Get user input
void Input()
{
//kbhit: Returns a non-zero integer if a key is in the keyboard buffer.
// It will not wait for a key to be pressed.
if(kbhit())
{
x_prev = x;
//Get key pressed
switch(getch())
{
case KEY_ESC: end = true; break;
//-- TODO:
case KEY_LEFT: x=x-1; break;
case KEY_RIGHT: x=x+1; break;
case KEY_UP: speed=speed+1; break;
case KEY_DOWN:
speed=speed-1;
if(speed-1<0)
speed=1;
break;
//--
}
}
}
//Update game state: move the road and detect collisions
void Update()
{
int i;
bool collision = false;
if(!begin)
{
//Move the road (the scroll)
meters++;
memcpy(&road[1],&road[0],(LENGTH-1)*sizeof(int));
//-- TODO:
//New piece of road
road[0] = road[0+1]+ Random();
//--
}
//Collisions
if(x<(road[40]-8)) collision = true;
else if(x>(road[40]+8)) collision = true; //TODO
if(collision)
{
//Wait 500ms
i = GetTickCount();
while((GetTickCount()-i)<500) {}
//-- TODO:
lifes=lifes-1;
//--
if(lifes == 0)
{
end = true;
}
else
{
//Reset speed value and place player in the center of the road
speed = 1;
x = road[40];
}
}
}
//Draw the GUI (lifes, meters, speed), the road and the player
void Draw()
{
//gotoxy(x,y): set cursor at (x,y) position
//Screen resolution = 80x50 => x=1..80, y=1..50
int i;
if(!end)
{
//GUI
//Speed
gotoxy(5,1);
textcolor(CYAN);
printf("Speed: ");
textcolor(LIGHTCYAN);
for(i=0;i<speed;i++)
printf("%c",0x04);
textcolor(CYAN);
for(i=0;i<(10-speed);i++)
printf("%c",0x04);
//Meters
gotoxy(40,1);
textcolor(YELLOW);
printf("%d Meters",meters);
//Lifes
gotoxy(70,1);
textcolor(RED);
printf("Life: ");
textcolor(LIGHTRED);
for(i=0;i<lifes;i++)
printf("%c",0x03);
textcolor(RED);
for(i=0;i<(3-lifes);i++)
printf("%c",0x03);
//ROAD
if(begin)
{
//Clear screen
clrscr();
//Draw the road
for(i=0;i<LENGTH;i++)
{
gotoxy(1,i+2);
DrawRoadLine(i);
}
begin = false;
}
else
{
//Delete player
textcolor(LIGHTGRAY);
gotoxy(x_prev,42);
printf("%c",0xDB);
if(x_prev!=x) x_prev = x;
//-- TODO:
//Road scrolling
//New piece of road
gotoxy(1,2);
DrawRoadLine(2);
movetext(1,2,80,48,1,3);
//--
}
//PLAYER
textcolor(BLUE);
textbackground(LIGHTGRAY);
gotoxy(x,42);
printf("%c",0x02);
textbackground(BLACK);
}
else
{
textcolor(BLUE);
gotoxy(34,20); printf("-------------");
gotoxy(34,21); printf("| GAME OVER |");
gotoxy(34,22); printf("-------------");
//Wait 500ms showing game over message
i=GetTickCount();
while((GetTickCount()-i)<500) {}
//Wait for a user key down to continue
while(!kbhit()) {}
}
}
//Draw the i-th segment line of the road
void DrawRoadLine(int i)
{
//Road line = grass kerb tarmac kerb grass
//kerb: 3 unit spaces, left and right
//tarmac: 8 + 1 + 8 = 17 unit spaces
//...123--------o--------321...
int j;
int num_left,num_right;
num_left = road[i]-12;
num_right = road[i]+12;
//Grass
textcolor(GREEN);
for(j=0;j<num_left;j++){
printf("%c",0xDB);
if(j%5==0){
textcolor(BROWN);
}else
textcolor(GREEN);
}
//Left limit road (kerb)
textcolor(RED);
printf("%c",0xB1);
textcolor(WHITE);
printf("%c",0xB1);
textcolor(RED);
printf("%c",0xB1);
//Tarmac
textcolor(LIGHTGRAY);
for(i=0;i<17;i++)
printf("%c",0xDB);
//Right limit road (kerb)
textcolor(RED);
printf("%c",0xB1);
textcolor(WHITE);
printf("%c",0xB1);
textcolor(RED);
printf("%c",0xB1);
//Grass
textcolor(BLUE);
for(j=num_right;j<=80;j++){
cprintf("%c",0xDB);
if(j%5==0){
textcolor(BROWN);
}else
textcolor(GREEN);
}
}