1

After analyzing my code for about 2-3 hours, I looked up the following links that were similar to my problem, which are provided below. But after recognizing what their issue was, I figured my must be the same, unfortunately that's not the case. I'll provide the code where I believe the issue lies, if you need to see more, i'll be more than happy to comply. Thank you.

Hyperlinks...

TypeError: __init__() takes 4 positional arguments but 5 were given

takes 1 positional argument but 2 were given

Here's the code...

def game_loop(player_img):
  player_group = pygame.sprite.GroupSingle()
  global player
  player = Sprites.Player(player_img)
  player.rect.x = WINDOW_WIDTH/2
  player.rect.y = WINDOW_HEIGHT - player.rect.height
  player_group.add(player)

  global asteroid_group
  asteroid_group = pygame.sprite.Group()
  asteroid_stopwatch = Stopwatch.Stopwatch()

  global ufo_group
  ufo_group = pygame.sprite.Group()
  ufo_stopwatch = Stopwatch.Stopwatch()

  global enemy_group
  enemy_group = pygame.sprite.Group()
  enemy_stopwatch = Stopwatch.Stopwatch()

  global bomb_group
  bomb_group = pygame.sprite.Group()
  bomb_stopwatch = Stopwatch.Stopwatch()

  global space_mine_group
  space_mine_group = pygame.sprite.Group()
  space_mine_stopwatch = Stopwatch.Stopwatch()

  explosion_group = pygame.sprite.Group()

  ammo_power_up = Sprites.Ammo_Powerup()
  ammo_power_up_group = pygame.sprite.GroupSingle()
  ammo_power_up_stopwatch = Stopwatch.Stopwatch()

  health_power_up = Sprites.Health_Powerup()
  health_power_up_group = pygame.sprite.GroupSingle()
  health_power_up_stopwatch = Stopwatch.Stopwatch()

  stun = Sprites.Stun()
  stun_group = pygame.sprite.GroupSingle()
  stun_stopwatch = Stopwatch.Stopwatch()
  player_stun_stopwatch = Stopwatch.Stopwatch()

  bullet_list = pygame.sprite.Group()
  enemy_bullet_list = pygame.sprite.Group()

  ammo, health, score = 10, 100, 0

  background = Sprites.Background("Sprites/Background/background.jpg", [0, 0])

  pygame.mixer.music.load("Sprites/Efx/Fantasy_Game_Background.mp3")
  pygame.mixer.music.play(-1)

  power_up_sound = pygame.mixer.Sound("Sprites/Efx/powerup.ogg")
  laser_sound = pygame.mixer.Sound("Sprites/Efx/laser1.ogg")
  space_mine_sound = pygame.mixer.Sound("Sprites/Efx/explosion.ogg")
  bomb_sound = pygame.mixer.Sound("Sprites/Efx/explosion.ogg")

  while True:
    clear_previous(player.rect.x, player.rect.y, player.rect.width, player.rect.height)

    if asteroid_stopwatch.get_seconds() >= 3:
        spawn_asteroid()
        asteroid_stopwatch.reset()

    if ufo_stopwatch.get_seconds() >= 8:
        spawn_ufo()
        ufo_stopwatch.reset()

    if enemy_stopwatch.get_seconds() >= 5:
        spawn_enemy()
        enemy_stopwatch.reset()

    if bomb_stopwatch.get_seconds() >= 6:
        spawn_space_bomb()
        bomb_stopwatch.reset()

    if space_mine_stopwatch.get_seconds() >= 6:
        spawn_space_mine()
        space_mine_stopwatch.reset()

    if ammo_power_up_stopwatch.get_seconds() >= 5:
        ammo_power_up_group.remove(ammo_power_up)
        clear_previous(ammo_power_up.rect.x, ammo_power_up.rect.y, ammo_power_up.rect.width, ammo_power_up.rect.height)

    if ammo_power_up_stopwatch.get_seconds() >= 10:
        ammo_power_up.rect.x = random.randrange(30, 700)
        ammo_power_up.rect.y = random.randrange(200, 930)
        ammo_power_up_group.add(ammo_power_up)
        ammo_power_up_stopwatch.reset()

    if health_power_up_stopwatch.get_seconds() >= 7.5:
        health_power_up_group.remove(health_power_up)
        clear_previous(health_power_up.rect.x, health_power_up.rect.y, health_power_up.rect.width, health_power_up.rect.height)

    if health_power_up_stopwatch.get_seconds() >= 15:
        health_power_up.rect.x = random.randrange(30, 700)
        health_power_up.rect.y = random.randrange(200, 930)
        health_power_up_group.add(health_power_up)
        health_power_up_stopwatch.reset()

    if player_stun_stopwatch.get_seconds() >= 4:
        player.x_vel = 5
        player.y_vel = 5

    if stun_stopwatch.get_seconds() >= 10:
        stun_group.remove(stun)
        clear_previous(stun.rect.x, stun.rect.y, stun.rect.width, stun.rect.height)

    if stun_stopwatch.get_seconds() >= 12:
        stun.rect.x = random.randrange(30, 700)
        stun.rect.y = random.randrange(200, 930)
        stun_group.add(stun)
        stun_stopwatch.reset()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit_game()
        if event.type == pygame.MOUSEBUTTONDOWN and ammo > 1:
            ammo -= 2
            clear_previous(20, 50, 100, 30)

            laser_sound.play()

            bullet = Sprites.Bullet()
            bullet.rect.x = player.rect.right
            bullet.rect.y = player.rect.top
            bullet_list.add(bullet)

            second_bullet = Sprites.Bullet()
            second_bullet.rect.x = player.rect.left
            second_bullet.rect.y = player.rect.top
            bullet_list.add(second_bullet)

    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
        player.move_right()
    if keys[pygame.K_LEFT] or keys[pygame.K_a]:
        player.move_left()
    if keys[pygame.K_UP] or keys[pygame.K_w]:
        player.move_up()
    if keys[pygame.K_DOWN] or keys[pygame.K_s]:
        player.move_down()

    for bullet in bullet_list:
        if bullet.rect.y <= 0:
            bullet_list.remove(bullet)
        clear_previous(bullet.rect.x, bullet.rect.y, bullet.rect.width, bullet.rect.height)

    for asteroid in asteroid_group:
        if asteroid.rect.y >= WINDOW_HEIGHT:
            asteroid_group.remove(asteroid)
        if pygame.sprite.spritecollide(player, asteroid_group, False):
            health -= 15
            clear_previous(20, 20, 100, 30)
            asteroid.hit = True
            bomb_sound.play()
            explosion = Sprites.Explosion(asteroid.rect.x, asteroid.rect.y)
            asteroid_group.remove(asteroid)
            clear_previous(asteroid.rect.x, asteroid.rect.y, asteroid.rect.width, asteroid.rect.height)
            explosion_group.add(explosion)

        clear_previous(asteroid.rect.x, asteroid.rect.y, asteroid.rect.width, asteroid.rect.height)

    for enemy in enemy_group:
        if enemy.rect.y >= WINDOW_HEIGHT:
            enemy_group.remove(enemy)
        if pygame.sprite.spritecollide(player, enemy_group, False):
            health -= 10
            clear_previous(20, 20, 100, 30)
            explosion = Sprites.Explosion(enemy.rect.x, enemy.rect.y)
            enemy_group.remove(enemy)
            clear_previous(enemy.rect.x, enemy.rect.y, enemy.rect.width, enemy.rect.height)
            explosion_group.add(explosion)
        clear_previous(enemy.rect.x, enemy.rect.y, enemy.rect.width, enemy.rect.height)

    for ufo in ufo_group:
        if ufo.rect.y >= WINDOW_HEIGHT:
            ufo_group.remove(ufo)
        if pygame.sprite.spritecollide(player, ufo_group, False):
            health -= 15
            clear_previous(20, 20, 100, 30)
            explosion = Sprites.Explosion(ufo.rect.x, ufo.rect.y)
            ufo_group.remove(ufo)
            clear_previous(ufo.rect.x, ufo.rect.y, ufo.rect.width, ufo.rect.height)
            explosion_group.add(explosion)
        clear_previous(ufo.rect.x, ufo.rect.y, ufo.rect.width, ufo.rect.height)

    for bomb in bomb_group:
        if pygame.sprite.spritecollide(player, bomb_group, False):
            health -= 20
            clear_previous(20, 20, 100, 30)
            bomb_sound.play()
            explosion = Sprites.Explosion(bomb.rect.x, bomb.rect.y)
            bomb_group.remove(bomb)
            clear_previous(bomb.rect.x, bomb.rect.y, bomb.rect.width, bomb.rect.height)
            explosion_group.add(explosion)
        if pygame.sprite.spritecollide(asteroid, bomb_group, True):
            bomb_sound.play()
            explosion = Sprites.Explosion(bomb.rect.x, bomb.rect.y)
            asteroid_group.remove(asteroid)
            explosion_group.add(explosion)
        if bomb_stopwatch.get_seconds() >= 3:
            bomb_group.remove(bomb)
            clear_previous(bomb.rect.x, bomb.rect.y, bomb.rect.width, bomb.rect.height)
        clear_previous(bomb.rect.x, bomb.rect.y, bomb.rect.width, bomb.rect.height)

    for mine in space_mine_group:
        if pygame.sprite.spritecollide(player, space_mine_group, False):
            health -= 20
            clear_previous(20, 20, 100, 30)
            space_mine_sound.play()
            explosion = Sprites.Explosion(mine.rect.x, mine.rect.y)
            space_mine_group.remove(mine)
            clear_previous(mine.rect.x, mine.rect.y, mine.rect.width, mine.rect.height)
            explosion_group.add(explosion)
        if pygame.sprite.spritecollide(asteroid, space_mine_group, True):
            space_mine_sound.play()
            explosion =  Sprites.Explosion(mine.rect.x, mine.rect.y)
            asteroid_group.remove(asteroid)
            explosion_group.add(explosion)
        if space_mine_stopwatch.get_seconds() >= 3:
            space_mine_group.remove(mine)
            clear_previous(mine.rect.x, mine.rect.y, mine.rect.width, mine.rect.height)
        clear_previous(mine.rect.x, mine.rect.y, mine.rect.width, mine.rect.height)

    for enemy_bullet in enemy_bullet_list:
        if enemy_bullet.rect.y >= WINDOW_HEIGHT:
            enemy_bullet_list.remove(enemy_bullet)
        if pygame.sprite.spritecollide(player, enemy_bullet_list, False):
            health -= 10
            clear_previous(20, 20, 100, 30)
            enemy_bullet_list.remove(enemy_bullet)
            clear_previous(enemy_bullet.rect.x, enemy_bullet.rect.y, enemy_bullet.rect.width, enemy_bullet.rect.height)
        clear_previous(enemy_bullet.rect.x, enemy_bullet.rect.y, enemy_bullet.rect.width, enemy_bullet.rect.height)

    if pygame.sprite.groupcollide(bullet_list, asteroid_group, True, True):
        score += 10
        clear_previous(20, 80, 200, 30)
        explosion = Sprites.Explosion(asteroid.rect.x, asteroid.rect.y)
        asteroid.hit = True
        explosion_group.add(explosion)

    if pygame.sprite.groupcollide(bullet_list, enemy_group, True, True):
        score += 20
        clear_previous(20, 80, 200, 30)
        explosion = Sprites.Explosion(enemy.rect.x, enemy.rect.y)
        enemy.hit = True
        enemy_group.add(explosion)

    if pygame.sprite.groupcollide(bullet_list, ufo_group, True, True):
        score += 25
        clear_previous(20, 80, 200, 30)
        explosion = Sprites.Explosion(ufo.rect.x, ufo.rect.y)
        ufo.hit = True
        explosion_group.add(explosion)

    if pygame.sprite.groupcollide(bomb_group, bullet_list, True, True):
        score += 15
        clear_previous(20, 80, 200, 30)
        explosion = Sprites.Explosion(bomb.rect.x, bomb.rect.y)
        bomb_sound.play()
        explosion_group.add(explosion)

    if pygame.sprite.groupcollide(space_mine_group, bullet_list, True, True):
        score += 15
        clear_previous(20, 80, 200, 30)
        explosion = Sprites.Explosion(mine.rect.x, mine.rect.y)
        space_mine_sound.play()
        explosion_group.add(explosion)

    if pygame.sprite.groupcollide(space_mine_group, ufo_group, True, True):
        bomb_sound.play()

    if pygame.sprite.groupcollide(bomb_group, ufo_group, True, True):
        bomb_sound.play()

    if pygame.sprite.groupcollide(space_mine_group, enemy_group, True, True):
        bomb_sound.play()

    if pygame.sprite.groupcollide(bomb_group, enemy_group, True, True):
        bomb_sound.play()

    if pygame.sprite.groupcollide(enemy_group, asteroid_group, True, True):
        bomb_sound.play()

    if pygame.sprite.spritecollide(player, stun_group, False):
        power_up_sound.play()
        stun_group.remove(stun)
        clear_previous(stun.rect.x, stun.rect.y, stun.rect.width, stun.rect.height)
        player.x_vel = 8
        player.y_vel = 8
        player_stun_stopwatch.reset()

    if pygame.sprite.spritecollide(player, ammo_power_up_group, False):
        power_up_sound.play()
        ammo += 10
        clear_previous(20, 50, 100, 30)
        ammo_power_up_group.remove(ammo_power_up)
        clear_previous(ammo_power_up.rect.x, ammo_power_up.rect.y, ammo_power_up.rect.width, ammo_power_up.rect.height)

    if pygame.sprite.spritecollide(player, health_power_up_group, False):
        power_up_sound.play()
        if health >= 90:
            diff = 100 - health
            health += diff
            clear_previous(20, 20, 200, 30)
        else:
            health += 10
            clear_previous(20, 20, 200, 30)
        health_power_up_group.remove(health_power_up)
        clear_previous(health_power_up.rect.x, health_power_up.rect.y, health_power_up.rect.width, health_power_up.rect.height)

    if health <= 0:
        game_over(score)

    window.blit(background.image, background.rect)

    text(20, 20, 20, "Comic Sans MS", "Health: " + str(health), WHITE)
    text(20, 50, 20, "Comic Sans MS", "Ammo: " + str(ammo), WHITE)
    text(20, 80, 20, "Comic Sans MS", "Score: " + str(score), WHITE)

    check_bounds(player)
    player_group.update()
    asteroid_group.update()
    explosion_group.update()
    enemy_group.update(enemy_bullet_list)
    bomb_group.update()
    space_mine_group.update()
    ufo_group.update(enemy_bullet_list)
    ammo_power_up_group.update()
    health_power_up_group.update()
    enemy_bullet_list.update()
    bullet_list.update()
    stun_group.update()

    bullet_list.draw(window)
    asteroid_group.draw(window)
    explosion_group.draw(window)
    enemy_group.draw(window)
    ufo_group.draw(window)
    bomb_group.draw(window)
    space_mine_group.draw(window)
    enemy_bullet_list.draw(window)
    player_group.draw(window)
    ammo_power_up_group.draw(window)
    health_power_up_group.draw(window)
    stun_group.draw(window)

    pygame.display.flip()
    clock.tick(FPS)

