From 9454eb3b245faea0a0db285127ece9b44bbc57a9 Mon Sep 17 00:00:00 2001 From: Plaza521 <89989298+Plaza521@users.noreply.github.com> Date: Wed, 26 Oct 2022 21:57:29 +0300 Subject: [PATCH] Add code --- food.py | 13 +++++++++++++ frame.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 45 ++++++++++++++++++++++++++++++++++++++++++++ out.py | 38 +++++++++++++++++++++++++++++++++++++ player.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ point.py | 19 +++++++++++++++++++ settings.py | 26 ++++++++++++++++++++++++++ 7 files changed, 245 insertions(+) create mode 100644 food.py create mode 100644 frame.py create mode 100644 main.py create mode 100644 out.py create mode 100644 player.py create mode 100644 point.py create mode 100644 settings.py diff --git a/food.py b/food.py new file mode 100644 index 0000000..2f9076d --- /dev/null +++ b/food.py @@ -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) diff --git a/frame.py b/frame.py new file mode 100644 index 0000000..73f2b92 --- /dev/null +++ b/frame.py @@ -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] diff --git a/main.py b/main.py new file mode 100644 index 0000000..f39097b --- /dev/null +++ b/main.py @@ -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() diff --git a/out.py b/out.py new file mode 100644 index 0000000..526dc3b --- /dev/null +++ b/out.py @@ -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() diff --git a/player.py b/player.py new file mode 100644 index 0000000..f609a57 --- /dev/null +++ b/player.py @@ -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 diff --git a/point.py b/point.py new file mode 100644 index 0000000..d9d0bc9 --- /dev/null +++ b/point.py @@ -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 = }" diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..6b5a75a --- /dev/null +++ b/settings.py @@ -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