Compiler in Rust (lexer, parser, checker, C codegen), maps, chars, dino fmt, dino lsp (diagnostics, formatting, outline, definition, hover, completion, signature help), docs, examples, VS Code/Vim files. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
34 lines
840 B
Plaintext
34 lines
840 B
Plaintext
// A game: bouncing square. Structs are values, no heap, no GC.
|
|
import gfx
|
|
|
|
struct Vec2 {
|
|
x, y: f32
|
|
|
|
func add(b: Vec2): Vec2 { return Vec2{x + b.x, y + b.y} }
|
|
func scale(k: f32): Vec2 { return Vec2{x * k, y * k} }
|
|
}
|
|
|
|
struct Player {
|
|
pos: Vec2
|
|
vel: Vec2
|
|
size: f32
|
|
|
|
func update(dt: f32) { // writes pos/vel: inferred as mutating
|
|
pos = pos.add(vel.scale(dt))
|
|
if pos.x < 0 || pos.x > 800 { vel.x = -vel.x }
|
|
if pos.y < 0 || pos.y > 600 { vel.y = -vel.y }
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
var win = gfx.open("Dino", 800, 600)
|
|
var player = Player{pos: Vec2{100, 100}, vel: Vec2{200, 150}, size: 32}
|
|
|
|
for win.is_open() {
|
|
player.update(win.dt())
|
|
win.clear(gfx.BLACK)
|
|
win.rect(player.pos.x, player.pos.y, player.size, player.size, gfx.GREEN)
|
|
win.present()
|
|
}
|
|
}
|