gpt4 book ai didi

python - Pygame,livewires : Attribute error: Message object has no attribute 'handle collide'

转载 作者:太空宇宙 更新时间:2023-11-03 17:41:50 25 4
gpt4 key购买 nike

我正在使用《Python 初学者编程》中的特殊“pygame”和“livewires”制作一个游戏,并且在大多数情况下它运行得很好。当您可以玩游戏时,计时器会滴答作响,然后当尖峰击中您时,会显示文字“输了!”时间:'然后无论你需要什么时间都会出现。但是,尽管消息生命周期为 300,但当屏幕关闭时消息持续时间不到一秒,您会看到以下错误:

Traceback (most recent call last):
File "run.py", line 105, in main
games.screen.mainloop()
File "C:\Python31\lib\site-packages\livewires\games.py, line 308, in mainloop


object._tick()
File "C:\Python31\lib\site-packages\livewires\games.py", live 506, in _tick
self.update()
File "run.py", line 27, in update
self.check_collide()
File "run.py", line 34, in check_collide
spike.handle_collide()
AttributeError: 'Message' object has no attribute 'handle_collide'

哇哦。这是相关代码(根据我的判断):

class Player(games.Sprite):
"""The player that must dodge the spikes."""

def __init__(self, image, x, y):
super().__init__(image=image, x=x, y=y)
"""
Timer
"""
self.timer = games.Text(value = 0,
size = 40,
color = color.black,
x = 600,
y = 440)
games.screen.add(self.timer)

def update(self):
"""Move to the mouse."""
self.x = games.mouse.x
self.y = games.mouse.y
self.check_collide()
self.timer.value = int(time.clock())


def check_collide(self):
"""Check for a collision with the spikes."""
for spike in self.overlapping_sprites:
spike.handle_collide()



class Spike(games.Sprite):
"""A spike!"""

def update(self):
"""Move to the left!"""
self.x -= random.randrange(3,12)
if self.x == 0 or self.x == 1 or self.x == 2 or self.x == 3 or self.x == 4 or self.x == 5 or self.x == 6 or self.x < 10:
self.x = 640
self.y = random.randrange(games.screen.height)

def handle_collide(self):
"""Destroy!"""

timebomb = int(time.clock())
man = ("Lose! Time:",timebomb)
lost_message = games.Message(value = man,
size = 100,
color = color.red,
x = games.screen.width/2,
y = games.screen.height/2,
lifetime = 300,
after_death = games.screen.quit)
games.screen.add(lost_message)

我很困惑。应该调用方法 handle_collide 以便游戏可以完成,但似乎handle_collide是对象消息的一个属性?如果这看起来很愚蠢,那么抱歉,因为我已经诚实地尽了最大努力,正如书名所暗示的那样,我是一个菜鸟。请帮忙,提前致谢。

最佳答案

找到了修复。

def check_collide(self):
"""Check for a collision with the spikes."""
for spike in self.overlapping_sprites:
spike.handle_collide()

for 循环中的“尖峰”只是与玩家发生碰撞的物体的名称。实际发生的情况是玩家与“消息”对象发生碰撞,然后询问消息对象处理 Sprite 碰撞的方式。消息对象未准备好,并且没有“handle_collide”方法来处理此问题。所以它会抛出一个错误。要修复此问题,您只需将文本移离播放器,或者使用“Message”对象并创建 for 循环所需的方法。

关于python - Pygame,livewires : Attribute error: Message object has no attribute 'handle collide' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30442127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com