游戏很简单,Pan对象捕获Chef对象分配的Pizza对象。当Pan对象(用户)错过Pizza对象并且它击中屏幕的地板时,打印“游戏结束”(def game_over(self))并且游戏终止。但是在游戏终止后,我的IDE给了我这些错误:
Traceback (most recent call last):
File "C:\Users\COMPAQ\My Documents\Aptana Studio Workspace\Pythonic Things\PizzaPanicAttempt\PizzaPanicAttempt3.py", line 125, in <module>
main()
File "C:\Users\COMPAQ\My Documents\Aptana Studio Workspace\Pythonic Things\PizzaPanicAttempt\PizzaPanicAttempt3.py", line 124, in main
games.screen.mainloop()
File "C:\Python27\lib\site-packages\livewires\games.py", line 308, in mainloop
object._tick()
File "C:\Python27\lib\site-packages\livewires\games.py", line 506, in _tick
self.update()
File "C:\Users\COMPAQ\My Documents\Aptana Studio Workspace\Pythonic Things\PizzaPanicAttempt\PizzaPanicAttempt3.py", line 67, in update
self.check_collision()
File "C:\Users\COMPAQ\My Documents\Aptana Studio Workspace\Pythonic Things\PizzaPanicAttempt\PizzaPanicAttempt3.py", line 72, in check_collision
Pizza.handle_caught()
AttributeError: 'Pan' object has no attribute 'handle_caught'
该计划的实际代码如下:
'''
Created on Jul 1, 2011
@author: ******** Louis
'''
#Watch me do.
from livewires import games, color
import random
games.init (screen_width = 640, screen_height = 480, fps = 50)
#Pizza Class
class Pizza (games.Sprite):
pizzaimage = games.load_image ("pizza.bmp", transparent = True)
def __init__(self, x, y = 90, dy = 4):
super (Pizza, self).__init__(x = x,
y = y,
image = Pizza.pizzaimage,
dy = dy)
def update (self):
if self.bottom>640:
self.game_over()
self.destroy()
def handle_caught (self):
self.destroy()
def game_over (self):
gameovermessage = games.Message(value = "Game Over",
size = 90,
color = color.red,
x = 320,
y = 240,
lifetime = 250,
after_death = games.screen.quit())
games.screen.add(gameovermessage)
#Pan/Cursorial Class
class Pan (games.Sprite):
panimage = games.load_image ("pan.bmp", transparent = True)
def __init__ (self, x = games.mouse.x, y = games.mouse.y):
super (Pan, self).__init__(x = x,
y = y,
image = Pan.panimage)
self.score = 0
self.textbox = games.Text (value = self.score,
size = 20,
color = color.black,
x = 550,
y = 50)
games.screen.add(self.textbox)
def update (self): #WWWWOW There is actually an *update* method
self.x = games.mouse.x
self.y = games.mouse.y
if self.left < 0:
self.left = 0
if self.right >640:
self.right = 640
if self.top < 0:
self.top = 0
if self.bottom > 480:
self.bottom = 480
self.check_collision()
def check_collision (self):
for Pizza in self.overlapping_sprites:
self.textbox.value += 10
Pizza.handle_caught()
#The Pizza Dispenser Class!
class Chef (games.Sprite):
chefimage = games.load_image ("chef.bmp", transparent = True)
def __init__(self, y = 55):
super (Chef, self).__init__(x = 320,
y = y,
dx = 2,
image = Chef.chefimage)
self.timervar = 0
def update (self):
if self.left < 0 or self.right > 640 or random.randrange(50) == 7:
self.dx = -self.dx
self.check_drop()
def check_drop (self):
if self.timervar > 0:
self.timervar = self.timervar - 1
else:
le_pizza = Pizza (x = self.x)
games.screen.add(le_pizza)
self.timervar = 100
#main
def main():
wallimage = games.load_image ("wall.jpg", transparent = True)
games.screen.background = wallimage
games.screen.add (Chef())
games.screen.add (Pan())
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main()
#main
def main():
wallbackground = games.load_image ("wall.jpg", transparent = False)
games.screen.background = wallbackground
games.screen.add(Chef())
games.screen.add(Pan())
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main()
答案 0 :(得分:1)
您的self.overlapping_sprites
之一是Pan
,而不是Pizza
。
此外,您还有两个main()
。