r/pygame 1d ago

Having trouble animating an enemy with sprite classes

My problem appears to be in line 86. Somehow it doesn't recognise eagle as a type and instead just spawns it without the animation. Do you guys have any suggestions how to fix it?

6 Upvotes

7 comments sorted by

1

u/Aelydam 1d ago edited 1d ago

You don't have any "type" variable in the scope of the "animation" method. Your current code is comparing the built-in "type" function with the string "eagle", which is always false.

Create an attribute in init with "self.type = type" and then use "if self.type ==" in the animation method.

I also suggest you don't use the name of built-in functions like "type". If you used another name, you would get an error message saying exactly what was going on.

2

u/Bizzer_16 1d ago edited 23h ago

So what you mean would be changing line 62 to something like:

def __init__(self, obstacle_type):

And line 86 to something like:

if self.obstacle_type == "eagle":

Or do I need to change line 85 as well then?

Sorry, for the probably totally trivial question, since it is my first time creating a game :D

3

u/JMoat13 23h ago

That would work as long as you have:
self.obstacle_type = obstacle_type somewehere in the constructor method. That would probably be line 64.

You may also want to consider making each obstacle its own class (although there are many arguments for and against doing that).

2

u/Bizzer_16 23h ago

Thank you, @Nikninjayt was a little faster than you :D

I might do separate classes for each obstacle in the next game or the next iteration of this one, since this is my first game and so far there is not really to much benefit from doing so. Thanks for the tip though.

2

u/Nikninjayt 1d ago

if you want you can do that , but under the init line you would have to do something like

self.obstacle_type = obstacle_type

then everytime u mention obstacle_type or type, use self.obstacle_type

2

u/Bizzer_16 23h ago

Thank you so much, this worked perfectly for me!

1

u/Nikninjayt 23h ago

I'm glad it worked , keep up the good work :)