small refactor
parent
3982e7ab40
commit
ffc75251f1
11
main.py
11
main.py
|
@ -1,16 +1,16 @@
|
||||||
# from frame import Frame
|
|
||||||
from settings import *
|
from settings import *
|
||||||
from player import Player
|
from player import Player
|
||||||
import keyboard as kb
|
import keyboard as kb
|
||||||
from out import Out
|
from out import Out
|
||||||
from os import system
|
from os import system
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.running = True
|
self.running = True
|
||||||
self.pl = Player()
|
self.pl = Player()
|
||||||
self.out = Out()
|
self.out = Out(self.pl)
|
||||||
|
|
||||||
kb.add_hotkey(QUIT_BUTTON, self.stop_game)
|
kb.add_hotkey(QUIT_BUTTON, self.stop_game)
|
||||||
kb.add_hotkey(LEFT_BUTTON, self.pl.left)
|
kb.add_hotkey(LEFT_BUTTON, self.pl.left)
|
||||||
|
@ -29,14 +29,19 @@ class Game:
|
||||||
|
|
||||||
def play(self) -> None:
|
def play(self) -> None:
|
||||||
system("clear||cls")
|
system("clear||cls")
|
||||||
|
time_to_sleep = 1 / FPS
|
||||||
|
|
||||||
while self.running:
|
while self.running:
|
||||||
try:
|
try:
|
||||||
|
sleep(time_to_sleep)
|
||||||
|
self.pl.input()
|
||||||
self.pl.update()
|
self.pl.update()
|
||||||
self.out.draw(self.pl)
|
self.out.draw()
|
||||||
except IndexError as e:
|
except IndexError as e:
|
||||||
self.running = False
|
self.running = False
|
||||||
print(e)
|
print(e)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
return
|
||||||
|
|
||||||
input("Press enter to leave from game")
|
input("Press enter to leave from game")
|
||||||
|
|
||||||
|
|
8
out.py
8
out.py
|
@ -4,13 +4,15 @@ from frame import Frame
|
||||||
|
|
||||||
|
|
||||||
class Out:
|
class Out:
|
||||||
def __init__(self) -> None:
|
def __init__(self, pl: Player) -> None:
|
||||||
self.width = WIDTH
|
self.width = WIDTH
|
||||||
self.height = HEIGHT
|
self.height = HEIGHT
|
||||||
self.fps = FPS
|
self.fps = FPS
|
||||||
|
self.pl = pl
|
||||||
|
|
||||||
def draw(self, pl: Player) -> None:
|
def draw(self) -> None:
|
||||||
frame = Frame(self.width, self.height)
|
frame = Frame(self.width, self.height)
|
||||||
|
pl = self.pl
|
||||||
|
|
||||||
for point in pl.body:
|
for point in pl.body:
|
||||||
if point.x < 0 or point.y < 0:
|
if point.x < 0 or point.y < 0:
|
||||||
|
@ -36,4 +38,4 @@ class Out:
|
||||||
)
|
)
|
||||||
|
|
||||||
frame.show()
|
frame.show()
|
||||||
print(F"Score: {pl.score}")
|
print(f"Score: {pl.score}")
|
||||||
|
|
13
player.py
13
player.py
|
@ -1,8 +1,6 @@
|
||||||
from settings import *
|
from settings import *
|
||||||
from point import Point
|
from point import Point
|
||||||
from food import Food
|
from food import Food
|
||||||
from time import sleep
|
|
||||||
from itertools import permutations
|
|
||||||
|
|
||||||
|
|
||||||
class Player:
|
class Player:
|
||||||
|
@ -13,24 +11,19 @@ class Player:
|
||||||
self.food = Food(0, 0, self)
|
self.food = Food(0, 0, self)
|
||||||
self.food.generate_new()
|
self.food.generate_new()
|
||||||
|
|
||||||
self.score = 0
|
self.score = 1
|
||||||
|
|
||||||
def update(self) -> None:
|
|
||||||
sleep(1 / FPS)
|
|
||||||
|
|
||||||
|
def input(self) -> None:
|
||||||
if self.direction == D_UP:
|
if self.direction == D_UP:
|
||||||
# self.body[0].y -= 1
|
|
||||||
self.body.append(Point(self.body[-1].x, self.body[-1].y - 1))
|
self.body.append(Point(self.body[-1].x, self.body[-1].y - 1))
|
||||||
elif self.direction == D_DOWN:
|
elif self.direction == D_DOWN:
|
||||||
# self.body[0].y += 1
|
|
||||||
self.body.append(Point(self.body[-1].x, self.body[-1].y + 1))
|
self.body.append(Point(self.body[-1].x, self.body[-1].y + 1))
|
||||||
elif self.direction == D_LEFT:
|
elif self.direction == D_LEFT:
|
||||||
# self.body[0].x -= 1
|
|
||||||
self.body.append(Point(self.body[-1].x - 1, self.body[-1].y))
|
self.body.append(Point(self.body[-1].x - 1, self.body[-1].y))
|
||||||
elif self.direction == D_RIGHT:
|
elif self.direction == D_RIGHT:
|
||||||
# self.body[0].x += 1
|
|
||||||
self.body.append(Point(self.body[-1].x + 1, self.body[-1].y))
|
self.body.append(Point(self.body[-1].x + 1, self.body[-1].y))
|
||||||
|
|
||||||
|
def update(self) -> None:
|
||||||
if self.body[-1] == self.food:
|
if self.body[-1] == self.food:
|
||||||
self.food.generate_new()
|
self.food.generate_new()
|
||||||
self.score += 1
|
self.score += 1
|
||||||
|
|
Loading…
Reference in New Issue