2011-07-02 24 views
0

在Pan對象捕獲由Chef對象分配的Pizza對象時,遊戲很簡單。當Pan對象(用戶)錯過了一個Pizza對象,並且它碰到屏幕的底部時,打印出「Game Over」(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() 

回答

1

你的一個self.overlapping_sprites的是Pan,而不是一個Pizza

而且,你有兩個main()秒。

+0

啊哈兩個電源都使泛對象重疊。他們被疊加。奇怪,我錯過了,謝謝。 – Louis93

相關問題