【问题标题】:Pygame - how do I remove only one life? more than one is removed with each collisionPygame - 我如何只删除一个生命?每次碰撞都会删除多个
【发布时间】:2018-04-13 11:57:42
【问题描述】:

并非所有代码 - 只是播放器类,如果需要更多详细信息,请告诉我

class Player(Entity):
    def __init__(self, x, y):
        Entity.__init__(self)
        self.xvel = 0
        self.yvel = 0
        self.onGround = False
        self.image = Surface((32,32))
        self.lives = 3
        self.image = stand
        self.rect = Rect(x, y, 64, 64)

    def update(self, up, down, left, right, running, platforms, enemygroup, 
attack):
        if attack:
            self.image = attack
        if up: #in air

            if self.onGround: self.yvel -= 10 #if player hits the ground 
 then move up
            if self.image == walk1:
                self.image = jump1
            if self.image == walk_flip:
                self.image = jump_flip

        if down:
            pass #do nothing
        if running:
            self.xvel = 12
        if left:
            self.xvel = -8 #differennt movement velocities
            self.image = walk_flip
        if right:
            self.xvel = 8
            self.image = walk1

        if not self.onGround:
            self.yvel += 0.3
            if self.yvel > 100: self.yvel = 100 #can't fall faster than 100 
pixels
        if not(left or right):
            self.xvel = 0 #no movement
        self.rect.left += self.xvel
        self.collide(self.xvel, 0, platforms, enemygroup)
        self.rect.top += self.yvel
        self.onGround = False;
        self.collide(0, self.yvel, platforms, enemygroup)

        #self.updatecharacter(walk1)
    def collide(self, xvel, yvel, platforms, enemygroup):
        for p in platforms:
            if pygame.sprite.collide_rect(self, p): #compares rect value of 
player and platform
                if isinstance(p, ExitBlock):
                    pygame.event.post(pygame.event.Event(QUIT))
                if xvel > 0:
                    self.rect.right = p.rect.left #right of the playr 
collides left of platform
                    print ("collide right")
            if xvel < 0:
                self.rect.left = p.rect.right #left of player collide right of platform
                print ("collide left")
            if yvel > 0:
                self.rect.bottom = p.rect.top
                self.onGround = True
                self.yvel = 0
            if yvel < 0:
                self.rect.top = p.rect.bottom

这是我的代码的碰撞部分,用于玩家和与敌人的碰撞。这个想法是从 3 到 0 开始删除生命。当达到 0 生命时,将再次播放介绍作为重新启动。但是,一旦玩家与敌​​人发生碰撞,所有生命都会立即丧生。

    for i in enemygroup:
        if pygame.sprite.collide_rect(self, i):
            boom = self.rect.bottom - i.rect.top
            if enemy.destroyed == False:
                enemy.destroyed = True
                self.lives = self.lives - 1
                enemy.destroyed = True


            if boom <= 8 and enemy.destroyed == False:
                self.yvel = - 8
                enemy.destroyed = False
            if self.lives == 0:
                game_intro()

【问题讨论】:

  • 有很多代码需要通过,但我猜它每次碰撞只会减去一个生命。问题更可能是连续触发了许多碰撞。也许你需要做到这一点,所以每秒只触发一次碰撞。
  • 知道如何每秒触发一次吗?
  • 我不知道 pygame,但你可以将碰撞时的布尔变量设置为 true,如果它为 false,则仅触发另一个碰撞,并在一秒钟后使用计时器将其重置为 true。跨度>

标签: python pygame collision


【解决方案1】:

您的代码应该已经可以工作了:玩家的生命取决于enemy.destroyed 的状态。在您的玩家与敌人相撞并失去生命后,enemy.destroyed 变为真。之后,enemy 实例将不会影响玩家的生命(假设这是敌人击倒玩家的唯一方法)。注意:您将enemy.destroyed 重新分配两次给True,这是不必要的。

enemy.destroyed = False
conditions = True  # conditions can include collision with enemy
def statements():
    pass # just your statements that you want to run

while True:
    if not enemy.destroyed and conditions: # no need for == True/False
        enemy.destroyed = True
        print("Enemy destroyed!")
        statements()

在这里,我模拟了您的情况。 statements() 函数将只运行一次,因为条件只为真一次。如果其中一些没有意义或不完全有效,请告诉我,可能包括更多代码(enemy 是什么,enemy.destroyed 的真正目的是什么)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2019-10-30
    • 1970-01-01
    相关资源
    最近更新 更多