semi-working snake game

This commit is contained in:
sawyer committed 2025-09-19 13:08:16 -06:00
1 parent 2012e68995
commit d00e644100
14 files changed
+252 -40

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
#![no_std]
#![no_main]
extern crate alloc;
use abi::{
KeyCode, Rng,
display::{Display, SCREEN_HEIGHT, SCREEN_WIDTH},
get_key, lock_display, print, sleep,
};
use alloc::format;
use core::panic::PanicInfo;
use embedded_graphics::{pixelcolor::Rgb565, prelude::RgbColor};
use embedded_snake::{Direction, SnakeGame};
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
print(&format!(
"user panic: {} @ {:?}",
info.message(),
info.location(),
));
loop {}
}
#[unsafe(no_mangle)]
pub extern "Rust" fn _start() {
main()
}
const CELL_SIZE: usize = 8;
pub fn main() {
print("Starting Snake app");
let mut display = Display;
let mut game = SnakeGame::<100, Rgb565, Rng>::new(
SCREEN_WIDTH as u16,
SCREEN_HEIGHT as u16,
CELL_SIZE as u16,
CELL_SIZE as u16,
Rng,
Rgb565::BLACK,
Rgb565::GREEN,
Rgb565::RED,
50,
);
loop {
if let Some(event) = get_key() {
let direction = match event.key {
KeyCode::Up => Direction::Up,
KeyCode::Down => Direction::Down,
KeyCode::Right => Direction::Right,
KeyCode::Left => Direction::Left,
KeyCode::Esc => return,
_ => Direction::None,
};
game.set_direction(direction);
}
// ensure all draws show up at once
lock_display(true);
game.pre_draw(&mut display);
game.draw(&mut display);
lock_display(false);
sleep(15);
}
}