-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
592 lines (491 loc) · 19.1 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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#include <stdio.h>
#include <raylib.h>
typedef enum
{
FIRST = 0,
SECOND,
THIRD,
QUARTA,
QUINTA,
SEXTA,
} EnemyWave;
typedef struct RecObs
{
Rectangle rec;
Vector2 speed;
bool active;
Color color;
} RecObs;
typedef struct Faca
{
Texture2D faca;
Vector2 facaPosition;
} Faca;
static RecObs enemy[4] = {0};
static EnemyWave wave = {0};
static Faca faquinha[4] = {0};
static int vidas = 0;
static int level = 0;
static int vitoria = 40;
// para compilar: mingw32-make PLATFORM=PLATFORM_DESKTOP
static void Iniciar(void);
static void Desenhar(void);
static void UnloadGame(void);
/*
static void UpdateGame(void); // Update game (one frame)
static void UpdateDrawFrame(void); // Update and Draw (one frame)
*/
typedef enum GameScreen
{
TITLE = 0,
GAMEPLAY,
GAMEOVER
} GameScreen;
int main(void)
{
InitWindow(1200, 600, "Salve os Animais");
Iniciar();
Desenhar();
UnloadGame();
CloseWindow();
return 0;
}
void Iniciar(void)
{
for (int i = 0; i < 3; i++)
{
enemy[i].rec.width = 40;
enemy[i].rec.height = 40;
enemy[i].rec.x = GetRandomValue(1200, 1200 + 1000);
enemy[i].rec.y = 460;
enemy[i].speed.x = 7;
enemy[i].speed.y = 7;
enemy[i].active = true;
enemy[i].color = BLUE;
faquinha[i].faca = LoadTexture("obstaculos/faca5.png");
faquinha[i].facaPosition.x = enemy[i].rec.x - 70;
faquinha[i].facaPosition.y = 380;
}
vidas = 0;
wave = FIRST;
level = 0;
vitoria = 40;
}
void Desenhar(void)
{
GameScreen currentScreen = TITLE;
float speed = 200.0;
Texture Background = LoadTexture("imgFundo/menu1.png");
for (int i = 0; i < 3; i++)
{
faquinha[i].faca = LoadTexture("obstaculos/faca5.png");
}
InitAudioDevice();
Music musica = LoadMusicStream("musica/terror.mp3");
Music musica2 = LoadMusicStream("musica/suspense.mp3");
Music musica3 = LoadMusicStream("musica/win.mp3");
Sound jump = LoadSound("musica/jump.mp3");
Sound grito = LoadSound("musica/grito.mp3");
// Upload do background
Texture backgroundTexture = LoadTexture("imgFundo/menu1.png");
Vector2 BP = {0, 0};
Rectangle FrameRecOut = {0.0f, 0.0f, (float)backgroundTexture.width, (float)backgroundTexture.height};
// Upload do personagem
Texture2D galinha = LoadTexture("personagem/galinha.png");
Vector2 position = {100.0f, 430.0f};
// Upload caixa amarela (chefão)
Rectangle boxA = {10, 430.0f, 60, 60};
int boxASpeedX = 4;
// Upload obstáculos
Rectangle boxB = {100.0f, 430.0f, 50, 70};
// Upload Anjolina Multiverso
Texture2D lady = LoadTexture("personagem/anjolina.png");
Vector2 ladyposition = {50.0f, 430.0f};
// Upload Background WIN!!
Texture BackgroundChoc = LoadTexture("imgFundo/chocolate3.png");
// Upload Background LOST!!
Texture BackgroundBlood = LoadTexture("imgFundo/blood.png");
Texture2D chat = LoadTexture("chat4.png");
Vector2 chatposition = {10.0f, 150.0f};
// Inicialização de váriaveis do gameplay;
int screenUpperLimit = 40; // Top menu limits
bool pause = false; // Movement pause
bool collision[5] = {false};
SetTargetFPS(60);
float time = 0.0f;
int frameAtual = 0;
int pulou = 0;
if (IsKeyPressed('P'))
pause = !pause;
StopMusicStream(musica);
PlayMusicStream(musica2);
while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(BLUE);
switch (currentScreen)
{
case TITLE:
{
PlayMusicStream(musica);
UpdateMusicStream(musica);
// TODO: Draw TITLE screen here!
DrawTexture(Background, 0, 0, WHITE);
DrawText("Salve os Animais", 21, 22, 70, BLACK);
DrawText("Salve os Animais", 20, 20, 70, WHITE);
DrawText("Seu objetivo é salvar a galinha do abatedouro", 21, 94, 20, BLACK);
DrawText("Seu objetivo é salvar a galinha do abatedouro", 20, 92, 20, RED);
DrawText("PRESSIONE ENTER PARA INICIAR", 421, 252, 20, BLACK);
DrawText("PRESSIONE ENTER PARA INICIAR", 420, 250, 20, WHITE);
}
break;
case GAMEPLAY:
{
// TODO: Draw GAMEPLAY screen here!
if(IsKeyDown(KEY_P)) pause=!pause;
UpdateMusicStream(musica2); // TOCAR MUSICA
if (!IsMusicStreamPlaying(musica2))
{
UpdateMusicStream(musica2);
}
if (pause)
{
StopMusicStream(musica2);
}
ClearBackground(RAYWHITE);
if (wave == FIRST)
{
for (int i = 0; i < 3; i++)
{
if (enemy[i].active)
{
enemy[i].rec.x -= enemy[i].speed.x;
faquinha[i].facaPosition.x -= enemy[i].speed.x;
if (enemy[i].rec.x < 0)
{
enemy[i].rec.x = GetRandomValue(1200, 1200 + 1000);
enemy[i].rec.y = 460;
faquinha[i].facaPosition.x = enemy[i].rec.x - 70;
faquinha[i].facaPosition.y = 380;
}
}
}
// condição de scrolling do background
FrameRecOut.x += GetFrameTime() * speed;
if (FrameRecOut.x >= FrameRecOut.width)
{
FrameRecOut.x = 0;
}
}
if (pause)
{
StopMusicStream(musica);
}
// CHECAGEM DE COLISÃO
if (vidas < 48 && level <= 40){
for (int i = 0; i < 3; i++)
{
collision[i] = CheckCollisionRecs(enemy[i].rec, boxB);
if (collision[i])
{
PlaySound(grito);
vidas += 1;
}
}
}
// Upload Barra de vida
Rectangle boxC = {140.0f, 20.0f, 192 - vidas * 4, 30};
Rectangle boxD = {139.0f, 19.0f, 194, 32};
// tempo para trocar frame do personagem
time += GetFrameTime();
if (time >= 0.1f)
{
time = 0.0f;
frameAtual += 1;
}
if(!pause){
// condição de pulo
if (IsKeyDown(KEY_SPACE) && (pulou == 0))
{
pulou = 1;
level++;
vitoria--;
PlaySound(jump);
}
if ((pulou == 1) && (position.y != 260))
{
position.y -= 10;
boxB.y -= 10;
}
if (position.y == 260)
pulou = 2;
if ((pulou == 2) && (position.y != 430))
{
position.y += 10;
boxB.y += 10;
}
if (position.y >= 430)
pulou = 0;
}
// Mudança de nível a partir da quantidade de pulos
if (level >= 5)
{
wave = SECOND;
}
if (level >= 10)
{
wave = THIRD;
}
if (level >= 15)
{
wave = QUARTA;
}
if (level >= 25)
{
wave = QUINTA;
}
if (level >= 30)
{
wave = SEXTA;
}
if (wave == SECOND)
{
ClearBackground(RAYWHITE);
if (!pause)
boxA.x += boxASpeedX;
if (((boxA.x + boxA.width) >= GetScreenWidth()) || (boxA.x <= 0))
boxASpeedX *= -1;
for (int i = 0; i < 3; i++)
{
if (enemy[i].active)
{
enemy[i].rec.x -= enemy[i].speed.x + 1;
faquinha[i].facaPosition.x -= enemy[i].speed.x + 1;
}
if (enemy[i].rec.x < 0)
{
enemy[i].rec.x = GetRandomValue(1200, 1200 + 1000);
enemy[i].rec.y = 460;
faquinha[i].facaPosition.x = enemy[i].rec.x - 70;
faquinha[i].facaPosition.y = 380;
}
}
FrameRecOut.x += GetFrameTime() * speed + 1;
if (FrameRecOut.x >= FrameRecOut.width)
{
FrameRecOut.x = 0;
}
}
if (wave == THIRD)
{
ClearBackground(RAYWHITE);
if (!pause)
boxA.x += boxASpeedX;
if (((boxA.x + boxA.width) >= GetScreenWidth()) || (boxA.x <= 0))
boxASpeedX *= -1;
for (int i = 0; i < 3; i++)
{
if (enemy[i].active)
{
enemy[i].rec.x -= enemy[i].speed.x + 2;
faquinha[i].facaPosition.x -= enemy[i].speed.x + 2;
}
if (enemy[i].rec.x < 0)
{
enemy[i].rec.x = GetRandomValue(1200, 1200 + 1000);
enemy[i].rec.y = 460;
faquinha[i].facaPosition.x = enemy[i].rec.x - 70;
faquinha[i].facaPosition.y = 380;
}
}
FrameRecOut.x += GetFrameTime() * speed + 2;
if (FrameRecOut.x >= FrameRecOut.width)
{
FrameRecOut.x = 0;
}
}
if (wave == QUARTA)
{
ClearBackground(RAYWHITE);
if (!pause)
boxA.x += boxASpeedX;
if (((boxA.x + boxA.width) >= GetScreenWidth()) || (boxA.x <= 0))
boxASpeedX *= -1;
for (int i = 0; i < 3; i++)
{
if (enemy[i].active)
{
enemy[i].rec.x -= enemy[i].speed.x + 3;
faquinha[i].facaPosition.x -= enemy[i].speed.x + 3;
}
if (enemy[i].rec.x < 0)
{
enemy[i].rec.x = GetRandomValue(1200, 1200 + 1000);
enemy[i].rec.y = 460;
faquinha[i].facaPosition.x = enemy[i].rec.x - 70;
faquinha[i].facaPosition.y = 380;
}
}
FrameRecOut.x += GetFrameTime() * speed + 3;
if (FrameRecOut.x >= FrameRecOut.width)
{
FrameRecOut.x = 0;
}
}
if (wave == QUINTA)
{
ClearBackground(RAYWHITE);
if (!pause)
boxA.x += boxASpeedX;
if (((boxA.x + boxA.width) >= GetScreenWidth()) || (boxA.x <= 0))
boxASpeedX *= -1;
for (int i = 0; i < 3; i++)
{
if (enemy[i].active)
{
enemy[i].rec.x -= enemy[i].speed.x + 5;
faquinha[i].facaPosition.x -= enemy[i].speed.x + 5;
}
if (enemy[i].rec.x < 0)
{
enemy[i].rec.x = GetRandomValue(1200, 1200 + 1000);
enemy[i].rec.y = 460;
faquinha[i].facaPosition.x = enemy[i].rec.x - 70;
faquinha[i].facaPosition.y = 380;
}
}
FrameRecOut.x += GetFrameTime() * speed + 4;
if (FrameRecOut.x >= FrameRecOut.width)
{
FrameRecOut.x = 0;
}
}
if (wave == SEXTA)
{
ClearBackground(RAYWHITE);
if (!pause)
boxA.x += boxASpeedX;
if (((boxA.x + boxA.width) >= GetScreenWidth()) || (boxA.x <= 0))
boxASpeedX *= -1;
for (int i = 0; i < 3; i++)
{
if (enemy[i].active)
{
enemy[i].rec.x -= enemy[i].speed.x + 7;
faquinha[i].facaPosition.x -= enemy[i].speed.x + 7;
}
if (enemy[i].rec.x < 0)
{
enemy[i].rec.x = GetRandomValue(1200, 1200 + 1000);
enemy[i].rec.y = 460;
faquinha[i].facaPosition.x = enemy[i].rec.x - 70;
faquinha[i].facaPosition.y = 380;
}
}
FrameRecOut.x += GetFrameTime() * speed + 7;
if (FrameRecOut.x >= FrameRecOut.width)
{
FrameRecOut.x = 0;
}
}
//DESENHANDO GAMEPLAY
if (vidas <= 32)
{
DrawTextureRec(backgroundTexture, FrameRecOut, BP, WHITE);
// DrawRectangleRec(boxB, BLUE);
DrawTextureRec(galinha, (Rectangle){(galinha.width / 4) * (frameAtual % 4), 0, galinha.width / 4, galinha.height}, position, WHITE);
/* DrawRectangleRec(boxA, GOLD);
*/
for (int i = 0; i < 3; i++)
{
if (collision[i])
{
// Draw collision message
DrawText("UGH UGH UGH!", GetScreenWidth() / 2 - MeasureText("COLLISION!", 20) / 2, screenUpperLimit / 2 - 10, 20, BLACK);
}
}
DrawText(TextFormat("LIFE:"), 20, 20, 40, RED);
DrawText(TextFormat("LIFE:"), 21, 21, 40, BLACK);
DrawText(TextFormat("RESTAM: %02i ", vitoria), 1051, 21, 20, BLACK);
DrawText(TextFormat("RESTAM: %02i ", vitoria), 1050, 20, 20, ORANGE);
DrawRectangleRec(boxD, WHITE);
DrawRectangleRec(boxC, RED);
for (int i = 0; i < 4; i++)
{
if (enemy[i].active)
{
// DrawRectangleRec(enemy[i].rec, enemy[i].color);
}
DrawTextureRec(
faquinha[i].faca,
(Rectangle){0, 0, faquinha[0].faca.width, faquinha[0].faca.height},
faquinha[i].facaPosition, WHITE);
}
}
// CONDIÇÃO DE VITÓRIA
if (level > 40)
{
StopMusicStream(musica2);
PlayMusicStream(musica3);
UpdateMusicStream(musica3);
DrawTexture(BackgroundChoc, 0, 0, WHITE);
DrawTextureRec(lady, (Rectangle){0, 0, lady.width, lady.height}, ladyposition, WHITE);
DrawTextureRec(chat, (Rectangle){0, 0, chat.width, chat.height}, chatposition, WHITE);
DrawText("YOU WIN ", GetScreenWidth() / 2 - MeasureText("YOU WIN!", 100) / 2, GetScreenHeight() / 2 - 48, 100, GRAY);
DrawText("YOU WIN!", GetScreenWidth() / 2 - MeasureText("YOU WIN!", 100) / 2, GetScreenHeight() / 2 - 50, 100, BLACK);
}
// CONDIÇÃO DE DERROTA
if (vidas > 32 && level <= 40)
{
StopMusicStream(musica2);
PlayMusicStream(musica);
UpdateMusicStream(musica);
DrawTexture(BackgroundBlood, 0, 0, WHITE);
DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth() / 2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20) / 2, GetScreenHeight() / 2 - 48, 20, GRAY);
DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth() / 2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20) / 2, GetScreenHeight() / 2 - 50, 20, BLACK);
DrawText("YOU LOST", 23, 24, 150, GRAY);
DrawText("YOU LOST", 21, 22, 150, BLACK);
}
//EndDrawing();
if (IsKeyPressed(KEY_ENTER) || IsGestureDetected(GESTURE_TAP))
{
StopMusicStream(musica);
StopMusicStream(musica3);
PlayMusicStream(musica2);
Iniciar();
vidas = 0;
}
if (IsKeyPressed(KEY_E) || IsGestureDetected(GESTURE_TAP))
{
CloseWindow();
}
}
break;
default:
break;
}
EndDrawing();
if (currentScreen == TITLE)
{
if (IsKeyPressed(KEY_ENTER) || IsGestureDetected(GESTURE_TAP))
{
currentScreen = GAMEPLAY;
}
}
if (IsKeyPressed(KEY_ENTER) || IsGestureDetected(GESTURE_TAP))
{
Iniciar();
vidas = 0;
}
if (IsKeyPressed(KEY_E) || IsGestureDetected(GESTURE_TAP))
{
UnloadMusicStream(musica2); // Unload music stream buffers from RAM
StopMusicStream(musica2);
CloseAudioDevice(); // parar a musica
CloseWindow();
}
}
}
void UnloadGame(void)
{
}