Ejemplo n.º 1
0
    def test_start_visited(self):
        maze = Maze(10, 10)
        create(maze)

        for x in range(0, len(maze[0])):
            if maze[0][x].getVisited() == False:
                assert maze[0][x].getVisited() == False
Ejemplo n.º 2
0
    def test_starting_position(self):
        maze = Maze(10, 10)
        create(maze)

        for x in range(0, len(maze[0])):
            if maze[0][x].getElement() == ' ':
                assert maze[0][x].getElement() == ' '
Ejemplo n.º 3
0
    def test_end_visited(self):
        maze = Maze(10, 10)
        create(maze)
        lastrow = len(maze) - 1

        for x in range(0, len(maze[lastrow])):
            if maze[lastrow][x].getElement() == ' ':
                assert maze[lastrow][x].getVisited() == True
def mm_maze_create(size):
    w, h = size.split('x')
    w, h = int(w), int(h)

    if w < 5 or h < 5:
        w, h = 5, 5

    maze = Maze(w, h)
    create(maze)
    bank.push(maze)

    print(messages("maze_created"))
    menu()
Ejemplo n.º 5
0
 def test_cell_get_element_pound(self):
     maze = Maze(10, 10)
     assert maze[1][1].getElement() == '#'
Ejemplo n.º 6
0
 def test_cell_get_y(self):
     maze = Maze(10,10)
     assert maze[6][4].getY() == 6
Ejemplo n.º 7
0
 def test_width(self):
     maze = Maze(20, 5)
     assert maze.getCols() == 20
Ejemplo n.º 8
0
 def test_cell_get_x(self):
     maze = Maze(10,10)
     assert maze[4][3].getX() == 3
Ejemplo n.º 9
0
 def test_height(self):
     maze = Maze(5, 50)
     assert maze.getRows() == 50
Ejemplo n.º 10
0
 def test_maze_fill_space(self):
     maze = Maze(5, 5)
     assert maze[4][4].getElement() != ' '
Ejemplo n.º 11
0
 def test_cell_get_visited_true(self):
     maze = Maze(10, 10)
     maze[1][1].setVisited(True)
     assert maze[1][1].getVisited() == True
Ejemplo n.º 12
0
 def test_maze_set_name(self):
     maze = Maze(5, 5)
     maze.setName("Testing")
     assert maze.getName() == "Testing"
Ejemplo n.º 13
0
 def test_maze_fill_pound(self):
     maze = Maze(5, 5)
     assert maze[0][0].getElement() == '#'
Ejemplo n.º 14
0
 def test_maze_get(self):
     maze = Maze(5, 5)
     assert isinstance(maze[0][0], Cell)
Ejemplo n.º 15
0
 def test_maze_name(self):
     maze = Maze(5, 5)
     assert maze.getName() == "MAZE_5x5"
Ejemplo n.º 16
0
 def test_min_height(self):
     maze = Maze(2, 2)
     assert maze.getRows() == 5
Ejemplo n.º 17
0
 def test_cell_get_element_space(self):
     maze = Maze(10, 10)
     maze[1][1].setElement(' ')
     assert maze[1][1].getElement() == ' '
Ejemplo n.º 18
0
 def test_maze_image_null(self):
     maze = Maze(5, 5)
     assert maze.getMazeImage() == []
Ejemplo n.º 19
0
 def test_cell_get_visited_false(self):
     maze = Maze(10, 10)
     assert maze[1][1].getVisited() == False
Ejemplo n.º 20
0
 def __init__(self):
     self.maze = Maze('level/maze.txt')
Ejemplo n.º 21
0
 def test_cell_neighbours(self):
     maze = Maze(10,10)
     assert maze[1][1].findUnvisited(maze)
Ejemplo n.º 22
0
 def __init__(self):
     self.maze = Maze()
     self.game_result = 0  # win_game = 1 and lose_game = 2
     self._initialisation()
     self.play_game()  # game management
Ejemplo n.º 23
0
 def test_maze_eimage_null(self):
     maze = Maze(5, 5)
     assert maze.getMazeEnhancedImage() == []
Ejemplo n.º 24
0
 def test_min_width(self):
     maze = Maze(2, 2)
     assert maze.getCols() == 5
Ejemplo n.º 25
0
 def test_maze_len(self):
     maze = Maze(5, 10)
     assert len(maze) == 10
