Моя программа работает хорошо, но почему она выдает ошибку атрибута после выхода? - PullRequest
0 голосов
/ 02 июля 2011

Игра проста, когда объект Pan ловит объекты Pizza, которые выдаются объектом Chef.Когда объект «Пан» (пользователь) пропускает объект «Пицца» и попадает на пол экрана, печатается «Игра окончена» (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 Ответ

1 голос
/ 02 июля 2011

Один из ваших self.overlapping_sprites является Pan, а не Pizza.

Кроме того, у вас есть два main() с.

...