-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
531 lines (482 loc) · 18 KB
/
game.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_timer.h>
#include <SDL2/SDL_image.h>
#include <time.h>
#include <math.h>
#define PI (3.14159265358979323846)
#define WINDOW_WIDTH (960)
#define WINDOW_HEIGHT (540)
#define PIXEL_SIZE (960/192.0)
// speed in pixels/second
#define SPEED (150.0)
#define GHOST_SPEED (0.50)
#define FPS (60.0)
int collides(SDL_Rect rect1, SDL_Rect rect2) {
if (rect1.x < rect2.x + rect2.w && rect2.x < rect1.x + rect1.w) {
if (rect1.y < rect2.y + rect2.h && rect2.y < rect1.y + rect1.h) {
return 1;
}
}
return 0;
}
int main(void) {
srand(time(NULL));
// attempt to initialize graphics and timer system
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0) {
printf("error initializing SDL: %s\n", SDL_GetError());
return 1;
}
SDL_Window* win = SDL_CreateWindow("Ghosts", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
if (!win) {
printf("error creating window: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
// create a renderer, which sets up the graphics hardware
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
if (!rend) {
printf("error creating renderer: %s\n", SDL_GetError());
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
// PLAYER
// load the image into memory using SDL_image library function
SDL_Surface* surfPlayer = IMG_Load("resources/player-10x11-4x4.png");
if (!surfPlayer) {
printf("error creating surface\n");
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
// load the image data into the graphics hardware's memory
SDL_Texture* texPlayer = SDL_CreateTextureFromSurface(rend, surfPlayer);
SDL_FreeSurface(surfPlayer);
if (!texPlayer) {
printf("error creating texture: %s\n", SDL_GetError());
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
// struct to hold the position and size of the sprite
SDL_Rect rectPlayer;
// get and scale the dimensions of texture
// SDL_QueryTexture(texPlayer, NULL, NULL, &rectPlayer.w, &rectPlayer.h);
// rectPlayer.w = 10 * PIXEL_SIZE;
// rectPlayer.h = 11 * PIXEL_SIZE;
rectPlayer.w = 30;
rectPlayer.h = 33;
float posPlayer[2] = {(WINDOW_WIDTH - rectPlayer.w) / 2, (WINDOW_HEIGHT - rectPlayer.h) / 2}; // {x, y}
int animationPlayer = 0; // 0 - 3
SDL_Rect rectSourceAnimation;
rectSourceAnimation.w = 10;
rectSourceAnimation.h = 11;
int posSourceAnimation[4][4][2] = { // [dir][animationPlayer][x/y]
{{0, 0}, {10, 0}, {20, 0}, {30, 0}}, // down
{{0, 11}, {10, 11}, {20, 11}, {30, 11}}, // up
{{0, 22}, {10, 22}, {20, 22}, {30, 22}}, // right
{{0, 33}, {10, 33}, {20, 33}, {30, 33}} // left
};
// GHOSTS
//load the image into memory using SDL_image library function
SDL_Surface* surfGhost = IMG_Load("resources/ghost.png");
if (!surfGhost) {
printf("error creating surface\n");
SDL_DestroyTexture(texPlayer);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
// load the image data into the graphics hardware's memory
SDL_Texture* texGhost = SDL_CreateTextureFromSurface(rend, surfGhost);
SDL_FreeSurface(surfGhost);
if (!texGhost) {
printf("error creating texture: %s\n", SDL_GetError());
SDL_DestroyTexture(texPlayer);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
float posGhostsArr[3][2];
SDL_Rect rectGhosts[3];
for (int i = 0; i < 3; i++) {
// struct to hold the position and size of the sprite
SDL_Rect rectGhost;
// get and scale the dimensions of texture
SDL_QueryTexture(texGhost, NULL, NULL, &rectGhost.w, &rectGhost.h);
rectGhost.w = 27;
rectGhost.h = 33;
posGhostsArr[i][0] = rand() % (WINDOW_WIDTH - rectGhost.w); // x
posGhostsArr[i][1] = rand() % (WINDOW_HEIGHT - rectGhost.h); // y
rectGhost.x = (int) posGhostsArr[i][0];
rectGhost.y = (int) posGhostsArr[i][1];
rectGhosts[i] = rectGhost;
}
// compass
// load the image into memory using SDL_image library function
SDL_Surface* surfCompass = IMG_Load("resources/compass.jpg");
if (!surfCompass) {
printf("error creating surface\n");
SDL_DestroyTexture(texPlayer);
SDL_DestroyTexture(texGhost);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
// load the image data into the graphics hardware's memory
SDL_Texture* texCompass = SDL_CreateTextureFromSurface(rend, surfCompass);
SDL_FreeSurface(surfCompass);
if (!texCompass) {
printf("error creating texture: %s\n", SDL_GetError());
SDL_DestroyTexture(texPlayer);
SDL_DestroyTexture(texGhost);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
// struct to hold the position and size of the sprite
SDL_Rect rectCompass;
// get and scale the dimensions of texture
SDL_QueryTexture(texCompass, NULL, NULL, &rectCompass.w, &rectCompass.h);
rectCompass.x = 0;
rectCompass.y = 20;
rectCompass.w = 60;
rectCompass.h = (int) rectCompass.w/3;
SDL_Surface* surfCoin = IMG_Load("resources/coin.png");
if (!surfCoin) {
printf("error creating surface\n");
SDL_DestroyTexture(texPlayer);
SDL_DestroyTexture(texGhost);
SDL_DestroyTexture(texCompass);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
// load the image data into the graphics hardware's memory
SDL_Texture* texCoin = SDL_CreateTextureFromSurface(rend, surfCoin);
SDL_FreeSurface(surfCoin);
if (!texPlayer) {
printf("error creating texture: %s\n", SDL_GetError());
SDL_DestroyTexture(texPlayer);
SDL_DestroyTexture(texGhost);
SDL_DestroyTexture(texCompass);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
// struct to hold the position and size of the sprite
SDL_Rect rectCoin;
// get and scale the dimensions of texture
SDL_QueryTexture(texCoin, NULL, NULL, &rectCoin.w, &rectCoin.h);
rectCoin.w = 30;
rectCoin.h = 30;
float posCoin[2] = {rand() % (WINDOW_WIDTH - rectCoin.w), rand() % (WINDOW_HEIGHT - rectCoin.h)};
// LIGHTING
// load the image into memory using SDL_image library function
SDL_Surface* surfBlack = IMG_Load("resources/black.png");
if (!surfBlack) {
printf("error creating surface\n");
SDL_DestroyTexture(texPlayer);
SDL_DestroyTexture(texGhost);
SDL_DestroyTexture(texCompass);
SDL_DestroyTexture(texCoin);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
// load the image data into the graphics hardware's memory
SDL_Texture* texBlack = SDL_CreateTextureFromSurface(rend, surfBlack);
SDL_FreeSurface(surfBlack);
if (!texBlack) {
printf("error creating texture: %s\n", SDL_GetError());
SDL_DestroyTexture(texPlayer);
SDL_DestroyTexture(texGhost);
SDL_DestroyTexture(texCompass);
SDL_DestroyTexture(texCoin);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
// load the image into memory using SDL_image library function
SDL_Surface* surfLight = IMG_Load("resources/light.png");
if (!surfLight) {
printf("error creating surface\n");
SDL_DestroyTexture(texPlayer);
SDL_DestroyTexture(texGhost);
SDL_DestroyTexture(texCompass);
SDL_DestroyTexture(texCoin);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
// load the image data into the graphics hardware's memory
SDL_Texture* texLight = SDL_CreateTextureFromSurface(rend, surfLight);
SDL_FreeSurface(surfLight);
if (!texLight) {
printf("error creating texture: %s\n", SDL_GetError());
SDL_DestroyTexture(texPlayer);
SDL_DestroyTexture(texGhost);
SDL_DestroyTexture(texCompass);
SDL_DestroyTexture(texCoin);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
return 1;
}
// struct to hold the position and size of the sprite
SDL_Rect rectLight;
// get and scale the dimensions of texture
SDL_QueryTexture(texLight, NULL, NULL, &rectLight.w, &rectLight.h);
rectLight.w = PIXEL_SIZE;
rectLight.h = PIXEL_SIZE;
int lights[1][3] = { // in case I want more later
{144, rectPlayer.x, rectPlayer.y} // power, x, y
};
float ditherMatrix[2][2] = {
{0.25, 0.75},
{1, 0.5}
};
int score = 0;
printf("Score: %d\n", score);
// player direction
int dir = 0; // 0, 1, 2, 3 :: down, up, right, left
// set to 1 when window close button is pressed
int closeRequested = 0;
int frame = 0;
// animation loop
while (closeRequested == 0) {
clock_t start = clock();
// process events
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
closeRequested = 2;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.scancode) {
case SDL_SCANCODE_W:
dir = 1;
break;
case SDL_SCANCODE_S:
dir = 0;
break;
case SDL_SCANCODE_A:
dir = 3;
break;
case SDL_SCANCODE_D:
dir = 2;
break;
}
break;
}
}
// move the player
switch (dir) {
case 1:
posPlayer[1] -= SPEED / FPS;
break;
case 0:
posPlayer[1] += SPEED / FPS;
break;
case 3:
posPlayer[0] -= SPEED / FPS;
break;
case 2:
posPlayer[0] += SPEED / FPS;
break;
}
// collision detection with bounds
if (posPlayer[0] <= 0) posPlayer[0] = 0;
if (posPlayer[1] <= 0) posPlayer[1] = 0;
if (posPlayer[0] >= WINDOW_WIDTH - rectPlayer.w) posPlayer[0] = WINDOW_WIDTH - rectPlayer.w;
if (posPlayer[1] >= WINDOW_HEIGHT - rectPlayer.h) posPlayer[1] = WINDOW_HEIGHT - rectPlayer.h;
// move ghosts 1 and 2, have them chase the player and 3 zone near the coin
for (int i = 0; i < 3; i++) {
float difX = posPlayer[0] - posGhostsArr[i][0];
float difY = posPlayer[1] - posGhostsArr[i][1];
float dist = sqrt(difX * difX + difY * difY);
if (i == 0 || i == 1 || dist < lights[0][0] - 10) {}
else {
difX = posCoin[0] - posPlayer[0];
difY = posCoin[1] - posPlayer[1];
float target[2] = {posCoin[0] - difX/3, posCoin[1] - difY/3};
difX = target[0] - posGhostsArr[i][0];
difY = target[1] - posGhostsArr[i][1];
}
if (abs(difX) > abs(difY)) {
if (difX > 0) { // on right
posGhostsArr[i][0] += SPEED * GHOST_SPEED * 1/FPS;
}
else {
posGhostsArr[i][0] -= SPEED * GHOST_SPEED * 1/FPS;
}
}
else {
if (difY > 0) { // on the down
posGhostsArr[i][1] += SPEED * GHOST_SPEED * 1/FPS;
}
else {
posGhostsArr[i][1] -= SPEED * GHOST_SPEED * 1/FPS;
}
}
rectGhosts[i].x = (int) posGhostsArr[i][0];
rectGhosts[i].y = (int) posGhostsArr[i][1];
}
// set the player positions in the struct
rectPlayer.x = (int) posPlayer[0];
rectPlayer.y = (int) posPlayer[1];
if (collides(rectPlayer, rectCoin)) {
posCoin[0] = rand() % (WINDOW_WIDTH - rectCoin.w);
posCoin[1] = rand() % (WINDOW_HEIGHT - rectCoin.h);
score += 1;
printf("Score: %d\n", score);
}
for (int i = 0; i < 3; i++) {
if (collides(rectPlayer, rectGhosts[i])) {
closeRequested = 1;
}
}
rectCoin.x = (int) posCoin[0];
rectCoin.y = (int) posCoin[1];
lights[0][1] = rectPlayer.x + rectPlayer.w/2;
lights[0][2] = rectPlayer.y + rectPlayer.h/2;
// DRAW THE FRAME
// clear the window
SDL_RenderClear(rend);
// LIGHTING, place the light sqaures
float topLeft[2] = {lights[0][1] - lights[0][0] - PIXEL_SIZE, lights[0][2] - lights[0][0] - PIXEL_SIZE};
float bottomRight[2] = {lights[0][1] + lights[0][0] + PIXEL_SIZE, lights[0][2] + lights[0][0] + PIXEL_SIZE};
int matrixPos[2] = {0, 0};
double r = (double)rand()/RAND_MAX; // flicker
r = r * 0.1 + 0.9;
for (int x = topLeft[0]; x < bottomRight[0]; x += PIXEL_SIZE) {
for (int y = topLeft[1]; y < bottomRight[1]; y += PIXEL_SIZE) {
float distFromCenter = sqrt((lights[0][1] - x) * (lights[0][1] - x) + (lights[0][2] - y) * (lights[0][2] - y));
float tempLightPower = ditherMatrix[matrixPos[0]][matrixPos[1]] * lights[0][0] * r;
if (tempLightPower - distFromCenter > 0) {
rectLight.x = x;
rectLight.y = y;
SDL_RenderCopy(rend, texLight, NULL, &rectLight);
}
if (matrixPos[0]) {
matrixPos[0] = 0;
}
else {
matrixPos[0] = 1;
}
}
if (matrixPos[1]) {
matrixPos[1] = 0;
}
else {
matrixPos[1] = 1;
}
}
for (int i = 0; i < 3; i++) {
SDL_RenderCopy(rend, texGhost, NULL, &rectGhosts[i]);
}
SDL_RenderCopy(rend, texCoin, NULL, &rectCoin);
matrixPos[0] = 0;
matrixPos[1] = 0;
for (int x = topLeft[0]; x < bottomRight[0]; x += PIXEL_SIZE) {
for (int y = topLeft[1]; y < bottomRight[1]; y += PIXEL_SIZE) {
float distFromCenter = sqrt((lights[0][1] - x) * (lights[0][1] - x) + (lights[0][2] - y) * (lights[0][2] - y));
float tempLightPower = ditherMatrix[matrixPos[0]][matrixPos[1]] * lights[0][0] * r;
if (tempLightPower - distFromCenter > 0) {}
else {
rectLight.x = x;
rectLight.y = y;
SDL_RenderCopy(rend, texBlack, NULL, &rectLight);
}
if (matrixPos[0]) {
matrixPos[0] = 0;
}
else {
matrixPos[0] = 1;
}
}
if (matrixPos[1]) {
matrixPos[1] = 0;
}
else {
matrixPos[1] = 1;
}
}
SDL_Rect rectBar;
//left
rectBar.x = 0;
rectBar.y = 0;
rectBar.w = lights[0][1] - lights[0][0];
rectBar.h = WINDOW_HEIGHT;
SDL_RenderCopy(rend, texBlack, NULL, &rectBar);
//right
rectBar.x = lights[0][1] - lights[0][0] + lights[0][0] * 2;
rectBar.y = 0;
rectBar.w = WINDOW_WIDTH - rectBar.x;
rectBar.h = WINDOW_HEIGHT;
SDL_RenderCopy(rend, texBlack, NULL, &rectBar);
//top
rectBar.x = lights[0][1] - lights[0][0];
rectBar.y = 0;
rectBar.w = lights[0][0] * 2;
rectBar.h = lights[0][2] - lights[0][0];
SDL_RenderCopy(rend, texBlack, NULL, &rectBar);
//bottom
rectBar.x = lights[0][1] - lights[0][0];
rectBar.y = lights[0][2] - lights[0][0] + lights[0][0] * 2;
rectBar.w = lights[0][0] * 2;
rectBar.h = WINDOW_HEIGHT - rectBar.y;
SDL_RenderCopy(rend, texBlack, NULL, &rectBar);
rectSourceAnimation.x = posSourceAnimation[dir][animationPlayer][0];
rectSourceAnimation.y = posSourceAnimation[dir][animationPlayer][1];
SDL_RenderCopy(rend, texPlayer, &rectSourceAnimation, &rectPlayer);
float angle = atan((posCoin[1] - posPlayer[1])/(posCoin[0] - posPlayer[0]));
angle = (angle * 180) / PI;
if (posCoin[0] - posPlayer[0] < 0) angle = angle + 180;
SDL_RenderCopyEx(rend, texCompass, NULL, &rectCompass, angle, NULL, SDL_FLIP_NONE);
SDL_RenderPresent(rend);
if (frame % 5 == 0) {
animationPlayer++;
if (animationPlayer > 3) animationPlayer = 0;
}
// wait 1/FPSth of a second
clock_t end = clock();
double frameTime = (double) (end - start) / CLOCKS_PER_SEC;
frameTime *= 1000; // seconds to milliseconds
double waitTime = (1000 - frameTime > 0) ? 1000 - frameTime : 0;
if (waitTime == 0) printf("Running slow!");
SDL_Delay(waitTime/FPS);
frame++;
}
if (closeRequested == 1) {
printf("\n\nYOU DIED! Score: %d\n", score);
SDL_Delay(2000);
}
else {
printf("\n\nGame Exited! Score: %d\n", score);
}
// clean up resources before exiting
SDL_DestroyTexture(texPlayer);
SDL_DestroyTexture(texGhost);
SDL_DestroyTexture(texCompass);
SDL_DestroyTexture(texCoin);
SDL_DestroyTexture(texLight);
SDL_DestroyTexture(texBlack);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(win);
SDL_Quit();
}