如何解決這個意外的關鍵字參數'size'?TypeError:__init __()得到了一個意外的關鍵字參數'size
Traceback (most recent call last):
File "H:\Documents\Astro game\astro games6.py", line 73, in <module>
main()
File "H:\Documents\Astro game\astro games6.py", line 61, in main
new_asteroid = Asteroid(x = x, y = y,size = size)
TypeError: __init__() got an unexpected keyword argument 'size'
的完整代碼:
import random
from superwires import games
games.init(screen_width = 640, screen_height = 480, fps = 50)
class Asteroid(games.Sprite):
""" An asteroid wich floats across the screen"""
SMALL = 1
MEDIUM = 2
LARGE = 3
images = {SMALL : games.load_image("asteroid_small.bmp"),
MEDIUM : games.load_image("asteroid_med.bmp"),
LARGE : games.load_image("asteroid_big.bmp")}
speed = 2
def _init_(self, x, y, size):
"""Initialize asteroid sprite"""
super(Asteroid, self)._init_(
image = Asteroid.images[size],
x = x, y = y,
dx = random.choice([1, -1]) *Asteroid.SPEED* random.random()/size,
dy = random.choice([1, -1]) *Asteroid.SPEED* random.random()/size)
self.size = size
def update (self):
""" Warp around screen"""
if self.top>games.screen.height:
self.bottom = 0
if self.bottom < 0:
self.top=games.screen.height
if self.left > games.screen.width:
self.right = 0
if self.left < 0:
self.left = games.screen.width
class Ship(games.Sprite):
"""The player's ship"""
image = games.load_image("ship.bmp")
ROTATION_STEP = 3
def update(self):
if games.keyboard.is_pressed(games.K_LEFT):
self.angle -= Ship.ROTATION_STEP
if games.keyboard.is_pressed(games.K_RIGHT):
self.angle += Ship.ROTATION_STEP
def main():
nebula_image = games.load_image("nebula.jpg")
games.screen.background = nebula_image
for i in range(8):
x = random.randrange(games.screen.width)
y = random.randrange(games.screen.height)
size = random.choice([Asteroid.SMALL, Asteroid.MEDIUM, Asteroid.LARGE])
new_asteroid = Asteroid(x = x, y = y,size = size)
games.screen.add(new_asteroid)
the_ship = Ship(image = Ship.image,
x = games.screen.width/2,
y = games.screen.height/2)
games.screen.add(the_ship)
games.screen.mainloop()
main()
我曾試圖消除大小參數,但它的代碼導致更多的錯誤。我們還嘗試將尺寸標籤更改爲其他內容,以查看是否有幫助,但也不起作用。所以我是我需要做的,以使這個代碼工作的東西。我正在爲它在學校的一個班級項目上工作。英語不是我的第一語言,所以我有一個同學寫這篇文章。
謝謝。
從參數列表中刪除大小... – Alexander
您能否顯示一些代碼?你想要初始化什麼對象?如果你寫這個對象,'__init__'是否有參數'size'?如果這是外部代碼,是否記錄了參數'size'? –
可以請你分享一下小行星__init __()函數代碼 –