Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionnary indexing error
#1
Hello, i try to animate a character with 3 frames named "playerf1", 2 and 3, in a "player animation" folder.
I use pygame but that is not at the origin of the problem ^^.

So, i have this error :
self.image2 = self.images[self.current_image]
KeyError: 1

I found that this error occurs when the thing i want to find doesnt exist but right here i dont know why it can not find the images in the dictionnary...
Thanks for your help ^^

class AnimateSprite(pygame.sprite.Sprite):

    def __init__(self, sprite_name):
        super().__init__()
        #load the player
        self.image2 = pygame.image.load(sprite_name + '.png')

        self.current_image = 0
        self.images = animations.get(sprite_name)

    # animate the sprite
    def animate(self):
        self.current_image += 1
        if self.current_image >= len(self.images):
            self.current_image = 0

        self.image2 = self.images[self.current_image]


def load_animation_images(sprite_name):
    images = []

    path = "player animation/" + sprite_name
    for num in range(1, 3):
        image_path = path + str(num) + '.png'
        #pygame.image.load is used to load the image_path
        images.append(pygame.image.load(image_path))

    return images

animations = {
    'player': load_animation_images('playerf')
}

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
Dictionnary indexing error - by Ander - Sep-10-2021, 10:19 AM
RE: Dictionnary indexing error - by Yoriz - Sep-10-2021, 11:15 AM
RE: Dictionnary indexing error - by Ander - Sep-10-2021, 11:25 AM
RE: Dictionnary indexing error - by Yoriz - Sep-10-2021, 11:49 AM
RE: Dictionnary indexing error - by Ander - Sep-10-2021, 12:02 PM
RE: Dictionnary indexing error - by Yoriz - Sep-10-2021, 12:11 PM
RE: Dictionnary indexing error - by Ander - Sep-10-2021, 12:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to change 0 based indexing to 1 based indexing in python..?? Ruthra 2 4,353 Jan-22-2020, 05:13 PM
Last Post: Ruthra
  python dictionnary Omar_Yatim 3 2,847 Dec-08-2019, 05:12 AM
Last Post: scidam
  Dictionnary brackets issue Reldaing 1 1,846 Nov-10-2019, 11:54 PM
Last Post: ichabod801
  Access to the elements of a dictionnary Reims 1 1,644 Oct-02-2019, 12:48 PM
Last Post: SheeppOSU
  from Json Time Serie file to python dictionnary Reims 1 2,039 Sep-11-2019, 08:17 AM
Last Post: DeaD_EyE
  convert a json file to a python dictionnary of array Reims 2 2,259 Sep-10-2019, 01:08 PM
Last Post: Reims
  dictionnary lateublegende 1 2,460 Apr-29-2019, 09:10 PM
Last Post: Yoriz
  Why do we need setdefault() method for dictionnary? DJ_Qu 3 2,721 Apr-21-2019, 11:00 AM
Last Post: Gribouillis
  Json dictionnary on Redis katsu707 1 2,447 Dec-04-2018, 11:59 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020