The biggest required change will be the level handling. You need to create an array with different times to increase the tetrimino's speed of descent and to check whether the level needs to be changed or not (based on the number of lines).
The score will be updated in the following cases:
- When the tetrimino is made permanent
- When a line is sent
- When the player makes a Tetris (no more blocks in the game map)
Let's start with the easiest change—the score.
First, let's add the following method into our Tetris type:
fn update_score(&mut self, to_add: u32) { self.score += to_add; }
We can suppose that no additional explanations are required here.
Next, let's update a few methods:
fn check_lines(&mut self) { let mut y = 0; let mut score_add = 0; while y < self.game_map.len() { let mut complete = true; for x in &self.game_map[y] { if *x == 0 { complete = false; break } } if complete == true { score_add += self.current_level; self.game_map.remove(y); y -= 1; } y += 1; } if self.game_map.len() == 0 { // A "tetris"! score_add += 1000; } self.update_score(score_add); while self.game_map.len() < 16 { // we'll add this method just after! self.increase_line(); self.game_map.insert(0, vec![0, 0, 0, 0, 0, 0, 0, 0, 0,
0]); } }
As usual, we create a temporary variable (here, score_add) and once the borrow of self is over, we call the update_score method. There is also the usage of the increase_line method. We haven't defined it yet; it'll come just after.
The second method is make_permanent:
fn make_permanent(&mut self) { let mut to_add = 0; if let Some(ref mut piece) = self.current_piece { let mut shift_y = 0; while shift_y < piece.states[piece.current_state as
usize].len() && piece.y + shift_y < self.game_map.len() { let mut shift_x = 0; while shift_x < piece.states[piece.current_state as usize]
[shift_y].len() && (piece.x + shift_x as isize) < self.game_map[piece.y
+ shift_y].len() as isize { if piece.states[piece.current_state as usize][shift_y]
[shift_x] != 0 { let x = piece.x + shift_x as isize; self.game_map[piece.y + shift_y][x as usize] = piece.states[piece.current_state as usize]
[shift_y][shift_x]; } shift_x += 1; } shift_y += 1; } to_add += self.current_level; } self.update_score(to_add); self.check_lines(); self.current_piece = None; }
Include this just above the self.check_lines call.
With these two methods updated, we now have the score handling fully implemented.