add pause and refactor
parent
ffc75251f1
commit
996eebe470
6
frame.py
6
frame.py
|
@ -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"
|
||||||
|
|
||||||
|
|
16
main.py
16
main.py
|
@ -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)
|
||||||
|
|
||||||
|
if not self.is_pause:
|
||||||
self.pl.input()
|
self.pl.input()
|
||||||
self.pl.update()
|
self.pl.update()
|
||||||
self.out.draw()
|
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
6
out.py
|
@ -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,
|
||||||
|
|
13
player.py
13
player.py
|
@ -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
|
||||||
|
|
|
@ -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'
|
||||||
|
|
Loading…
Reference in New Issue