update of the docs and implementing a proper scorebord in snake demo

This commit is contained in:
Zenn 2023-08-14 19:32:34 +02:00
parent c21aa97915
commit 0721c6cc45
5 changed files with 46 additions and 22 deletions

View file

@ -270,6 +270,8 @@ const BUTTON_DELAY: u32 = 200;
pub unsafe extern "C" fn setup() { pub unsafe extern "C" fn setup() {
// put your setup code here, to run once: // put your setup code here, to run once:
arduboy.begin(); arduboy.begin();
arduboy.audio_on();
arduboy.audio_save_on_off()
} }
// The loop() function repeats forever after setup() is done // The loop() function repeats forever after setup() is done
#[no_mangle] #[no_mangle]

View file

@ -7,15 +7,28 @@ struct Scorebord {
places: [u16; 3], places: [u16; 3],
} }
impl Scorebord { impl Scorebord {
fn check_score(&mut self, mut score: u16) -> bool { fn check_score(&self, score: u16) -> bool {
let res = self.places[2] < score; self.places[2] < score
//todo }
res fn update_score(&mut self, score: u16) {
match score {
s if self.places[0] < s => {
self.places[2] = self.places[1];
self.places[1] = self.places[0];
self.places[0] = s
}
s if self.places[1] < s => {
self.places[2] = self.places[1];
self.places[1] = s
}
s if self.places[2] < s => self.places[2] = s,
_ => (),
}
} }
} }
static mut scoreboard: Scorebord = Scorebord { places: [0, 0, 0] }; static mut scoreboard: Scorebord = Scorebord { places: [0, 0, 0] };
static eeprom: EEPROM = EEPROM::new(100); static eeprom: EEPROM = EEPROM::new(101);
const WORLD_WIDTH: u8 = 32; const WORLD_WIDTH: u8 = 32;
const WORLD_HEIGHT: u8 = 16; const WORLD_HEIGHT: u8 = 16;
const SCALE_FACTOR: u8 = 4; const SCALE_FACTOR: u8 = 4;
@ -216,12 +229,15 @@ pub unsafe extern "C" fn loop_() {
arduboy.set_text_size(2); arduboy.set_text_size(2);
arduboy.print(f!(b"RustySnake\n\0")); arduboy.print(f!(b"RustySnake\n\0"));
arduboy.set_text_size(1); arduboy.set_text_size(1);
arduboy.print(f!(b"\nControls: \nB for Pause\nA&B for reset\nScore: \0")); arduboy.print(f!(b"\nControls: \nB Pause / A&B reset\n\0"));
arduboy.print(f!(b"Press A for Scorebord\0")); arduboy.print(f!(b"Press B for Scorebord\0"));
arduboy.print(f!(b"\nZennDev 2023\n\0")); arduboy.print(f!(b"\nZennDev 2023\n\0"));
if A.just_pressed() { if A.just_pressed() {
snake.game_state = State::Game; snake.game_state = State::Game;
} }
if B.just_pressed() {
snake.game_state = State::Scorebord;
}
} }
State::Game => { State::Game => {
snake.get_new_dir(); snake.get_new_dir();
@ -257,33 +273,33 @@ pub unsafe extern "C" fn loop_() {
if scoreboard.check_score(snake.points) { if scoreboard.check_score(snake.points) {
eeprom.put(&scoreboard); eeprom.put(&scoreboard);
arduboy.print(f!(b"New Highscore!\0")); arduboy.print(f!(b"New Highscore!\0"));
arduboy.print(f!(b"\n\n\0")); arduboy.print(f!(b"\nYou are under the\ntop three player\0"));
arduboy.print(f!(b"\nHigh Score: \0")); arduboy.print(f!(b"\n\nYour Score: \0"));
arduboy.print(scoreboard.places[0] as u16); arduboy.print(snake.points as u16);
arduboy.print(f!(b"\n\n\0")); arduboy.print(f!(b"\n\0"));
arduboy.print(f!(b"Press A to Play Again\0")); arduboy.print(f!(b"\nPress A to save the \nscore and play again\0"));
} else { } else {
arduboy.print(f!(b"Game Over!\0")); arduboy.print(f!(b"Game Over!\0"));
arduboy.print(f!(b"\n\n\0")); arduboy.print(f!(b"\n\n\0"));
arduboy.print(f!(b"Score: \0")); arduboy.print(f!(b"Score: \0"));
arduboy.print(snake.points as u16); arduboy.print(snake.points as u16);
} }
arduboy.print(f!(b"\nHigh Score: \0"));
arduboy.print(scoreboard.places[0] as u16);
arduboy.print(f!(b"\n\n\0"));
arduboy.print(f!(b"Press A to Play Again\0"));
if A.just_pressed() { if A.just_pressed() {
scoreboard.update_score(snake.points);
snake.game_state = State::Reset; snake.game_state = State::Reset;
} }
} }
State::Scorebord => { State::Scorebord => {
arduboy.set_cursor(0, 0); arduboy.set_cursor(0, 10);
arduboy.print(f!(b"1 place: \0")); arduboy.print(f!(b"1 place: \0"));
arduboy.print(scoreboard.places[0]); arduboy.print(scoreboard.places[0]);
arduboy.print(f!(b"\n2 place: \0")); arduboy.print(f!(b"\n\n2 place: \0"));
arduboy.print(scoreboard.places[1]); arduboy.print(scoreboard.places[1]);
arduboy.print(f!(b"\n3 place: \0")); arduboy.print(f!(b"\n\n3 place: \0"));
arduboy.print(scoreboard.places[2]); arduboy.print(scoreboard.places[2]);
if A.just_pressed() || B.just_pressed() {
snake.game_state = State::Title;
}
} }
} }
if (A | B).pressed() { if (A | B).pressed() {

View file

@ -8,7 +8,7 @@ pub mod prelude;
mod print; mod print;
pub use crate::library::arduboy::{Arduboy, Color, FONT_SIZE, HEIGHT, WIDTH}; pub use crate::library::arduboy::{Arduboy, Color, FONT_SIZE, HEIGHT, WIDTH};
pub use crate::library::arduboy_tone::Sound; pub use crate::library::arduboy_tone::Sound;
pub use crate::library::eeprom::EEPROM; pub use crate::library::eeprom::{EEPROM, EEPROMBYTE};
pub use crate::library::{arduboy_tone_pitch, c, sprites}; pub use crate::library::{arduboy_tone_pitch, c, sprites};
pub use hardware::buttons; pub use hardware::buttons;

View file

@ -328,9 +328,15 @@ impl Arduboy {
pub fn audio_save_on_off(&self) { pub fn audio_save_on_off(&self) {
unsafe { arduboy_audio_save_on_off() } unsafe { arduboy_audio_save_on_off() }
} }
pub fn audio_save_toggle(&self) { pub fn audio_toggle(&self) {
unsafe { arduboy_audio_toggle() } unsafe { arduboy_audio_toggle() }
} }
pub fn audio_on_and_save(&self) {
unsafe {
arduboy_audio_on();
arduboy_audio_save_on_off()
}
}
///Get the current sound state. ///Get the current sound state.
/// ///
///### Returns ///### Returns