当我尝试运行游戏时,Pygame窗口会冻结

时间:2013-10-09 15:10:26

标签: python pygame

所以我尝试在我的游戏中添加一个随机掉落产生一个导弹包然后玩家可以拿起它并向玩家库存添加一个导弹,但是在创建'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课程的一部分,它将随机生成导弹包以供玩家选择。

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)

1 个答案:

答案 0 :(得分:1)

drop_missile包含无限循环(while True:)。你需要以某种方式退出该循环以便PyGame继续。