Commit 514830cf authored by Anthony Jacob's avatar Anthony Jacob
Browse files

add candy to eat randomly located

parent cd42797a
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ def choose_mode():


def start_game(mode):
    gameState = model.Game(30)
    gameState = model.Game(29)

    if mode == 1:
        start_game_terminal(gameState)
+29 −9
Original line number Diff line number Diff line
import random


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.player_length = 1
        self.lost = False
        self.current_direction = "right"
        self.candy = self.new_candy()

    def move(self, direction):
        """sumary_line
        
        Keyword arguments:
        direction: key pressed by the user
        Return: none
        """
        # the snake can't make a U turn 
        if(direction == "left arrow" and self.current_direction == "right"
           or direction == "right arrow" and self.current_direction == "left"
           or direction == "up arrow" and self.current_direction == "down"
           or direction == "down arrow" and self.current_direction == "up"):
            return self
        
        if direction == "left arrow" and self.check_move(
            self.player_coord[0] - 1, self.player_coord[1]
@@ -37,6 +43,8 @@ class Game:
        else:
            self.lost = True

        self.check_candy()

        return self

    def exit_game(self):
@@ -57,5 +65,17 @@ class Game:
        else:
            return False

    def step(self):
        self.move(self.current_direction + " arrow")
    def new_candy(self):
        return [random.randint(0, self.grid_size), random.randint(0, self.grid_size)]

    def check_candy(self):
        if (
            self.player_coord[0] == self.candy[0]
            and self.player_coord[1] == self.candy[1]
        ):
            self.eat_candy()
            return True

    def eat_candy(self):
        self.player_length += 1
        self.candy = self.new_candy()
+33 −7
Original line number Diff line number Diff line
from pythonsnake import terminalColor
from pythonsnake import terminalColor, model
import os


@@ -6,16 +6,20 @@ def cls():
    os.system("cls" if os.name == "nt" else "clear")


def refresh_grid(gameState):
def refresh_grid(gameState: model.Game) -> None:
    grid_to_print = ""

    for coordY, line in enumerate(gameState.grid):
        # if first row, display the top border
        if coordY == 0:
            grid_to_print += "_" * len(line) * 2
            grid_to_print += "_" * len(line)
            grid_to_print += "\n"

        for coordX, column in enumerate(line):

            line_contain_candy = False
            line_contain_player = False

            # if left border, display border
            if coordX == 0:
                grid_to_print += "|"
@@ -25,8 +29,22 @@ def refresh_grid(gameState):
                coordY == gameState.player_coord[1]
                and coordX == gameState.player_coord[0]
            ):
                grid_to_print += " X"  # type: ignore
            else:
                grid_to_print += terminalColor.OKGREEN + "X" + terminalColor.ENDC  # type: ignore
                line_contain_player = True

            # display candy except if same coord as player
            if (
                coordY == gameState.candy[1]
                and coordX == gameState.candy[0]
                and (
                    gameState.candy[1] != gameState.player_coord[1]
                    or gameState.candy[0] != gameState.player_coord[0]
                )
            ):
                grid_to_print += terminalColor.OKBLUE + "O" + terminalColor.ENDC  # type: ignore
                line_contain_candy = True

            if not line_contain_candy and not line_contain_player:
                grid_to_print += " "  # type: ignore

            # if right border, display border
@@ -35,7 +53,7 @@ def refresh_grid(gameState):

    # if first row, display the top border
    if coordY == len(gameState.grid) - 1:
        grid_to_print += "\u203E" * len(line) * 2
        grid_to_print += "\u203E" * len(line)
        grid_to_print += "\n"

    if gameState.lost:
@@ -52,6 +70,14 @@ def refresh_grid(gameState):
        terminalColor.OKCYAN
        + "pour revenir au menu, appuyer sur la touche (Esc)"
        + terminalColor.ENDC
        + "\n"
    )

    grid_to_print += (
        terminalColor.OKBLUE
        + "Longueur: "
        + str(gameState.player_length)
        + terminalColor.ENDC
    )

    cls()