首页 > 解决方案 > 如何在滚动地图游戏中移动玩家健康条?

问题描述

我在 python 中的代码一直有问题,我正在使用模块 pygame、random 和 math。我似乎无法在每个玩家的矩形 x 和 y 位置设置两个玩家的玩家健康栏,这意味着我需要玩家矩形左上角的每个玩家的健康栏。玩家的健康条会不断移动,有时会出现在地图上的随机位置。这些是代表健康栏的三个代码部分。

我在网上搜索了帮助,但似乎找不到任何东西。另外,我尝试移动健康栏的矩形,并使其成为一个精灵。

class Camera(pygame.sprite.Sprite):
  def __init__(self, display_height, display_width):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.Surface((display_width, display_height))
    self.camera = pygame.Rect(0, 0, display_width, display_height)
    self.rect = pygame.Rect(0, 0, display_width, display_height)
    self.rect.x = x
    self.rect.y = y
    self.width = display_width
    self.height = display_height

 def apply(self, entity):
    return entity.rect.move(self.camera.topleft)
    return entity.rect.move(health_bars(player_health,player2_health))



 def update(self):
    x = (-player.rect.x + -player2.rect.x)/2 + int(display_width/2)
    y = (-player.rect.y + -player2.rect.y)/2 + int(display_height/2)
    self.camera = pygame.Rect(x, y, self.width, self.height)





def health_bars(player_health, player2_health):

   if player_health > 75:
     player_health_color = green
   elif player_health > 50:
     player_health_color = yellow
   else:
     player_health_color = red
   if player2_health > 75:
     player2_health_color = green
   elif player2_health > 50:
     player2_health_color = yellow
   else:
     player2_health_color = red
   pygame.draw.rect(gameDisplay, player2_health_color, (player2.rect.x, player2.rect.y, player2_health, 25))
   pygame.draw.rect(gameDisplay, player_health_color, (player.rect.x, player.rect.y, player_health, 25))





health_bars(player_health, player2_health)

标签: pythonpygame

解决方案


我通过创建一个健康条精灵,一个组合了玩家精灵和健康条精灵的组合精灵,并添加了像clamp_ip和contains这样的pygame.rect函数来解决这个问题。我还将健康条图像粘贴到播放器图像上。

bullets1 = pygame.sprite.Group()





class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.original_image = personImg
        self.image = self.original_image.copy()
        self.rect = self.image.get_rect(center=(person_width/2, person_height/2))
        self.angle = 0
        self.rect.x = x
        self.rect.y = y
        self.player_health = 100
        self.x_change = 0
        self.y_change = 0




    def update(self):
        self.x_change = 0
        self.y_change = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_w] and pygame.key.get_mods() and pygame.KMOD_LSHIFT and not pygame.sprite.collide_rect(self, player2): #and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width / 2, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width / 4, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width - 10, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/2, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/4, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width - 10, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height - 10) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height - 10):
            self.rect.x += math.cos(math.radians(self.angle)) * 10
            self.rect.y += math.sin(math.radians(self.angle)) * -10
        elif keystate[pygame.K_w] and not pygame.sprite.collide_rect(self, player2): #and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/2, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/4, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width - 10, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/2, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/4, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width - 10, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height - 10) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height - 10):
            self.rect.x += math.cos(math.radians(self.angle)) * 5
            self.rect.y += math.sin(math.radians(self.angle)) * -5
        if keystate[pygame.K_s] and not pygame.sprite.collide_rect(self, player2): #and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/2, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/4, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width - 10, player2.rect.y) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/2, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width/4, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width - 10, player2.rect.y + person2_height) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height - 10) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height - 10):
            self.rect.x += math.cos(math.radians(self.angle)) * -5
            self.rect.y += math.sin(math.radians(self.angle)) * 5
        if pygame.sprite.collide_rect(self, player2):
            self.x_change += 7
            self.y_change += 7
        #if keystate[pygame.K_d] and pygame.key.get_mods() and pygame.KMOD_LSHIFT and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height - 10):
            #self.x_change = 10
        #elif keystate[pygame.K_d] and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x, player2.rect.y + person2_height - 10):
            #self.x_change = 5
        #if keystate[pygame.K_a] and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/2) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height/4) and not pygame.Rect.collidepoint(self.rect, player2.rect.x + person2_width, player2.rect.y + person2_height - 10):
            #self.x_change = -5
        self.rect.x += self.x_change
        self.rect.y += self.y_change
        if self.rect.right > map_width:
            self.rect.right = map_width
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.top < 0:
            self.rect.top = 0
        if self.rect.bottom > map_height:
            self.rect.bottom = map_height
        self.rect.contains(healthbar1.rect)
        self.image.blit(healthbar1.image, (self.rect.x, self.rect.y))
        healthbar1.rect.clamp_ip(self.rect)



    def shoot(self):
        bullet = Bullet1(self.rect.x, self.rect.y)
        all_sprites.add(bullet)
        bullets.add(bullet)

    def modifyRotation(self):
        self.angle += 5

    def modifyRotation1(self):
        self.angle += -5

    def getRotation(self):
        return self.angle

    def getRotation1(self):
        return self.angle

class Healthbar1(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        player_health = 100
        for bullet1 in bullets1:
            if pygame.sprite.collide_rect(bullet1, player):
                bullet1.kill()
                player_health -= 5
                break
        if player_health == 0 or player_health <= 0:
            died()
        if player_health > 75:
            player_health_color = green
        elif player_health > 50:
            player_health_color = yellow
        else:
            player_health_color = red
        self.image = pygame.Surface((player_health, 25))
        self.rect = self.image.get_rect()
        self.image.fill(player_health_color)


class Combined(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.rect = player.rect
        self.rect.contains(healthbar1.rect)
        self.image = player.image
        self.image.blit(healthbar1.image, (player.rect.x, player.rect.y))

推荐阅读