extract render method
This commit is contained in:
70
src/main.zig
70
src/main.zig
@@ -33,11 +33,6 @@ pub fn main() !void {
|
||||
const deltaTime = rl.getFrameTime();
|
||||
tickTimer += deltaTime;
|
||||
|
||||
rl.beginDrawing();
|
||||
defer rl.endDrawing();
|
||||
|
||||
rl.clearBackground(.white);
|
||||
|
||||
if (rl.isKeyPressed(.up)) {
|
||||
game.setDirection(g.Direction.up);
|
||||
}
|
||||
@@ -57,28 +52,47 @@ pub fn main() !void {
|
||||
try game.tick();
|
||||
}
|
||||
|
||||
// Border
|
||||
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);
|
||||
}
|
||||
render(
|
||||
game,
|
||||
scaleFactor,
|
||||
gameOverText,
|
||||
screenWidth,
|
||||
gameOverTextWidth,
|
||||
screenHeight,
|
||||
fontSize,
|
||||
youWinText,
|
||||
youWinTextWidth,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
rl.clearBackground(.white);
|
||||
|
||||
// Border
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user