调试一个简单的 pygame 游戏

发布于 2024-11-17 18:26:40 字数 3847 浏览 1 评论 0原文

当我运行它时,它在其中一个导入中执行时出错。我在代码的末尾发布了错误。该程序本质上不运行,我无法从回溯中判断发生了什么错误。任何见解将不胜感激。

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'

When I run this, it executes to an error in one of the imports. I posted the error at tend end of the code. The program essentially doesn't run, I can't tell what errors are occurring from the tracebacks. Any insight would be appreciated.

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()

ERRORS REPORT:

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在看孤独的风景 2024-11-24 18:26:40

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>)

Don't know anything about livewires, but the following looks suspicious:

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

You have classes called Chef, and Pan, but you're not doing anything with these - just assigning these things an empty tuple, and then adding them to games.screen. Were they supposed to be initialized instead?

eg.

le_chef = Chef(<some args>)
le_pan = Pan(<some args>)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文