Update raylib examples.

This commit is contained in:
Christoffer Lerno
2025-11-27 20:42:35 +01:00
parent 6e4614b6a4
commit 9e14338b77
3 changed files with 869 additions and 928 deletions

View File

@@ -1,5 +1,5 @@
module arkanoid;
import raylib5;
import raylib55;
import std::math;
/**
*
@@ -34,23 +34,23 @@ enum GameScreen
struct Player
{
Vector2 position;
Vector2 size;
int life;
RLVector2 position;
RLVector2 size;
int life;
}
struct Ball
{
Vector2 position;
Vector2 speed;
int radius;
bool active;
RLVector2 position;
RLVector2 speed;
int radius;
bool active;
}
struct Brick
{
Vector2 position;
bool active;
RLVector2 position;
bool active;
}
//------------------------------------------------------------------------------------
@@ -63,36 +63,36 @@ bool pause = false;
Player player;
Ball ball;
Brick[BRICKS_PER_LINE][LINES_OF_BRICKS] brick;
Vector2 brick_size;
RLVector2 brick_size;
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
fn void main()
{
// Initialization (Note windowTitle is unused on Android)
//---------------------------------------------------------
rl::initWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "classic game: arkanoid");
// Initialization (Note windowTitle is unused on Android)
//---------------------------------------------------------
rl::init_window(SCREEN_WIDTH, SCREEN_HEIGHT, "classic game: arkanoid");
init_game();
rl::setTargetFPS(60);
//--------------------------------------------------------------------------------------
rl::set_target_fps(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!rl::windowShouldClose()) // Detect window close button or ESC key
{
// Update and Draw
//----------------------------------------------------------------------------------
update_draw_frame();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
unload_game(); // Unload loaded data (textures, sounds, models...)
// Main game loop
while (!rl::window_should_close()) // Detect window close button or ESC key
{
// Update and Draw
//----------------------------------------------------------------------------------
update_draw_frame();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
unload_game(); // Unload loaded data (textures, sounds, models...)
rl::closeWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
rl::close_window(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}
@@ -103,30 +103,30 @@ fn void main()
// Initialize game variables
fn void init_game()
{
brick_size = { (float)rl::getScreenWidth() / BRICKS_PER_LINE, 40 };
brick_size = { (float)rl::get_screen_width() / BRICKS_PER_LINE, 40 };
// Initialize player
player.position = { SCREEN_WIDTH/2, SCREEN_HEIGHT * 7 / 8 };
player.size = { SCREEN_WIDTH / 10, 20 };
player.life = PLAYER_MAX_LIFE;
// Initialize player
player.position = { SCREEN_WIDTH / 2, SCREEN_HEIGHT * 7 / 8 };
player.size = { SCREEN_WIDTH / 10, 20 };
player.life = PLAYER_MAX_LIFE;
// Initialize ball
ball.position = { SCREEN_WIDTH / 2, SCREEN_HEIGHT * 7 / 8 - 30 };
ball.speed = { 0, 0 };
ball.radius = 7;
ball.active = false;
// Initialize ball
ball.position = { SCREEN_WIDTH / 2, SCREEN_HEIGHT * 7 / 8 - 30 };
ball.speed = { 0, 0 };
ball.radius = 7;
ball.active = false;
// Initialize bricks
int initial_down_position = 50;
// Initialize bricks
int initial_down_position = 50;
for (int i = 0; i < LINES_OF_BRICKS; i++)
{
for (int j = 0; j < BRICKS_PER_LINE; j++)
{
brick[i][j].position = { j * brick_size.x + brick_size.x / 2, i * brick_size.y + initial_down_position };
brick[i][j].active = true;
}
}
for (int i = 0; i < LINES_OF_BRICKS; i++)
{
for (int j = 0; j < BRICKS_PER_LINE; j++)
{
brick[i][j].position = { j * brick_size.x + brick_size.x / 2, i * brick_size.y + initial_down_position };
brick[i][j].active = true;
}
}
}
// Update game (one frame)
@@ -134,178 +134,187 @@ fn void update_game()
{
if (game_over)
{
if (rl::isKeyPressed(rl::KEY_ENTER))
{
init_game();
game_over = false;
}
if (rl::is_key_pressed(ENTER))
{
init_game();
game_over = false;
}
}
if (rl::isKeyPressed((KeyboardKey)'P')) pause = !pause;
if (rl::is_key_pressed(P)) pause = !pause;
if (pause) return;
// Player movement logic
if (rl::isKeyDown(rl::KEY_LEFT)) player.position.x -= 5;
if ((player.position.x - player.size.x/2) <= 0) player.position.x = player.size.x/2;
if (rl::isKeyDown(rl::KEY_RIGHT)) player.position.x += 5;
if ((player.position.x + player.size.x/2) >= SCREEN_WIDTH) player.position.x = SCREEN_WIDTH - player.size.x/2;
// Player movement logic
if (rl::is_key_down(LEFT)) player.position.x -= 5;
if ((player.position.x - player.size.x / 2) <= 0) player.position.x = player.size.x / 2;
if (rl::is_key_down(RIGHT)) player.position.x += 5;
if ((player.position.x + player.size.x / 2) >= SCREEN_WIDTH) player.position.x = SCREEN_WIDTH - player.size.x / 2;
// Ball launching logic
if (!ball.active)
{
if (rl::isKeyPressed(rl::KEY_SPACE))
{
ball.active = true;
ball.speed = { 0, -5 };
}
}
// Ball launching logic
if (!ball.active)
{
if (rl::is_key_pressed(SPACE))
{
ball.active = true;
ball.speed = { 0, -5 };
}
}
// Ball movement logic
if (ball.active)
{
ball.position.x += ball.speed.x;
ball.position.y += ball.speed.y;
}
else
{
ball.position = { player.position.x, SCREEN_HEIGHT * 7 / 8 - 30 };
}
// Ball movement logic
if (ball.active)
{
// Collision logic: ball vs walls
if (((ball.position.x + ball.radius) >= SCREEN_WIDTH) || ((ball.position.x - ball.radius) <= 0)) ball.speed.x *= -1;
if ((ball.position.y - ball.radius) <= 0) ball.speed.y *= -1;
if ((ball.position.y + ball.radius) >= SCREEN_HEIGHT)
{
ball.speed = { 0, 0 };
ball.active = false;
ball.position += ball.speed;
}
else
{
ball.position = { player.position.x, SCREEN_HEIGHT * 7 / 8 - 30 };
}
player.life--;
}
// Collision logic: ball vs walls
// Collision logic: ball vs player
if (rl::checkCollisionCircleRec(ball.position, ball.radius, { player.position.x - player.size.x / 2, player.position.y - player.size.y / 2, player.size.x, player.size.y}))
{
if (ball.speed.y > 0)
{
ball.speed.y *= -1;
ball.speed.x = (ball.position.x - player.position.x) / (player.size.x / 2) * 5;
}
}
if ((ball.position.x + ball.radius >= SCREEN_WIDTH) || (ball.position.x - ball.radius <= 0)) ball.speed.x *= -1;
if (ball.position.y - ball.radius <= 0) ball.speed.y *= -1;
if (ball.position.y + ball.radius >= SCREEN_HEIGHT)
{
ball.speed = { 0, 0 };
ball.active = false;
player.life--;
}
// Collision logic: ball vs bricks
for (int i = 0; i < LINES_OF_BRICKS; i++)
{
for (int j = 0; j < BRICKS_PER_LINE; j++)
{
if (brick[i][j].active)
{
// Hit below
if (((ball.position.y - ball.radius) <= (brick[i][j].position.y + brick_size.y / 2)) &&
((ball.position.y - ball.radius) > (brick[i][j].position.y + brick_size.y / 2 + ball.speed.y)) &&
((math::abs(ball.position.x - brick[i][j].position.x)) < (brick_size.x / 2 + ball.radius * 2.0f / 3)) && (ball.speed.y < 0))
{
brick[i][j].active = false;
ball.speed.y *= -1;
}
// Hit above
else if (((ball.position.y + ball.radius) >= (brick[i][j].position.y - brick_size.y/2)) &&
((ball.position.y + ball.radius) < (brick[i][j].position.y - brick_size.y/2 + ball.speed.y)) &&
((math::abs(ball.position.x - brick[i][j].position.x)) < (brick_size.x / 2 + ball.radius * 2.0f / 3)) && (ball.speed.y > 0))
{
brick[i][j].active = false;
ball.speed.y *= -1;
}
// Hit left
else if (((ball.position.x + ball.radius) >= (brick[i][j].position.x - brick_size.x/2)) &&
((ball.position.x + ball.radius) < (brick[i][j].position.x - brick_size.x/2 + ball.speed.x)) &&
((math::abs(ball.position.y - brick[i][j].position.y)) < (brick_size.y / 2 + ball.radius * 2.0f / 3)) && (ball.speed.x > 0))
{
brick[i][j].active = false;
ball.speed.x *= -1;
}
// Hit right
else if (((ball.position.x - ball.radius) <= (brick[i][j].position.x + brick_size.x/2)) &&
((ball.position.x - ball.radius) > (brick[i][j].position.x + brick_size.x/2 + ball.speed.x)) &&
((math::abs(ball.position.y - brick[i][j].position.y)) < (brick_size.y / 2 + ball.radius * 2.0f / 3)) && (ball.speed.x < 0))
{
brick[i][j].active = false;
ball.speed.x *= -1;
}
}
}
}
// Collision logic: ball vs player
if (rl::check_collision_circle_rec(ball.position, ball.radius, { player.position.x - player.size.x / 2, player.position.y - player.size.y / 2, player.size.x, player.size.y}))
{
if (ball.speed.y > 0)
{
ball.speed.y *= -1;
ball.speed.x = (ball.position.x - player.position.x) / (player.size.x / 2) * 5;
}
}
// Game over logic
if (player.life <= 0)
{
game_over = true;
}
else
{
game_over = true;
for (int i = 0; i < LINES_OF_BRICKS; i++)
{
for (int j = 0; j < BRICKS_PER_LINE; j++)
{
if (brick[i][j].active) game_over = false;
}
}
}
// Collision logic: ball vs bricks
for (int i = 0; i < LINES_OF_BRICKS; i++)
{
for (int j = 0; j < BRICKS_PER_LINE; j++)
{
if (brick[i][j].active)
{
// Hit below
if (ball.position.y - ball.radius <= brick[i][j].position.y + brick_size.y / 2
&& ball.position.y - ball.radius > brick[i][j].position.y + brick_size.y / 2 + ball.speed.y
&& math::abs(ball.position.x - brick[i][j].position.x) < brick_size.x / 2 + ball.radius * 2.0f / 3
&& ball.speed.y < 0)
{
brick[i][j].active = false;
ball.speed.y *= -1;
}
// Hit above
else if (ball.position.y + ball.radius >= brick[i][j].position.y - brick_size.y / 2
&& ball.position.y + ball.radius < brick[i][j].position.y - brick_size.y/2 + ball.speed.y
&& math::abs(ball.position.x - brick[i][j].position.x) < brick_size.x / 2 + ball.radius * 2.0f / 3
&& ball.speed.y > 0)
{
brick[i][j].active = false;
ball.speed.y *= -1;
}
// Hit left
else if (ball.position.x + ball.radius >= brick[i][j].position.x - brick_size.x / 2
&& ball.position.x + ball.radius < brick[i][j].position.x - brick_size.x / 2 + ball.speed.x
&& math::abs(ball.position.y - brick[i][j].position.y) < brick_size.y / 2 + ball.radius * 2.0f / 3
&& ball.speed.x > 0)
{
brick[i][j].active = false;
ball.speed.x *= -1;
}
// Hit right
else if (ball.position.x - ball.radius <= brick[i][j].position.x + brick_size.x / 2
&& ball.position.x - ball.radius > brick[i][j].position.x + brick_size.x / 2 + ball.speed.x
&& math::abs(ball.position.y - brick[i][j].position.y) < brick_size.y / 2 + ball.radius * 2.0f / 3
&& ball.speed.x < 0)
{
brick[i][j].active = false;
ball.speed.x *= -1;
}
}
}
}
// Game over logic
if (player.life <= 0)
{
game_over = true;
return;
}
for (int i = 0; i < LINES_OF_BRICKS; i++)
{
for (int j = 0; j < BRICKS_PER_LINE; j++)
{
if (brick[i][j].active)
{
game_over = false;
return;
}
}
}
game_over = true;
}
// Draw game (one frame)
fn void draw_game()
{
rl::beginDrawing();
defer rl::endDrawing();
rl::clearBackground(rl::RAYWHITE);
rl::begin_drawing();
defer rl::end_drawing();
if (!game_over)
{
// Draw player bar
rl::drawRectangle((int)(player.position.x - player.size.x/2), (int)(player.position.y - player.size.y/2), (int)player.size.x, (int)player.size.y, rl::BLACK);
rl::clear_background(rl::RAYWHITE);
// Draw player lives
for (int i = 0; i < player.life; i++) rl::drawRectangle(20 + 40*i, SCREEN_HEIGHT - 30, 35, 10, rl::LIGHTGRAY);
if (!game_over)
{
// Draw player bar
rl::draw_rectangle((int)(player.position.x - player.size.x/2), (int)(player.position.y - player.size.y/2), (int)player.size.x, (int)player.size.y, rl::BLACK);
// Draw ball
rl::drawCircleV(ball.position, ball.radius, rl::MAROON);
// Draw player lives
for (int i = 0; i < player.life; i++) rl::draw_rectangle(20 + 40*i, SCREEN_HEIGHT - 30, 35, 10, rl::LIGHTGRAY);
// Draw bricks
for (int i = 0; i < LINES_OF_BRICKS; i++)
{
for (int j = 0; j < BRICKS_PER_LINE; j++)
{
if (brick[i][j].active)
{
if ((i + j) % 2 == 0)
{
rl::drawRectangle((int)(brick[i][j].position.x - brick_size.x/2), (int)(brick[i][j].position.y - brick_size.y/2), (int)brick_size.x, (int)brick_size.y, rl::GRAY);
}
else
{
rl::drawRectangle((int)(brick[i][j].position.x - brick_size.x/2), (int)(brick[i][j].position.y - brick_size.y/2), (int)brick_size.x, (int)brick_size.y, rl::DARKGRAY);
}
}
}
}
// Draw ball
rl::draw_circle_v(ball.position, ball.radius, rl::MAROON);
if (pause) rl::drawText("GAME PAUSED", SCREEN_WIDTH/2 - rl::measureText("GAME PAUSED", 40)/2, SCREEN_HEIGHT/2 - 40, 40, rl::GRAY);
}
else
{
rl::drawText("PRESS [ENTER] TO PLAY AGAIN", rl::getScreenWidth()/2 - rl::measureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, rl::getScreenHeight()/2 - 50, 20, rl::GRAY);
}
// Draw bricks
for (int i = 0; i < LINES_OF_BRICKS; i++)
{
for (int j = 0; j < BRICKS_PER_LINE; j++)
{
if (brick[i][j].active)
{
RLVector2 brick_pos = brick[i][j].position - brick_size / 2;
if ((i + j) % 2 == 0)
{
rl::draw_rectangle((int)brick_pos.x, (int)brick_pos.y, (int)brick_size.x, (int)brick_size.y, rl::GRAY);
}
else
{
rl::draw_rectangle((int)brick_pos.x, (int)brick_pos.y, (int)brick_size.x, (int)brick_size.y, rl::DARKGRAY);
}
}
}
}
if (pause) rl::draw_text("GAME PAUSED", SCREEN_WIDTH / 2 - rl::measure_text("GAME PAUSED", 40) / 2, SCREEN_HEIGHT / 2 - 40, 40, rl::GRAY);
}
else
{
rl::draw_text("PRESS [ENTER] TO PLAY AGAIN", rl::get_screen_width()/2 - rl::measure_text("PRESS [ENTER] TO PLAY AGAIN", 20) / 2, rl::get_screen_height() / 2 - 50, 20, rl::GRAY);
}
}
// Unload game variables
fn void unload_game()
{
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
}
// Update and Draw (one frame)
fn void update_draw_frame()
{
update_game();
draw_game();
update_game();
draw_game();
}

View File

@@ -1,5 +1,5 @@
module snake;
import raylib5;
import raylib55;
/**
*
* raylib - classic game: snake
@@ -16,26 +16,26 @@ const SQUARE_SIZE = 32;
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 450;
enum SnakeDirection
enum SnakeDirection : (RLVector2 dir)
{
RIGHT,
DOWN,
LEFT,
UP
RIGHT = { SQUARE_SIZE, 0 },
DOWN = { 0, SQUARE_SIZE },
LEFT = { -SQUARE_SIZE, 0 },
UP = { 0, -SQUARE_SIZE }
}
struct Snake
{
Vector2 position;
Vector2 size;
Color color;
RLVector2 position;
RLVector2 size;
RLColor color;
}
struct Food
{
Vector2 position;
Vector2 size;
bool active;
Color color;
RLVector2 position;
RLVector2 size;
bool active;
RLColor color;
}
@@ -46,201 +46,169 @@ bool pause = false;
Food fruit;
SnakeDirection snake_direction;
Snake[SNAKE_LENGTH] snake;
Vector2[SNAKE_LENGTH] snake_position;
RLVector2[SNAKE_LENGTH] snake_position;
bool allow_move = false;
Vector2 offset;
RLVector2 offset;
int counter_tail = 0;
fn void main()
{
rl::initWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "classic game: snake");
rl::init_window(SCREEN_WIDTH, SCREEN_HEIGHT, "classic game: snake");
init_game();
rl::setTargetFPS(60);
rl::set_target_fps(60);
while (!rl::windowShouldClose()) // Detect window close button or ESC key
{
update_draw_frame();
}
while (!rl::window_should_close()) // Detect window close button or ESC key
{
update_draw_frame();
}
unload_game();
unload_game();
rl::closeWindow();
rl::close_window();
}
// Initialize game variables
fn void init_game()
{
frames_counter = 0;
game_over = false;
pause = false;
frames_counter = 0;
game_over = false;
pause = false;
counter_tail = 1;
allow_move = false;
counter_tail = 1;
allow_move = false;
snake_direction = RIGHT;
offset.x = SCREEN_WIDTH % SQUARE_SIZE;
offset.y = SCREEN_HEIGHT % SQUARE_SIZE;
offset.x = SCREEN_WIDTH % SQUARE_SIZE;
offset.y = SCREEN_HEIGHT % SQUARE_SIZE;
foreach (i, &s : snake)
{
s.position = offset / 2;
s.size = (RLVector2)SQUARE_SIZE;
s.color = i ? rl::BLUE : rl::DARKBLUE;
}
for (int i = 0; i < SNAKE_LENGTH; i++)
{
snake[i].position = { offset.x / 2, offset.y / 2 };
snake[i].size = { SQUARE_SIZE, SQUARE_SIZE };
foreach (&pos : snake_position)
{
*pos = { 0, 0 };
}
if (i == 0)
{
snake[i].color = rl::DARKBLUE;
}
else
{
snake[i].color = rl::BLUE;
}
}
for (int i = 0; i < SNAKE_LENGTH; i++)
{
snake_position[i] = { 0.0f, 0.0f };
}
fruit.size = { SQUARE_SIZE, SQUARE_SIZE };
fruit.color = rl::SKYBLUE;
fruit.active = false;
fruit = { .size = (RLVector2)SQUARE_SIZE, .color = rl::SKYBLUE, .active = false };
}
fn RLVector2 random_fruit_position()
{
return offset / 2 + { (float)rl::get_random_value(0, SCREEN_WIDTH / SQUARE_SIZE - 1) * SQUARE_SIZE, (float)rl::get_random_value(0, (SCREEN_HEIGHT / SQUARE_SIZE) - 1) * SQUARE_SIZE };
}
fn void update_game()
{
if (game_over)
{
if (rl::isKeyPressed(rl::KEY_ENTER))
{
init_game();
game_over = false;
}
if (rl::is_key_pressed(ENTER))
{
init_game();
game_over = false;
}
return;
}
if (rl::isKeyPressed((KeyboardKey)'P')) pause = !pause;
if (rl::is_key_pressed(P)) pause = !pause;
if (pause) return;
if (rl::isKeyPressed(rl::KEY_RIGHT) && allow_move)
{
snake_direction = SnakeDirection.from_ordinal((snake_direction.ordinal + 1) % 4);
allow_move = false;
}
if (rl::isKeyPressed(rl::KEY_LEFT) && allow_move)
{
snake_direction = SnakeDirection.from_ordinal((snake_direction.ordinal + 3) % 4);
allow_move = false;
}
// Snake movement
for (int i = 0; i < counter_tail; i++) snake_position[i] = snake[i].position;
if (frames_counter++ % 5 != 0) return;
allow_move = true;
switch (snake_direction)
if (rl::is_key_pressed(RIGHT) && allow_move)
{
case RIGHT:
snake[0].position.x += SQUARE_SIZE;
snake[0].position.y += 0;
case UP:
snake[0].position.x += 0;
snake[0].position.y += -SQUARE_SIZE;
case DOWN:
snake[0].position.x += 0;
snake[0].position.y += SQUARE_SIZE;
case LEFT:
snake[0].position.x += -SQUARE_SIZE;
snake[0].position.y += 0;
default:
unreachable();
snake_direction = SnakeDirection.from_ordinal((snake_direction.ordinal + 1) % 4);
allow_move = false;
}
if (rl::is_key_pressed(LEFT) && allow_move)
{
snake_direction = SnakeDirection.from_ordinal((snake_direction.ordinal + 3) % 4);
allow_move = false;
}
for (int i = 1; i < counter_tail; i++)
{
snake[i].position = snake_position[i - 1];
}
// Wall behaviour
if (((snake[0].position.x) > (SCREEN_WIDTH - offset.x)) ||
((snake[0].position.y) > (SCREEN_HEIGHT - offset.y)) ||
(snake[0].position.x < 0) || (snake[0].position.y < 0))
{
game_over = true;
}
// Snake movement
foreach (i, &sp : snake_position[:counter_tail]) *sp = snake[i].position;
// Collision with yourself
for (int i = 1; i < counter_tail; i++)
{
if ((snake[0].position.x == snake[i].position.x) && (snake[0].position.y == snake[i].position.y)) game_over = true;
}
if (frames_counter++ % 20 != 0) return;
// Fruit position calculation
if (!fruit.active)
{
fruit.active = true;
fruit.position = { (float)rl::getRandomValue(0, (SCREEN_WIDTH / SQUARE_SIZE) - 1) * SQUARE_SIZE + offset.x / 2, (float)rl::getRandomValue(0, (SCREEN_HEIGHT / SQUARE_SIZE) - 1) * SQUARE_SIZE + offset.y / 2 };
allow_move = true;
snake[0].position += snake_direction.dir;
foreach (i, &s : snake[1 .. counter_tail - 1]) s.position = snake_position[i];
for (int i = 0; i < counter_tail; i++)
{
while ((fruit.position.x == snake[i].position.x) && (fruit.position.y == snake[i].position.y))
{
fruit.position = { (float)rl::getRandomValue(0, (SCREEN_WIDTH / SQUARE_SIZE) - 1) * SQUARE_SIZE + offset.x/2, (float)rl::getRandomValue(0, (SCREEN_HEIGHT / SQUARE_SIZE) - 1) * SQUARE_SIZE + offset.y / 2 };
i = 0;
}
}
}
// Wall behaviour
if (snake[0].position.comp_ge({SCREEN_WIDTH - offset.x, SCREEN_HEIGHT - offset.y}).or() || snake[0].position.comp_lt({ 0, 0 }).or())
{
game_over = true;
}
// Collision
if ((snake[0].position.x < (fruit.position.x + fruit.size.x) && (snake[0].position.x + snake[0].size.x) > fruit.position.x) &&
(snake[0].position.y < (fruit.position.y + fruit.size.y) && (snake[0].position.y + snake[0].size.y) > fruit.position.y))
{
snake[counter_tail].position = snake_position[counter_tail - 1];
counter_tail += 1;
fruit.active = false;
}
// Collision with yourself
for (int i = 1; i < counter_tail; i++)
{
if (snake[0].position == snake[i].position) game_over = true;
}
// Fruit position calculation
if (!fruit.active)
{
fruit.active = true;
fruit.position = random_fruit_position();
for (int i = 0; i < counter_tail; i++)
{
while (fruit.position == snake[i].position)
{
fruit.position = random_fruit_position();
i = 0;
}
}
}
// Collision
if (snake[0].position.comp_lt(fruit.position + fruit.size).and() && fruit.position.comp_lt(snake[0].position + snake[0].size).and())
{
snake[counter_tail].position = snake_position[counter_tail - 1];
counter_tail += 1;
fruit.active = false;
}
}
// Draw game (one frame)
fn void draw_game()
{
rl::beginDrawing();
rl::begin_drawing();
rl::clearBackground(rl::RAYWHITE);
rl::clear_background(rl::RAYWHITE);
if (!game_over)
{
// Draw grid lines
for (int i = 0; i < SCREEN_WIDTH / SQUARE_SIZE + 1; i++)
{
rl::drawLineV({(float)SQUARE_SIZE * i + offset.x/2, offset.y/2}, {(float)SQUARE_SIZE * i + offset.x/2, SCREEN_HEIGHT - offset.y/2}, rl::LIGHTGRAY);
}
defer rl::end_drawing();
if (game_over)
{
rl::draw_text("PRESS [ENTER] TO PLAY AGAIN", rl::get_screen_width() / 2 - rl::measure_text("PRESS [ENTER] TO PLAY AGAIN", 20)/2, rl::get_screen_height() / 2 - 50, 20, rl::GRAY);
return;
}
// Draw grid lines
for (int i = 0; i < SCREEN_WIDTH / SQUARE_SIZE + 1; i++)
{
rl::draw_line_v(offset / 2 + { (float)SQUARE_SIZE * i, 0}, {(float)SQUARE_SIZE * i + offset.x / 2, SCREEN_HEIGHT - offset.y / 2}, rl::LIGHTGRAY);
}
for (int i = 0; i < SCREEN_HEIGHT/SQUARE_SIZE + 1; i++)
{
rl::drawLineV({offset.x/2, (float)SQUARE_SIZE * i + offset.y / 2 }, { SCREEN_WIDTH - offset.x/2, (float)SQUARE_SIZE * i + offset.y / 2 }, rl::LIGHTGRAY);
}
for (int i = 0; i < SCREEN_HEIGHT/SQUARE_SIZE + 1; i++)
{
rl::draw_line_v(offset / 2 + { 0, (float)SQUARE_SIZE * i }, { SCREEN_WIDTH - offset.x/2, (float)SQUARE_SIZE * i + offset.y / 2 }, rl::LIGHTGRAY);
}
// Draw snake
for (int i = 0; i < counter_tail; i++) rl::drawRectangleV(snake[i].position, snake[i].size, snake[i].color);
// Draw snake
foreach (&s : snake[:counter_tail]) rl::draw_rectangle_v(s.position, s.size, s.color);
// Draw fruit to pick
rl::drawRectangleV(fruit.position, fruit.size, fruit.color);
// Draw fruit to pick
rl::draw_rectangle_v(fruit.position, fruit.size, fruit.color);
if (pause) rl::drawText("GAME PAUSED", SCREEN_WIDTH/2 - rl::measureText("GAME PAUSED", 40)/2, SCREEN_HEIGHT / 2 - 40, 40, rl::GRAY);
}
else
{
rl::drawText("PRESS [ENTER] TO PLAY AGAIN", rl::getScreenWidth()/2 - rl::measureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, rl::getScreenHeight()/2 - 50, 20, rl::GRAY);
}
if (pause) rl::draw_text("GAME PAUSED", SCREEN_WIDTH / 2 - rl::measure_text("GAME PAUSED", 40) / 2, SCREEN_HEIGHT / 2 - 40, 40, rl::GRAY);
rl::endDrawing();
}
// Unload game variables
fn void unload_game()
{
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
// TODO: Unload all dynamic loaded data (textures, sounds, models...)
}
// Update and Draw (one frame)

File diff suppressed because it is too large Load Diff