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/bouncing_ball.zig | |
| parent | 82fca5c9f29124f37f6dcd08e73e6999ce44a69d (diff) | |
| download | sketchbook-6933f99ddb2d7103515d26838496309a807bba26.tar.gz sketchbook-6933f99ddb2d7103515d26838496309a807bba26.zip | |
Move standalone sketches into sketchbook examples
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); +} |
