introduce timer + no bomb on 1er click

This commit is contained in:
_N3m0 2024-03-15 13:28:04 +01:00
parent 0de7726160
commit 914d59f13f
1 changed files with 37 additions and 19 deletions

56
main.c
View File

@ -58,6 +58,9 @@ Rectangle grid = {0};
const Color text_color = BLACK; const Color text_color = BLACK;
const Color hover_color = RED; const Color hover_color = RED;
float timer = 0.0f;
int need_to_fill = 1;
#define INIT_TEXTURE(img, tex) \ #define INIT_TEXTURE(img, tex) \
Image img = {0}; \ Image img = {0}; \
Texture tex = {0} Texture tex = {0}
@ -140,7 +143,7 @@ int count_bomb(int x, int y)
return count; return count;
} }
void fill_game(void) void setup_game(void)
{ {
switch (current_game_type) { switch (current_game_type) {
case BEGINNER: case BEGINNER:
@ -167,11 +170,18 @@ void fill_game(void)
default: default:
assert(0 && "game mode not supported"); assert(0 && "game mode not supported");
} }
}
void fill_game(int click_x, int click_y)
{
setup_game();
for (int i=0; i<nb_bomb; i++) { for (int i=0; i<nb_bomb; i++) {
int x = rand() % game_size.x; int x = rand() % game_size.x;
int y = rand() % game_size.y; int y = rand() % game_size.y;
if (click_x == x && click_y == y)
continue;
if (game[y][x] == 'X') if (game[y][x] == 'X')
i--; i--;
else else
@ -228,8 +238,9 @@ void reload_game(void)
memset(game, 0, game_cap); memset(game, 0, game_cap);
memset(discover, 0, game_cap); memset(discover, 0, game_cap);
memset(zero, 0, game_cap); memset(zero, 0, game_cap);
fill_game(); need_to_fill = 1;
screen_resize_handle(); screen_resize_handle();
timer = 0.0f;
} }
void switch_mode(GameType gt) void switch_mode(GameType gt)
@ -288,22 +299,23 @@ int main(void)
double_tile_texture = LoadTexture("ressources/tile_2.png"); double_tile_texture = LoadTexture("ressources/tile_2.png");
triple_tile_texture = LoadTexture("ressources/tile_3.png"); triple_tile_texture = LoadTexture("ressources/tile_3.png");
fill_game(); setup_game();
screen_resize_handle(); screen_resize_handle();
int take_screenshot = 0; int take_screenshot = 0;
while (!WindowShouldClose()) { while (!WindowShouldClose()) {
if (IsWindowResized())
screen_resize_handle();
if (take_screenshot) {
TakeScreenshot("minesweeper-screenshot.png");
take_screenshot = 0;
}
BeginDrawing(); BeginDrawing();
{ {
if (IsWindowResized())
screen_resize_handle();
if (take_screenshot) {
TakeScreenshot("minesweeper-screenshot.png");
take_screenshot = 0;
}
timer += GetFrameTime();
ClearBackground((Color) { ClearBackground((Color) {
.r = 0x8E, .g = 0x8E, .b = 0x8E, .a = 255 .r = 0x8E, .g = 0x8E, .b = 0x8E, .a = 255
}); });
@ -394,12 +406,12 @@ int main(void)
int timer_pad = 10; int timer_pad = 10;
int timer_mid = (screen_width * 3) / 4; int timer_mid = (screen_width * 3) / 4;
DrawTexture( DrawTexture(
fixed_tile_texture, double_tile_texture,
timer_mid - timer_pad - fixed_tile_texture.width, 0, timer_mid - timer_pad - double_tile_texture.width, 0,
WHITE WHITE
); );
DrawTexture( DrawTexture(
fixed_tile_texture, double_tile_texture,
timer_mid + timer_pad, 0, timer_mid + timer_pad, 0,
WHITE WHITE
); );
@ -439,7 +451,8 @@ int main(void)
int mouse_in_grid = 0; int mouse_in_grid = 0;
int mouse_in_menu = 0; int mouse_in_menu = 0;
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { int mouse_pressed = IsMouseButtonPressed(MOUSE_BUTTON_LEFT);
if (mouse_pressed) {
if (grid.x < mouse_x && mouse_x < grid.x + grid.width && if (grid.x < mouse_x && mouse_x < grid.x + grid.width &&
grid.y < mouse_y && mouse_y < grid.y + grid.height) grid.y < mouse_y && mouse_y < grid.y + grid.height)
{ {
@ -451,10 +464,15 @@ int main(void)
} }
} }
if (mouse_in_grid && game_state == PLAYING) { mouse_x = ((mouse_x - grid.x) / grid.width) * game_size.x;
mouse_x = ((mouse_x - grid.x) / grid.width) * game_size.x; mouse_y = ((mouse_y - grid.y) / grid.height) * game_size.y;
mouse_y = ((mouse_y - grid.y) / grid.height) * game_size.y;
if (need_to_fill && mouse_pressed) {
fill_game(mouse_x, mouse_y);
need_to_fill = 0;
}
if (mouse_in_grid && game_state == PLAYING) {
discover[mouse_y][mouse_x] = 1; discover[mouse_y][mouse_x] = 1;
zero_click(mouse_x, mouse_y); zero_click(mouse_x, mouse_y);