1
0
Fork 0
mirror of https://github.com/imjasonh/nescript synced 2026-07-08 00:45:38 +00:00
nescript/examples/arrays_and_functions.ne
Claude b8c9e41276
Add README, LICENSE, examples, fix draw parser lookahead
README: project overview, quick start, feature list, example table
LICENSE: MIT
4 new examples covering all language features:
  - arrays_and_functions: arrays, while loops, inline/regular functions
  - state_machine: multi-state flow with on enter/exit handlers
  - sprites_and_palettes: inline CHR data, palette switching, scroll, cast
  - mmc1_banked: MMC1 mapper, bank declarations, software multiply

Parser fix: draw statement keyword-arg parsing now checks for ':'
lookahead before consuming an identifier, preventing it from eating
the next statement as a keyword argument (e.g., `i += 1` after a draw).

https://claude.ai/code/session_01W6eQFStA66EuMKHUFo2rx3
2026-04-12 00:38:19 +00:00

83 lines
1.8 KiB
Text

// Arrays and Functions — demonstrates M2 features.
//
// Shows: arrays, functions with parameters and return values,
// while loops, constants, inline functions.
//
// Build: cargo run -- build examples/arrays_and_functions.ne
game "Arrays Demo" {
mapper: NROM
}
const NUM_ENEMIES: u8 = 4
const SPEED: u8 = 1
var player_x: u8 = 128
var player_y: u8 = 200
// Enemy positions stored in arrays
var enemy_x: u8[4] = [30, 80, 130, 200]
var enemy_y: u8[4] = [40, 80, 120, 60]
// Function: clamp value to screen bounds
fun clamp(val: u8, max: u8) -> u8 {
if val > max {
return max
}
return val
}
// Inline function: absolute difference
inline fun abs_diff(a: u8, b: u8) -> u8 {
if a > b {
return a - b
}
return b - a
}
// Function: check collision between two points
fun check_collision(x1: u8, y1: u8, x2: u8, y2: u8) -> u8 {
var dx: u8 = abs_diff(x1, x2)
var dy: u8 = abs_diff(y1, y2)
if dx < 8 {
if dy < 8 {
return 1
}
}
return 0
}
on frame {
// Player movement
if button.right { player_x += SPEED }
if button.left { player_x -= SPEED }
if button.down { player_y += SPEED }
if button.up { player_y -= SPEED }
player_x = clamp(player_x, 240)
player_y = clamp(player_y, 224)
// Update and draw enemies
var i: u8 = 0
while i < NUM_ENEMIES {
// Move enemies down slowly
enemy_y[i] += 1
if enemy_y[i] > 224 {
enemy_y[i] = 0
}
// Check collision with player
var hit: u8 = check_collision(player_x, player_y, enemy_x[i], enemy_y[i])
if hit == 1 {
// Reset enemy position on collision
enemy_y[i] = 0
}
draw Enemy at: (enemy_x[i], enemy_y[i])
i += 1
}
draw Player at: (player_x, player_y)
}
start Main