Skip to content

Commit

Permalink
Menu e novo obstaculo e inicio do Ranking
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcardosoy committed Nov 18, 2023
1 parent d0ae3a4 commit 17caaf9
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 41 deletions.
Binary file modified MarioRun_win64.exe
Binary file not shown.
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added src/assets/images/obstacles/obstacle4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/assets/scores.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Igor - 138532;
Caua - 11;
2 changes: 1 addition & 1 deletion src/include/mario_run.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void game_frame(Game game, bool* quit);
bool game_events(Game game);

// Executa o jogo em loop até o player decidir sair do jogo.
void game_run(Game game, bool* quit);
void game_run(Game game);

// Reseta o jogo e todos os paramestros de outros TADs.
void game_reset(Game game);
Expand Down
19 changes: 19 additions & 0 deletions src/include/ranking.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "utils.h"

typedef struct type_ranking* Ranking;

// Função para inicializar o ranking, abrindo o arquivo e lendo o ranking.
void ranking_init(Ranking *ranking);

// Função para salvar o ranking no arquivo.
void ranking_save(Ranking ranking);

// Função para adicionar um score ao ranking, no ranking temp.
void ranking_add(Ranking ranking, char* name, int score);

// Função para recuperar uma informação do raking, baseado no index.
char* ranking_get(Ranking ranking, int index);

// Função para destruir o ranking, fechando o arquivo e liberando memoria.
void ranking_destroy(Ranking *ranking);

56 changes: 43 additions & 13 deletions src/lib/mario_run.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void game_init(Game* game)
srand(time(NULL));

for (int i = 0; i < 5; i++) {
int type = (rand() % 3) + 1;
int type = (rand() % 5) + 1;

obstacle_init(&(*game)->obstacle, (*game)->renderer, (*game)->width, (*game)->height, type);
queue_enqueue((*game)->queue, (*game)->obstacle);
Expand All @@ -65,21 +65,37 @@ void game_init(Game* game)

bool game_menu(Game game, bool is_dead)
{
bool quit = false;
bool quit, stop;
quit = stop = false;
SDL_Event event;

while (!quit) {
if (!is_dead) {
text_render(game->text, game->renderer, game->width * 0.35, game->height * 0.1, game->width * 3, game->height, "src/assets/fonts/font.ttf", 100, "Mario Run");
text_render(game->text, game->renderer, game->width * 0.3, game->height * 0.5, game->width * 4, game->height / 2, "src/assets/fonts/font.ttf", 100, "Pressione qualquer tecla para jogar");
text_render(game->text, game->renderer, 10, 0, game->width * 2, game->height / 3, "src/assets/fonts/font.ttf", 50, "Pressione ESC para sair");
} else {
text_render(game->text, game->renderer, game->width * 0.35, game->height * 0.1, game->width * 3, game->height, "src/assets/fonts/font.ttf", 100, "Game Over");
text_render(game->text, game->renderer, game->width * 0.3, game->height * 0.5, game->width * 4, game->height / 2, "src/assets/fonts/font.ttf", 100, "Pressione qualquer tecla para jogar novamente");
text_render(game->text, game->renderer, 10, 0, game->width * 2, game->height / 3, "src/assets/fonts/font.ttf", 50, "Pressione ESC para sair");
}

SDL_RenderPresent(game->renderer);

while (!stop) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
quit = true;
stop = true;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_SPACE:
case SDLK_ESCAPE:
case SDLK_UP:
quit = true;
stop = true;
break;
default:
stop = true;
break;
}
}
Expand Down Expand Up @@ -109,7 +125,7 @@ void game_animate(Game game)
ground_animate(game->ground, game->renderer, game->width, game->height, game->speed);

if (obstacle_get_position_x(game->obstacle) <= -obstacle_get_width(game->obstacle)) {
int type = (rand() % 3) + 1;
int type = (rand() % 5) + 1;

obstacle_destroy(&game->obstacle);
game->obstacle = NULL;
Expand Down Expand Up @@ -208,16 +224,20 @@ void game_pause(Game game)
}
}

void game_run(Game game, bool* quit)
void game_run(Game game)
{
bool quit = false;

game_animate(game);
game_menu(game, false);

Mix_PlayMusic(game->main_song, -1);
Mix_VolumeMusic(MIX_MAX_VOLUME / 2);
if (!game_menu(game, false)) {

while (!*quit) {
game_frame(game, quit);
Mix_PlayMusic(game->main_song, -1);
Mix_VolumeMusic(MIX_MAX_VOLUME / 2);

while (!quit) {
game_frame(game, &quit);
}
}

game_destroy(&game);
Expand All @@ -244,6 +264,10 @@ void game_frame(Game game, bool* quit)
}

*quit = game_menu(game, true);

if (!*quit)
game_reset(game);

}

