如何在滚动地图游戏中移动玩家健康栏?

时间:2019-02-05 04:27:45

标签: python pygame

我的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)

1 个答案:

答案 0 :(得分:0)

我通过创建一个健康栏精灵,一个用于播放器精灵和健康栏精灵的组合的组合精灵,并添加了诸如clip_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))