1

so recently someone helped me with removing my health of I collid with my trap Tile But its Removing all instead of just 1 Video --- as you can see in the video it removes all my health instead of just 1 all my healths are stored in a list 1 by 1 its not just 1 image

here is what I did I said if my player collides with my trap tile it should remove 1 of my health and move me back 50

            for dude in range(len(dicing)-1,-1,-1):
                if playerman.rect.colliderect(dicing[dude].rect):
                    if dude < len(healths): #Check
                        del healths[dude]
                        playerman.x = 50

this is my health class I stored each of the healths in a list V below on my full code

class health:
   def __init__(self,x,y,height,width,color):
       self.x = x
       self.y = y
       self.height = height
       self.width = width
       self.color = color
       self.heat = pygame.image.load("health.png")
       self.heat = pygame.transform.scale(self.heat,(self.heat.get_width()//2,self.heat.get_height()//2))
       self.rect = pygame.Rect(x,y,height,width)
   def draw(self):
       self.rect.topleft = (self.x,self.y)
       window.blit(self.heat,self.rect)

and this is my trap tile class

class dice:
   def __init__(self,x,y,height,width,color):
       self.x = x
       self.y = y
       self.height = height
       self.width = width
       self.color = color
       self.ice = pygame.image.load("ice1.png")
       self.ice = pygame.transform.scale(self.ice,(self.ice.get_width()-44,self.ice.get_height()-44))
       self.rect = pygame.Rect(x,y,height,width)
   def draw(self):
       self.rect.topleft = (self.x,self.y)
       player_rect = ice.get_rect(center = self.rect.center) 
       player_rect.centerx -= -900 # 10 is just an example
       player_rect.centery -= 0 # 15 is just an example
       window.blit(self.ice,self.rect)

black = (0,0,0)
ice2 = dice(250,390,10,10,black)
dicing = [ice2]

my full code -- its to long to render here full code

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Habib Ismail
  • 69
  • 5
  • 16

1 Answers1

1

The issue is that you do the test multiple time per frame, without updating playerman.rect. Actually the collision is evaluated for each platform. See your code:

for platform in platforms:
   if playerman.rect.colliderect(platform.rect):
       collide = True
       isJump = False
       playerman.y = platform.rect.top - playerman.height + 1
       if playerman.rect.right > platform.rect.left and playerman.rect.left < platform.rect.left - playerman.width:
           playerman.x = platform.rect.left - playerman.width
       if playerman.rect.left < platform.rect.right and playerman.rect.right > platform.rect.right + playerman.width:
           playerman.x = platform.rect.right

   # draw the coin class
   for one in range(len(Coins_list)-1,-1,-1):
       if playerman.rect.colliderect(Coins_list[one].rect):
           del Coins_list[one]
           score += 1
           text = font.render("Hearts Collected  " + str(score), True, (255,255,255))
           textRect.center = (390,160)


   for dude in range(len(dicing)-1,-1,-1):
       if playerman.rect.colliderect(dicing[dude].rect):
           if dude < len(healths): #Check
               del healths[dude]
               playerman.x = 50

Do the test after the loop for platform in platforms: not in the loop and break the loop if a collision is detected. Further more just remove the last heart in the list rather than the heart with index dude:

for platform in platforms:
    if playerman.rect.colliderect(platform.rect):
        collide = True
        isJump = False
        playerman.y = platform.rect.top - playerman.height + 1
        if playerman.rect.right > platform.rect.left and playerman.rect.left < platform.rect.left - playerman.width:
            playerman.x = platform.rect.left - playerman.width
        if playerman.rect.left < platform.rect.right and playerman.rect.right > platform.rect.right + playerman.width:
            playerman.x = platform.rect.right


#<--| INDENTATION
# draw the coin class
for one in range(len(Coins_list)-1,-1,-1):
    if playerman.rect.colliderect(Coins_list[one].rect):
        del Coins_list[one]
        score += 1
        text = font.render("Hearts Collected  " + str(score), True, (255,255,255))
        textRect.center = (390,160)

#<--| INDENTATION
for dude in range(len(dicing)-1,-1,-1):
    if playerman.rect.colliderect(dicing[dude].rect):
        if len(healths) > 0: #Check
            del healths[-1]
            playerman.x = 50
        break
Rabbid76
  • 202,892
  • 27
  • 131
  • 174