Commit 6de05c53 authored by Anthony Jacob's avatar Anthony Jacob
Browse files

first draft with a basic menu and a X able to move in a grid

parent 746147e8
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
from pythonsnake import game
from pythonsnake import controller


if __name__ == "__main__":
    game.start_game()
    controller.game_menu()
+71 −0
Original line number Diff line number Diff line
from pythonsnake import model, view
import keyboard
import time


def game_menu():

    while True:
        start_game(choose_mode())


def choose_mode():
    view.menu()

    choice = None
    while choice != "1" and choice != "2" and choice != "3":

        choice = input("=>")
        if choice != "1" and choice != "2" and choice != "3":
            view.menu_choice_incorrect()

    if choice == "3":
        exit()

    return int(choice)


def start_game(mode):
    gameState = model.Game(14)

    if mode == 1:
        start_game_terminal(gameState)
    elif mode == 2:
        start_game_UI(gameState)


def start_game_terminal(gameState):
    view.refresh_grid(gameState)

    keyboard.unhook_all()
    listen_escape_game(gameState)
    listen_move(gameState)

    while not gameState.lost:
        continue

    time.sleep(1)
    keyboard.unhook_all()


def start_game_UI(game):
    pass


def listen_escape_game(gameState):
    keyboard.on_press_key("esc", lambda _: gameState.exit_game())


def listen_move(gameState):
    keyboard.on_press_key(
        "left arrow", lambda _: view.refresh_grid(gameState.move("left arrow"))
    )
    keyboard.on_press_key(
        "right arrow", lambda _: view.refresh_grid(gameState.move("right arrow"))
    )
    keyboard.on_press_key(
        "up arrow", lambda _: view.refresh_grid(gameState.move("up arrow"))
    )
    keyboard.on_press_key(
        "down arrow", lambda _: view.refresh_grid(gameState.move("down arrow"))
    )

pythonsnake/game.py

deleted100644 → 0
+0 −2
Original line number Diff line number Diff line
def start_game():
    print("start")
 No newline at end of file

pythonsnake/model.py

0 → 100644
+53 −0
Original line number Diff line number Diff line
class Game:
    def __init__(self, grid_size) -> None:
        self.grid_size = grid_size
        self.grid = [[i for i in range(grid_size)] for j in range(grid_size)]
        self.player_coord = [0, 0]
        self.lost = False

    def move(self, direction):
        """sumary_line

        Keyword arguments:
        direction: key pressed by the user
        Return: none
        """

        if direction == "left arrow" and self.check_move(
            self.player_coord[0] - 1, self.player_coord[1]
        ):
            self.player_coord[0] -= 1
        elif direction == "right arrow" and self.check_move(
            self.player_coord[0] + 1, self.player_coord[1]
        ):
            self.player_coord[0] += 1
        elif direction == "up arrow" and self.check_move(
            self.player_coord[0], self.player_coord[1] - 1
        ):
            self.player_coord[1] -= 1
        elif direction == "down arrow" and self.check_move(
            self.player_coord[0], self.player_coord[1] + 1
        ):
            self.player_coord[1] += 1
        else:
            self.lost = True

        return self
    
    def exit_game(self):
        self.lost = True

    """
    check if the move is still in the grid
    """

    def check_move(self, coordX, coordY):
        if (
            coordX >= 0
            and coordX <= len(self.grid) - 1
            and coordY >= 0
            and coordY <= len(self.grid[0]) - 1
        ):
            return True
        else:
            return False
+9 −0
Original line number Diff line number Diff line
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
Loading