Files
rexlang/examples/c/bounce.dino
T
Your NameandClaude Opus 5.5 2f44f09b6a Add C and C++ library support
import "zlib.h" reads the header (cc -E + a small C declaration
parser), links via pkg-config or link "name", and builds .c/.cpp
files next to the program. C structs become Dino structs, pointers
become pointer types (nil = NULL), T* parameters take variables or
lists. Adds dino header, editor completion/hover for C names,
docs chapter 13, a raylib example, and tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 12:55:41 +01:00

53 lines
1.6 KiB
Plaintext

// A ball bouncing around a window, drawn with raylib (https://www.raylib.com).
// Needs raylib installed: `dino run bounce.dino`. Space adds a ball, Escape quits.
import "raylib.h"
const WIDTH = 800
const HEIGHT = 450
struct Ball {
pos: raylib.Vector2
speed: raylib.Vector2
radius: f32
color: raylib.Color
func step(dt: f32) {
pos.x += speed.x * dt
pos.y += speed.y * dt
if pos.x < radius || pos.x > f32(WIDTH) - radius { speed.x = -speed.x }
if pos.y < radius || pos.y > f32(HEIGHT) - radius { speed.y = -speed.y }
}
}
func new_ball(n: int): Ball {
var colors = [raylib.RED, raylib.GOLD, raylib.SKYBLUE, raylib.LIME, raylib.VIOLET]
var x = f32(100 + n * 37 % 600)
var y = f32(80 + n * 53 % 290)
return Ball{raylib.Vector2{x, y}, raylib.Vector2{f32(180 + n * 20), 140.0}, 20.0, colors[n % colors.len()]}
}
func main() {
raylib.InitWindow(WIDTH, HEIGHT, "Dino + raylib")
raylib.SetTargetFPS(60)
var balls = [new_ball(0)]
for !raylib.WindowShouldClose() {
if raylib.IsKeyPressed(raylib.KEY_SPACE) {
balls.push(new_ball(balls.len()))
}
var dt = raylib.GetFrameTime()
for i in 0..balls.len() {
balls[i].step(dt)
}
raylib.BeginDrawing()
raylib.ClearBackground(raylib.RAYWHITE)
for b in balls {
raylib.DrawCircleV(b.pos, b.radius, b.color)
}
raylib.DrawText("balls: {balls.len()} (space adds one)", 10, 10, 20, raylib.DARKGRAY)
raylib.EndDrawing()
}
raylib.CloseWindow()
}