diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-16 21:28:20 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-16 21:28:20 -0300 |
| commit | 6933f99ddb2d7103515d26838496309a807bba26 (patch) | |
| tree | 991c2fe7d4a57957222876e190fa4d16a0fb889c /examples/noc/src/flappy_bird.zig | |
| parent | 82fca5c9f29124f37f6dcd08e73e6999ce44a69d (diff) | |
| download | sketchbook-6933f99ddb2d7103515d26838496309a807bba26.tar.gz sketchbook-6933f99ddb2d7103515d26838496309a807bba26.zip | |
Move standalone sketches into sketchbook examples
Diffstat (limited to 'examples/noc/src/flappy_bird.zig')
| -rw-r--r-- | examples/noc/src/flappy_bird.zig | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/examples/noc/src/flappy_bird.zig b/examples/noc/src/flappy_bird.zig new file mode 100644 index 0000000..77c1e4a --- /dev/null +++ b/examples/noc/src/flappy_bird.zig @@ -0,0 +1,140 @@ +//! Nature of Code, Example 11.1: Flappy Bird, as a game: click or space +//! flaps, a pipe ends the round, a click starts the next. The book's +//! version only prints "OOPS!" and flies on; the score, the end of a round +//! and the restart are added here. +const std = @import("std"); +const sk = @import("sketch"); + +pub const name = "flappy_bird"; +pub const width = 640; +pub const height = 360; + +const bird_x = 50; +const gravity = 0.5; +const flap_force = -10; +const pipe_width = 20; +const pipe_speed = 2; +/// The gap between a pipe's top and bottom. +const spacing = 100; +/// Steps between new pipes (the book's `frameCount % 100`). +const pipe_every = 100; + +const Bird = struct { + y: f32 = 120, + velocity: f32 = 0, + + fn step(b: *Bird) void { + b.velocity += gravity; + b.y += b.velocity; + b.velocity *= 0.95; + // The floor. + if (b.y > height) { + b.y = height; + b.velocity = 0; + } + } +}; + +const Pipe = struct { + x: f32 = width, + top: f32, + /// Counted in the score once the bird is past it. + passed: bool = false, + + fn collides(p: Pipe, b: Bird) bool { + const vertical = b.y < p.top or b.y > p.top + spacing; + const horizontal = bird_x > p.x and bird_x < p.x + pipe_width; + return vertical and horizontal; + } +}; + +pub const State = struct { + rng: std.Random.Xoshiro256 = .init(11), + bird: Bird = .{}, + pipes: sk.List(Pipe, 8) = .{}, + /// Steps this round, for spacing the pipes. + steps: u32 = 0, + score: u32 = 0, + best: u32 = 0, + /// The round is over: the bird hit pipe `hit`. + over: bool = false, + hit: u8 = 0, + clock: sk.Clock = .{}, +}; + +pub fn init(s: *State) void { + newRound(s); +} + +fn newRound(s: *State) void { + s.bird = .{}; + s.pipes.clear(); + s.steps = 0; + s.score = 0; + s.over = false; + addPipe(s); +} + +fn addPipe(s: *State) void { + s.pipes.appendBounded(.{ .top = sk.math.uniform(s.rng.random(), 0, height - spacing) }) catch {}; +} + +pub fn update(s: *State, in: *const sk.Input) void { + const flap = in.mousePressed(.left) != null or in.pressed(sk.Key.space); + if (s.over) { + // The round is over: nothing moves until the next one starts. + if (flap) newRound(s); + return; + } + if (flap) s.bird.velocity += flap_force; + for (0..s.clock.steps(in)) |_| if (!s.over) step(s); +} + +fn step(s: *State) void { + // The book's order: pipes, then the bird, then maybe a new pipe. + var i = s.pipes.count(); + while (i > 0) { + i -= 1; + const p = &s.pipes.slice()[i]; + p.x -= pipe_speed; + if (p.collides(s.bird)) { + s.over = true; + s.hit = @intCast(i); + } + if (!p.passed and p.x + pipe_width < bird_x) { + p.passed = true; + s.score += 1; + s.best = @max(s.best, s.score); + } + if (p.x < -pipe_width) { + _ = s.pipes.orderedRemove(i); + if (s.over and s.hit > i) s.hit -= 1; + } + } + if (s.over) return; + s.bird.step(); + s.steps += 1; + if (s.steps % pipe_every == 0) addPipe(s); +} + +const pipe_pen: sk.draw.Pen = .{ .fill = .black }; +const panel: sk.draw.Pen = .{ .fill = sk.Color.white.alpha(220), .stroke = .gray(100), .width = 2 }; +const bird_pen: sk.draw.Pen = .{ .fill = .gray(127), .stroke = .black, .width = 2 }; + +pub fn draw(s: *const State, f: *sk.Frame) void { + f.clear(sk.Color.white); + for (s.pipes.constSlice()) |p| { + sk.draw.rect(f, .{ .x = p.x, .y = 0, .w = pipe_width, .h = p.top }, pipe_pen); + sk.draw.rect(f, .{ .x = p.x, .y = p.top + spacing, .w = pipe_width, .h = height - p.top - spacing }, pipe_pen); + } + sk.draw.circle(f, .{ bird_x, s.bird.y }, 8, bird_pen); + // Text on panels of its own, so pipes do not hide it. + sk.draw.rect(f, .{ .x = -2, .y = -2, .w = 252, .h = 42 }, panel); + f.print(8, 8, 2, sk.Color.gray(100), "score {d} best {d}", .{ s.score, s.best }); + if (s.over) { + const p = s.pipes.constSlice()[s.hit]; + f.print(p.x, p.top + 20, 2, sk.Color.hex(0xd02020), "OOPS!", .{}); + sk.draw.rect(f, .{ .x = 150, .y = 158, .w = 340, .h = 46 }, panel); + f.print(176, 166, 2, sk.Color.black, "click to fly again", .{}); + } +} |
