dimanche 30 août 2020

Displaying images randomly in a Python game with Pygame

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




Aucun commentaire:

Enregistrer un commentaire