add ui score for implement restart

This commit is contained in:
2026-02-27 22:13:15 +01:00
parent c592e32bb3
commit 39600b2bf8
2 changed files with 52 additions and 45 deletions

View File

@@ -25,6 +25,8 @@ pub const State = enum {
won,
};
const startLen = 3;
pub const Game = struct {
playFieldSize: Vec2,
direction: Direction,
@@ -37,14 +39,14 @@ pub const Game = struct {
grow: bool,
pub fn score(self: Game) usize {
return self.body.items.len;
return self.body.items.len - startLen;
}
pub fn create(allocator: std.mem.Allocator) !Game {
var body = try std.ArrayList(Vec2).initCapacity(allocator, 400);
const startPos: u8 = 10;
for (0..3) |i| {
for (0..startLen) |i| {
const index = std.math.cast(i32, i) orelse unreachable;
try body.append(allocator, Vec2{ startPos - index, 10 });
}

View File

@@ -24,65 +24,51 @@ pub fn main() !void {
var timer: f32 = 0;
var isFast = false;
const fontSize = 50;
const gameOverText = "Game Over!";
const gameOverTextWidth = rl.measureText(gameOverText, fontSize);
const youWinText = "You Win!";
const youWinTextWidth = rl.measureText(youWinText, fontSize);
while (!rl.windowShouldClose()) {
const deltaTime = rl.getFrameTime();
timer += deltaTime;
if (rl.isKeyPressed(.up)) {
game.setDirection(g.Direction.up);
}
if (rl.isKeyPressed(.down)) {
game.setDirection(g.Direction.down);
}
if (rl.isKeyPressed(.right)) {
game.setDirection(g.Direction.right);
}
if (rl.isKeyPressed(.left)) {
game.setDirection(g.Direction.left);
}
if (rl.isKeyDown(.s)) {
if (!isFast) {
isFast = true;
timer = 0;
if (game.state == g.State.running) {
if (rl.isKeyPressed(.up)) game.setDirection(g.Direction.up);
if (rl.isKeyPressed(.down)) game.setDirection(g.Direction.down);
if (rl.isKeyPressed(.right)) game.setDirection(g.Direction.right);
if (rl.isKeyPressed(.left)) game.setDirection(g.Direction.left);
if (rl.isKeyDown(.s)) {
if (!isFast) {
isFast = true;
timer = 0;
}
} else {
isFast = false;
}
const interval = if (isFast) fastTickInterval else tickInterval;
if (timer >= interval) {
timer -= interval;
try game.tick();
}
} else {
isFast = false;
}
const interval = if (isFast) fastTickInterval else tickInterval;
if (timer >= interval) {
timer -= interval;
try game.tick();
if (rl.isKeyPressed(.r)) {
game.deinit();
game = try g.Game.create(allocator);
timer = 0;
isFast = false;
}
}
render(
game,
scaleFactor,
gameOverText,
screenWidth,
gameOverTextWidth,
screenHeight,
fontSize,
youWinText,
youWinTextWidth,
);
render(game, scaleFactor, screenWidth, screenHeight);
}
}
fn render(game: g.Game, scaleFactor: comptime_int, gameOverText: [:0]const u8, screenWidth: i32, gameOverTextWidth: i32, screenHeight: i32, fontSize: comptime_int, youWinText: [:0]const u8, youWinTextWidth: i32) void {
fn render(game: g.Game, scaleFactor: comptime_int, screenWidth: i32, screenHeight: i32) void {
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.white);
// Border
const fieldWidth = game.playFieldSize[0] * scaleFactor + scaleFactor;
const fieldHeight = game.playFieldSize[1] * scaleFactor + scaleFactor;
// Border
rl.drawRectangle(0, 0, fieldWidth, scaleFactor, .black); // top
rl.drawRectangle(0, 0, scaleFactor, fieldHeight, .black); // left
rl.drawRectangle(0, game.playFieldSize[1] * scaleFactor, fieldWidth, scaleFactor, .black); // bottom
@@ -97,10 +83,29 @@ fn render(game: g.Game, scaleFactor: comptime_int, gameOverText: [:0]const u8, s
rl.drawRectangle(part[0] * scaleFactor, part[1] * scaleFactor, scaleFactor, scaleFactor, color);
}
// Score in top border
var scoreBuf: [32]u8 = undefined;
const scoreText = std.fmt.bufPrintZ(&scoreBuf, "Score: {d}", .{game.score()}) catch unreachable;
rl.drawText(scoreText, scaleFactor + 5, 6, 18, .white);
// Game over / win overlays
const bigFontSize = 50;
const smallFontSize = 22;
const restartText = "Press R to restart";
const restartTextWidth = rl.measureText(restartText, smallFontSize);
const cx = @divTrunc(screenWidth, 2);
const cy = @divTrunc(screenHeight, 2);
if (game.state == g.State.lost) {
rl.drawText(gameOverText, @divTrunc(screenWidth - gameOverTextWidth, 2), @divTrunc(screenHeight - fontSize, 2), fontSize, .dark_gray);
const text = "Game Over!";
const tw = rl.measureText(text, bigFontSize);
rl.drawText(text, cx - @divTrunc(tw, 2), cy - @divTrunc(bigFontSize, 2), bigFontSize, .dark_gray);
rl.drawText(restartText, cx - @divTrunc(restartTextWidth, 2), cy + @divTrunc(bigFontSize, 2) + 8, smallFontSize, .dark_gray);
}
if (game.state == g.State.won) {
rl.drawText(youWinText, @divTrunc(screenWidth - youWinTextWidth, 2), @divTrunc(screenHeight - fontSize, 2), fontSize, .dark_gray);
const text = "You Win!";
const tw = rl.measureText(text, bigFontSize);
rl.drawText(text, cx - @divTrunc(tw, 2), cy - @divTrunc(bigFontSize, 2), bigFontSize, .dark_gray);
rl.drawText(restartText, cx - @divTrunc(restartTextWidth, 2), cy + @divTrunc(bigFontSize, 2) + 8, smallFontSize, .dark_gray);
}
}