-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
454 lines (362 loc) · 14.8 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
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
#include <stdio.h>
#include <SDL2/SDL.h>
#include <time.h>
#include "headers/definitions.h"
#include "headers/screen.h"
/*
Grupo
José Luis Bastos Donin
Luiz Felipe Nagatani
Pedro Gabriel
*/
/*Links:
Compilar: gcc -o main main.c -lSDL2 -lSDL2_image
*/
void loadGame(gamestate *gameState, screen_texture *start)
{
/*
Função carrega todas as texturas do jogo, criamos as superficies (surfaces), depois definimos as
posições da llama, grama, chão, hitbox (usado pra checar se o cactus chegou no final do mapa), também usamos
para tela de Game Over.
Depois descobrimos que não era necessário, pois podia definir direto na função doRender().
*/
int i = 0;
SDL_Surface *llamaSurface = NULL;
SDL_Surface *cactusSurface = NULL;
SDL_Surface *gameOverScreenSurface = NULL;
SDL_Surface *skySurface = NULL;
SDL_Surface *startScreenSurface = NULL;
SDL_Surface *pauseSurface = NULL;
SDL_Surface *logoSurface = NULL;
SDL_Surface *studioSurface = NULL;
SDL_Surface *restartLlammaSurface = NULL;
gameState->llamas.x = llamaPositionX;
gameState->llamas.y = llamaPositionY;
gameState->llamas.w = llamaWidth;
gameState->llamas.h = llammaHeight;
gameState->llamas.falling = 0;
gameState->grass.x = floorXAxisStart;
gameState->grass.y = floorYAxisStart;
gameState->grass.w = floorXAxisEnd;
gameState->grass.h = grassHeight;
gameState->floor.x = floorXAxisStart;
gameState->floor.y = floorYAxisStart;
gameState->floor.w = floorXAxisEnd;
gameState->floor.h = floorHeight;
gameState->hitbox.x = hitboxPostionX;
gameState->hitbox.y = hitboxPostionY;
gameState->hitbox.w = hitboxWidth;
gameState->hitbox.h = hitboxHeight;
gameState->gameOverScreen.x = gameOverScreenX;
gameState->gameOverScreen.y = gameOverScreenY;
gameState->gameOverScreen.w = gameOverScreenW;
gameState->gameOverScreen.h = gameOverScreenH;
gameState->sky.x = skyPositionX;
gameState->sky.y = skyPositionY;
gameState->sky.w = skyWidth;
gameState->sky.h = skyHeight;
//Img_load() carrega as imagens png nas superficies criadas acima.
llamaSurface = IMG_Load("images/llamma-main.png");
cactusSurface = IMG_Load("images/cactus.png");
gameOverScreenSurface = IMG_Load("images/LLAME-OVER.png");
skySurface = IMG_Load("images/sky.png");
startScreenSurface = IMG_Load("images/startscreen.png");
pauseSurface = IMG_Load("images/pause.png");
logoSurface = IMG_Load("images/logo.png");
studioSurface = IMG_Load("images/studio.png");
restartLlammaSurface = IMG_Load("images/gameoverllama.png");
//Aqui é onde definimos a posição do cactus que são gerandos randômicamente na posição X.
for (i = 0; i < 100; i++)
{
gameState->cactus[i].x = cactusPositionX + rand() % 500;
gameState->cactus[i].y = cactusPositionY;
}
/*Aqui são carregadas as texturas definidas em definitions.h e screen.h, essas funções
transformam as superficies em texturas e usa alocação dinâmica para transforma-las em texturas.
*/
gameState->llama = SDL_CreateTextureFromSurface(gameState->renderer, llamaSurface);
SDL_FreeSurface(llamaSurface);
gameState->cactu = SDL_CreateTextureFromSurface(gameState->renderer, cactusSurface);
SDL_FreeSurface(cactusSurface);
gameState->gameOverScreenTexture = SDL_CreateTextureFromSurface(gameState->renderer, gameOverScreenSurface);
SDL_FreeSurface(gameOverScreenSurface);
gameState->skyTexture = SDL_CreateTextureFromSurface(gameState->renderer, skySurface);
SDL_FreeSurface(skySurface);
start->screenTexture = SDL_CreateTextureFromSurface(gameState->renderer, startScreenSurface);
SDL_FreeSurface(startScreenSurface);
start->pauseTexture = SDL_CreateTextureFromSurface(gameState->renderer, pauseSurface);
SDL_FreeSurface(pauseSurface);
start->logoTexture = SDL_CreateTextureFromSurface(gameState->renderer, logoSurface);
SDL_FreeSurface(logoSurface);
start->studioTexture = SDL_CreateTextureFromSurface(gameState->renderer, studioSurface);
SDL_FreeSurface(studioSurface);
start->restartLlammaTexture = SDL_CreateTextureFromSurface(gameState->renderer, restartLlammaSurface);
SDL_FreeSurface(restartLlammaSurface);
}
void ScoreCounting(gamestate *gameState, screen_texture *start)
{
/*
Aqui definimos o score e escrevemos em um arquivo (score.txt), usamos a função time*60 para que mostre em
números inteiros.
*/
clock_t t;
FILE *fp = fopen("score/score.txt", "w");
if(gameState->telaAtual == game)
{
start->lastTime = clock()*60;
int score = ((int)start->lastTime)/CLOCKS_PER_SEC;
printf("\nscore: %d", score);
fscanf(fp, "%d", &score);
fprintf(fp,"\nscore: %d", score);
}
fclose(fp);
}
int processEvents(SDL_Window *window, gamestate *gameState, screen_texture *start)
{
/*Nessa função definimos todos os eventos do jogo, registramos uso do teclado e definimos
as funções que cada tecla faz
1-Espaço -> começa o jogo
2- P -> pausa o jogo
3- Esc -> fecha o jogo
4- também são definidas as telas do jogo*/
int done = 0;
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_WINDOWEVENT_CLOSE:
{
if (window)
{
SDL_DestroyWindow(window);
window == NULL;
done = 1;
}
}
break;
case SDL_KEYDOWN:
{
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
done = 1;
break;
case SDLK_SPACE:
if (gameState->telaAtual == initscreen){
gameState->telaAtual = game;
gameState->cactus[0].x = cactusPositionX;
return done;
}
else
if(gameState->telaAtual == gameOver)
{
gameState->telaAtual = game;
gameState->cactus[0].x = cactusPositionX;
return done;
}else
break;
case SDLK_p:
if(gameState->telaAtual == game)
{
gameState->telaAtual = pause;
return done;
}else
if(gameState->telaAtual == pause)
{
gameState->telaAtual = game;
return done;
}
break;
}
}
break;
case SDL_QUIT:
done = 1;
break;
}
}
const Uint8 *state = SDL_GetKeyboardState(NULL);
/*Não definimos o pulo usando SDLK pois diminui a fluidez do pulo, no entando
quando fazemos dessa forma, temos que criar uma solução para o limite do pulo
a função "SDL_SCANCODE_UP" registra todo o tempo em que a tecla esta apertada
entao criamos um limitador com a ajuda do professor.
*/
if (state[SDL_SCANCODE_UP] && !gameState->llamas.falling)
{
printf("\npulo");
gameState->llamas.y -= 20;
if(gameState->llamas.y < 5 )
{
gameState->llamas.y = 10;
gameState->llamas.falling = 1;
}
}
return done;
}
void collisionDetection(gamestate *gameState)
{
// Loop through all cacti in the game state
for (int i = 0; i < 100; i++)
{
// Get the dimensions and position of the llama and cactus sprites
int llamah = llammaHeight, llamaw = llamaWidth;
int llamax = gameState->llamas.x, llamay = gameState->llamas.y;
int cactusx = gameState->cactus[i].x, cactusy = gameState->cactus[i].y, cactusw = gameState->cactus[i].w, cactush = gameState->cactus[i].h;
// Check if there is a collision between the llama and the current cactus
if ((llamax > cactusx && llamax < cactusx + cactusw) && (llamay > cactusy))
{
// If there is a collision and the current screen is the game screen, change the game state to the game over screen
if (gameState->telaAtual == game)
{
gameState->telaAtual = gameOver;
break; // Exit the loop as soon as a collision is detected
}
}
}
}
void gameLogic(gamestate *gameState)
{
int score = 0;
int llama_y = gameState->llamas.y;
int cactus_move = gameState->cactus[0].x;
float speed = 2;
float acceleration = 3;
int clouds_move = gameState->sky.x;
// Update the position of the llama sprite
if (llama_y < 191)
{
// If the llama is falling, update its position based on gravity
gameState->llamas.y += 5;
}
else
{
gameState->llamas.falling = 0;
}
// Control the movement of the cacti and the speed of the game
if (cactus_move >= gameState->hitbox.x)
{
acceleration++;
gameState->cactus[0].x -= speed * acceleration;
}
// Reset cacti and create new ones as needed
if (cactus_move <= gameState->hitbox.x)
{
for (int i = 1; i < 100; i++)
{
gameState->cactus[i - 1].x = gameState->cactus[i].x;
}
gameState->cactus[99].x = cactusPositionX + rand() % 500;
gameState->cactus[99].y = cactusPositionY;
}
}
void doRender(SDL_Renderer *renderer, gamestate *gameState,screen_texture *start )
{
/*Nessa função é onde renderizamos todos os sprites e retângulos na tela
de acordo com o estado do jogo.*/
SDL_RenderClear(renderer);
if(gameState->telaAtual == initscreen || gameState->telaAtual == gameOver )
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);//define a cor do plano de fundo (preta)
}else
SDL_SetRenderDrawColor(renderer, 0, 180, 255, 255); //define a cor do plano de fundo (azul)
SDL_RenderClear(renderer);
if(gameState->telaAtual == game || gameState->telaAtual == pause)
{
/*Quando o jogo está no estado "game" e "pause" todas as texturas são renderizadas
usando SDL_rect e SDL_RenderCopy (usado para as texturas definidas em loadgame())*/
SDL_RenderClear(renderer);
SDL_Rect sky_texture = {gameState->sky.x, gameState->sky.y, gameState->sky.w, gameState->sky.h};
SDL_RenderCopyEx(renderer, gameState->skyTexture, NULL, &sky_texture, 0, NULL, 0);
SDL_Rect llamaTexture = {gameState->llamas.x, gameState->llamas.y, gameState->llamas.h, gameState->llamas.w};
SDL_RenderCopyEx(renderer, gameState->llama, NULL, &llamaTexture, 0, NULL, 0);
SDL_Rect cactusTexture = {gameState->cactus[0].x, gameState->cactus[0].y, cactusWidth, cactusHeight};
SDL_RenderCopyEx(renderer, gameState->cactu, NULL, &cactusTexture, 0, NULL, 0);
SDL_SetRenderDrawColor(renderer, 180, 155, 0, 0);
SDL_Rect rect = {gameState->floor.x, gameState->floor.y, gameState->floor.w, gameState->floor.h};
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 0);
SDL_Rect grassRect = {gameState->grass.x, gameState->grass.y, gameState->grass.w, gameState->grass.h};
SDL_RenderFillRect(renderer, &grassRect);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 0);
SDL_Rect hitboxRect = {gameState->hitbox.x, gameState->hitbox.y, gameState->hitbox.w, gameState->hitbox.h};
SDL_RenderFillRect(renderer, &hitboxRect);
SDL_Rect pause_Over_Text= {50, 20, 150, 30};
SDL_RenderCopyEx(renderer, start->pauseTexture, NULL, &pause_Over_Text, 0, NULL, 0);
}
if(gameState->telaAtual == gameOver)
{
SDL_Rect Game_Over_Text= {gameState->gameOverScreen.x, gameState->gameOverScreen.y, gameState->gameOverScreen.w, gameState->gameOverScreen.h};
SDL_RenderCopyEx(renderer, gameState->gameOverScreenTexture, NULL, &Game_Over_Text, 0, NULL, 0);
SDL_Rect game_over_llama_Text= {400, 250, 217, 210};
SDL_RenderCopyEx(renderer, start->restartLlammaTexture, NULL, &game_over_llama_Text, 0, NULL, 0);
SDL_Rect Start_Game_Text= {230, 480, 640, 40};
SDL_RenderCopyEx(renderer, start->screenTexture, NULL, &Start_Game_Text, 0, NULL, 0);
}else if(gameState->telaAtual == initscreen)
{
SDL_Rect Start_Game_Text= {250, 170, 640, 40};
SDL_RenderCopyEx(renderer, start->screenTexture, NULL, &Start_Game_Text, 0, NULL, 0);
SDL_Rect logo_Text= {450, 520, 148, 249};
SDL_RenderCopyEx(renderer, start->logoTexture, NULL, &logo_Text, 0, NULL, 0);
SDL_Rect studio_Text= {450, 480, 149, 30};
SDL_RenderCopyEx(renderer, start->studioTexture, NULL, &studio_Text, 0, NULL, 0);
}
SDL_RenderPresent(renderer);
}
void doUpdate(gamestate *gameState, int *done, screen_texture *start)
{
/*Essa função controla o estado principal do jogo e as funçoes de score, colisão e lógica*/
if (gameState->telaAtual == game)
{
ScoreCounting(gameState, start);
collisionDetection(gameState);
gameLogic(gameState);
}
}
int main(int argc, char *argv[])
{
int done = 0;
SDL_Renderer *renderer = NULL;
SDL_Window *window = NULL;
gamestate gameState;
gameState.telaAtual = initscreen;
screen_texture start;
SDL_Init(SDL_INIT_VIDEO);
srandom((int)time(NULL));
// Definição e criação da janela.
window = SDL_CreateWindow(
"Jack LLaman - Llaming Around!", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
1080, // width, in pixels
720, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
// Check that the window was successfully created
if (window == NULL)
{
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
gameState.renderer = renderer;
loadGame(&gameState, &start);
doRender(renderer, &gameState, &start);
//laço principal do jogo
while (!done)
{
done = processEvents(window, &gameState, &start);
doUpdate(&gameState, &done, &start);
doRender(renderer, &gameState, &start);
}
/*sdl2 collision check*/
// Close and destroy the window
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_DestroyTexture(gameState.llama);
SDL_DestroyTexture(gameState.cactu);
// Clean up
SDL_Quit();
return 0;
}