如果我的陷阱牌与我的陷阱牌相撞,如何使它只移除我的一名玩家的生命值?如何移除我的所有玩家的生命值?

2024-05-14 02:35:29 发布

您现在位置:Python中文网/ 问答频道 /正文

所以最近有人帮我移除了我的健康,我和我的陷阱牌碰撞了,但是它移除了所有而不是1 Video--正如您在视频中看到的,它删除了所有我的健康状况,而不是仅1。所有我的健康状况都存储在一个列表中1乘1,而不仅仅是1个图像

这就是我所做的,我说过如果我的玩家与我的陷阱牌相撞,它将移除我的1点生命值,并将我向后移动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

这是我的健康类,我在下面的完整代码列表V中存储了每个健康

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)

这是我的陷阱瓷砖课

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]

我的完整代码——在这里渲染太长了 full code


Tags: rectselfgetdefwidthpygamecolor陷阱
1条回答
网友
1楼 · 发布于 2024-05-14 02:35:29

问题是您在不更新playerman.rect的情况下,每帧执行多次测试。实际上,碰撞是针对每个平台计算的。查看您的代码:

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

在循环for platform in platforms:不在循环中后执行测试,如果检测到冲突,则中断循环。此外,只需删除列表中的最后一个心脏,而不是索引为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

相关问题 更多 >