add pause and refactor

main
Plaza521 2022-10-27 15:29:28 +03:00 committed by GitHub
parent ffc75251f1
commit 996eebe470
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 21 deletions

View File

@ -9,10 +9,10 @@ if platform == "win32":
class COORDSET(Structure): class COORDSET(Structure):
_fields_ = [("X", c_long), ("Y", c_long)] _fields_ = [("X", c_long), ("Y", c_long)]
def _set_cursor_position(x: int, y: int) -> None: def set_cursor_position(x: int, y: int) -> None:
windll.kernel32.SetConsoleCursorPosition(STDHANDLE, COORDSET(x, y)) windll.kernel32.SetConsoleCursorPosition(STDHANDLE, COORDSET(x, y))
else: else:
def _set_cursor_position(x: int, y: int) -> None: def set_cursor_position(x: int, y: int) -> None:
print(f"\033[{x};{y}H") print(f"\033[{x};{y}H")
@ -40,7 +40,7 @@ class Frame:
self.matrix[y + line][x + column] = value self.matrix[y + line][x + column] = value
def show(self) -> None: def show(self) -> None:
_set_cursor_position(0, 0) set_cursor_position(0, 0)
out_string = f"{'' * (self.width * 2)}\n" out_string = f"{'' * (self.width * 2)}\n"

22
main.py
View File

@ -11,22 +11,21 @@ class Game:
self.running = True self.running = True
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.pause_game)
kb.add_hotkey(LEFT_BUTTON, self.pl.left) kb.add_hotkey(LEFT_BUTTON, self.pl.left)
kb.add_hotkey(RIGHT_BUTTON, self.pl.right) kb.add_hotkey(RIGHT_BUTTON, self.pl.right)
kb.add_hotkey(UP_BUTTON, self.pl.up) kb.add_hotkey(UP_BUTTON, self.pl.up)
kb.add_hotkey(DOWN_BUTTON, self.pl.down) kb.add_hotkey(DOWN_BUTTON, self.pl.down)
def pause_game(self) -> None:
self.is_pause = not self.is_pause
def stop_game(self) -> None: def stop_game(self) -> None:
self.running = False self.running = False
def update(self) -> None:
pass
def output(self) -> None:
pass
def play(self) -> None: def play(self) -> None:
system("clear||cls") system("clear||cls")
time_to_sleep = 1 / FPS time_to_sleep = 1 / FPS
@ -34,16 +33,19 @@ class Game:
while self.running: while self.running:
try: try:
sleep(time_to_sleep) sleep(time_to_sleep)
self.pl.input()
self.pl.update() if not self.is_pause:
self.out.draw() self.pl.input()
self.pl.update()
self.out.draw()
except IndexError as e: except IndexError as e:
self.running = False self.running = False
print(e) print(e)
except KeyboardInterrupt: except KeyboardInterrupt:
return return
input("Press enter to leave from game") input("Press enter to leave from game.\n")
def main() -> None: def main() -> None:

6
out.py
View File

@ -7,7 +7,6 @@ class Out:
def __init__(self, pl: Player) -> None: def __init__(self, pl: Player) -> None:
self.width = WIDTH self.width = WIDTH
self.height = HEIGHT self.height = HEIGHT
self.fps = FPS
self.pl = pl self.pl = pl
def draw(self) -> None: def draw(self) -> None:
@ -15,11 +14,6 @@ class Out:
pl = self.pl pl = self.pl
for point in pl.body: for point in pl.body:
if point.x < 0 or point.y < 0:
raise IndexError("Snake has collision with walls")
elif point.x >= WIDTH or point.y >= HEIGHT:
raise IndexError("Snake has collision with walls")
frame.draw( frame.draw(
x=point.x, y=point.y, x=point.x, y=point.y,
value=WALL, value=WALL,

View File

@ -23,6 +23,16 @@ class Player:
elif self.direction == D_RIGHT: elif self.direction == D_RIGHT:
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 _check_collision(body: list[Point]) -> None:
for point in body:
if point.x < 0 or point.y < 0:
raise IndexError("Snake has collision with walls")
elif point.x >= WIDTH or point.y >= HEIGHT:
raise IndexError("Snake has collision with walls")
if len(set(body)) != len(body):
raise IndexError("Player has collision with self")
def update(self) -> None: def update(self) -> None:
if self.body[-1] == self.food: if self.body[-1] == self.food:
self.food.generate_new() self.food.generate_new()
@ -30,8 +40,7 @@ class Player:
else: else:
self.body.pop(0) self.body.pop(0)
if len(set(self.body)) != len(self.body): Player._check_collision(self.body)
raise IndexError("Player has collision with self")
def left(self) -> None: def left(self) -> None:
self.direction = D_LEFT self.direction = D_LEFT

View File

@ -5,6 +5,7 @@ FPS = 5
# Buttons # Buttons
QUIT_BUTTON = 'q' QUIT_BUTTON = 'q'
PAUSE_BUTTON = 'p'
LEFT_BUTTON = 'a' LEFT_BUTTON = 'a'
RIGHT_BUTTON = 'd' RIGHT_BUTTON = 'd'
UP_BUTTON = 'w' UP_BUTTON = 'w'