diff --git a/build.zig b/build.zig index d8eee33..60fa64c 100644 --- a/build.zig +++ b/build.zig @@ -43,12 +43,13 @@ pub fn build(b: *std.Build) void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); - const exe_unit_tests = b.addTest(.{ - .root_module = exe_mod, - }); + const main_tests = b.addTest(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize }); + const game_tests = b.addTest(.{ .root_source_file = b.path("src/game.zig"), .target = target, .optimize = optimize }); - const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); + const run_exe_main_tests = b.addRunArtifact(main_tests); + const run_exe_game_tests = b.addRunArtifact(game_tests); const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&run_exe_unit_tests.step); + test_step.dependOn(&run_exe_main_tests.step); + test_step.dependOn(&run_exe_game_tests.step); } diff --git a/src/game.zig b/src/game.zig index a367df7..5dbe83d 100644 --- a/src/game.zig +++ b/src/game.zig @@ -1,7 +1,6 @@ const std = @import("std"); -const testing = std.testing; -const Direction = enum { +pub const Direction = enum { up, down, left, @@ -20,12 +19,12 @@ const Direction = enum { /// x, y const Vec2 = @Vector(2, i32); -const State = enum { +pub const State = enum { running, lost, }; -const Game = struct { +pub const Game = struct { playFieldSize: Vec2, direction: Direction, body: std.ArrayList(Vec2), @@ -121,8 +120,8 @@ const Game = struct { fn getEmptySpace(self: *Game) Vec2 { outer: while (true) { - const x = self.rng.random().intRangeLessThan(i32, 0, self.playFieldSize[0]); - const y = self.rng.random().intRangeLessThan(i32, 0, self.playFieldSize[1]); + const x = self.rng.random().intRangeLessThan(i32, 1, self.playFieldSize[0]); + const y = self.rng.random().intRangeLessThan(i32, 1, self.playFieldSize[1]); const potentialResult = Vec2{ x, y }; for (self.body.items) |pos| { @@ -136,6 +135,8 @@ const Game = struct { } }; +const testing = std.testing; + test "should initialize game" { const allocator = testing.allocator; diff --git a/src/main.zig b/src/main.zig index f6c7684..642b0e8 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,19 +1,19 @@ const std = @import("std"); const rl = @import("raylib"); -comptime { - _ = @import("game.zig"); -} +const g = @import("game.zig"); pub fn main() anyerror!void { - const alloc = std.heap.page_allocator; + const allocator = std.heap.page_allocator; + const scaleFactor = 20; - const screenWidth = 800; - const screenHeight = 450; + const screenWidth = 1000; + const screenHeight = 600; rl.initWindow(screenWidth, screenHeight, "Snake"); defer rl.closeWindow(); + rl.setTargetFPS(2); - rl.setTargetFPS(100); + var game = try g.Game.create(allocator); while (!rl.windowShouldClose()) { rl.beginDrawing(); @@ -21,10 +21,38 @@ pub fn main() anyerror!void { rl.clearBackground(.white); - const info = try std.fmt.allocPrintZ(alloc, "FPS: {}\n", .{rl.getFPS()}); + 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); + } - rl.drawText("Congrats! You created your first window!", 190, 200, 20, .light_gray); - rl.drawText(info, 0, 0, 20, .light_gray); - rl.drawRectangle(10, 10, 10, 10, .black); + try game.tick(); + + // Border + rl.drawRectangle(0, 0, scaleFactor, game.playFieldSize[1] * scaleFactor, .black); + rl.drawRectangle(0, 0, game.playFieldSize[0] * scaleFactor, scaleFactor, .black); + rl.drawRectangle(game.playFieldSize[0] * scaleFactor, 0, scaleFactor, game.playFieldSize[1] * scaleFactor, .black); + rl.drawRectangle(0, game.playFieldSize[1] * scaleFactor, game.playFieldSize[0] * scaleFactor, scaleFactor, .black); + rl.drawRectangle(game.playFieldSize[0] * scaleFactor, game.playFieldSize[1] * scaleFactor, scaleFactor, scaleFactor, .black); + + // Food + rl.drawRectangle(game.food[0] * scaleFactor, game.food[1] * scaleFactor, scaleFactor, scaleFactor, .red); + + // Snake + for (game.body.items) |part| { + rl.drawRectangle(part[0] * scaleFactor, part[1] * scaleFactor, scaleFactor, scaleFactor, .green); + } + + if (game.state == g.State.lost) { + rl.drawText("Congrats! You lost!", 190, 200, 20, .light_gray); + } } }