diff --git a/src/board.rs b/src/board.rs index 6ff2989..4dc9dd2 100644 --- a/src/board.rs +++ b/src/board.rs @@ -52,6 +52,10 @@ impl Board { let x = *x_offset as i8 + x; let y = *y_offset as i8 + y; + if x < 0 || x >= Board::WIDTH as i8 { + return true; + } + if y < 0 { continue; } @@ -60,10 +64,6 @@ impl Board { return true; } - if x < 0 || x >= Board::WIDTH as i8 { - return true; - } - if self.0[y as usize][x as usize].is_some() { return true; } diff --git a/src/game.rs b/src/game.rs index d4acecf..5690f98 100644 --- a/src/game.rs +++ b/src/game.rs @@ -2,6 +2,7 @@ use crate::actions::{Action, ActionsHeld}; use crate::board::Board; use crate::tetromino::{Direction, DirectionDiff, Tetromino}; +#[derive(Debug)] pub struct CurrentTetromino { pub tetromino: Tetromino, pub direction: Direction, @@ -18,12 +19,21 @@ impl CurrentTetromino { .max() .expect("pattern length > 0") + 1; + + let height = tetromino + .pattern(&Direction::Up) + .into_iter() + .map(|(_x, y)| y) + .max() + .expect("pattern length > 0") + + 1; + let x = ((Board::WIDTH - width) / 2) as i8; Self { tetromino, direction: Direction::Up, x, - y: -1, + y: -(height as i8), } } } @@ -259,7 +269,14 @@ impl Game { let current = std::mem::replace(&mut self.current_tetromino, next); let pattern = current.tetromino.pattern(¤t.direction); - if current.y <= 0 { + let y_start = pattern + .iter() + .map(|(_x, y)| *y) + .min() + .expect("pattern length > 0") as i8; + + let y = current.y + y_start; + if y < 0 { self.game_over = true; } diff --git a/src/gui/sdl.rs b/src/gui/sdl.rs index ce3a676..57f7ba3 100644 --- a/src/gui/sdl.rs +++ b/src/gui/sdl.rs @@ -301,18 +301,20 @@ fn config_from_file>(path: P) -> Result