python - 如何在滚动地图游戏中移动玩家健康条?
问题描述
我在 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)
解决方案
我通过创建一个健康条精灵,一个组合了玩家精灵和健康条精灵的组合精灵,并添加了像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))
推荐阅读
- java - 429 gitlab-api 上的请求太多
- javascript - 是否可以在实例化时检测给定类型的所有可调用方法,然后为每个方法动态创建转发过程
- r - 在 R 中安装包会出现以下错误
- visual-studio-code - 如何避免 VSCode 格式化部分代码
- http - 发布请求后如何删除帐户
- android - Firestore:存储架构更多领域或文档?在 Android 传感器应用程序中
- openlayers - OpenLayers 6.22 上的矢量平铺 (tegola) / WMS 叠加问题
- roblox - 钥匙卡门问题,它不想关
- google-apps-marketplace - 允许管理员使用 GSuite 应用阅读用户邮件
- gremlin - Gremlin 查询所有出边与给定列表列表相关的节点