2015-07-12 71 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 ...我今晚會給這個嘗試它接縫更容易我圍繞這種方法。我也可能應該創建一個最小的,完整的和可驗證的例子。 –