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?
Sword.killdo? It appears to be trying to callself.set_colorkeyrather thanself.image.set_colorkey.