Как переместить панель здоровья игрока в игре с прокруткой карты? - PullRequest
0 голосов
/ 05 февраля 2019

У меня были проблемы с моим кодом на 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 голосов
/ 07 февраля 2019

Я понял это, создав спрайт индикатора здоровья, комбинированный спрайт для комбинации спрайта игрока и спрайта здоровья, а также добавив функции pygame.rect, такие как Clamp_ip и contains.Я также добавляю изображение индикатора здоровья на изображение игрока.

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))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...