From 996eebe4709fd5f4a6883b2f10de4ec92f4c30f1 Mon Sep 17 00:00:00 2001 From: Plaza521 <89989298+Plaza521@users.noreply.github.com> Date: Thu, 27 Oct 2022 15:29:28 +0300 Subject: [PATCH] add pause and refactor --- frame.py | 6 +++--- main.py | 22 ++++++++++++---------- out.py | 6 ------ player.py | 13 +++++++++++-- settings.py | 1 + 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/frame.py b/frame.py index ec3de3b..2d007b3 100644 --- a/frame.py +++ b/frame.py @@ -9,10 +9,10 @@ if platform == "win32": class COORDSET(Structure): _fields_ = [("X", c_long), ("Y", c_long)] - def _set_cursor_position(x: int, y: int) -> None: + def set_cursor_position(x: int, y: int) -> None: windll.kernel32.SetConsoleCursorPosition(STDHANDLE, COORDSET(x, y)) else: - def _set_cursor_position(x: int, y: int) -> None: + def set_cursor_position(x: int, y: int) -> None: print(f"\033[{x};{y}H") @@ -40,7 +40,7 @@ class Frame: self.matrix[y + line][x + column] = value def show(self) -> None: - _set_cursor_position(0, 0) + set_cursor_position(0, 0) out_string = f"┍{'━' * (self.width * 2)}┑\n" diff --git a/main.py b/main.py index c50b5ba..5be036d 100644 --- a/main.py +++ b/main.py @@ -11,22 +11,21 @@ class Game: self.running = True self.pl = Player() self.out = Out(self.pl) + self.is_pause = False kb.add_hotkey(QUIT_BUTTON, self.stop_game) + kb.add_hotkey(PAUSE_BUTTON, self.pause_game) kb.add_hotkey(LEFT_BUTTON, self.pl.left) kb.add_hotkey(RIGHT_BUTTON, self.pl.right) kb.add_hotkey(UP_BUTTON, self.pl.up) kb.add_hotkey(DOWN_BUTTON, self.pl.down) + def pause_game(self) -> None: + self.is_pause = not self.is_pause + def stop_game(self) -> None: self.running = False - def update(self) -> None: - pass - - def output(self) -> None: - pass - def play(self) -> None: system("clear||cls") time_to_sleep = 1 / FPS @@ -34,16 +33,19 @@ class Game: while self.running: try: sleep(time_to_sleep) - self.pl.input() - self.pl.update() - self.out.draw() + + if not self.is_pause: + self.pl.input() + self.pl.update() + self.out.draw() + except IndexError as e: self.running = False print(e) except KeyboardInterrupt: return - input("Press enter to leave from game") + input("Press enter to leave from game.\n") def main() -> None: diff --git a/out.py b/out.py index cff7e46..365d94b 100644 --- a/out.py +++ b/out.py @@ -7,7 +7,6 @@ class Out: def __init__(self, pl: Player) -> None: self.width = WIDTH self.height = HEIGHT - self.fps = FPS self.pl = pl def draw(self) -> None: @@ -15,11 +14,6 @@ class Out: pl = self.pl for point in pl.body: - if point.x < 0 or point.y < 0: - raise IndexError("Snake has collision with walls") - elif point.x >= WIDTH or point.y >= HEIGHT: - raise IndexError("Snake has collision with walls") - frame.draw( x=point.x, y=point.y, value=WALL, diff --git a/player.py b/player.py index 493dabf..ae96838 100644 --- a/player.py +++ b/player.py @@ -23,6 +23,16 @@ class Player: elif self.direction == D_RIGHT: self.body.append(Point(self.body[-1].x + 1, self.body[-1].y)) + def _check_collision(body: list[Point]) -> None: + for point in body: + if point.x < 0 or point.y < 0: + raise IndexError("Snake has collision with walls") + elif point.x >= WIDTH or point.y >= HEIGHT: + raise IndexError("Snake has collision with walls") + + if len(set(body)) != len(body): + raise IndexError("Player has collision with self") + def update(self) -> None: if self.body[-1] == self.food: self.food.generate_new() @@ -30,8 +40,7 @@ class Player: else: self.body.pop(0) - if len(set(self.body)) != len(self.body): - raise IndexError("Player has collision with self") + Player._check_collision(self.body) def left(self) -> None: self.direction = D_LEFT diff --git a/settings.py b/settings.py index 6b5a75a..4d53acc 100644 --- a/settings.py +++ b/settings.py @@ -5,6 +5,7 @@ FPS = 5 # Buttons QUIT_BUTTON = 'q' +PAUSE_BUTTON = 'p' LEFT_BUTTON = 'a' RIGHT_BUTTON = 'd' UP_BUTTON = 'w'