From ffc75251f163a24808302093b13297456ac677e9 Mon Sep 17 00:00:00 2001 From: Plaza521 <89989298+Plaza521@users.noreply.github.com> Date: Thu, 27 Oct 2022 00:29:36 +0300 Subject: [PATCH] small refactor --- main.py | 11 ++++++++--- out.py | 8 +++++--- player.py | 13 +++---------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index 8fd694f..c50b5ba 100644 --- a/main.py +++ b/main.py @@ -1,16 +1,16 @@ -# from frame import Frame from settings import * from player import Player import keyboard as kb from out import Out from os import system +from time import sleep class Game: def __init__(self) -> None: self.running = True self.pl = Player() - self.out = Out() + self.out = Out(self.pl) kb.add_hotkey(QUIT_BUTTON, self.stop_game) kb.add_hotkey(LEFT_BUTTON, self.pl.left) @@ -29,14 +29,19 @@ class Game: def play(self) -> None: system("clear||cls") + time_to_sleep = 1 / FPS while self.running: try: + sleep(time_to_sleep) + self.pl.input() self.pl.update() - self.out.draw(self.pl) + self.out.draw() except IndexError as e: self.running = False print(e) + except KeyboardInterrupt: + return input("Press enter to leave from game") diff --git a/out.py b/out.py index c7736ef..cff7e46 100644 --- a/out.py +++ b/out.py @@ -4,13 +4,15 @@ from frame import Frame class Out: - def __init__(self) -> None: + def __init__(self, pl: Player) -> None: self.width = WIDTH self.height = HEIGHT self.fps = FPS + self.pl = pl - def draw(self, pl: Player) -> None: + def draw(self) -> None: frame = Frame(self.width, self.height) + pl = self.pl for point in pl.body: if point.x < 0 or point.y < 0: @@ -36,4 +38,4 @@ class Out: ) frame.show() - print(F"Score: {pl.score}") + print(f"Score: {pl.score}") diff --git a/player.py b/player.py index 66b4963..493dabf 100644 --- a/player.py +++ b/player.py @@ -1,8 +1,6 @@ from settings import * from point import Point from food import Food -from time import sleep -from itertools import permutations class Player: @@ -13,24 +11,19 @@ class Player: self.food = Food(0, 0, self) self.food.generate_new() - self.score = 0 - - def update(self) -> None: - sleep(1 / FPS) + self.score = 1 + def input(self) -> None: if self.direction == D_UP: - # self.body[0].y -= 1 self.body.append(Point(self.body[-1].x, self.body[-1].y - 1)) elif self.direction == D_DOWN: - # self.body[0].y += 1 self.body.append(Point(self.body[-1].x, self.body[-1].y + 1)) elif self.direction == D_LEFT: - # self.body[0].x -= 1 self.body.append(Point(self.body[-1].x - 1, self.body[-1].y)) elif self.direction == D_RIGHT: - # self.body[0].x += 1 self.body.append(Point(self.body[-1].x + 1, self.body[-1].y)) + def update(self) -> None: if self.body[-1] == self.food: self.food.generate_new() self.score += 1