Enemy Class Code...

class Enemy(pygame.sprite.Sprite):
  def __init__(self, img):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.image.load(img)
    self.rect = self.image.get_rect()
    self.hit = False
    self.vel_x = 5
    self.vel_y = 5
    self.laser_sound = pygame.mixer.Sound("Sprites/Efx/sfx_laser1.ogg")
    self.timer = Stopwatch.Stopwatch()

  def update(self, bullet_group):
    if self.hit == False:
        self.rect.y += self.vel_y

    if self.timer.get_seconds() >= .7:
        self.laser_sound.play()

        bullet = EnemyBullet()
        bullet.rect.x = self.rect.x + 20
        bullet.rect.y = self.rect.y + 50

        second_bullet = EnemyBullet()
        second_bullet.rect.x = self.rect.x + 65
        second_bullet.rect.y = self.rect.y + 50

        bullet_group.add(bullet)
        bullet_group.add(second_bullet)

        self.timer.reset()

Here's the error...

Traceback (most recent call last):
  File "C:/Users/trist_000/PycharmProjects/SpaceWars/Game.py", line 763, in <module> main()
  File "C:/Users/trist_000/PycharmProjects/SpaceWars/Game.py", line 760, in main
    game_loop(player_img)
  File "C:/Users/trist_000/PycharmProjects/SpaceWars/Game.py", line 724, in game_loop
    enemy_group.update(enemy_bullet_list)
  File "C:\Users\trist_000\AppData\Local\Programs\Python\Python35\lib\site-packages\pygame\sprite.py", line 462, in update
    s.update(*args)
