diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 12 | ||||
-rw-r--r-- | src/tetris/piece.rs | 116 |
2 files changed, 92 insertions, 36 deletions
@@ -7,29 +7,31 @@ use std::io::stdin; use crate::input::Input; pub struct Game { - tickrate: u8, // Between + tickrate: u8, // How many times things are checked a second maxfps: u8, // Between 1 and 240 + gamespeed: u8, // How quickly r#move(down) is called in ms } impl Game { pub fn new() -> Game { Game{ tickrate: 20, maxfps: 30, + gamespeed: 250, } } // The actual game loop pub fn game_loop(&self, input: &Input) { let mut tetris = tetris::Tetris::new(); - let mut piece = piece::Piece::random(); + let mut piece = piece::Piece::random(piece::Pos(0,0)); loop { piece.r#move(piece::Dir::Down, &tetris); // Check if piece is dead if piece.is_alive() == false { piece.apply_to_grid(&mut tetris); - // piece = piece::Piece::random(); - piece = piece::Piece::new(piece::Pieces::Cube); + piece = piece::Piece::random(piece::Pos(0,0)); + // piece = piece::Piece::new(piece::Pieces::Cube, piece::Pos(0,0)); } let grid = tetris.return_grid(); @@ -43,7 +45,7 @@ impl Game { "a" => piece.r#move(piece::Dir::Left, &tetris), "d" => piece.r#move(piece::Dir::Right, &tetris), "e" => piece.rotate(piece::Rotate::Right), - "q" => piece.rotate(piece::Rotate::Right), + "q" => piece.rotate(piece::Rotate::Left), " " => break, _ => (), } diff --git a/src/tetris/piece.rs b/src/tetris/piece.rs index ca93c59..520de01 100644 --- a/src/tetris/piece.rs +++ b/src/tetris/piece.rs @@ -1,5 +1,5 @@ use rand::Rng; -use std::ops::{AddAssign, Add}; +use std::ops::{AddAssign, Sub, Add}; use crate::tetris::Tetris; #[derive(Copy)] @@ -25,8 +25,17 @@ impl Add for Pos { } } } +impl Sub for Pos { + type Output = Self; + + fn sub(self, other: Self) -> Self::Output { + Self { + 0: self.0 - other.0, + 1: self.1 - other.1, + } + } +} -const UP: Pos = Pos(-1,0); const DOWN: Pos = Pos(1,0); const LEFT: Pos = Pos(0,-1); const RIGHT: Pos = Pos(0,1); @@ -36,8 +45,8 @@ pub enum Rotate { Right } +#[derive(PartialEq)] pub enum Dir { - Up, Down, Left, Right, @@ -45,7 +54,6 @@ pub enum Dir { impl Dir { fn get(&self) -> Pos { match self { - Dir::Up => UP, Dir::Down => DOWN, Dir::Left => LEFT, Dir::Right => RIGHT, @@ -53,12 +61,49 @@ impl Dir { } } -const NORMALL: [[i8; 3]; 3] = [[0,1,0],[0,1,0],[0,1,1]]; -const REVERSEL: [[i8; 3]; 3] = [[0,1,0],[0,1,0],[1,1,0]]; -const CUBE: [[i8; 3]; 3] = [[0,1,1],[0,1,1],[0,0,0]]; -const TEE: [[i8; 3]; 3] = [[0,1,0],[1,1,1],[0,0,0]]; -const DIAG: [[i8; 3]; 3] = [[0,1,1],[1,1,0],[0,0,0]]; -const REVERSEDIAG: [[i8; 3]; 3] = [[1,1,0],[0,1,1],[0,0,0]]; +const NORMALL: [[i8; 4]; 4] = [ + [0,0,0,0], + [1,0,0,0], + [1,0,0,0], + [1,1,0,0], +]; +const REVERSEL: [[i8; 4]; 4] = [ + [0,0,0,0], + [0,0,0,1], + [0,0,0,1], + [0,0,1,1], +]; +const CUBE: [[i8; 4]; 4] = [ + [0,0,0,0], + [0,1,1,0], + [0,1,1,0], + [0,0,0,0], +]; +const TEE: [[i8; 4]; 4] = [ + [0,0,0,0], + [0,0,0,0], + [0,1,0,0], + [1,1,1,0], +]; +const DIAG: [[i8; 4]; 4] = [ + [0,0,0,0], + [0,0,0,0], + [1,1,0,0], + [0,1,1,0], +]; +const REVERSEDIAG: [[i8; 4]; 4] = [ + [0,0,0,0], + [0,0,0,0], + [0,0,1,1], + [0,1,1,0], +]; +const STRAIGHT: [[i8; 4]; 4] = [ + [1,0,0,0], + [1,0,0,0], + [1,0,0,0], + [1,0,0,0], +]; + pub enum Pieces { NormalL, @@ -67,23 +112,25 @@ pub enum Pieces { Tee, Diag, ReverseDiag, + Straight, } impl Pieces { pub fn random() -> Pieces { use Pieces::*; let mut rand = rand::thread_rng(); - match rand.gen_range(0..6) { + match rand.gen_range(0..7) { 0 => NormalL, 1 => ReverseL, 2 => Cube, 3 => Tee, 4 => Diag, 5 => ReverseDiag, + 6 => Straight, _ => NormalL, } } - fn get(&self) -> [[i8; 3]; 3] { + fn get(&self) -> [[i8; 4]; 4] { use Pieces::*; match &self { NormalL => NORMALL, @@ -92,46 +139,51 @@ impl Pieces { Tee => TEE, Diag => DIAG, ReverseDiag => REVERSEDIAG, + Straight => STRAIGHT, } } fn get_origin(&self) -> Pos { use Pieces::*; match &self { - NormalL => Pos(0,0), - ReverseL => Pos(0,0), - Cube => Pos(0,0), - Tee => Pos(0,0), - Diag => Pos(0,0), - ReverseDiag => Pos(0,0), + NormalL => Pos(3,0), + ReverseL => Pos(3,3), + Cube => Pos(1,1), + Tee => Pos(3,1), + Diag => Pos(2,1), + ReverseDiag => Pos(2,2), + Straight => Pos(3,0), } } } pub struct Piece { - area: [[i8; 3]; 3], // A static 3x3 area + area: [[i8; 4]; 4], // A static 3x3 area position: Pos, origin: Pos, alive: bool, } impl Piece { - pub fn new(p: Pieces) -> Piece { + pub fn new(p: Pieces, pos: Pos) -> Piece { + let origin = p.get_origin(); Piece{ area: p.get(), - position: Pos(0,5), - origin: p.get_origin(), + position: pos + origin, + origin: origin, alive: true, } } // Return a random piece // out of the enum Pieces - pub fn random() -> Piece { + pub fn random(pos: Pos) -> Piece { let piece = Pieces::random(); + let origin = piece.get_origin(); + dbg!(pos - origin); Piece{ area: piece.get(), - position: Pos(0,4), - origin: piece.get_origin(), + position: pos - origin, + origin: origin, alive: true, } } @@ -145,10 +197,12 @@ impl Piece { for pos in self.get_bits_pos() { let new_pos = pos + dir.get(); - // Detect blocks and kill - if grid.get_grid_pos(new_pos) == 1 { + // Detect blocks and kill only if Dir is down + if grid.get_grid_pos(new_pos) == 1 && *dir == Dir::Down { self.kill(); return Err(()) + } else if grid.get_grid_pos(new_pos) == 1 { + return Err(()) } else if new_pos.0 > 19 { // Detect bottom and kill self.kill(); return Err(()) @@ -195,7 +249,7 @@ impl Piece { self.position } - pub fn get_area(&self) -> [[i8; 3]; 3] { + pub fn get_area(&self) -> [[i8; 4]; 4] { self.area } @@ -224,7 +278,7 @@ mod tests { #[test] fn apply_down() { - let mut p = Piece::new(Pieces::Cube); + let mut p = Piece::new(Pieces::Cube, Pos(0,0)); let d = Dir::Down; p.apply_dir(&d); @@ -235,7 +289,7 @@ mod tests { #[test] fn hit_test() { - let mut p = Piece::new(Pieces::Cube); + let mut p = Piece::new(Pieces::Cube, Pos(0,0)); let d = Dir::Down; let tetris = Tetris::new(); @@ -249,7 +303,7 @@ mod tests { } #[test] fn hit_nothing_test() { - let mut p = Piece::new(Pieces::Cube); + let mut p = Piece::new(Pieces::Cube, Pos(0,0)); let d = Dir::Down; let tetris = Tetris::new(); |