Basic Tetris gameplay working

This commit is contained in:
Greg Shuflin 2021-09-20 22:50:12 -07:00
parent c8e329ff6c
commit 6a780efa4a
1 changed files with 38 additions and 0 deletions

View File

@ -18,6 +18,7 @@ struct Tetris {
blocks: BlockGrid,
ticks: usize,
paused: bool,
lines_removed: u32,
}
impl Tetris {
@ -27,6 +28,7 @@ impl Tetris {
blocks: BlockGrid::new(),
ticks: 0,
paused: false,
lines_removed: 0,
}
}
}
@ -77,6 +79,9 @@ impl Application for Tetris {
}
}
};
let lines_removed = self.blocks.clear_pieces();
self.lines_removed += lines_removed;
Command::none()
}
@ -344,6 +349,39 @@ impl BlockGrid {
}
}
/// Returns number of lines removed
fn clear_pieces(&mut self) -> u32 {
let mut new_state = [[None; 20]; 10];
let mut offset = 0;
for row in (0..=19).rev() {
let mut all_filled = true;
let mut all_empty = true;
for col in 0..10 {
if self.state[col][row].is_none() {
all_filled = false;
}
if self.state[col][row].is_some() {
all_empty = false;
}
}
if all_filled && !all_empty {
offset += 1;
} else {
if offset != 0 {
println!("Offset {} row {}", offset, row);
}
for col in 0..10 {
new_state[col][row+offset] = self.state[col][row];
}
}
}
self.state = new_state;
println!("OFFSET: {}", offset);
offset as u32
}
fn iter<'a>(&'a self) -> BlockGridIter<'a> {
let active_piece_blocks = self.active_piece.as_ref().map(|piece| {
let b = Self::piece_blocks(piece);