对象行为异常 [英] Object not behaving correctly

查看:90
本文介绍了对象行为异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Livewires和pygame,游戏中我为您提供额外生命的物体之一被误认为是小行星物体,并且当额外生命物体与玩家发生碰撞时,它会返回额外生命物体没有属性handle_caught"错误消息,请给我一些帮助.

I'm using Livewires and pygame and one of my objects in the game that gives you extra lives is being mistaken as an asteroid object, and when the extra lives objects collides with the player it returns the 'Extra lives object has no attribute handle_caught' error message, so can I please have some help.

class Extralives(games.Sprite):
global lives

image = games.load_image('lives.png', transparent = True)
speed = 2

def __init__(self,x,y = 10):
    """ Initialize a asteroid object. """
    super(Extralives, self).__init__(image = Extralives.image,
                                x = x, y = y,
                                dy = Extralives.speed)
def update(self):
    """ Check if bottom edge has reached screen bottom. """
    if self.bottom>games.screen.height:
        self.destroy()

    self.add_extralives

def add_extralives(self):
    lives+=1

小行星类:

class Asteroid(games.Sprite):
global lives
global score
"""
A asteroid which falls through space.
"""

image = games.load_image("asteroid_med.bmp")
speed = 1.7

def __init__(self, x,image, y = 10):
    """ Initialize a asteroid object. """
    super(Asteroid, self).__init__(image = image,
                                x = x, y = y,
                                dy = Asteroid.speed)


def update(self):
    """ Check if bottom edge has reached screen bottom. """
    if self.bottom>games.screen.height:
        self.destroy()
        score.value+=10

def handle_caught(self):
    if lives.value>0:
        lives.value-=1
        self.destroy_asteroid()

    if lives.value <= 0:
        self.destroy_asteroid()
        self.end_game()


def destroy_asteroid(self):
    self.destroy()

用于处理碰撞的玩家类的一部分:

part of the player class which handles the collisions:

def update(self):
    """ uses A and D keys to move the ship """
    if games.keyboard.is_pressed(games.K_a):
        self.x-=4
    if games.keyboard.is_pressed(games.K_d):
        self.x+=4

    if self.left < 0:
        self.left = 0

    if self.right > games.screen.width:
        self.right = games.screen.width

    self.check_collison()

def ship_destroy(self):
    self.destroy()

def check_collison(self):
    """ Check if catch pizzas. """
    global lives
    for asteroid in self.overlapping_sprites:
        asteroid.handle_caught()
        if lives.value <=0:
            self.ship_destroy()

    for extralives in self.overlapping_sprites:
        extralives.add_extralives()

推荐答案

这是您的问题:

for asteroid in self.overlapping_sprites:
    asteroid.handle_caught()
    if lives.value <=0:
        self.ship_destroy()

调用 循环变量asteroid的事实并不意味着它神奇地只会成为小行星.如果您还有其他可以碰撞的物体,则不会! overlapping_sprites是所有重叠的精灵,而不仅仅是小行星.在某些时候,asteroidExtraLives对象.当您尝试在其上调用handle_caught()时,这显然会失败,因为ExtraLives没有handle_caught()方法.

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.

这里最简单的解决方案是在ExtraLives类上将add_extralives重命名为handle_caught.毕竟,您在做相同的事情:处理与对象碰撞(或捕获")的情况,它只是一种不同的对象,因此结果需要有所不同,您可以通过提供不同的代码来指定.能够通过调用相同的方法(称为多态")来实现完全不同的行为,这完全是面向对象编程的重点.

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.

以下循环存在类似的问题,因为您正在对可能不是ExtraLives类型的对象调用add_extralives().幸运的是,您可以删除此代码,因为您已经通过将add_extralives重命名为handle_caught来处理这种情况.

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.

for extralives in self.overlapping_sprites:
    extralives.add_extralives()

这篇关于对象行为异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