center text on screen
This commit is contained in:
+13
-6
@@ -6,9 +6,9 @@ test {
|
|||||||
_ = g;
|
_ = g;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() anyerror!void {
|
pub fn main() !void {
|
||||||
const allocator = std.heap.page_allocator;
|
const allocator = std.heap.page_allocator;
|
||||||
const scaleFactor = 40;
|
const scaleFactor = 30;
|
||||||
|
|
||||||
var game = try g.Game.create(allocator);
|
var game = try g.Game.create(allocator);
|
||||||
defer game.deinit();
|
defer game.deinit();
|
||||||
@@ -23,6 +23,12 @@ pub fn main() anyerror!void {
|
|||||||
const fastTickInterval: f32 = tickInterval / 3.0;
|
const fastTickInterval: f32 = tickInterval / 3.0;
|
||||||
var tickTimer: f32 = 0;
|
var tickTimer: f32 = 0;
|
||||||
|
|
||||||
|
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()) {
|
while (!rl.windowShouldClose()) {
|
||||||
const deltaTime = rl.getFrameTime();
|
const deltaTime = rl.getFrameTime();
|
||||||
tickTimer += deltaTime;
|
tickTimer += deltaTime;
|
||||||
@@ -63,15 +69,16 @@ pub fn main() anyerror!void {
|
|||||||
rl.drawRectangle(game.food[0] * scaleFactor, game.food[1] * scaleFactor, scaleFactor, scaleFactor, .red);
|
rl.drawRectangle(game.food[0] * scaleFactor, game.food[1] * scaleFactor, scaleFactor, scaleFactor, .red);
|
||||||
|
|
||||||
// Snake
|
// Snake
|
||||||
for (game.body.items) |part| {
|
for (game.body.items, 0..) |part, i| {
|
||||||
rl.drawRectangle(part[0] * scaleFactor, part[1] * scaleFactor, scaleFactor, scaleFactor, .green);
|
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) {
|
if (game.state == g.State.lost) {
|
||||||
rl.drawText("Game Over!", 150, 200, 20, .light_gray);
|
rl.drawText(gameOverText, @divTrunc(screenWidth - gameOverTextWidth, 2), @divTrunc(screenHeight - fontSize, 2), fontSize, .dark_gray);
|
||||||
}
|
}
|
||||||
if (game.state == g.State.won) {
|
if (game.state == g.State.won) {
|
||||||
rl.drawText("You Win!", 150, 200, 20, .light_gray);
|
rl.drawText(youWinText, @divTrunc(screenWidth - youWinTextWidth, 2), @divTrunc(screenHeight - fontSize, 2), fontSize, .dark_gray);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user