summaryrefslogtreecommitdiff
path: root/examples/noc/src/flappy_bird.zig
blob: 77c1e4a7b2cbec4ee40e531964533dabf3c5669d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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", .{});
    }
}