繁体   English   中英

Pygame-我该如何消除一条生命? 每次碰撞都会移除多个

[英]Pygame - how do I remove only one life? more than one is removed with each collision

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

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()

您的代码应该已经可以工作了:玩家的生命取决于enemy.destroyed的状态。 当玩家与敌人碰撞并enemy.destroyedenemy.destroyed成为事实。 此后, enemy实例将不会影响玩家的生命(假设这是敌人击落玩家的唯一方法)。 注意:您重新分配enemy.destroyed两次True ,这是unnecesary。

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的真正目的是enemy.destroyed )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM