small refactor

main
Plaza521 2022-10-27 00:29:36 +03:00 committed by GitHub
parent 3982e7ab40
commit ffc75251f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 16 deletions

11
main.py
View File

@ -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")

8
out.py
View File

@ -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}")

View File

@ -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