TypeError: update() takes 1 positional argument but 2 were given
Taku
  • 31,927
  • 11
  • 74
  • 85
  • Ya, I trying to limit my question as much as possible, but to be truly honest, I really don't know where the error is, that and I have over 2000 lines of code, so I already kinda limited it. Sorry about that mate :( – 18 Triston Lehmann Jun 10 '17 at 04:29
  • Looking at your enormous function: it might be a good idea to refactor. – Klaus D. Jun 10 '17 at 04:31
  • True, I considered that but at the same time, my game is basically complete. I just need to resolve this error. But, for sure the next game I produce. I'll make sure to "refactor", my code. – 18 Triston Lehmann Jun 10 '17 at 04:35
  • @KlausD. makes a valid point, breaking down this massive function into several would make it easier to read and fix. – gold_cy Jun 10 '17 at 04:38
  • @18TristonLehmann The problem is that you are using `Group.update()` the wrong way. The function is meant to be called in your game loop to continually render the sprites in the group to the screen. Pygame does not allow you to pass in arguments to the `update()` function - only `self`. – Christian Dean Jun 10 '17 at 04:38
  • Ohh really, I'm still relatively new to the pygame module, but your saying I can't provide arguments in the update function. – 18 Triston Lehmann Jun 10 '17 at 04:41
  • @18TristonLehmann I'm not exactly sure what you mean. The specific line is included in the error you posted. Anyhow, what I recommend you do is pass in `enemy_bullet_list` as an instance argument to your `Enemy` classes. Then you can use it in your `update` function as needed. – Christian Dean Jun 10 '17 at 04:43
  • @18TristonLehmann Okay, but my comment above still applies. Just re-factor your code like I outlined above. – Christian Dean Jun 10 '17 at 04:49
  • Alright, perfect I got my code working by getting rid of the arguments in the update functions below. Thx, Christian. – 18 Triston Lehmann Jun 10 '17 at 04:50
  • @18TristonLehmann Glad I could help. – Christian Dean Jun 10 '17 at 04:51

1 Answers1

1

Usually that error is because you are calling a method that just takes self with an additional argument. In this case it looks like you are calling .update(bullet_list) with something that is defined as def update(self) and the implicit self in your method call is the first of the 2 positional args.

Quickly searching through your code and looking at the error you set

enemy_group = pygame.sprite.Group()

and then call enemy_group.add(explosion) which isn't an Enemy and i assume doesn't have a custom .update defined on it.

in the future i'd recommend looking into pdb.

dreamriver
  • 1,291
  • 2
  • 15
  • 20