【问题标题】:Pygame make sprite invisible/flashing while invinciblePygame使精灵在无敌时不可见/闪烁
【发布时间】:2022-12-11 07:08:32
【问题描述】:

我已经实现了子弹与坦克碰撞时的无敌/免疫部分,现在我希望精灵在免疫时“闪烁”/不透明/不可见。这是我想在坦克图像中实现更改的相关代码和 TODO。

class Tank(GamePhysicsObject):
    # Handle respawn
    self.time_since_death       = pygame.time.get_ticks()
    self.immune                 = False
    self.immune_time            = 3000


def collision_bullet_tank(arb, space, data):
    # Set immunity, save time since death, start flashing
    tank.parent.immune = True
    tank.parent.time_since_death = pygame.time.get_ticks()
    tank.parent.start_flashing()


def post_update(self):

    # If tank has been immune for some time, make tank vulnerable again
    current_time = pygame.time.get_ticks()
    if current_time - self.time_since_death > self.immune_time:
        self.immune = False
        # TODO: Stop flashing

基本上,我的问题是如何改变视觉精灵?

编辑:更具体地说,我在这里输入什么?

def start_flashing(self):
    """ Call this function to make the tank flash. """
    #TODO: White layer opacity 50% ?
    1

【问题讨论】:

  • 您可以使用变量进行闪烁,例如 isFlashActive,然后使用纯白色图层相应地设置它们的性质,isFlashActive 的不透明度为 100% 为真,0% 为假值。
  • isFlashActive 等同于 Tank 类的 immune。我的问题是在这种情况下如何在水箱上方创建一个白色层?

标签: python pygame


【解决方案1】:

最简单的解决方案是只要设置了self.immune,就只在每第二帧绘制对象Tank

class Tank(GamePhysicsObject):
    def __init__(self):
        self.immune = False
        self.frame_count = 0

    def draw(self)
        draw_tank = True
        if self.immune:
            self.frame_count += 1
            draw_tank = self.frameCount % 2 == 0
        if draw_tank:
            # draw the tank
        

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多