Skip to content

Commit

Permalink
Added a pause feature (#5)
Browse files Browse the repository at this point in the history
Pressing 'p' will pause game execution and display a "Paused" message. Pressing 'p' again will unpause.

Implemented via new game state.
  • Loading branch information
johannptl committed Sep 8, 2023
1 parent 06e2e72 commit 3b3d1cf
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 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"
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion src/game.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -77,7 +82,8 @@ pub struct GameState {
enum UpdateEndState {
Quit,
Lost,
Continue
Continue,
Pause
}

impl GameState {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Expand Up @@ -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... ║",
"║ ║",
"║ ║",
Expand Down

0 comments on commit 3b3d1cf

Please sign in to comment.