Files
2048/main.c
T
SnailMan eb8834324e Split the game core and ui code into their respective modules
Game state struct ends the variable mess in main.c
2026-03-06 15:24:33 +01:00

73 lines
2.0 KiB
C

#include "raylib.h"
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include "game.h"
#include "ui.h"
const Color tile_colors[] = {
LIGHTGRAY, // 0 - Empty
BEIGE, // 2
GOLD, // 4
ORANGE, // 8
ORANGE, // 16
RED, // 32
MAROON, // 64
YELLOW, // 128
GOLD, // 256
ORANGE, // 512
RED, // 1024
MAROON, // 2048
PINK, // 4096
MAGENTA, // 8192
};
static GameState_t game_state;
int main() {
srand(time(NULL));
reset_board(&game_state);
InitWindow(800, 600, "2048");
SetTargetFPS(30); // So my laptop doesn't burn
while (!WindowShouldClose()) {
ClearBackground(RAYWHITE);
BeginDrawing();
for (int y = 0; y < BOARD_HEIGHT; y++) {
for (int x = 0; x < BOARD_WIDTH; x++) {
int tile_value = game_state.board[y][x];
int pos_x = BOARD_MARGIN + x * (TILE_SIZE + TILE_SPACING);
int pos_y = BOARD_MARGIN + y * (TILE_SIZE + TILE_SPACING);
int color_index = (tile_value == 0) ? 0 : (int)log2(tile_value);
if (color_index >= sizeof(tile_colors) / sizeof(tile_colors[0])) {
color_index = sizeof(tile_colors) / sizeof(tile_colors[0]) - 1; // Cap to the last color
}
DrawRectangle(pos_x, pos_y, TILE_SIZE, TILE_SIZE, tile_colors[color_index]);
if (tile_value != 0) {
const char *text = TextFormat("%d", tile_value);
DrawText(text, pos_x + TILE_SIZE / 2 - MeasureText(text, 20) / 2, pos_y + TILE_SIZE / 2 - 10, 20, BLACK);
}
}
}
DrawText(TextFormat("Score: %d", game_state.score), 600, BOARD_MARGIN, 20, BLACK);
DrawText(TextFormat("Moves used: %d", game_state.moves_made), 600, BOARD_MARGIN + 20, 20, BLACK);
// Reset button
DrawRectangle(500, 455, 275, 24, RED);
DrawText("Reset", 500 + 275 / 2 - MeasureText("Reset", 20) / 2, 455 + 4, 20, WHITE);
EndDrawing();
handle_mouse_input(&game_state);
}
CloseWindow();
return 0;
}