Rust-for-Arduboy/Examples/Arduboy-Tutorials/demo5/src/lib.rs

96 lines
3 KiB
Rust
Raw Normal View History

2023-08-06 18:14:49 +02:00
#![no_std]
#![allow(non_upper_case_globals)]
//Include the Arduboy Library
//Initialize the arduboy object
use arduboy_rust::prelude::*;
//Initialize variables used in this game
static mut playerwin: c_int = 0;
static mut attempts: c_int = 0;
static mut guessednumber: c_int = 0;
static mut randomnumber: c_int = 0;
static mut lastguess: c_int = 0;
//The setup() function runs once when you turn your Arduboy on
#[no_mangle]
pub unsafe extern "C" fn setup() {
// put your setup code here, to run once:
arduboy.begin();
arduboy.clear();
arduboy.init_random_seed();
randomnumber = random_between(1, 101) as i16;
}
//The loop() function repeats forever after setup() is done
#[no_mangle]
#[export_name = "loop"]
pub unsafe extern "C" fn loop_() {
// put your main code here, to run repeatedly:
arduboy.clear();
arduboy.poll_buttons();
if playerwin == 0 {
//Ask the player for a number and play the game
if attempts == 7 {
//Game Over screen
arduboy.set_cursor(0, 0);
arduboy.print(f!(b"You lost!\0"));
arduboy.print(f!(b"\n\0"));
arduboy.print(f!(b"Correct Number: \0"));
2023-08-06 18:14:49 +02:00
arduboy.print(randomnumber);
if A.just_pressed() {
randomnumber = random_between(1, 101) as i16;
attempts = 0;
playerwin = 0;
}
} else {
//Player has more attempts
if UP.just_pressed() {
guessednumber += 1;
}
if DOWN.just_pressed() {
guessednumber -= 1;
}
if A.just_pressed() {
if guessednumber == randomnumber {
playerwin += 1;
} else {
attempts += 1;
lastguess = guessednumber
}
}
arduboy.set_cursor(0, 0);
arduboy.print(f!(b"Attempt: \0"));
2023-08-06 18:14:49 +02:00
arduboy.print(attempts);
arduboy.print(f!(b"\n\0"));
arduboy.print(f!(b"Number to guess: \0"));
2023-08-06 18:14:49 +02:00
arduboy.print(guessednumber);
arduboy.print(f!(b"\n\0"));
2023-08-06 18:14:49 +02:00
if attempts == 0 {
arduboy.print(f!(b"Good luck!\0"));
2023-08-06 18:14:49 +02:00
} else {
arduboy.print(lastguess);
if lastguess > randomnumber {
arduboy.print(f!(b" is too high!\0"));
2023-08-06 18:14:49 +02:00
}
if lastguess < randomnumber {
arduboy.print(f!(b" is too low!\0"));
2023-08-06 18:14:49 +02:00
}
}
}
} else {
//Tell the player that they won!
arduboy.set_cursor(0, 0);
arduboy.print(f!(b"You won!\0"));
arduboy.print(f!(b"\n\0"));
arduboy.print(f!(b"Correct Number: \0"));
2023-08-06 18:14:49 +02:00
arduboy.print(randomnumber);
if A.just_pressed() {
randomnumber = random_between(1, 101) as c_int;
attempts = 0;
playerwin = 0;
}
}
arduboy.display();
}