console_snake/main.py

55 lines
1.2 KiB
Python
Raw Normal View History

2022-10-26 21:57:29 +03:00
from settings import *
from player import Player
import keyboard as kb
from out import Out
2022-10-26 22:40:18 +03:00
from os import system
2022-10-27 00:29:36 +03:00
from time import sleep
2022-10-26 21:57:29 +03:00
class Game:
def __init__(self) -> None:
self.running = True
self.pl = Player()
2022-10-27 00:29:36 +03:00
self.out = Out(self.pl)
2022-10-26 21:57:29 +03:00
kb.add_hotkey(QUIT_BUTTON, self.stop_game)
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)
def stop_game(self) -> None:
self.running = False
def update(self) -> None:
pass
def output(self) -> None:
pass
def play(self) -> None:
2022-10-26 22:40:18 +03:00
system("clear||cls")
2022-10-27 00:29:36 +03:00
time_to_sleep = 1 / FPS
2022-10-26 21:57:29 +03:00
while self.running:
try:
2022-10-27 00:29:36 +03:00
sleep(time_to_sleep)
self.pl.input()
2022-10-26 21:57:29 +03:00
self.pl.update()
2022-10-27 00:29:36 +03:00
self.out.draw()
2022-10-26 21:57:29 +03:00
except IndexError as e:
self.running = False
print(e)
2022-10-27 00:29:36 +03:00
except KeyboardInterrupt:
return
2022-10-26 21:57:29 +03:00
2022-10-26 23:43:49 +03:00
input("Press enter to leave from game")
2022-10-26 21:57:29 +03:00
def main() -> None:
Game().play()
if __name__ == '__main__':
main()