[Python] TypeError: __init__() got an unexpected keyword argument 'size [duplicate]

Discussão em 'Python' iniciado por Stack, Janeiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    How do I fix this unexpected keyword argument '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'


    The full code:

    import random
    from superwires import games

    games.init(screen_width = 640, screen_height = 480, fps = 50)

    class Asteroid(games.Sprite):
    """ An asteroid which 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()


    I have tried removing the size argument, but it caused more errors in the code. We also tried changing the size tag to something else to see if that helps and it didn't work either. So I'm stuck for what I need to do to make this code work. I am working on it for a class project in school. English is not my first language so I have had a classmate write this up.

    Thank you.

    Continue reading...

Compartilhe esta Página