From 3b3d1cf55329ded340a5a44317225d48e9107912 Mon Sep 17 00:00:00 2001 From: Johann Petersell <70780063+johannptl@users.noreply.github.com> Date: Fri, 8 Sep 2023 21:39:56 +0200 Subject: [PATCH] Added a pause feature (#5) Pressing 'p' will pause game execution and display a "Paused" message. Pressing 'p' again will unpause. Implemented via new game state. --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 1 + src/game.rs | 16 +++++++++++++++- src/main.rs | 2 +- 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eb40a18..aa4cd5b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -280,7 +280,7 @@ dependencies = [ [[package]] name = "tetris-cli" -version = "22.67.2" +version = "23.96.1" dependencies = [ "big_num", "confy", diff --git a/Cargo.toml b/Cargo.toml index 08f97f7..c2d3745 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tetris-cli" -version = "22.67.2" +version = "23.96.1" edition = "2021" description = "A tetris clone for your terminal." license = "GPL-3.0" diff --git a/README.md b/README.md index 7228365..e894989 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ and on NetBSD [tetris-cli](https://pkgsrc.se/games/tetris-cli) from the official + q -> Rotate left + e -> Rotate right + s -> Instant drop ++ p -> Pause + Backspace -> Quit ## Build dependencies diff --git a/src/game.rs b/src/game.rs index f442319..3904b11 100644 --- a/src/game.rs +++ b/src/game.rs @@ -49,6 +49,11 @@ const BORDER: [&'static str; DISP_HEIGHT as usize] = [ "║ ║", "╚════════════════════╝" ]; +const PAUSE: [&'static str; 3 as usize] = [ + "╔════════╗", + "║ PAUSED ║", + "╚════════╝" +]; const BORDER_COLOR: &dyn Color = &White; const SCORE_COLOR: &dyn Color = &White; const SHAPE_DRAW_OFFSET: i16 = 5; @@ -77,7 +82,8 @@ pub struct GameState { enum UpdateEndState { Quit, Lost, - Continue + Continue, + Pause } impl GameState { @@ -130,6 +136,13 @@ impl GameState { return 0; }, UpdateEndState::Lost => { break; + }, + UpdateEndState::Pause => { + // Keep the game paused until 'p' is pressed again + while inp.get_key() != b'p' { + cnv.draw_strs(&PAUSE.to_vec(), (7, 13), BORDER_COLOR, &Reset); // Drawing the pause text + sleep(Duration::from_millis(interval_ms)); + } } } self.draw(cnv, hs_disp); @@ -142,6 +155,7 @@ impl GameState { let key = inp.get_key(); match key { 127 => return UpdateEndState::Quit, // Backspace -> back to menu + b'p' => return UpdateEndState::Pause, // p -> Pause b'a' => { if self.can_move_curr_shape(Dir::Left) { self.curr_shape.pos.0 -= 1.0; diff --git a/src/main.rs b/src/main.rs index ca67e1e..a45dd3d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,10 +40,10 @@ const MENU: [&'static str; DISP_HEIGHT as usize] = [ "║ - a/d - left/right ║", "║ - q/e - rotate ║", "║ - s -> drop piece ║", + "║ - p -> pause ║", "║ - back -> quit ║", "║ ║", "║ ║", - "║ ║", "║ Enter to begin... ║", "║ ║", "║ ║",