extract render method

This commit is contained in:
2026-02-22 00:15:34 +01:00
parent 62a043372d
commit b1bc503bd5

View File

@@ -33,11 +33,6 @@ pub fn main() !void {
const deltaTime = rl.getFrameTime(); const deltaTime = rl.getFrameTime();
tickTimer += deltaTime; tickTimer += deltaTime;
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.white);
if (rl.isKeyPressed(.up)) { if (rl.isKeyPressed(.up)) {
game.setDirection(g.Direction.up); game.setDirection(g.Direction.up);
} }
@@ -57,28 +52,47 @@ pub fn main() !void {
try game.tick(); try game.tick();
} }
// Border render(
const fieldWidth = game.playFieldSize[0] * scaleFactor + scaleFactor; game,
const fieldHeight = game.playFieldSize[1] * scaleFactor + scaleFactor; scaleFactor,
rl.drawRectangle(0, 0, fieldWidth, scaleFactor, .black); // top gameOverText,
rl.drawRectangle(0, 0, scaleFactor, fieldHeight, .black); // left screenWidth,
rl.drawRectangle(0, game.playFieldSize[1] * scaleFactor, fieldWidth, scaleFactor, .black); // bottom gameOverTextWidth,
rl.drawRectangle(game.playFieldSize[0] * scaleFactor, 0, scaleFactor, fieldHeight, .black); // right screenHeight,
fontSize,
// Food youWinText,
rl.drawRectangle(game.food[0] * scaleFactor, game.food[1] * scaleFactor, scaleFactor, scaleFactor, .red); youWinTextWidth,
);
// Snake }
for (game.body.items, 0..) |part, i| { }
const color: rl.Color = if (i == 0) .green else .dark_green;
rl.drawRectangle(part[0] * scaleFactor, part[1] * scaleFactor, scaleFactor, scaleFactor, color); 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 {
} rl.beginDrawing();
defer rl.endDrawing();
if (game.state == g.State.lost) {
rl.drawText(gameOverText, @divTrunc(screenWidth - gameOverTextWidth, 2), @divTrunc(screenHeight - fontSize, 2), fontSize, .dark_gray); rl.clearBackground(.white);
}
if (game.state == g.State.won) { // Border
rl.drawText(youWinText, @divTrunc(screenWidth - youWinTextWidth, 2), @divTrunc(screenHeight - fontSize, 2), fontSize, .dark_gray); const fieldWidth = game.playFieldSize[0] * scaleFactor + scaleFactor;
} const fieldHeight = game.playFieldSize[1] * scaleFactor + scaleFactor;
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
rl.drawRectangle(game.playFieldSize[0] * scaleFactor, 0, scaleFactor, fieldHeight, .black); // right
// Food
rl.drawRectangle(game.food[0] * scaleFactor, game.food[1] * scaleFactor, scaleFactor, scaleFactor, .red);
// Snake
for (game.body.items, 0..) |part, i| {
const color: rl.Color = if (i == 0) .green else .dark_green;
rl.drawRectangle(part[0] * scaleFactor, part[1] * scaleFactor, scaleFactor, scaleFactor, color);
}
if (game.state == g.State.lost) {
rl.drawText(gameOverText, @divTrunc(screenWidth - gameOverTextWidth, 2), @divTrunc(screenHeight - fontSize, 2), fontSize, .dark_gray);
}
if (game.state == g.State.won) {
rl.drawText(youWinText, @divTrunc(screenWidth - youWinTextWidth, 2), @divTrunc(screenHeight - fontSize, 2), fontSize, .dark_gray);
} }
} }