如何使用 Pygame 让 Visual Health Meter 跟随玩家?
How to make Visual Health Meter follow the Player using Pygame?
我遇到的问题是找到组合玩家位置的方法并添加到它我正在尝试解决这个问题以帮助向正在学习的孩子解释pygame 并不断提出超出我知识范围的问题。
我的目标: 是让 视觉寿命计 跟随或固定在 Player/Enemy 漂浮在其上方为了以更直观的方式跟踪碰撞检测。
我对如何将播放器 img 的 PlayerPos 与 sprite meter 结合起来的概念感到困惑
我的问题领域是了解如何将 healthbarpos 粘贴到当前玩家位置
playerpos + player x position ,player y position + 10 pixels
#How I keep the player position
playerpos=[50,50]
#How I keep health bar above the player position
healthbarpos=[
#the player image
PLAYER = pygame.image.load('player.png')
#the position of the player [x,y]
playerPos = [0,0]
当我在棋盘上移动玩家时,它会增加玩家的位置
#if a key is pressed
elif event.type == KEYDOWN:
#if the right arrow is pressed
if event.key == K_RIGHT and playerPos[0] < MAPWIDTH - 1:
#change the player's x position
playerPos[0] += 1
if event.key == K_LEFT and playerPos[0] > 0:
#change the player's x position
playerPos[0] -= 1
if event.key == K_UP and playerPos[1] > 0:
#change the player's x position
playerPos[1] -= 1
if event.key == K_DOWN and playerPos[1] < MAPHEIGHT -1:
#change the player's x position
playerPos[1] += 1
我的新手假设是我需要存储玩家当前的 x 位置加上 0,因为它从相同的 x 位置开始,而且玩家当前的 y 位置 + 10 像素
明白
pygame.draw.rect(window, color, (x,y,width,height), thickness)
所以我假设
pygame.draw.rect(window, color, (player x position ,player y position + 10 pixels,width,height), thickness)
我相信理论上理解下面的寿命计部分就足够了
#Initialize the game
pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
#How I keep the player position
playerpos=[50,50]
#How I keep health bar above the player position
healthbarpos=[
playerpos + player x position ,player y position + 10 pixels
-----------------
#full bar 34 pixel minus 3 on each side so 28
healthvalue=28
#loading images for healthbar
healthbar = pygame.image.load("healthbar.png")
health = pygame.image.load("health.png")
#rest inside the while true loop
#Drawing health bar
screen.blit(healthbar, (5,5))
for health1 in range(healthvalue):
screen.blit(health, (health1+8,8))
#update the screen
pygame.display.flip()
#Moving player in the event loop
#if a key is pressed
elif event.type == KEYDOWN:
#if the right arrow is pressed
if event.key == K_RIGHT and playerPos[0] < MAPWIDTH - 1:
#change the player's x position
playerPos[0] += 1
if event.key == K_LEFT and playerPos[0] > 0:
#change the player's x position
playerPos[0] -= 1
if event.key == K_UP and playerPos[1] > 0:
#change the player's x position
playerPos[1] -= 1
if event.key == K_DOWN and playerPos[1] < MAPHEIGHT -1:
#change the player's x position
playerPos[1] += 1
我的建议是不要为生命条和播放器使用单独的坐标。拥有两组坐标是不必要的复杂。
绘制玩家和健康栏时,使用从玩家派生的坐标,并应用偏移量。
screen.blit(healthbar, (playerPos[0], playerPos[1] - heightOfHealthBar))
或绘图函数中的类似内容。
我遇到的问题是找到组合玩家位置的方法并添加到它我正在尝试解决这个问题以帮助向正在学习的孩子解释pygame 并不断提出超出我知识范围的问题。
我的目标: 是让 视觉寿命计 跟随或固定在 Player/Enemy 漂浮在其上方为了以更直观的方式跟踪碰撞检测。
我对如何将播放器 img 的 PlayerPos 与 sprite meter 结合起来的概念感到困惑
我的问题领域是了解如何将 healthbarpos 粘贴到当前玩家位置
playerpos + player x position ,player y position + 10 pixels
#How I keep the player position
playerpos=[50,50]
#How I keep health bar above the player position
healthbarpos=[
#the player image
PLAYER = pygame.image.load('player.png')
#the position of the player [x,y]
playerPos = [0,0]
当我在棋盘上移动玩家时,它会增加玩家的位置
#if a key is pressed
elif event.type == KEYDOWN:
#if the right arrow is pressed
if event.key == K_RIGHT and playerPos[0] < MAPWIDTH - 1:
#change the player's x position
playerPos[0] += 1
if event.key == K_LEFT and playerPos[0] > 0:
#change the player's x position
playerPos[0] -= 1
if event.key == K_UP and playerPos[1] > 0:
#change the player's x position
playerPos[1] -= 1
if event.key == K_DOWN and playerPos[1] < MAPHEIGHT -1:
#change the player's x position
playerPos[1] += 1
我的新手假设是我需要存储玩家当前的 x 位置加上 0,因为它从相同的 x 位置开始,而且玩家当前的 y 位置 + 10 像素
明白
pygame.draw.rect(window, color, (x,y,width,height), thickness)
所以我假设
pygame.draw.rect(window, color, (player x position ,player y position + 10 pixels,width,height), thickness)
我相信理论上理解下面的寿命计部分就足够了
#Initialize the game
pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
#How I keep the player position
playerpos=[50,50]
#How I keep health bar above the player position
healthbarpos=[
playerpos + player x position ,player y position + 10 pixels
-----------------
#full bar 34 pixel minus 3 on each side so 28
healthvalue=28
#loading images for healthbar
healthbar = pygame.image.load("healthbar.png")
health = pygame.image.load("health.png")
#rest inside the while true loop
#Drawing health bar
screen.blit(healthbar, (5,5))
for health1 in range(healthvalue):
screen.blit(health, (health1+8,8))
#update the screen
pygame.display.flip()
#Moving player in the event loop
#if a key is pressed
elif event.type == KEYDOWN:
#if the right arrow is pressed
if event.key == K_RIGHT and playerPos[0] < MAPWIDTH - 1:
#change the player's x position
playerPos[0] += 1
if event.key == K_LEFT and playerPos[0] > 0:
#change the player's x position
playerPos[0] -= 1
if event.key == K_UP and playerPos[1] > 0:
#change the player's x position
playerPos[1] -= 1
if event.key == K_DOWN and playerPos[1] < MAPHEIGHT -1:
#change the player's x position
playerPos[1] += 1
我的建议是不要为生命条和播放器使用单独的坐标。拥有两组坐标是不必要的复杂。
绘制玩家和健康栏时,使用从玩家派生的坐标,并应用偏移量。
screen.blit(healthbar, (playerPos[0], playerPos[1] - heightOfHealthBar))
或绘图函数中的类似内容。