Я понял это, создав спрайт индикатора здоровья, комбинированный спрайт для комбинации спрайта игрока и спрайта здоровья, а также добавив функции 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))