frame_time = SDL_GetTicks() - startLoop;
Expand All @@ -254,7 +278,13 @@ void game_frame(Game game, bool* quit)

void game_reset(Game game)
{
// Não sei se essa implementação funciona, deixar provisioriamente até futuros testes.
obstacle_reset_position(game->obstacle, game->width, game->height);
game->score = 0;
game->int_score = 50;
game->speed = 4;

character_destroy(&game->character);
character_init(&game->character, game->renderer, game->width, game->height);
}

bool are_colliding(Character character, Obstacle obstacle)
Expand Down
63 changes: 39 additions & 24 deletions src/lib/obstacle.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ void obstacle_init(Obstacle* obstacle, SDL_Renderer* renderer, int width, int he
*obstacle = malloc(sizeof(struct obstacle_type));

if (*obstacle != NULL) {

(*obstacle)->x = width;
(*obstacle)->y = height * 0.704;
(*obstacle)->type = type;

char path[40];
snprintf(path, 40, "./src/assets/images/pipes/pipe%d.png", type);
char path[45];
snprintf(path, 45, "./src/assets/images/obstacles/obstacle%d.png", type == 5 ? 4 : type);
(*obstacle)->texture = IMG_LoadTexture(renderer, path);

if ((*obstacle)->texture == NULL) {
Expand All @@ -32,27 +32,40 @@ void obstacle_init(Obstacle* obstacle, SDL_Renderer* renderer, int width, int he

void obstacle_animate(Obstacle obstacle, SDL_Renderer* renderer, int width, int height, double speed)
{

switch (obstacle->type) {
case 1:
obstacle->width = width * 0.08;
obstacle->height = height * 0.15;
break;
case 2:
obstacle->width = width * 0.13;
obstacle->height = height * 0.15;
break;
case 3:
obstacle->width = width * 0.18;
obstacle->height = height * 0.15;
break;
}
case 1:
obstacle->width = width * 0.08;
obstacle->height = height * 0.15;
break;
case 2:
obstacle->width = width * 0.13;
obstacle->height = height * 0.15;
break;
case 3:
obstacle->width = width * 0.18;
obstacle->height = height * 0.15;
break;
case 4:
obstacle->width = width * 0.11;
obstacle->height = height * 0.11;
if (obstacle->x == width) {
obstacle->y = height * 0.65;
}
break;
case 5:
obstacle->width = width * 0.11;
obstacle->height = height * 0.11;
if (obstacle->x == width) {
obstacle->y = height * 0.5;
}
break;
}

obstacle->x -= speed;

SDL_Rect rect = { obstacle->x, obstacle->y, obstacle->width, obstacle->height };
SDL_RenderCopy(renderer, obstacle->texture, NULL, &rect);

}

int obstacle_get_position_x(Obstacle obstacle)
Expand All @@ -74,20 +87,22 @@ int obstacle_get_width(Obstacle obstacle)

void obstacle_get_colision(Obstacle obstacle, int* x1, int* x2, int* y1, int* y2)
{
if (obstacle->type != 3)
{
if (obstacle->type < 3) {
*x1 = obstacle->x;
*x2 = obstacle->x + obstacle->width;
*y1 = obstacle->y;
*y2 = obstacle->y + obstacle->height;
} else {
} else if (obstacle->type == 3) {
*x1 = obstacle->x;
*x2 = obstacle->x + obstacle->width;
*y1 = obstacle->y + (obstacle->height * 0.19);
*y2 = obstacle->y + obstacle->height;
} else if (obstacle->type > 3) {
*x1 = obstacle->x + (obstacle->width * 0.15);
*x2 = obstacle->x + obstacle->width;
*y1 = obstacle->y;
*y2 = obstacle->y + obstacle->height;
}


}

void obstacle_destroy(Obstacle* obstacle)
Expand Down
Empty file added src/lib/ranking.c
Empty file.
5 changes: 2 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
int main(int argc, char* argv[])
{
Game game;
bool quit = false;


game_init(&game);

game_run(game, &quit);
game_run(game);

return 0;
}

0 comments on commit 17caaf9

Please sign in to comment.