Clear filled rows
This commit is contained in:
parent
57a634d4ee
commit
921f68a081
@ -165,7 +165,7 @@ extern "x86-interrupt" fn timer_handler(_stack_frame: InterruptStackFrame) {
|
||||
board.draw(&mut v, fall.as_ref());
|
||||
|
||||
if let Some(fall_inner) = fall.as_mut() {
|
||||
if t % 6 == 0 {
|
||||
if t % 5 == 0 {
|
||||
let mut fall_test = fall_inner.clone();
|
||||
fall_test.translate(0, 1);
|
||||
|
||||
@ -175,6 +175,7 @@ extern "x86-interrupt" fn timer_handler(_stack_frame: InterruptStackFrame) {
|
||||
let mut x = None;
|
||||
core::mem::swap(&mut x, &mut fall);
|
||||
board.place_tetromino(x.unwrap());
|
||||
board.collapse();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -28,6 +28,39 @@ impl TetrisBoard {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn collapse(&mut self) {
|
||||
let mut y = Self::BOARD_HEIGHT - 1;
|
||||
'outer: loop {
|
||||
for x in 0..Self::BOARD_WIDTH {
|
||||
let cell = self.get_cell(x, y);
|
||||
if cell == Some(&TetrisCell::Empty) {
|
||||
if y == 0 {
|
||||
break 'outer;
|
||||
}
|
||||
|
||||
y -= 1;
|
||||
continue 'outer;
|
||||
}
|
||||
}
|
||||
|
||||
// We found a row that needs to be cleared
|
||||
|
||||
// Shift everything down
|
||||
for yy in (1..=y).rev() {
|
||||
for x in 0..Self::BOARD_WIDTH {
|
||||
let top = *self.get_cell(x, yy - 1).unwrap();
|
||||
let bot = self.get_cell_mut(x, yy).unwrap();
|
||||
*bot = top;
|
||||
}
|
||||
}
|
||||
|
||||
// Clear the top row
|
||||
for x in 0..Self::BOARD_WIDTH {
|
||||
*self.get_cell_mut(x, 0).unwrap() = TetrisCell::Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn place_tetromino(&mut self, tetromino: FallingTetromino) {
|
||||
for (x, y) in tetromino.tiles() {
|
||||
let cell = self.get_cell_mut(x, y);
|
||||
|
Loading…
x
Reference in New Issue
Block a user