2016-08-22 75 views
-3

如何解决这个意外的关键字参数'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() 

我曾试图消除大小参数,但它的代码导致更多的错误。我们还尝试将尺寸标签更改为其他内容,以查看是否有帮助,但也不起作用。所以我是我需要做的,以使这个代码工作的东西。我正在为它在学校的一个班级项目上工作。英语不是我的第一语言,所以我有一个同学写这篇文章。

谢谢。

+2

从参数列表中删除大小... – Alexander

+3

您能否显示一些代码?你想要初始化什么对象?如果你写这个对象,'__init__'是否有参数'size'?如果这是外部代码,是否记录了参数'size'? –

+1

可以请你分享一下小行星__init __()函数代码 –

回答

0

您的小行星类定义_init_而不是__init__。对于Python魔术方法,你需要两边下划线。

-2

删除代码中的size参数。

注意:Python模块名称不应该有空格:您将无法将其导入到另一个模块中。

+1

但是如果他需要'size'参数呢?他不应该只是删除它。他应该学会如何解决这个问题。 –

相关问题