Add console tetris demo

This commit is contained in:
Toby Jaffey 2025-12-07 21:02:22 +00:00
parent cd95e63af5
commit c0711213ae
9 changed files with 261 additions and 0 deletions

41
apps/zigtris/src/main.zig Normal file
View file

@ -0,0 +1,41 @@
const uvm = @import("uvm.zig");
const mibu = @import("mibu");
const zigtris = @import("zigtris");
var nextEvent:mibu.events.Event = .none;
const console = @import("console.zig").getWriter();
export fn main() void {
var gameRunning = true;
var lastUpdate:u32 = 0;
zigtris.gamesetup(console, uvm.millis()) catch |err| {
_ = console.print("err {any}\n", .{err}) catch 0;
_ = console.flush() catch 0;
return;
};
while(gameRunning) {
const now = uvm.millis();
if (uvm.getch()) |key| {
switch(key) {
' ' => nextEvent = mibu.events.Event{.key = .{.code = .{.char = ' '}}},
'a' => nextEvent = mibu.events.Event{.key = .{.code = .left}},
'd' => nextEvent = mibu.events.Event{.key = .{.code = .right}},
'w' => nextEvent = mibu.events.Event{.key = .{.code = .up}},
's' => nextEvent = mibu.events.Event{.key = .{.code = .down}},
'q' => gameRunning = false,
else => {},
}
}
if (now > lastUpdate + 100 or nextEvent != .none) {
lastUpdate = now;
gameRunning = zigtris.gameloop(console, now, nextEvent) catch false;
nextEvent = .none;
_ = console.flush() catch 0;
}
}
}