我的程序运行良好,但为什么退出后出现属性错误?

发布于 2024-11-18 12:40:53 字数 4895 浏览 2 评论 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()

The game is simple where the Pan object catches the Pizza objects that are dispensed by the Chef object. When the Pan object (the user) misses a Pizza object, and it hits the floor of the screen, "Game Over" is printed (def game_over(self)) and the game terminates. But after the game terminates, my IDE gives me these error:

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'

The actual code for the program is as follows:

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

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

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

发布评论

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

评论(1

兲鉂ぱ嘚淚 2024-11-25 12:40:53

您的其中一个 self.overlapping_sprites 是一个 Pan,而不是 Pizza

另外,您还有两个 main()

One of your self.overlapping_sprites is a Pan, not a Pizza.

Also, you have two main()s.

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