Added functional UI
This also was very fun to implement with Raylib
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "raylib.h"
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
@@ -7,40 +8,35 @@
|
||||
#define BOARD_HEIGHT 4
|
||||
#define STARTING_TILES 3
|
||||
|
||||
const char* tile_colors[] = {
|
||||
"\033[0m", // 0 - Reset/default (empty tile)
|
||||
"\033[48;5;230m", // 2 - Light beige
|
||||
"\033[48;5;223m", // 4 - Tan
|
||||
"\033[48;5;216m", // 8 - Orange-ish
|
||||
"\033[48;5;209m", // 16 - Light orange
|
||||
"\033[48;5;208m", // 32 - Orange
|
||||
"\033[48;5;202m", // 64 - Dark orange
|
||||
"\033[48;5;226m", // 128 - Yellow
|
||||
"\033[48;5;220m", // 256 - Gold
|
||||
"\033[48;5;214m", // 512 - Orange-gold
|
||||
"\033[48;5;208m", // 1024 - Bright orange
|
||||
"\033[48;5;202m", // 2048 - Red-orange
|
||||
"\033[48;5;196m", // 4096 - Red
|
||||
"\033[48;5;160m", // 8192 - Dark red
|
||||
#define TILE_SIZE 100
|
||||
#define TILE_SPACING 10
|
||||
#define BOARD_MARGIN 50
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
typedef enum { UP = 0, DOWN, LEFT, RIGHT } MOVE_DIRECTION;
|
||||
static int board[BOARD_HEIGHT][BOARD_WIDTH] = {0};
|
||||
|
||||
void print_board() {
|
||||
for (int y = 0; y < BOARD_HEIGHT; y++) {
|
||||
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
|
||||
}
|
||||
bool is_dragging = false;
|
||||
Vector2 drag_start_pos = {0, 0};
|
||||
|
||||
// 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
|
||||
// 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) {
|
||||
bool merged[BOARD_HEIGHT][BOARD_WIDTH] = {0};
|
||||
switch (direction) {
|
||||
@@ -109,7 +105,7 @@ void move(MOVE_DIRECTION direction) {
|
||||
board[target_y][x] = board[y][x];
|
||||
board[y][x] = 0;
|
||||
} 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[y][x] = 0;
|
||||
}
|
||||
@@ -175,7 +171,7 @@ void move(MOVE_DIRECTION direction) {
|
||||
board[y][target_x] = board[y][x];
|
||||
board[y][x] = 0;
|
||||
} 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][x] = 0;
|
||||
}
|
||||
@@ -211,6 +207,40 @@ bool spawn_tile() {
|
||||
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() {
|
||||
srand(time(NULL));
|
||||
|
||||
@@ -225,28 +255,30 @@ int main() {
|
||||
tile_count++;
|
||||
}
|
||||
|
||||
printf("2048 machen\n");
|
||||
print_board();
|
||||
move(UP);
|
||||
printf("After move up:\n");
|
||||
print_board();
|
||||
printf("After spawning a tile:\n");
|
||||
spawn_tile();
|
||||
print_board();
|
||||
move(DOWN);
|
||||
printf("After move down:\n");
|
||||
print_board();
|
||||
spawn_tile();
|
||||
printf("After spawning a tile:\n");
|
||||
print_board();
|
||||
move(LEFT);
|
||||
printf("After move left:\n");
|
||||
print_board();
|
||||
spawn_tile();
|
||||
printf("After spawning a tile:\n");
|
||||
print_board();
|
||||
move(RIGHT);
|
||||
printf("After move right:\n");
|
||||
print_board();
|
||||
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 = 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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
EndDrawing();
|
||||
handle_mouse_input();
|
||||
}
|
||||
CloseWindow();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user