semi-working snake game

This commit is contained in:
2025-09-19 13:08:16 -06:00
parent 2012e68995
commit d00e644100
14 changed files with 252 additions and 40 deletions

View File

@@ -0,0 +1,10 @@
[package]
name = "snake"
version = "0.1.0"
edition = "2024"
[dependencies]
abi = { path = "../../abi" }
embedded-graphics = "0.8.1"
embedded-snake = { path = "../../../embedded-snake-rs" }
rand = { version = "0.9.0", default-features = false }

28
user-apps/snake/build.rs Normal file
View File

@@ -0,0 +1,28 @@
//! This build script copies the `memory.x` file from the crate root into
//! a directory where the linker can always find it at build time.
//! For many projects this is optional, as the linker always searches the
//! project root directory -- wherever `Cargo.toml` is. However, if you
//! are using a workspace or have a more complicated build setup, this
//! build script becomes required. Additionally, by requesting that
//! Cargo re-run the build script whenever `memory.x` is changed,
//! updating `memory.x` ensures a rebuild of the application with the
//! new memory settings.
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
fn main() {
// Put `memory.x` in our output directory and ensure it's
// on the linker search path.
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("memory.x"))
.unwrap()
.write_all(include_bytes!("../memory.x"))
.unwrap();
println!("cargo:rustc-link-search={}", out.display());
println!("cargo:rerun-if-changed=memory.x");
println!("cargo:rustc-link-arg-bins=-Tmemory.x");
}

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);
}
}