diff options
Diffstat (limited to 'examples/noc/src/bouncing_ball.zig')
| -rw-r--r-- | examples/noc/src/bouncing_ball.zig | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/examples/noc/src/bouncing_ball.zig b/examples/noc/src/bouncing_ball.zig new file mode 100644 index 0000000..24d951b --- /dev/null +++ b/examples/noc/src/bouncing_ball.zig @@ -0,0 +1,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); +} |
