1

I am trying to make a sword appear when I press the space bar and disappear when I hit key 5.

if event.type == pg.KEYUP:
                if event.key == pg.K_ESCAPE:
                    self.quit()
                if event.key == pg.K_SPACE:
                    self.sword = Sword(self, self.player.rect.centerx-7, self.player.rect.bottom, self.player)
                if event.key == pg.K_5:
                    self.sword.kill()

I can make the first sword appear and disappear without any issue, but when I try to press the spacebar again, I get this error message:

File "/Users/(User)/Desktop/ZeldaGame/sprites.py", line 183, in __init__
    self.image.set_colorkey(WHITE)
AttributeError: 'Sword' object has no attribute 'set_colorkey'

Here is my Sword class:

class Sword(pg.sprite.Sprite):
    def __init__(self, game, x, y, entity):
        self.groups = game.all_sprites
        pg.sprite.Sprite.__init__(self, self.groups)
        self.game = game
        self.image = self.game.sword
        self.image.set_colorkey(WHITE)
        self.rect = self.image.get_rect()
        self.x = x
        self.y = y
        self.rect.x = x 
        self.rect.y = y 
        if entity.direction == 'down':
            self.image = pg.transform.rotate(self.image, -90)

        def update(self):
            kill()

Could anyone help me make the sword able to appear, disappear, and appear over and over again?

2
  • What exactly does Sword.kill do? It appears to be trying to call self.set_colorkey rather than self.image.set_colorkey. Commented Mar 6, 2020 at 15:41
  • 1
    @chepner sword.kill() is a builtin function of pg.sprite.Sprite, it removes the sprite from the sprite group game.all_sprites. I am blitting all the sprites in game.all_sprites, so when I remove it with sword.kill(), it stops being blit onto the screen. Commented Mar 6, 2020 at 15:42

1 Answer 1

3

Warning: I don't know anything about PyGame.

It seems as if game.sword initially, before the very first sword is created, is a pygame.Surface object - This is just a guess, because you didn't show that part of the code. (set_colorkey seems to be a pygame.Surface method, and by virtue of the fact that you can invoke Sword.__init__ the first time without any errors tells me that game.sword must initially be a pygame.Surface object, otherwise self.image.set_colorkey(WHITE) would raise an error).

Then, the second time you press the spacebar, game.sword will be refering to a Sword object, since you executed self.sword = Sword(... the first time you pressed the spacebar. You enter the second sword's __init__, but now game.sword refers to a Sword, not a pygame.Surface. Swords don't have a set_colorkey method, hence the error (I'm guessing pygame.Sprite does not inherit from pygame.Surface).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.