Compare commits

..

No commits in common. "main" and "2" have entirely different histories.
main ... 2

8 changed files with 79 additions and 143 deletions

View File

@ -5,4 +5,3 @@ Default keys:
--
W A S D - move
Q - quit
P - pause

View File

@ -4,8 +4,9 @@ from settings import *
class Food(Point):
def __init__(self, x, y):
def __init__(self, x, y, pl):
Point.__init__(self, x, y)
self.pl = pl
def generate_new(self) -> None:
self.x = randint(0, WIDTH - 1)

View File

@ -9,10 +9,10 @@ if platform == "win32":
class COORDSET(Structure):
_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))
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")
@ -23,20 +23,14 @@ class Frame:
self.matrix = [[SPACE] * width for line in range(height)]
def __str__(self) -> str:
return (
f"Width:\n {self.width}\n"
f"Height:\n {self.height}"
)
out_string = f"Width:\n {self.width}\n"
out_string += f"Height:\n {self.height}\n"
return out_string
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")
@ -46,29 +40,27 @@ class Frame:
self.matrix[y + line][x + column] = value
def show(self) -> None:
set_cursor_position(0, 0)
_set_cursor_position(0, 0)
out_string = f"{WALL_COLOR}{'' * (self.width * 2)}{RESET_COLOR}\n"
out_string = f"{'' * (self.width * 2)}\n"
for line in self.matrix:
to_str = ''
to_str += f'{WALL_COLOR}{RESET_COLOR}'
to_str += ''
for elem in line:
if elem == SPACE:
to_str += TT_SPACE
to_str += " "
elif elem == WALL:
to_str += TT_WALL
to_str += "██"
elif elem == FOOD:
to_str += TT_FOOD
to_str += "@@"
elif elem == WALL_FOOD:
to_str += TT_WALL_FOOD
elif elem == HEAD:
to_str += TT_HEAD
to_str += f'{WALL_COLOR}{RESET_COLOR}\n'
to_str += "@█"
to_str += '\n'
out_string += to_str
out_string += f"{WALL_COLOR}{'' * (self.width * 2)}{RESET_COLOR}"
out_string += f"{'' * (self.width * 2)}"
print(out_string)

62
main.py
View File

@ -1,80 +1,48 @@
# from frame import Frame
from settings import *
from player import Player
import keyboard as kb
from out import Out
from os import system
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.out = Out()
kb.add_hotkey(QUIT_BUTTON, self.stop_game)
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 switch_pause(self) -> None:
self.is_pause = not self.is_pause
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:
if not self.is_pause:
self.running = False
def update(self) -> None:
pass
def output(self) -> None:
pass
def play(self) -> None:
system("clear||cls")
print()
start_time = time()
different_between_time = 1 / FPS
while self.running:
try:
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
self.out.draw(self.pl)
except IndexError as e:
self.running = False
print(e)
except KeyboardInterrupt:
return
input("Press enter to leave from game.\n")
input("Press enter to leave from game")
def main() -> None:
game = Game()
game.play()
Game().play()
if __name__ == '__main__':

20
out.py
View File

@ -4,28 +4,26 @@ from frame import Frame
class Out:
def __init__(self, pl: Player) -> None:
def __init__(self) -> None:
self.width = WIDTH
self.height = HEIGHT
self.pl = pl
self.fps = FPS
def draw(self) -> None:
def draw(self, pl: Player) -> None:
frame = Frame(self.width, self.height)
pl = self.pl
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(
x=point.x, y=point.y,
value=WALL,
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,
@ -38,4 +36,4 @@ class Out:
)
frame.show()
print(f"Score: {pl.score}")
print(F"Score: {pl.score}")

View File

@ -1,41 +1,35 @@
from settings import *
from point import Point
from food import Food
from time import sleep
from itertools import permutations
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.food = Food(0, 0, self)
self.food.generate_new()
self.score = 1
def input(self) -> None:
if self.direction == D_UP:
self.body.append(Point(self.body[-1].x, self.body[-1].y - 1))
elif self.direction == D_DOWN:
self.body.append(Point(self.body[-1].x, self.body[-1].y + 1))
elif self.direction == D_LEFT:
self.body.append(Point(self.body[-1].x - 1, self.body[-1].y))
elif self.direction == D_RIGHT:
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")
self.score = 0
def update(self) -> None:
self.start_direction = self.direction
sleep(1 / FPS)
if self.direction == D_UP:
# self.body[0].y -= 1
self.body.append(Point(self.body[-1].x, self.body[-1].y - 1))
elif self.direction == D_DOWN:
# self.body[0].y += 1
self.body.append(Point(self.body[-1].x, self.body[-1].y + 1))
elif self.direction == D_LEFT:
# self.body[0].x -= 1
self.body.append(Point(self.body[-1].x - 1, self.body[-1].y))
elif self.direction == D_RIGHT:
# self.body[0].x += 1
self.body.append(Point(self.body[-1].x + 1, self.body[-1].y))
if self.body[-1] == self.food:
self.food.generate_new()
@ -43,20 +37,17 @@ class Player:
else:
self.body.pop(0)
Player._check_collision(self.body)
if len(set(self.body)) != len(self.body):
raise IndexError("Player has collision with self")
def left(self, game) -> None:
if not game.is_pause and self.start_direction != D_RIGHT:
def left(self) -> None:
self.direction = D_LEFT
def right(self, game) -> None:
if not game.is_pause and self.start_direction != D_LEFT:
def right(self) -> None:
self.direction = D_RIGHT
def up(self, game) -> None:
if not game.is_pause and self.start_direction != D_DOWN:
def up(self) -> None:
self.direction = D_UP
def down(self, game) -> None:
if not game.is_pause and self.start_direction != D_UP:
def down(self) -> None:
self.direction = D_DOWN

View File

@ -4,13 +4,13 @@ class Point:
self.y = y
def __eq__(self, other) -> bool:
if not isinstance(other, Point):
return False
if isinstance(other, Point):
if self.x == other.x and self.y == other.y:
return True
else:
return False
else:
raise TypeError("You can compare only Point with Point")
def __hash__(self) -> int:
return int(f"{abs(self.x)}000{abs(self.y)}")

View File

@ -2,12 +2,9 @@
WIDTH = 20
HEIGHT = 20
FPS = 5
WALL_COLOR = "\u001b[31m"
RESET_COLOR = "\u001b[0m"
# Buttons
QUIT_BUTTON = 'q'
PAUSE_BUTTON = 'p'
LEFT_BUTTON = 'a'
RIGHT_BUTTON = 'd'
UP_BUTTON = 'w'
@ -27,13 +24,3 @@ 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