Compare commits
No commits in common. "main" and "4" have entirely different histories.
31
main.py
31
main.py
|
@ -9,43 +9,25 @@ from time import sleep, time
|
||||||
class Game:
|
class Game:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.running = True
|
self.running = True
|
||||||
self.is_pause = False
|
|
||||||
self.pl = Player()
|
self.pl = Player()
|
||||||
self.out = Out(self.pl)
|
self.out = Out(self.pl)
|
||||||
|
self.is_pause = False
|
||||||
|
|
||||||
kb.add_hotkey(QUIT_BUTTON, self.stop_game)
|
kb.add_hotkey(QUIT_BUTTON, self.stop_game)
|
||||||
kb.add_hotkey(PAUSE_BUTTON, self.switch_pause)
|
kb.add_hotkey(PAUSE_BUTTON, self.switch_pause)
|
||||||
kb.add_hotkey(
|
kb.add_hotkey(LEFT_BUTTON, self.pl.left)
|
||||||
LEFT_BUTTON,
|
kb.add_hotkey(RIGHT_BUTTON, self.pl.right)
|
||||||
self.pl.left,
|
kb.add_hotkey(UP_BUTTON, self.pl.up)
|
||||||
args=[self]
|
kb.add_hotkey(DOWN_BUTTON, self.pl.down)
|
||||||
)
|
|
||||||
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:
|
def switch_pause(self) -> None:
|
||||||
self.is_pause = not self.is_pause
|
self.is_pause = not self.is_pause
|
||||||
|
|
||||||
def stop_game(self) -> None:
|
def stop_game(self) -> None:
|
||||||
if not self.is_pause:
|
|
||||||
self.running = False
|
self.running = False
|
||||||
|
|
||||||
def play(self) -> None:
|
def play(self) -> None:
|
||||||
system("clear||cls")
|
system("clear||cls")
|
||||||
print()
|
|
||||||
|
|
||||||
start_time = time()
|
start_time = time()
|
||||||
different_between_time = 1 / FPS
|
different_between_time = 1 / FPS
|
||||||
|
@ -73,8 +55,7 @@ class Game:
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
game = Game()
|
Game().play()
|
||||||
game.play()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
15
player.py
15
player.py
|
@ -5,7 +5,6 @@ from food import Food
|
||||||
|
|
||||||
class Player:
|
class Player:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.start_direciton = MAIN_DIRECTION
|
|
||||||
self.direction = MAIN_DIRECTION
|
self.direction = MAIN_DIRECTION
|
||||||
self.body = [Point(MAIN_X, MAIN_Y)]
|
self.body = [Point(MAIN_X, MAIN_Y)]
|
||||||
|
|
||||||
|
@ -35,8 +34,6 @@ class Player:
|
||||||
raise IndexError("Player has collision with self")
|
raise IndexError("Player has collision with self")
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
self.start_direction = self.direction
|
|
||||||
|
|
||||||
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
|
||||||
|
@ -45,18 +42,14 @@ class Player:
|
||||||
|
|
||||||
Player._check_collision(self.body)
|
Player._check_collision(self.body)
|
||||||
|
|
||||||
def left(self, game) -> None:
|
def left(self) -> None:
|
||||||
if not game.is_pause and self.start_direction != D_RIGHT:
|
|
||||||
self.direction = D_LEFT
|
self.direction = D_LEFT
|
||||||
|
|
||||||
def right(self, game) -> None:
|
def right(self) -> None:
|
||||||
if not game.is_pause and self.start_direction != D_LEFT:
|
|
||||||
self.direction = D_RIGHT
|
self.direction = D_RIGHT
|
||||||
|
|
||||||
def up(self, game) -> None:
|
def up(self) -> None:
|
||||||
if not game.is_pause and self.start_direction != D_DOWN:
|
|
||||||
self.direction = D_UP
|
self.direction = D_UP
|
||||||
|
|
||||||
def down(self, game) -> None:
|
def down(self) -> None:
|
||||||
if not game.is_pause and self.start_direction != D_UP:
|
|
||||||
self.direction = D_DOWN
|
self.direction = D_DOWN
|
||||||
|
|
|
@ -34,6 +34,3 @@ TT_WALL = "\u001b[34m{}\u001b[0m".format("██")
|
||||||
TT_FOOD = "\u001b[33m{}\u001b[0m".format("██")
|
TT_FOOD = "\u001b[33m{}\u001b[0m".format("██")
|
||||||
TT_WALL_FOOD = "\u001b[32m{}\u001b[0m".format("██")
|
TT_WALL_FOOD = "\u001b[32m{}\u001b[0m".format("██")
|
||||||
TT_HEAD = "\u001b[36m{}\u001b[0m".format("██")
|
TT_HEAD = "\u001b[36m{}\u001b[0m".format("██")
|
||||||
|
|
||||||
# Globals
|
|
||||||
is_pause = False
|
|
||||||
|
|
Loading…
Reference in New Issue