Skip to content
Snippets Groups Projects
Commit b2def381 authored by Nora's avatar Nora
Browse files

control the paddle by accelerometer x direction

parent 37955426
No related branches found
No related tags found
No related merge requests found
Pipeline #3522 passed
......@@ -162,6 +162,62 @@ enum GameResult {
LevelFinish(u32),
}
struct Input {
buttons: Buttons,
accelerometer: BHI160<Accelerometer>,
accel_data_x: f32,
}
impl Input {
pub fn update(&mut self) {
self.buttons = Buttons::read();
for data in &self.accelerometer.read() {
self.accel_data_x = data.x;
}
}
pub fn left_bottom(&self) -> bool {
self.buttons.left_bottom()
|| self.accel_is_left()
}
pub fn right_bottom(&self) -> bool {
self.buttons.right_bottom()
|| self.accel_is_right()
}
pub fn left_top(&self) -> bool {
self.buttons.left_top()
}
pub fn right_top(&self) -> bool {
self.buttons.right_top()
}
pub fn reset(&self) -> bool {
self.buttons.reset()
}
pub fn new() -> Self {
Input {
buttons: Buttons::read(),
accelerometer: BHI160::<Accelerometer>::start(),
accel_data_x: Default::default(),
}
}
fn accel_is_right(&self) -> bool {
// technically, true if < 0.0
// false if >= 0.0
// but need static middle space
self.accel_data_x > 0.1
}
fn accel_is_left(&self) -> bool {
self.accel_data_x < -0.1
}
}
fn game(level: u16, mut score: u32) -> GameResult {
let start_time = Seconds::time();
let display = Display::open();
......@@ -172,8 +228,9 @@ fn game(level: u16, mut score: u32) -> GameResult {
let mut ball_y = Display::H - PADDLE_HEIGHT - BALL_RADIUS;
let mut ball_direction = Direction::UR;
let mut blocks = Blocks::generate((0x3F + 0x10 * level).min(0xff) as u8);
let mut input = Input::new();
for tick in 0.. {
let input = Buttons::read();
input.update();
let old_paddle = paddle;
if input.left_bottom() {
paddle -= PADDLE_SPEED;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment