0
在我的程序中,我的'導彈包'對象被誤認爲是小行星對象,並返回錯誤'Missilepack對象沒有屬性'handle_caught',我不知道不明白爲什麼,修復這個錯誤的任何幫助都會很大。編輯:它似乎導彈被誤認爲小行星,如果這有助於有人幫助我。Python中的對象被誤認爲是另一個對象,所以它不能正常工作
class MissilePack(games.Sprite):
speed = 1.7
image = games.load_image('MissilePack.bmp')
global inventory
def __init__(self, x,y = 10):
""" Initialize a missilepack object. """
super(MissilePack, self).__init__(image = MissilePack.image,
x = x, y = y,
dy = MissilePack.speed)
def update(self):
""" Check if bottom edge has reached screen bottom. """
if self.bottom>games.screen.height:
self.destroy()
def handle_caughtpack(self):
inventory.append('missile')
self.destroy.()
的部分均發生錯誤是在 'asteroid.handlecaught()比特'
def check_collison(self):
""" Check if catch pizzas. """
global lives
for asteroid in self.overlapping_sprites:
asteroid.handle_caught()
if lives.value <=0:
self.ship_destroy()
def check_pack(self):
for missilepack in self.overlapping_sprites:
missilepack.handle_caughtpack()
這是在我的小行星類handle_caught()方法:
def handle_caught(self):
if lives.value>0:
lives.value-=1
self.destroy_asteroid()
if lives.value <= 0:
self.destroy_asteroid()
self.end_game()
這是導彈包和小行星產卵的一部分。
def check_drop(self):
""" Decrease countdown or drop asteroid and reset countdown. """
if self.time_til_drop > 0:
self.time_til_drop -= 0.7
else:
asteroid_size = random.choice(images)
new_asteroid = Asteroid(x = self.x,image = asteroid_size)
games.screen.add(new_asteroid)
# makes it so the asteroid spawns slightly below the spawner
self.time_til_drop = int(new_asteroid.height * 1.3/Asteroid.speed) + 1
def drop_missile(self):
"""Randomely drops a missile pack for the player to use"""
dropmissile = random.randrange(0, 100)
if dropmissile == 5:
missilepack = MissilePack(x = self.x)
games.screen.add(missilepack)
有人嗎?我非常難過。 –
您是否從Mark Dawson的絕對初學者學習Python編程,因爲該代碼看起來與本書中的某個程序相似。如果是從那本書開始,那麼只需閱讀該程序的整個章節,你就可以找到你的答案 – 2013-10-09 17:26:47
顯示整個代碼而不是這些段,這可能會更容易找到這個錯誤 – 2013-10-09 17:31:30