Compare commits
8 Commits
Author | SHA1 | Date |
---|---|---|
Plaza521 | bf638e711e | |
Plaza521 | a89e41cd68 | |
Plaza521 | aff3036a0d | |
Plaza521 | a77fe7840d | |
Plaza521 | 4840b016db | |
Plaza521 | b6a097bbb3 | |
Plaza521 | a96f8fcc71 | |
Plaza521 | 9e82c9478a |
3
food.py
3
food.py
|
@ -4,9 +4,8 @@ from settings import *
|
|||
|
||||
|
||||
class Food(Point):
|
||||
def __init__(self, x, y, pl):
|
||||
def __init__(self, x, y):
|
||||
Point.__init__(self, x, y)
|
||||
self.pl = pl
|
||||
|
||||
def generate_new(self) -> None:
|
||||
self.x = randint(0, WIDTH - 1)
|
||||
|
|
34
frame.py
34
frame.py
|
@ -23,14 +23,20 @@ class Frame:
|
|||
self.matrix = [[SPACE] * width for line in range(height)]
|
||||
|
||||
def __str__(self) -> str:
|
||||
out_string = f"Width:\n {self.width}\n"
|
||||
out_string += f"Height:\n {self.height}\n"
|
||||
return out_string
|
||||
return (
|
||||
f"Width:\n {self.width}\n"
|
||||
f"Height:\n {self.height}"
|
||||
)
|
||||
|
||||
def draw(
|
||||
self, x: int, y: int,
|
||||
self,
|
||||
|
||||
x: int,
|
||||
y: int,
|
||||
|
||||
value: int = WALL,
|
||||
width: int = 1, height: int = 1
|
||||
width: int = 1,
|
||||
height: int = 1
|
||||
) -> None:
|
||||
if not isinstance(value, int):
|
||||
raise TypeError("Value must be int")
|
||||
|
@ -42,25 +48,27 @@ class Frame:
|
|||
def show(self) -> None:
|
||||
set_cursor_position(0, 0)
|
||||
|
||||
out_string = f"┍{'━' * (self.width * 2)}┑\n"
|
||||
out_string = f"{WALL_COLOR}┍{'━' * (self.width * 2)}┑{RESET_COLOR}\n"
|
||||
|
||||
for line in self.matrix:
|
||||
to_str = ''
|
||||
|
||||
to_str += '│'
|
||||
to_str += f'{WALL_COLOR}│{RESET_COLOR}'
|
||||
for elem in line:
|
||||
if elem == SPACE:
|
||||
to_str += " "
|
||||
to_str += TT_SPACE
|
||||
elif elem == WALL:
|
||||
to_str += "██"
|
||||
to_str += TT_WALL
|
||||
elif elem == FOOD:
|
||||
to_str += "@@"
|
||||
to_str += TT_FOOD
|
||||
elif elem == WALL_FOOD:
|
||||
to_str += "@█"
|
||||
to_str += '│\n'
|
||||
to_str += TT_WALL_FOOD
|
||||
elif elem == HEAD:
|
||||
to_str += TT_HEAD
|
||||
to_str += f'{WALL_COLOR}│{RESET_COLOR}\n'
|
||||
|
||||
out_string += to_str
|
||||
out_string += f"┕{'━' * (self.width * 2)}┙"
|
||||
out_string += f"{WALL_COLOR}┕{'━' * (self.width * 2)}┙{RESET_COLOR}"
|
||||
|
||||
print(out_string)
|
||||
|
||||
|
|
49
main.py
49
main.py
|
@ -3,42 +3,66 @@ from player import Player
|
|||
import keyboard as kb
|
||||
from out import Out
|
||||
from os import system
|
||||
from time import sleep
|
||||
from time import sleep, time
|
||||
|
||||
|
||||
class Game:
|
||||
def __init__(self) -> None:
|
||||
self.running = True
|
||||
self.is_pause = False
|
||||
self.pl = Player()
|
||||
self.out = Out(self.pl)
|
||||
self.is_pause = False
|
||||
|
||||
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(RIGHT_BUTTON, self.pl.right)
|
||||
kb.add_hotkey(UP_BUTTON, self.pl.up)
|
||||
kb.add_hotkey(DOWN_BUTTON, self.pl.down)
|
||||
kb.add_hotkey(PAUSE_BUTTON, self.switch_pause)
|
||||
kb.add_hotkey(
|
||||
LEFT_BUTTON,
|
||||
self.pl.left,
|
||||
args=[self]
|
||||
)
|
||||
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 pause_game(self) -> None:
|
||||
def switch_pause(self) -> None:
|
||||
self.is_pause = not self.is_pause
|
||||
|
||||
def stop_game(self) -> None:
|
||||
self.running = False
|
||||
if not self.is_pause:
|
||||
self.running = False
|
||||
|
||||
def play(self) -> None:
|
||||
system("clear||cls")
|
||||
time_to_sleep = 1 / FPS
|
||||
print()
|
||||
|
||||
start_time = time()
|
||||
different_between_time = 1 / FPS
|
||||
|
||||
while self.running:
|
||||
try:
|
||||
sleep(time_to_sleep)
|
||||
sleep(1 / FPS - different_between_time)
|
||||
start_time = time()
|
||||
|
||||
if not self.is_pause:
|
||||
self.pl.input()
|
||||
self.pl.update()
|
||||
self.out.draw()
|
||||
|
||||
end_time = time()
|
||||
different_between_time = start_time - end_time
|
||||
|
||||
except IndexError as e:
|
||||
self.running = False
|
||||
print(e)
|
||||
|
@ -49,7 +73,8 @@ class Game:
|
|||
|
||||
|
||||
def main() -> None:
|
||||
Game().play()
|
||||
game = Game()
|
||||
game.play()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
6
out.py
6
out.py
|
@ -20,6 +20,12 @@ class Out:
|
|||
width=1, height=1
|
||||
)
|
||||
|
||||
frame.draw(
|
||||
x=pl.body[-1].x, y=pl.body[-1].y,
|
||||
value=HEAD,
|
||||
width=1, height=1
|
||||
)
|
||||
|
||||
if frame.see(pl.food.x, pl.food.y) == WALL:
|
||||
frame.draw(
|
||||
x=pl.food.x, y=pl.food.y,
|
||||
|
|
25
player.py
25
player.py
|
@ -5,10 +5,11 @@ from food import Food
|
|||
|
||||
class Player:
|
||||
def __init__(self) -> None:
|
||||
self.start_direciton = MAIN_DIRECTION
|
||||
self.direction = MAIN_DIRECTION
|
||||
self.body = [Point(MAIN_X, MAIN_Y)]
|
||||
|
||||
self.food = Food(0, 0, self)
|
||||
self.food = Food(0, 0)
|
||||
self.food.generate_new()
|
||||
|
||||
self.score = 1
|
||||
|
@ -34,6 +35,8 @@ class Player:
|
|||
raise IndexError("Player has collision with self")
|
||||
|
||||
def update(self) -> None:
|
||||
self.start_direction = self.direction
|
||||
|
||||
if self.body[-1] == self.food:
|
||||
self.food.generate_new()
|
||||
self.score += 1
|
||||
|
@ -42,14 +45,18 @@ class Player:
|
|||
|
||||
Player._check_collision(self.body)
|
||||
|
||||
def left(self) -> None:
|
||||
self.direction = D_LEFT
|
||||
def left(self, game) -> None:
|
||||
if not game.is_pause and self.start_direction != D_RIGHT:
|
||||
self.direction = D_LEFT
|
||||
|
||||
def right(self) -> None:
|
||||
self.direction = D_RIGHT
|
||||
def right(self, game) -> None:
|
||||
if not game.is_pause and self.start_direction != D_LEFT:
|
||||
self.direction = D_RIGHT
|
||||
|
||||
def up(self) -> None:
|
||||
self.direction = D_UP
|
||||
def up(self, game) -> None:
|
||||
if not game.is_pause and self.start_direction != D_DOWN:
|
||||
self.direction = D_UP
|
||||
|
||||
def down(self) -> None:
|
||||
self.direction = D_DOWN
|
||||
def down(self, game) -> None:
|
||||
if not game.is_pause and self.start_direction != D_UP:
|
||||
self.direction = D_DOWN
|
||||
|
|
12
point.py
12
point.py
|
@ -4,13 +4,13 @@ class Point:
|
|||
self.y = y
|
||||
|
||||
def __eq__(self, other) -> bool:
|
||||
if isinstance(other, Point):
|
||||
if self.x == other.x and self.y == other.y:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
if not isinstance(other, Point):
|
||||
return False
|
||||
|
||||
if self.x == other.x and self.y == other.y:
|
||||
return True
|
||||
else:
|
||||
raise TypeError("You can compare only Point with Point")
|
||||
return False
|
||||
|
||||
def __hash__(self) -> int:
|
||||
return int(f"{abs(self.x)}000{abs(self.y)}")
|
||||
|
|
12
settings.py
12
settings.py
|
@ -2,6 +2,8 @@
|
|||
WIDTH = 20
|
||||
HEIGHT = 20
|
||||
FPS = 5
|
||||
WALL_COLOR = "\u001b[31m"
|
||||
RESET_COLOR = "\u001b[0m"
|
||||
|
||||
# Buttons
|
||||
QUIT_BUTTON = 'q'
|
||||
|
@ -25,3 +27,13 @@ SPACE = 0
|
|||
WALL = 1
|
||||
FOOD = 2
|
||||
WALL_FOOD = 3
|
||||
HEAD = 4
|
||||
|
||||
TT_SPACE = " "
|
||||
TT_WALL = "\u001b[34m{}\u001b[0m".format("██")
|
||||
TT_FOOD = "\u001b[33m{}\u001b[0m".format("██")
|
||||
TT_WALL_FOOD = "\u001b[32m{}\u001b[0m".format("██")
|
||||
TT_HEAD = "\u001b[36m{}\u001b[0m".format("██")
|
||||
|
||||
# Globals
|
||||
is_pause = False
|
||||
|
|
Loading…
Reference in New Issue