Ejemplo n.º 26
0
class Game:
    """
        This class manages the animation
        and the graphics part of the program
    """
    def __init__(self):
        self.maze = Maze()
        self.game_result = 0  # win_game = 1 and lose_game = 2
        self._initialisation()
        self.play_game()  # game management

    def display_maze_game(self):
        """ displays the items of maze after each movement of MacGyver """
        # the sleep mode wil be actived at the end of game
        sleep = False
        # display the background in the window "fenetre"
        self.fenetre.blit(self.fond_pic, (0, 0))
        # display MacGyver in the window "fenetre"
        self.fenetre.blit(self.macgyver_pic, self.macgyver_pic_position)

        # display differents items of the maze in the window "fenetre"
        for (coords, element) in self.maze.grid.items():
            if element == "w":  # "w" is equivalent to a wall
                self.fenetre.blit(self.mur_pic,
                                  (coords[0] * 40, coords[1] * 40))
            elif element == "G":  # "G" is equivalent to the Guardian
                # game_result != 0 => "end of game" => no more Guardian
                if self.game_result == 0:
                    self.fenetre.blit(self.guardian_pic,
                                      (coords[0] * 40, coords[1] * 40))
            elif element == "needle":
                self.fenetre.blit(self.needle_pic,
                                  (coords[0] * 40, coords[1] * 40))
            elif element == "tube":
                self.fenetre.blit(self.tube_pic,
                                  (coords[0] * 40, coords[1] * 40))
            elif element == "ether":
                self.fenetre.blit(self.ether_pic,
                                  (coords[0] * 40, coords[1] * 40))

        if self.game_result == 1:  # if the player win
            self.fenetre.blit(self.win_pic, (40, 10))
            # actives the sleep mode
            sleep = True
        elif self.game_result == 2:  # if the player lose
            self.fenetre.blit(self.game_over_pic, (0, 0))
            # actives the sleep mode
            sleep = True
        display.flip()  # Screen refresh
        # maintains display for a few seconds after the game ends
        if sleep:
            time.sleep(DELAY)

    def play_game(self):
        """ detects pressing the keyboard's arrows,
            calculates new coordinates of MacGyver and moves him
        """
        # movement when an arrow is held down
        key.set_repeat(FAST[0], FAST[1])

        # this loop continues while game_result == 0
        while self.game_result == 0:
            # browses the list of all the events received
            for evt in event.get():

                if evt.type == QUIT:  # if an event is "QUIT"
                    pygame.quit()  # deactivates the Pygame library
                    exit()  # stop the game

                if evt.type == KEYDOWN:
                    self.test_destination_after_keydown(evt.key)

            self.display_maze_game()

    def test_destination_after_keydown(self, eventkey):
        """ tests the destination after "KEYDOWN" """
        # corresponding movement in the game
        mouvements = {
            K_DOWN: (0, STEP_MOV),
            K_UP: (0, -STEP_MOV),
            K_RIGHT: (STEP_MOV, 0),
            K_LEFT: (-STEP_MOV, 0)
        }
        # corresponding movement in the Maze class
        mvt_in_maze = {
            K_DOWN: "bottom",
            K_UP: "top",
            K_RIGHT: "right",
            K_LEFT: "left"
        }
        try:
            # calculate new coordinates
            # of MacGyver in the Maze class
            x_new, y_new = self.maze.macgyver.move_to(mvt_in_maze[eventkey])
            # tests if destination is ok
            if self.maze.test_destination_is_valid(x_new, y_new):
                # assigns news coordinates to MacGyver
                # after displacement and collects
                # if an object is on the new cell
                self.maze.consequences_macgyver_displacement(x_new, y_new)

                # tests presence of "Guardian" for news coordinates
                if self.maze.test_presence_of_guardian(x_new, y_new):
                    # tests if MacGyver has collect
                    # all objects to win
                    if sorted(self.maze.macgyver.bag) == \
                            ['ether', 'needle', 'tube']:
                        self.game_result = 1  # it's wined
                    else:
                        self.game_result = 2  # it's lost
                # calculate new position of Angus
                # after "event.key"
                self.macgyver_pic_position = \
                    self.macgyver_pic_position.move(
                        mouvements[eventkey])
        except KeyError:
            print("PAS LA BONNE TOUCHE !!!")

    def _initialisation(self):

        pygame.init()

        # path to graphics resources
        os.chdir("c:/Users/utilisateur/Desktop/Formation_OpenClassRoom/"
                 "Projet_3/Projet/Ressources_graphiques")

        # creates a window to put in items of the maze
        self.fenetre = display.set_mode((600, 600))

        # loads the differents pics of the maze
        self.fond_pic = image.load("fond_mcg.jpg").convert()
        self.mur_pic = image.load("mur.png").convert()
        self.macgyver_pic = image.load("macgyver.png").convert_alpha()
        self.guardian_pic = image.load("gardien.png").convert_alpha()
        self.needle_pic = image.load("seringue.png").convert_alpha()
        self.tube_pic = image.load("tube_plastique.png").convert_alpha()
        self.ether_pic = image.load("ether.png").convert_alpha()

        self.win_pic = image.load("win.png").convert_alpha()
        self.game_over_pic = image.load("game_over.png").convert_alpha()

        # calculates the coordinates of MacGyver \
        #  which will change during the play
        self.macgyver_pic_position = self.macgyver_pic.get_rect()
        self.macgyver_pic_position = self.macgyver_pic_position.move(
            self.maze.macgyver.x * 40, self.maze.macgyver.y * 40)