Added functional UI

This also was very fun to implement with Raylib
This commit is contained in:
2026-03-05 23:59:40 +01:00
parent 9c7697371f
commit 93995ce276
+84 -52
View File
@@ -1,5 +1,6 @@
#include "raylib.h"
#include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
@@ -7,40 +8,35 @@
#define BOARD_HEIGHT 4 #define BOARD_HEIGHT 4
#define STARTING_TILES 3 #define STARTING_TILES 3
const char* tile_colors[] = { #define TILE_SIZE 100
"\033[0m", // 0 - Reset/default (empty tile) #define TILE_SPACING 10
"\033[48;5;230m", // 2 - Light beige #define BOARD_MARGIN 50
"\033[48;5;223m", // 4 - Tan
"\033[48;5;216m", // 8 - Orange-ish const Color tile_colors[] = {
"\033[48;5;209m", // 16 - Light orange LIGHTGRAY, // 0 - Empty
"\033[48;5;208m", // 32 - Orange BEIGE, // 2
"\033[48;5;202m", // 64 - Dark orange GOLD, // 4
"\033[48;5;226m", // 128 - Yellow ORANGE, // 8
"\033[48;5;220m", // 256 - Gold ORANGE, // 16
"\033[48;5;214m", // 512 - Orange-gold RED, // 32
"\033[48;5;208m", // 1024 - Bright orange MAROON, // 64
"\033[48;5;202m", // 2048 - Red-orange YELLOW, // 128
"\033[48;5;196m", // 4096 - Red GOLD, // 256
"\033[48;5;160m", // 8192 - Dark red ORANGE, // 512
RED, // 1024
MAROON, // 2048
PINK, // 4096
MAGENTA, // 8192
}; };
typedef enum { UP = 0, DOWN, LEFT, RIGHT } MOVE_DIRECTION; typedef enum { UP = 0, DOWN, LEFT, RIGHT } MOVE_DIRECTION;
static int board[BOARD_HEIGHT][BOARD_WIDTH] = {0}; static int board[BOARD_HEIGHT][BOARD_WIDTH] = {0};
void print_board() { bool is_dragging = false;
for (int y = 0; y < BOARD_HEIGHT; y++) { Vector2 drag_start_pos = {0, 0};
for (int x = 0; x < BOARD_WIDTH; x++) {
printf("%s%d ", tile_colors[board[y][x]], board[y][x]);
}
printf("\n");
}
printf("\033[0m"); // Reset color
}
// Depending on the direction, move the tiles and check if we can merge with the // Depending on the direction, move the tiles and check if we can merge with the
// tiles in that direction. We merge the tiles only if they are of the same value // tiles in that direction. We merge the tiles only if they are of the same value
// TODO: Add a guard agains double merge!
// 2 2 0 4 => 8 0 0 0 which is incorrect and should be 4 4 0 0
void move(MOVE_DIRECTION direction) { void move(MOVE_DIRECTION direction) {
bool merged[BOARD_HEIGHT][BOARD_WIDTH] = {0}; bool merged[BOARD_HEIGHT][BOARD_WIDTH] = {0};
switch (direction) { switch (direction) {
@@ -109,7 +105,7 @@ void move(MOVE_DIRECTION direction) {
board[target_y][x] = board[y][x]; board[target_y][x] = board[y][x];
board[y][x] = 0; board[y][x] = 0;
} else if (board[target_y][x] == board[y][x]) { } else if (board[target_y][x] == board[y][x]) {
merged[target_y][x] = true; merged[target_y][x] = true;
board[target_y][x] *= 2; board[target_y][x] *= 2;
board[y][x] = 0; board[y][x] = 0;
} }
@@ -175,7 +171,7 @@ void move(MOVE_DIRECTION direction) {
board[y][target_x] = board[y][x]; board[y][target_x] = board[y][x];
board[y][x] = 0; board[y][x] = 0;
} else if (board[y][target_x] == board[y][x]) { } else if (board[y][target_x] == board[y][x]) {
merged[y][target_x] = true; merged[y][target_x] = true;
board[y][target_x] *= 2; board[y][target_x] *= 2;
board[y][x] = 0; board[y][x] = 0;
} }
@@ -211,6 +207,40 @@ bool spawn_tile() {
return true; return true;
} }
void handle_mouse_input() {
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
is_dragging = true;
drag_start_pos = GetMousePosition();
} else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON) && is_dragging) {
Vector2 current_pos = GetMousePosition();
float dx = current_pos.x - drag_start_pos.x;
float dy = current_pos.y - drag_start_pos.y;
float drag_distance = sqrt(dx * dx + dy * dy);
// We don't want to register single clicks as a move
if (drag_distance >= 30) {
if (fabs(dx) > fabs(dy)) {
if (dx > 0) {
move(RIGHT);
} else {
move(LEFT);
}
} else {
if (dy > 0) {
move(DOWN);
} else {
move(UP);
}
}
spawn_tile();
}
is_dragging = false;
}
}
int main() { int main() {
srand(time(NULL)); srand(time(NULL));
@@ -225,28 +255,30 @@ int main() {
tile_count++; tile_count++;
} }
printf("2048 machen\n"); InitWindow(800, 600, "2048");
print_board(); SetTargetFPS(30); // So my laptop doesn't burn
move(UP);
printf("After move up:\n"); while (!WindowShouldClose()) {
print_board(); ClearBackground(RAYWHITE);
printf("After spawning a tile:\n"); BeginDrawing();
spawn_tile();
print_board(); for (int y = 0; y < BOARD_HEIGHT; y++) {
move(DOWN); for (int x = 0; x < BOARD_WIDTH; x++) {
printf("After move down:\n"); int tile_value = board[y][x];
print_board(); int pos_x = BOARD_MARGIN + x * (TILE_SIZE + TILE_SPACING);
spawn_tile(); int pos_y = BOARD_MARGIN + y * (TILE_SIZE + TILE_SPACING);
printf("After spawning a tile:\n"); int color_index = (tile_value == 0) ? 0 : (int)log2(tile_value);
print_board();
move(LEFT); DrawRectangle(pos_x, pos_y, TILE_SIZE, TILE_SIZE, tile_colors[color_index]);
printf("After move left:\n"); if (tile_value != 0) {
print_board(); const char *text = TextFormat("%d", tile_value);
spawn_tile(); DrawText(text, pos_x + TILE_SIZE / 2 - MeasureText(text, 20) / 2, pos_y + TILE_SIZE / 2 - 10, 20, BLACK);
printf("After spawning a tile:\n"); }
print_board(); }
move(RIGHT); }
printf("After move right:\n"); EndDrawing();
print_board(); handle_mouse_input();
}
CloseWindow();
return 0; return 0;
} }