Object Not Behaving Correctly
Solution 1:
Here is your problem:
forasteroidinself.overlapping_sprites:
asteroid.handle_caught()
if lives.value <=0:
self.ship_destroy()
The fact that you call your loop variable asteroid
does not mean that it's magically only going to ever be an asteroid. Not if you have other kinds of objects you can collide with! overlapping_sprites
is all overlapping sprites, not just asteroids. At some point asteroid
is an ExtraLives
object. When you try to call handle_caught()
on it, this obviously fails because ExtraLives
doesn't have a handle_caught()
method.
The simplest solution here is to rename add_extralives
to handle_caught
on your ExtraLives
class. After all, you're doing the same thing: handling the situation where you collide with (or "catch") the object, it's just a different kind of object so the result needs to be different, which you specify by providing different code. Being able to implement entirely different kinds of behavior by calling the same methods (called "polymorphism") is kinda the whole point of object-oriented programming.
The following loop has a similar problem, in that you're calling add_extralives()
on objects that might not be of type ExtraLives
. Fortunately you can remove this code since you're already handling this situation by renaming add_extralives
to handle_caught
.
forextralivesinself.overlapping_sprites:
extralives.add_extralives()
Post a Comment for "Object Not Behaving Correctly"