From aff3036a0d17e7aaeaef0ebf08a980e5209728a9 Mon Sep 17 00:00:00 2001 From: Plaza521 <89989298+Plaza521@users.noreply.github.com> Date: Sun, 6 Nov 2022 22:18:02 +0300 Subject: [PATCH] fix issue #1 and fix pause --- main.py | 35 +++++++++++++++++++++++++++-------- player.py | 22 +++++++++++++--------- settings.py | 3 +++ 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/main.py b/main.py index 8ca671f..8dbc0ea 100644 --- a/main.py +++ b/main.py @@ -9,25 +9,43 @@ from time import sleep, time class Game: def __init__(self) -> None: self.running = True - self.pl = Player() - self.out = Out(self.pl) self.is_pause = False + self.pl = Player(self) + self.out = Out(self.pl) kb.add_hotkey(QUIT_BUTTON, self.stop_game) kb.add_hotkey(PAUSE_BUTTON, self.switch_pause) - 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) + kb.add_hotkey( + LEFT_BUTTON, + self.pl.left, + args=[self] + ) + kb.add_hotkey( + RIGHT_BUTTON, + self.pl.right, + args=[self] + ) + kb.add_hotkey( + UP_BUTTON, + self.pl.up, + args=[self] + ) + kb.add_hotkey( + DOWN_BUTTON, + self.pl.down, + args=[self] + ) def switch_pause(self) -> None: self.is_pause = not self.is_pause def stop_game(self) -> None: - self.running = False + if not self.is_pause: + self.running = False def play(self) -> None: system("clear||cls") + print() start_time = time() different_between_time = 1 / FPS @@ -55,7 +73,8 @@ class Game: def main() -> None: - Game().play() + game = Game() + game.play() if __name__ == '__main__': diff --git a/player.py b/player.py index b7a6b99..9db7e63 100644 --- a/player.py +++ b/player.py @@ -4,7 +4,7 @@ from food import Food class Player: - def __init__(self) -> None: + def __init__(self, game) -> None: self.direction = MAIN_DIRECTION self.body = [Point(MAIN_X, MAIN_Y)] @@ -42,14 +42,18 @@ class Player: Player._check_collision(self.body) - def left(self) -> None: - self.direction = D_LEFT + def left(self, game) -> None: + if not game.is_pause and self.direction != D_RIGHT: + self.direction = D_LEFT - def right(self) -> None: - self.direction = D_RIGHT + def right(self, game) -> None: + if not game.is_pause and self.direction != D_LEFT: + self.direction = D_RIGHT - def up(self) -> None: - self.direction = D_UP + def up(self, game) -> None: + if not game.is_pause and self.direction != D_DOWN: + self.direction = D_UP - def down(self) -> None: - self.direction = D_DOWN + def down(self, game) -> None: + if not game.is_pause and self.direction != D_UP: + self.direction = D_DOWN diff --git a/settings.py b/settings.py index 16a1976..1f6adb0 100644 --- a/settings.py +++ b/settings.py @@ -34,3 +34,6 @@ TT_WALL = "\u001b[34m{}\u001b[0m".format("██") TT_FOOD = "\u001b[33m{}\u001b[0m".format("██") TT_WALL_FOOD = "\u001b[32m{}\u001b[0m".format("██") TT_HEAD = "\u001b[36m{}\u001b[0m".format("██") + +# Globals +is_pause = False