2011-06-29 55 views
0

当我运行这个时,它会执行一个导入错误。我在代码的结尾处发布了错误。该程序本质上不运行,我不知道回溯发生了什么错误。任何洞察力将不胜感激。调试一个简单的pygame游戏

from livewires import games, color 
import random 

games.init (screen_width = 640, screen_height = 480, fps = 50) 

class Pizza (games.Sprite): 
     def __init__(self, screen, x, y, image): 
      self.pizzaimage = games.load_image ("pizza.bmp", transparent = True) 
      self.init_sprite (screen = screen, 
           x = x, 
           y = 90, 
           image = self.pizzaimage, 
           dy = 1) 
     def moved (self): 
      if self.bottom > 480: 
       self.game_over() 

     def handle_caught (self): 
      self.destroy() 

     def game_over(self): 
      games.Message(value = "Game Over", 
          size = 90, 
          color = color.red, 
          x = 320, 
          y = 240, 
          lifetime = 250, 
          after_death = games.screen.quit()) 
class Pan (games.Sprite): 
    def __init__(self, screen, x, image): 
     self.panimage = games.load_image ("pan.bmp", transparent = True) 
     self.init_sprite (screen = screen, 
          x = games.mouse.x, 
          image = self.panimage) 
     self.score_value = 0 
     self.score_text = games.Text (value = "Score"+str(self.score_value), 
             size = 20, 
             color = color.black, 
             x = 500, 
             y = 20) 

    def update (self): 
     self.x = games.mouse.x 

     #When it reaches end, return value to corner. Example: 
     if self.left < 0: 
      self.left = 0 
     if self.right > 640: 
      self.right = 640 

    def handling_pizzas (self): 
     for Pizza in self.overlapping_sprites: 
      self.score_value += 10 
      Pizza.handle_caught() 

class Chef (games.Sprite): 
    def __init__(self, screen, x, y, image, dx, dy,): 
     self.chefimage = games.load_image ("chef.bmp", transparent = True) 
     self.timer = 80 
     self.init_sprite (screen = screen, 
         x = random.randrange(640), 
         y = y, 
         dx = 20, 
         dy = 0) 

     def update (self): 
      if self.left < 0: 
       self.dx = -self.dx 

      if self.right > 640: 
       self.dx = -self.dx 

      elif random.randrange (10) == 5: 
       self.dx = -self.dx 


     def add_teh_pizzas (self): 
      if self.timer > 80: 
       self.timer = self.timer - 1 
      else: 
       new_pizza = Pizza (x = self.x) 
       games.screen.add (new_pizza) 
       self.timer = 80 

#main 
def main(): 
    backgroundwall = games.load_image ("wall.jpg", transparent = False) 
    games.screen.background = backgroundwall 

    le_chef = Chef =() 
    games.screen.add(le_chef) 
    le_pan = Pan =() 
    games.screen.add (le_pan) 
    games.mouse.is_visible = False 
    games.screen.event_grab = False 
    games.screen.mainloop() 

main() 

错误报告:

Traceback (most recent call last): 
    File "C:\Users\COMPAQ\My Documents\Aptana Studio Workspace\Pythonic Things\PizzaPanicAttempt\PizzaPanicAttempt1.py", line 98, in <module> 
    main() 
    File "C:\Users\COMPAQ\My Documents\Aptana Studio Workspace\Pythonic Things\PizzaPanicAttempt\PizzaPanicAttempt1.py", line 96, in main 
    games.screen.mainloop() 
    File "C:\Python27\lib\site-packages\livewires\games.py", line 303, in mainloop 
    object._erase() 
AttributeError: 'tuple' object has no attribute '_erase' 
+0

我建议你看看这个[guide](http://www.python.org/dev/peps/pep-0008/)。由于您的风格,我无法理解您的代码。 –

+0

这可能是因为你正在使用Rect()或Surface(),当它期待一个Sprite() – ninMonkey

回答

7

不知道livewires什么,但下面看起来可疑:

le_chef = Chef =() 
games.screen.add(le_chef) 
le_pan = Pan =() 
games.screen.add (le_pan) 

你上课叫厨师,潘,但你不会对这些做任何事情 - 只需将这些东西分配给一个空元组,然后将它们添加到games.screen。他们是否应该初始化?

例如。

le_chef = Chef(<some args>) 
le_pan = Pan(<some args>) 
+0

这是许多错误中的第一个,谢谢! – Louis93