2015-07-12 66 views
0

问题我正在查找组合玩家位置并添加到其中的方法我想弄清楚这一点,以帮助向正在学习pygame的孩子解释,并且继续提出超出我知识范围的问题。如何使视觉健康仪使用Pygame跟随玩家?

healthhealthbarplayerBetter example

我的目标:是有一个视觉生命表是以下或贴在播放器/敌人为了跟踪碰撞检测的更它上面漂浮视觉方式。

我陷入对如何给玩家IMG的PlayerPos与精灵米

我方面的问题是了解的事结合起来的理念为healthbarpos粘上它目前的播放器位置

playerpos +玩家x位置,玩家y位置+ 10个像素

#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 
+0

2评论,你有一些不完整的代码'healthbarpos = [',如果你可以给这两个图像的链接,人们可以使用它们来尝试你的设置。 – jonnybazookatone

+0

耶健康吧pos是我不知道该放哪里的部分。这是我一直坚持的确切部分。我添加了一些图片,我没有我的孩子图片,但我做了类似的快速添加。我实际上更了解这个问题。 –

+0

你是否尝试过你实际说过的话,并遇到一些问题?乍一看,它看起来像你有正确的想法。每次播放器在重新绘制前移动时,您只需要将相同的偏移量添加到健康栏精灵,这就是您所说的。 – jonnybazookatone

回答

0

我的建议是不要使用单独的坐标健康酒吧和球员。有两组坐标是不必要的复杂。

当您绘制播放器和健康栏时,使用从播放器派生的坐标并应用偏移量。

screen.blit(healthbar, (playerPos[0], playerPos[1] - heightOfHealthBar)) 

或类似的绘图功能。

+0

伟大的建议关于没有重复套coords,我错过了关于保持简单的黄金法则stu ...我今晚会给这个尝试它接缝更容易我围绕这种方法。我也可能应该创建一个最小的,完整的和可验证的例子。 –