summaryrefslogtreecommitdiff
path: root/examples/noc/src/bouncing_ball.zig
blob: 24d951b3a508fd01829f391bc1bdbb22a56a880f (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
//! Example 1.2: Bouncing Ball, with vectors.
const sk = @import("sketch");
const vec = sk.math;

pub const name = "bouncing_ball";
pub const width = 640;
pub const height = 360;

pub const State = struct {
    position: vec.Vec = .{ 100, 100 },
    velocity: vec.Vec = .{ 2.5, 2 },
    clock: sk.Clock = .{},
};

pub fn update(s: *State, in: *const sk.Input) void {
    for (0..s.clock.steps(in)) |_| {
        s.position += s.velocity;
        if (s.position[0] > width or s.position[0] < 0) s.velocity[0] *= -1;
        if (s.position[1] > height or s.position[1] < 0) s.velocity[1] *= -1;
    }
}

const ball: 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);
    sk.draw.circle(f, s.position, 24, ball);
}