Compare commits

..

2 Commits

Author SHA1 Message Date
Greg Shuflin
6a780efa4a Basic Tetris gameplay working 2021-09-20 22:50:12 -07:00
Greg Shuflin
c8e329ff6c Rotation and other stuff 2021-09-20 21:58:38 -07:00

View File

@ -6,6 +6,9 @@ use iced::{
use iced_native::{event, subscription, Event}; use iced_native::{event, subscription, Event};
use rand::distributions::{Distribution, Standard}; use rand::distributions::{Distribution, Standard};
const START_LOCATION: (u8, u8) = (5, 1);
type PieceBlocks = [(i8, i8); 4];
fn main() -> iced::Result { fn main() -> iced::Result {
Tetris::run(Settings::default()) Tetris::run(Settings::default())
} }
@ -15,6 +18,7 @@ struct Tetris {
blocks: BlockGrid, blocks: BlockGrid,
ticks: usize, ticks: usize,
paused: bool, paused: bool,
lines_removed: u32,
} }
impl Tetris { impl Tetris {
@ -24,6 +28,7 @@ impl Tetris {
blocks: BlockGrid::new(), blocks: BlockGrid::new(),
ticks: 0, ticks: 0,
paused: false, paused: false,
lines_removed: 0,
} }
} }
} }
@ -49,7 +54,9 @@ impl Application for Tetris {
self.ticks += 1 self.ticks += 1
} }
} }
Message::Up => (), Message::Up => {
self.blocks.rotate_active_piece();
}
Message::Down => { Message::Down => {
self.blocks.move_active_piece(MoveDirection::HardDrop); self.blocks.move_active_piece(MoveDirection::HardDrop);
} }
@ -72,6 +79,9 @@ impl Application for Tetris {
} }
} }
}; };
let lines_removed = self.blocks.clear_pieces();
self.lines_removed += lines_removed;
Command::none() Command::none()
} }
@ -164,6 +174,7 @@ struct BlockGrid {
struct ActivePiece { struct ActivePiece {
location: (u8, u8), location: (u8, u8),
tetromino: Tetromino, tetromino: Tetromino,
orientation: Orientation,
} }
impl ActivePiece { impl ActivePiece {
@ -172,6 +183,7 @@ impl ActivePiece {
let (cur_x, cur_y) = self.location; let (cur_x, cur_y) = self.location;
ActivePiece { ActivePiece {
tetromino: self.tetromino, tetromino: self.tetromino,
orientation: self.orientation,
location: match direction { location: match direction {
Left => (cur_x.checked_sub(1).unwrap_or(0), cur_y), Left => (cur_x.checked_sub(1).unwrap_or(0), cur_y),
Right => (cur_x + 1, cur_y), Right => (cur_x + 1, cur_y),
@ -180,6 +192,19 @@ impl ActivePiece {
}, },
} }
} }
fn rotate_piece(&self) -> ActivePiece {
ActivePiece {
tetromino: self.tetromino,
location: self.location,
orientation: match self.orientation {
Orientation::A => Orientation::B,
Orientation::B => Orientation::C,
Orientation::C => Orientation::D,
Orientation::D => Orientation::A,
},
}
}
} }
impl BlockGrid { impl BlockGrid {
@ -195,11 +220,12 @@ impl BlockGrid {
fn drop_piece(&mut self, tetromino: Tetromino) -> bool { fn drop_piece(&mut self, tetromino: Tetromino) -> bool {
if let None = self.active_piece { if let None = self.active_piece {
let piece = ActivePiece { let piece = ActivePiece {
location: (4, 0), location: START_LOCATION,
tetromino, tetromino,
orientation: Orientation::A,
}; };
let piece_blocks = Self::piece_blocks(&piece); let piece_blocks = Self::piece_blocks(&piece);
if self.piece_blocks_in_bounds(&piece_blocks) { if self.piece_blocks_in_bounds(piece_blocks) {
self.active_piece = Some(piece); self.active_piece = Some(piece);
true true
} else { } else {
@ -210,11 +236,11 @@ impl BlockGrid {
} }
} }
fn piece_blocks_in_bounds(&self, piece_blocks: &[(u8, u8); 4]) -> bool { fn piece_blocks_in_bounds(&self, piece_blocks: PieceBlocks) -> bool {
piece_blocks.iter().all(|(x, y)| { piece_blocks.iter().all(|(x, y)| {
let x = *x; let x = *x;
let y = *y; let y = *y;
x < 10 && y < 20 && self.state[x as usize][y as usize].is_none() x >= 0 && y >= 0 && x < 10 && y < 20 && self.state[x as usize][y as usize].is_none()
}) })
} }
@ -227,7 +253,7 @@ impl BlockGrid {
(Some(piece), MoveDirection::Left | MoveDirection::Right) => { (Some(piece), MoveDirection::Left | MoveDirection::Right) => {
let new_piece = piece.move_piece(&direction); let new_piece = piece.move_piece(&direction);
let new_blocks = Self::piece_blocks(&new_piece); let new_blocks = Self::piece_blocks(&new_piece);
if self.piece_blocks_in_bounds(&new_blocks) { if self.piece_blocks_in_bounds(new_blocks) {
self.active_piece = Some(new_piece); self.active_piece = Some(new_piece);
true true
} else { } else {
@ -239,7 +265,7 @@ impl BlockGrid {
loop { loop {
let p = new_piece.move_piece(&MoveDirection::SoftDrop); let p = new_piece.move_piece(&MoveDirection::SoftDrop);
let new_blocks = Self::piece_blocks(&p); let new_blocks = Self::piece_blocks(&p);
if self.piece_blocks_in_bounds(&new_blocks) { if self.piece_blocks_in_bounds(new_blocks) {
new_piece = p; new_piece = p;
} else { } else {
break; break;
@ -253,7 +279,7 @@ impl BlockGrid {
(Some(ref piece), MoveDirection::SoftDrop) => { (Some(ref piece), MoveDirection::SoftDrop) => {
let new_piece = piece.move_piece(&MoveDirection::SoftDrop); let new_piece = piece.move_piece(&MoveDirection::SoftDrop);
let new_blocks = Self::piece_blocks(&new_piece); let new_blocks = Self::piece_blocks(&new_piece);
if self.piece_blocks_in_bounds(&new_blocks) { if self.piece_blocks_in_bounds(new_blocks) {
self.active_piece = Some(new_piece); self.active_piece = Some(new_piece);
true true
} else { } else {
@ -264,6 +290,22 @@ impl BlockGrid {
} }
} }
fn rotate_active_piece(&mut self) -> bool {
let active = self.active_piece;
if let Some(piece) = active {
let new_piece = piece.rotate_piece();
let new_blocks = Self::piece_blocks(&new_piece);
if self.piece_blocks_in_bounds(new_blocks) {
self.active_piece = Some(new_piece);
true
} else {
false
}
} else {
false
}
}
/// Remove the currently active piece and place its blocks onto the board. /// Remove the currently active piece and place its blocks onto the board.
fn place_active_piece(&mut self) { fn place_active_piece(&mut self) {
if let Some(piece) = self.active_piece { if let Some(piece) = self.active_piece {
@ -277,29 +319,82 @@ impl BlockGrid {
} }
} }
fn piece_blocks(piece: &ActivePiece) -> [(u8, u8); 4] { fn piece_blocks(piece: &ActivePiece) -> PieceBlocks {
use Orientation::*;
use Tetromino::*; use Tetromino::*;
let (x, y) = piece.location; let (x, y) = piece.location;
let (x, y) = (x as i8, y as i8);
match piece.tetromino { // These use the "Original Rotation System" cf. Tetris wiki
I => [(x, y), (x, y + 1), (x, y + 2), (x, y + 3)], match (piece.tetromino, piece.orientation) {
J => [(x, y), (x, y + 1), (x, y + 2), (x - 1, y + 2)], (I, A | C) => [(x - 2, y), (x - 1, y), (x, y), (x + 1, y)],
L => [(x, y), (x, y + 1), (x, y + 2), (x + 1, y + 2)], (I, B | D) => [(x, y - 1), (x, y), (x, y + 1), (x, y + 2)],
O => [(x, y), (x + 1, y), (x, y + 1), (x + 1, y + 1)], (J, A) => [(x - 1, y), (x, y), (x + 1, y), (x + 1, y + 1)],
S => [(x, y + 1), (x + 1, y + 1), (x + 1, y), (x + 2, y)], (J, B) => [(x, y), (x + 1, y), (x, y + 1), (x, y + 2)],
T => [(x, y), (x + 1, y), (x + 2, y), (x + 1, y + 1)], (J, C) => [(x - 1, y), (x - 1, y + 1), (x, y + 1), (x + 1, y + 1)],
Z => [(x, y), (x + 1, y), (x + 1, y + 1), (x + 2, y + 1)], (J, D) => [(x, y), (x, y + 1), (x, y + 2), (x - 1, y + 2)],
(L, A) => [(x - 1, y + 1), (x - 1, y), (x, y), (x + 1, y)],
(L, B) => [(x, y), (x, y + 1), (x, y + 2), (x + 1, y + 2)],
(L, C) => [(x - 1, y + 1), (x, y + 1), (x + 1, y + 1), (x + 1, y)],
(L, D) => [(x - 1, y), (x, y), (x, y + 1), (x, y + 2)],
(O, _) => [(x, y), (x, y + 1), (x - 1, y), (x - 1, y + 1)],
(S, A | C) => [(x - 1, y + 1), (x, y + 1), (x, y), (x + 1, y)],
(S, B | D) => [(x, y), (x, y + 1), (x + 1, y + 1), (x + 1, y + 2)],
(T, A) => [(x - 1, y), (x, y), (x + 1, y), (x, y + 1)],
(T, B) => [(x, y), (x, y + 1), (x + 1, y + 1), (x, y + 2)],
(T, C) => [(x, y), (x - 1, y + 1), (x, y + 1), (x + 1, y + 1)],
(T, D) => [(x - 1, y + 1), (x, y), (x, y + 1), (x, y + 2)],
(Z, A | C) => [(x - 1, y + 1), (x, y + 1), (x, y + 2), (x + 1, y + 2)],
(Z, B | D) => [(x, y), (x, y + 1), (x + 1, y), (x + 1, y - 1)],
} }
} }
fn active_piece_blocks(&self) -> Option<(Tetromino, [(u8, u8); 4])> { /// Returns number of lines removed
self.active_piece fn clear_pieces(&mut self) -> u32 {
.as_ref() let mut new_state = [[None; 20]; 10];
.map(|piece| (piece.tetromino, Self::piece_blocks(&piece))) 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> { fn iter<'a>(&'a self) -> BlockGridIter<'a> {
BlockGridIter::new(&self.state, self.active_piece_blocks()) let active_piece_blocks = self.active_piece.as_ref().map(|piece| {
let b = Self::piece_blocks(piece);
let blocks = [
(b[0].0 as u8, b[0].1 as u8),
(b[1].0 as u8, b[1].1 as u8),
(b[2].0 as u8, b[2].1 as u8),
(b[3].0 as u8, b[3].1 as u8),
];
(piece.tetromino, blocks)
});
BlockGridIter::new(&self.state, active_piece_blocks)
} }
} }
@ -355,7 +450,7 @@ struct Block {
source: Tetromino, source: Tetromino,
} }
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone)]
enum MoveDirection { enum MoveDirection {
Left, Left,
Right, Right,
@ -363,6 +458,14 @@ enum MoveDirection {
SoftDrop, SoftDrop,
} }
#[derive(Debug, Copy, Clone)]
enum Orientation {
A,
B,
C,
D,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum Tetromino { enum Tetromino {
I, I,