我在使用pygame继续我的工作,我正在研究玩家精灵,特别是当它受到伤害的时候。我想要做的是,当玩家受到敌人的伤害时,我希望玩家的精灵闪烁2-3次,让玩家有一两秒的时间离开受到伤害的地方。我有一个生命条(3颗心),我设置它,每次敌人和玩家的精灵发生碰撞时,它都会删除1。我使用kill()函数(我知道这是错误的,因为它完全删除了精灵)。我怎样才能让精灵闪现一两秒钟呢?任何帮助或建议都将不胜感激。谢谢。
enemy_hit_list = pygame.sprite.spritecollide(self, self.level.enemy_list, False)
if enemy_hit_list:
self.health -= 1
self.kill() 发布于 2015-05-15 04:38:48
您必须通过添加一个额外的属性来专门化您的类,该属性将向您显示闪烁状态。"kill“方法从任何组中移除精灵-我不能从上面的代码片段中知道你是否是,以及效果是什么。如果你正在使用一个组来实际绘制玩家(self),那么从there中删除它并不是最好的做法。
例如,你可以在你的精灵上有一个"hit_countdown“属性,并使用它的更新方法来相应地改变它的图像,并测量它恢复正常的时间:
class Player(Sprite):
def __init__(self, ...):
...
self.hit_countdown = 0
...
def update(self, ...):
if self.hit_coundown:
if not hasattr(self, original_image):
self.original_image = self.image
if self.hit_countdown % 2:
self.image = None # (or other suitable pre-loaded image)
else:
self.image = self.original_image
self.hit_countdown = max(0, self.hit_countdown - 1)
super(Player, self).update(...)
# and on your hit code above:
...
if enemy_hit_list:
self.health -= 1
self.hit_countdown = 6 发布于 2017-05-21 11:14:27
def __init__(self):
super(classname,self).__init__()
self.image,self.rect = load_image("character.png")
self.value = 0
self.newColor = [0,0,0,0]
def update(self):
self.cp = self.image.copy()
self.value += 50
self.newColor[0] = self.value%255
self.cp.fill(self.newColor[0:3] + [0,], None, pygame.BLEND_RGBA_ADD)
def render(self,screen):
screen.blit(self.cp,self.rect)我正在使用这个与时钟节拍40和工作很好。update方法不断增加红色的值以获得效果。在使用RGB_ADD命令填充纯色之前,可以使用另一种颜色或此颜色进行填充。
self.cp.fill((0, 0, 0, 255), None, pygame.BLEND_RGBA_MULT)我希望它对你有用
https://stackoverflow.com/questions/30245155
复制相似问题