feat: build UI/Graphics
This commit is contained in:
11
build.zig
11
build.zig
@@ -43,12 +43,13 @@ pub fn build(b: *std.Build) void {
|
|||||||
const run_step = b.step("run", "Run the app");
|
const run_step = b.step("run", "Run the app");
|
||||||
run_step.dependOn(&run_cmd.step);
|
run_step.dependOn(&run_cmd.step);
|
||||||
|
|
||||||
const exe_unit_tests = b.addTest(.{
|
const main_tests = b.addTest(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize });
|
||||||
.root_module = exe_mod,
|
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");
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
13
src/game.zig
13
src/game.zig
@@ -1,7 +1,6 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const testing = std.testing;
|
|
||||||
|
|
||||||
const Direction = enum {
|
pub const Direction = enum {
|
||||||
up,
|
up,
|
||||||
down,
|
down,
|
||||||
left,
|
left,
|
||||||
@@ -20,12 +19,12 @@ const Direction = enum {
|
|||||||
/// x, y
|
/// x, y
|
||||||
const Vec2 = @Vector(2, i32);
|
const Vec2 = @Vector(2, i32);
|
||||||
|
|
||||||
const State = enum {
|
pub const State = enum {
|
||||||
running,
|
running,
|
||||||
lost,
|
lost,
|
||||||
};
|
};
|
||||||
|
|
||||||
const Game = struct {
|
pub const Game = struct {
|
||||||
playFieldSize: Vec2,
|
playFieldSize: Vec2,
|
||||||
direction: Direction,
|
direction: Direction,
|
||||||
body: std.ArrayList(Vec2),
|
body: std.ArrayList(Vec2),
|
||||||
@@ -121,8 +120,8 @@ const Game = struct {
|
|||||||
|
|
||||||
fn getEmptySpace(self: *Game) Vec2 {
|
fn getEmptySpace(self: *Game) Vec2 {
|
||||||
outer: while (true) {
|
outer: while (true) {
|
||||||
const x = self.rng.random().intRangeLessThan(i32, 0, self.playFieldSize[0]);
|
const x = self.rng.random().intRangeLessThan(i32, 1, self.playFieldSize[0]);
|
||||||
const y = self.rng.random().intRangeLessThan(i32, 0, self.playFieldSize[1]);
|
const y = self.rng.random().intRangeLessThan(i32, 1, self.playFieldSize[1]);
|
||||||
|
|
||||||
const potentialResult = Vec2{ x, y };
|
const potentialResult = Vec2{ x, y };
|
||||||
for (self.body.items) |pos| {
|
for (self.body.items) |pos| {
|
||||||
@@ -136,6 +135,8 @@ const Game = struct {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const testing = std.testing;
|
||||||
|
|
||||||
test "should initialize game" {
|
test "should initialize game" {
|
||||||
const allocator = testing.allocator;
|
const allocator = testing.allocator;
|
||||||
|
|
||||||
|
|||||||
50
src/main.zig
50
src/main.zig
@@ -1,19 +1,19 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const rl = @import("raylib");
|
const rl = @import("raylib");
|
||||||
comptime {
|
const g = @import("game.zig");
|
||||||
_ = @import("game.zig");
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn main() anyerror!void {
|
pub fn main() anyerror!void {
|
||||||
const alloc = std.heap.page_allocator;
|
const allocator = std.heap.page_allocator;
|
||||||
|
const scaleFactor = 20;
|
||||||
|
|
||||||
const screenWidth = 800;
|
const screenWidth = 1000;
|
||||||
const screenHeight = 450;
|
const screenHeight = 600;
|
||||||
|
|
||||||
rl.initWindow(screenWidth, screenHeight, "Snake");
|
rl.initWindow(screenWidth, screenHeight, "Snake");
|
||||||
defer rl.closeWindow();
|
defer rl.closeWindow();
|
||||||
|
rl.setTargetFPS(2);
|
||||||
|
|
||||||
rl.setTargetFPS(100);
|
var game = try g.Game.create(allocator);
|
||||||
|
|
||||||
while (!rl.windowShouldClose()) {
|
while (!rl.windowShouldClose()) {
|
||||||
rl.beginDrawing();
|
rl.beginDrawing();
|
||||||
@@ -21,10 +21,38 @@ pub fn main() anyerror!void {
|
|||||||
|
|
||||||
rl.clearBackground(.white);
|
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);
|
try game.tick();
|
||||||
rl.drawText(info, 0, 0, 20, .light_gray);
|
|
||||||
rl.drawRectangle(10, 10, 10, 10, .black);
|
// 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user