3
votes

I am working on a maze game, the main actor has to find a path by moving between the walls to access the exit.

I did a part of the project, but just that I cannot display objects (syringe, needle and plastic tube) in a random way (they have to change position at each start) in the maze, then to pick up and display a counter which will list the collected items.

I have to modify my GENERATE function, in the loop that goes through my text file I have to retrieve the empty spaces (sprite == 0), put them in a list, then use a random I imagine to retrieve 3 random positions for each object . For example for the three random positions the syringe object stored in a list I have to replace the (sprite == 0) by (sprite == s), s = syringe. So in the end I would have three s positions that I would use in my show function to make the display.

    def generer(self):
    """Method for generating the start based on the file.
    we create a general list, containing one list per line to display"""
    # We open the file
    with open(self.file, "r") as file:
        structure_level = []
        # We browse the lines of the file
        for line in file:
            line_level = []
            # We browse the sprites (letters) contained in the file
            for sprite in line:
                # We ignore the end of line "\ n"
                if sprite != '\n':
                    # We add the sprite to the list of the line
                    line_level.append(sprite)
            # Add the line to the level list
            structure_level.append(line_level)
        # We save this structure
        self.structure = structure_level

    def show(self, window):
    """Méthode permettant d'afficher le niveau en fonction
    de la liste de structure renvoyée par generer()"""
    # Chargement des images (seule celle d'arrivée contient de la transparence)
    wall = pygame.image.load(wall_image).convert()
    departure = pygame.image.load(departure_image).convert_alpha()
    arrived = pygame.image.load(Gardien_image).convert_alpha()
    syringe = pygame.image.load(syringe_image).convert_alpha()

    # We go through the list of the level
    number_line = 0
    for line in self.structure:
        # On parcourt les listes de lignes
        num_case = 0
        for sprite in line:
            # We calculate the real position in pixels
            x = num_case * sprite_size
            y = number_line * sprite_size
            if sprite == 'w':  # w = Wall
                window.blit(wall, (x, y))
            elif sprite == 'd':  # d = Départure
                window.blit(departure, (x, y))
            elif sprite == 'a':  # a = Arrived
                window.blit(arrived, (x, y))
            elif sprite == 's':  # s = syringe
                window.blit(syringe, (x, y))

            num_case += 1
        number_line += 1

After that I have to find a way to compare the position (x and y) of an object (syringe for example) to the current position of the main character, and if the two are equal then I could say that the character is exactly on the spot. 'object.

Here is mn problem, I hope I have explained well.

Thank you

1

1 Answers

0
votes

For randomly placing (3) syringes, try this logic:

  • Count all the spaces in the maze
  • Divide space count by 3
  • In each third, place a syringe in a random position

This should keep the syringe positions random, but evenly distributed.

Here is the updated code. It is not tested so you may need to adjust it some.

def generer(self):
"""Method for generating the start based on the file.
we create a general list, containing one list per line to display"""
import random
zerocnt = 0 # counter for empty spaces
# We open the file
with open(self.file, "r") as file:
    structure_level = []
    # We browse the lines of the file
    for line in file:
        line_level = []
        # We browse the sprites (letters) contained in the file
        for sprite in line:
            # We ignore the end of line "\ n"
            if sprite != '\n':
                # We add the sprite to the list of the line
                line_level.append(sprite)
                if sprite == 0: zerocnt += 1 # another space
        # Add the line to the level list
        structure_level.append(line_level)

    # 3 syringes, distribute evenly
    div3 = zerocnt//3  # divide empty spaces by 3
    # generate 3 positions, 1 syringe per third of maze
    poslst = [random.randint(i*div3, (i+1)*div3-1) for i in [0,1,2]]
    ctr=0
    # scan empty spaces, update 3 spots
    for lvl in structure_level:
       for i in range(len(lvl)):
           if ctr in poslst:
               lvl[i]='s' # put syringe here
               ctr += 1
    
    # We save this structure
    self.structure = structure_level

For the other part of the post, checking if the player found a syringe, I don't know how you're moving the player, so I can only guess. Assuming the player has an (x,y) coordinate, you can check if the that position has an 's' in the array.

Try something like this:

Player.X = 5
Player.Y = 5
if self.structure[Player.X][Player.Y] == 's':
    print("Found syringe")
    Player.Syringes += 1
    self.structure[Player.X][Player.Y] = 0  # remove syringe from maze (or set to player)