1
0
Fork 0
mirror of https://github.com/imjasonh/nescript synced 2026-07-08 08:55:38 +00:00
nescript/examples/sprites_and_palettes.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

69 lines
1.5 KiB
Text

// Sprites and Palettes — demonstrates M3 asset features.
//
// Shows: sprite declarations with inline CHR data,
// palette declarations, set_palette, type casting, scroll.
//
// Build: cargo run -- build examples/sprites_and_palettes.ne
game "Asset Demo" {
mapper: NROM
}
// Define a sprite with inline CHR tile data (16 bytes = one 8x8 tile)
// This is a simple arrow pointing right
sprite Arrow {
chr: [0x18, 0x1C, 0xFE, 0xFF, 0xFF, 0xFE, 0x1C, 0x18,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
}
// Define a sprite with a heart shape
sprite Heart {
chr: [0x66, 0xFF, 0xFF, 0xFF, 0x7E, 0x3C, 0x18, 0x00,
0x66, 0xFF, 0xFF, 0xFF, 0x7E, 0x3C, 0x18, 0x00]
}
// Custom color palette
palette Warm {
colors: [0x0F, 0x06, 0x16, 0x26]
}
palette Cool {
colors: [0x0F, 0x01, 0x11, 0x21]
}
var px: u8 = 128
var py: u8 = 120
var scroll_x: u8 = 0
var use_warm: u8 = 1
on frame {
// Movement
if button.right { px += 2 }
if button.left { px -= 2 }
if button.down { py += 2 }
if button.up { py -= 2 }
// Scroll background
scroll_x += 1
scroll(scroll_x, 0)
// Toggle palette with A button
if button.a {
if use_warm == 1 {
set_palette Cool
use_warm = 0
} else {
set_palette Warm
use_warm = 1
}
}
// Type cast demo
var wide: u16 = px as u16
// Draw sprites
draw Arrow at: (px, py)
draw Heart at: (px, py - 16)
}
start Main