main
Plaza521 2022-10-26 21:57:29 +03:00 committed by GitHub
parent 8509c37e6c
commit 9454eb3b24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 245 additions and 0 deletions

13
food.py Normal file
View File

@ -0,0 +1,13 @@
from point import Point
from random import randint
from settings import *
class Food(Point):
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)
self.y = randint(0, HEIGHT - 1)

54
frame.py Normal file
View File

@ -0,0 +1,54 @@
from settings import *
import os
class Frame:
def __init__(self, width: int, height: int) -> None:
self.width = width
self.height = height
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
def draw(
self, x: int, y: int,
value: int = WALL,
width: int = 1, height: int = 1
) -> None:
if not isinstance(value, int):
raise TypeError("Value must be int")
for line in range(height):
for column in range(width):
self.matrix[y + line][x + column] = value
def show(self) -> None:
os.system("clear||cls")
out_string = f"{'' * (self.width * 2)}\n"
for line in self.matrix:
to_str = ''
to_str += ''
for elem in line:
if elem == SPACE:
to_str += " "
elif elem == WALL:
to_str += "██"
elif elem == FOOD:
to_str += "@@"
elif elem == WALL_FOOD:
to_str += "@█"
to_str += '\n'
out_string += to_str
out_string += f"{'' * (self.width * 2)}"
print(out_string)
def see(self, x, y) -> str:
return self.matrix[y][x]

45
main.py Normal file
View File

@ -0,0 +1,45 @@
# from frame import Frame
from settings import *
from player import Player
import keyboard as kb
from out import Out
class Game:
def __init__(self) -> None:
self.running = True
self.pl = Player()
self.out = Out()
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:
while self.running:
try:
self.pl.update()
self.out.draw(self.pl)
except IndexError as e:
self.running = False
print(e)
def main() -> None:
Game().play()
if __name__ == '__main__':
main()

38
out.py Normal file
View File

@ -0,0 +1,38 @@
from settings import *
from player import Player
from frame import Frame
class Out:
def __init__(self) -> None:
self.width = WIDTH
self.height = HEIGHT
self.fps = FPS
def draw(self, pl: Player) -> None:
frame = Frame(self.width, self.height)
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
)
if frame.see(pl.food.x, pl.food.y) == WALL:
frame.draw(
x=pl.food.x, y=pl.food.y,
value=WALL_FOOD
)
else:
frame.draw(
x=pl.food.x, y=pl.food.y,
value=FOOD
)
frame.show()

50
player.py Normal file
View File

@ -0,0 +1,50 @@
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.direction = MAIN_DIRECTION
self.body = [Point(MAIN_X, MAIN_Y)]
self.food = Food(0, 0, self)
self.food.generate_new()
def update(self) -> None:
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()
else:
self.body.pop(0)
if len(set(self.body)) != len(self.body):
raise IndexError("Player has collision with self")
def left(self) -> None:
self.direction = D_LEFT
def right(self) -> None:
self.direction = D_RIGHT
def up(self) -> None:
self.direction = D_UP
def down(self) -> None:
self.direction = D_DOWN

19
point.py Normal file
View File

@ -0,0 +1,19 @@
class Point:
def __init__(self, x, y):
self.x = x
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
else:
raise TypeError("You can compare only Point with Point")
def __hash__(self) -> None:
return int(f"{self.x}000{self.y}")
def __str__(self) -> str:
return f"{self.x = } | {self.y = }"

26
settings.py Normal file
View File

@ -0,0 +1,26 @@
# Main settings
WIDTH = 20
HEIGHT = 20
FPS = 5
# Buttons
QUIT_BUTTON = 'q'
LEFT_BUTTON = 'a'
RIGHT_BUTTON = 'd'
UP_BUTTON = 'w'
DOWN_BUTTON = 's'
# Player settings
D_LEFT = "LEFT"
D_RIGHT = "RIGHT"
D_UP = "UP"
D_DOWN = "DOWN"
MAIN_DIRECTION = D_DOWN
MAIN_X = 0
MAIN_Y = 0
# Out types
SPACE = 0
WALL = 1
FOOD = 2
WALL_FOOD = 3