Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
platformer enemy animation
#1
Im working on a platformer for my game and im trying to learn how to add animation to enemies i was looking at a tutorial on how to add animation too enemies i copied it into my own but it just come up with an error saying pygame has no attribute image what does this mean and how can i fix it


class Enemy1(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.animation_images  = [pygame.image.load('Arlong_1.png'), pygame.image.load('Arlong_2.png'),
        pygame.image.load('Arlong_3.png')]
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y 
        self.move_direction = 1
        self.move_counter = 0

    def update(self):
        self.rect.x += self.move_direction
        self.move_counter += 1
        if abs(self.move_counter) > 50:
            self.move_direction *= -1
            self.move_counter *= -1
I tried changing the word image to img or images but it still didnt work I dont know why
Reply
#2
Please post entire error message, including the stack trace
Reply
#3
(Jan-10-2023, 03:06 AM)deanhystad Wrote: Please post entire error message, including the stack trace

Here I can send an image

Attached Files

Thumbnail(s)
   
Reply
#4
Post text, not links to images. If you post the code and error messages as text I can reference the text in my response.

The quick answer is that when your Arlong_1 object does not have an image attribute. Later in the __iniit__ method you assign a value to sefl.image, creating the attribute, but at the point where the error is raised, you don't yet have an image.

I guess the same is true for your Enemy class. You reference self.image in line 6, but nowhere do you assign a value to self.image. Should you be using one of the animation images?
self.image = self.animation_images[0]
A shortcut for making multiple images. Instead of this:
self.animation_images  = [pygame.image.load('Arlong_1.png'), pygame.image.load('Arlong_2.png'),
        pygame.image.load('Arlong_3.png')]
do this
self.animation_images  = [pygame.image.load(f'Arlong_{x}.png') for x in (1, 2, 3)]
Reply
#5
(Jan-10-2023, 04:56 PM)deanhystad Wrote: Post text, not links to images. If you post the code and error messages as text I can reference the text in my response.

The quick answer is that when your Arlong_1 object does not have an image attribute. Later in the __iniit__ method you assign a value to sefl.init, creating the attribute, but at the point where the error is raised, you don't yet have an image.

I guess the same is true for your Enemy class. You reference self.image in line 6, but nowhere do you assign a value to self.image. Should you be using one of the animation images?
self.image = self.animation_images[0]
A shortcut for making multiple images. Instead of this:
self.animation_images  = [pygame.image.load('Arlong_1.png'), pygame.image.load('Arlong_2.png'),
        pygame.image.load('Arlong_3.png')]
do this
self.animation_images  = [pygame.image.load(f'Arlong_{x}.png') for x in (1, 2, 3)]

my bad for sending a photo thanks for the help
Reply


Forum Jump:

User Panel Messages

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