简体   繁体   中英

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

Not all the code - just the player class, if more detail is needed let me know

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

This is the collision section of my code for the player and collision with enemys. The idea is to remove lifes starting at 3 to 0. when 0 lives are reached the intro is played again as a restart. However, all the lives are lost at once as soon as the player collides with the enemy.

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

Your code should already work: the lives to your player depend on the state of enemy.destroyed . After your player collides with enemy and loses a life, the enemy.destroyed becomes true. After that, the enemy instance will have no effect on the player lives (assuming that was the only way for the enemy to take down a player). Note: you reassign enemy.destroyed twice to True , which is 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()

Here I have kind of modeled your situation. The statements() function will be run only once, because the conditions are only ever true once. If some of this doesn't make sense or doesn't exactly work, tell me, maybe including more code (what is enemy and what is the true purpose of enemy.destroyed ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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