2016-08-22 3 views
-3

이 예기치 않은 키워드 인수를 어떻게 수정합니까? TypeError : __init __()에 예상치 못한 키워드 인수가 있습니다. '크기가

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_을 정의합니다. 파이썬 매직 메소드에는 양쪽에 밑줄이 두 개 필요합니다.

-2

코드에서 size 인수를 제거하십시오.

참고 : 파이썬 모듈 이름에는 공백이 없어야합니다. 다른 모듈에서 가져올 수 없습니다.

+1

을 공유 할 수 있습니까?하지만 'size' 인수가 필요한 경우 어떻게해야합니까? 그는 단지 그것을 삭제해서는 안됩니다. 그는 문제를 해결하는 방법을 배워야합니다. –

관련 문제