0

所以我尝试在我的游戏中添加一个随机掉落物,它会生成一个导弹包,然后玩家可以拾取它,它会在玩家的库存中添加一枚导弹,但是在创建 'MissilePack对象并更新我的 Spawner 类以随机生成导弹包之后Pygame 窗口完全冻结并且不显示任何错误消息,所以我不知道该怎么做,任何帮助将不胜感激。

inventory=[]

class MissilePack(games.Sprite):
    speed = 1.7
    image = games.load_image('MissilePack.bmp')
    global inventory

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


    def update(self):
        """ Check if bottom edge has reached screen bottom. """
        if self.bottom>games.screen.height:
            self.destroy()

    def handle_caughtpack(self):
        inventory.append('missile')

这是我的 Spawner 类的一部分,它将随机生成 Missile Pack 供玩家拾取。

def update(self):
    """ Determine if direction needs to be reversed. """
    if self.left < 0 or self.right > games.screen.width:
        self.dx = -self.dx
    elif random.randrange(self.odds_change) == 0:
       self.dx = -self.dx

    self.check_drop()
    self.drop_missile()

 def drop_missile(self):
    """Randomely drops a missile pack for the player to use"""
    while True:
        dropmissile = random.randrange(0, 10)
        if dropmissile == 5:
            missilepack = MissilePack(x = self.x)
            games.screen.add(missilepack)
4

1 回答 1

1

drop_missile包含一个无限循环 ( while True:)。您需要以某种方式退出该循环,PyGame 才能继续。

于 2013-10-09T15:12:48.367 回答