From: Geoffrey Allott Date: Fri, 2 Dec 2022 20:14:36 +0000 (+0000) Subject: allow vim keybindings and add pause key X-Git-Url: https://git.pointlesshacks.com/?a=commitdiff_plain;h=d903b2aec662fc2c8ac98a0c3392a1dc2cdcc507;p=snake.git allow vim keybindings and add pause key --- diff --git a/snake.py b/snake.py index b70afa8..ebf842d 100644 --- a/snake.py +++ b/snake.py @@ -81,27 +81,29 @@ class GameArea: def getch(self): ch = self.win.getch() - if ch == curses.KEY_LEFT: + if ch == curses.KEY_LEFT or ch == ord('h'): if self.dir == Direction.Right: print('\a', end='') else: self.dir = Direction.Left - if ch == curses.KEY_RIGHT: + if ch == curses.KEY_RIGHT or ch == ord('l'): if self.dir == Direction.Left: print('\a', end='') else: self.dir = Direction.Right - if ch == curses.KEY_UP: + if ch == curses.KEY_UP or ch == ord('k'): if self.dir == Direction.Down: print('\a', end='') else: self.dir = Direction.Up - if ch == curses.KEY_DOWN: + if ch == curses.KEY_DOWN or ch == ord('j'): if self.dir == Direction.Up: print('\a', end='') else: self.dir = Direction.Down - if ch == 27 or ch == ord('q'): + if ch == 27 or ch == ord('p'): + self.pause() + if ch == ord('q'): curses.endwin() sys.exit(0) @@ -150,6 +152,23 @@ class GameArea: os.makedirs(os.path.dirname(self.highscore_file), exist_ok=True) with open(self.highscore_file, 'w') as f: f.write(f'{self.highscore}\n') + + def pause(self): + self.paint() + height, width = self.win.getmaxyx() + self.win.addstr(height // 2, (width - 6) // 2, 'PAUSED') + self.win.refresh() + sleep(1) + self.win.nodelay(False) + while True: + ch = self.win.getch() + if ch == 27 or ch == ord('p'): + break + if ch == ord('q'): + curses.endwin() + sys.exit(0) + self.win.nodelay(True) + self.paint() def game_over(self): self.paint()