调试一个简单的pygame游戏

时间:2011-06-29 17:12:00

标签: python debugging pygame

当我运行它时,它会在其中一个导入中执行错误。我在代码的tend末尾发布了错误。该程序基本上没有运行,我无法分辨回溯中发生的错误。任何见解都将不胜感激。

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'

1 个答案:

答案 0 :(得分:7)

livewires 一无所知,但以下内容看起来很可疑:

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

你有一个名为Chef和Pan的课程,但是你没有做任何事情 - 只需将这些东西分配给空元组,然后将它们添加到games.screen中。他们应该被初始化吗?

例如

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