2013-10-08 60 views
-1

我在製作遊戲的過程,但我有說我正在爲這個遊戲我的HP酒吧類的麻煩,使用下面的代碼:停止一個循環,我的HP條

import pygame, sys 

class hpbar(): 
    def __init__(self, hpchunk, screen, posx, posy): 
     # hpchunk can either be 125 or 250 
     self.hpchunk = hpchunk 
     self.screen = screen 
     self.posx = posx 
     self.posy = posy 
     self.unit_h = 18 
     self.unit_w = 250 
     self.image = pygame.image.load('hpbar.png') 
     self.total_hp = [self.posx + 3, self.posy + 3, self.unit_w, self.unit_h] # +3 is there due to the thickness of the actual HP bar 
     self.val_per_chunk = self.unit_w/self.hpchunk      # units of a single chunk e.g. 250/125 = 2 
     self.startPos = 253 
     screen.blit(self.image, [self.posx, self.posy]) 
     pygame.draw.rect(screen, [0, 255, 0], self.total_hp, 0) 

    def loss(self, loss_val): 
     self.loss_val = loss_val 
     total_chunk = self.loss_val * self.val_per_chunk      
     chunkPosx = self.posx + self.startPos        # e.g. if hpchunk = 125, then the hp will be in chunks of two 
     healthbar = [0, 255, 0] 
     chunkRangeEnd = self.unit_w - total_chunk       
     total_chunk = 0              # first iterative value 
     stop_val = chunkPosx - total_chunk 
     for lossx in range(self.unit_w, chunkRangeEnd, -self.val_per_chunk): 
      pygame.draw.rect(self.screen, healthbar, self.total_hp, 0)  # hp bar 
      chunkPosx = chunkPosx - self.val_per_chunk      # x position of chunk 
      total_chunk = total_chunk + self.val_per_chunk     # total size of chunk 
      if chunkPosx <= self.posx + 141:        # yellow zone 
       healthbar = [255, 255, 0] 
      if chunkPosx <= self.posx + 48:         # red zone 
       if self.val_per_chunk == 25: 
        healthbar = [255, 255, 255] 
       else: 
        healthbar = [255, 0, 0] 
      pygame.draw.rect(self.screen, [255, 0, 255], [chunkPosx, self.posy + 3, total_chunk, self.unit_h], 0) 
      pygame.time.delay(200) 
      pygame.display.flip() 

    # chunkPosx = 253 + 150 = 403 
    # total_chunk = 5 * 2 = 10 
    # chunkRangeEnd = 250 - 20 = 230 

    # chunkPosx iteration 
    # x = x - 2 
    # 403 = 403 - 2 
    # 401 = 403 - 2  1st 
    # 399 = 401 - 2  2nd 
    # 397 = 399 - 2  3rd 
    # 395 = 397 - 2  4th 
    # 393 = 395 - 2  5th 

    # total_chunk iteration 
    # x = x + 2 
    # 0 = 0 + 2 
    # 2 = 0 + 2  1st 
    # 4 = 2 + 2  2nd 
    # 6 = 4 + 2  3rd 
    # 8 = 6 + 2  4th 
    #10 = 8 + 2  5th 

pygame.init() 
screen = pygame.display.set_mode([720, 480]) 
screen.fill((255, 255, 255)) 
pygame.display.flip() 

while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      sys.exit() 

    # test, using hp bar instance 
    newbar = hpbar(125, screen, 150, 150) 
    newbar.loss(5) 
    pygame.display.flip() 

我這樣寫了我的模塊,以便在遊戲的不同區域重新使用它。另一件需要注意的事情是,圖像和一系列直方圖用於顯示hp。

每當我給我的損失方法一個值,它不斷迭代相同數量的塊從初始位置丟失;這使我難以在前一次調用第二種甚至第三種損失方法時扣除扣除的值,是否有反正我可以阻止它? 另一方面,大塊在那裏動畫的損失/收益隨着時間的推移,所有顯示在我的惠普欄圖像;我嘗試在代碼的不同區域使用break語句,但它們不會生成預期的輸出,而是會限制整個塊的丟失值。如果您可以幫助我,我將非常感激。 謝謝和apoligies,如果我沒有正確解釋這一點。

+0

作爲您當前方法的替代方法,我會建議您根據實體的當前和最大健康狀況之間的比例來設置大小。你可以使用比例來做到這一點。您當前使用欄的「塊」實現看起來過於複雜。 – kevintodisco

回答

0

我不清楚什麼是不工作。

我想你正在繪製一個HP條,它顯示出當前位置x%。隨着時間的推移,塊是否會造成損失?如果是這樣,在那裏暫停,將阻止你的程序的其餘部分能夠更新,直到完成。還是在酒吧裏顯示大塊?你可以做到這一點

一種方法是這樣的:

class HpBar(): 
    def __init__(self, max_hp=56, cur_hp=20): 
     self.max_hp = 56 
     self.cur_hp = 20 
     self.animating = False 
     self.animate_delay = 200 

    def damage(self, dmg): 
     # damage and start animation 
     self.cur_hp -= dmg 
     self.started_ticks = pygame.time.get_ticks()   
     self.animating = True 

    @property 
    def ratio(self): 
     # get ratio in the range [0., 1.] from original range [0, max_hp] 
     return self.cur_hp * (1./self.max_hp) 

    def draw(self): 
     # draw background max and current 

     # start the same 
     hp_box_max = Rect(...) 
     hp_box_cur = Rect(hp_box_max) 
     hp_box_animate = Rect(hp_box_max) 
     hp_box_cur.width = self.ratio * hp_box_max.width 

     # always show hp cur/max 
     draw(hp_box_max) 
     draw(hp_box_cur) 

     # animation over? 
     now = pygame.time.get_ticks() 
     elapsed = now - self.started_ticks 
     if now - elapsed > self.animate_duration: 
      self.animating = False 

     # only show extra effect if animating 
     if self.animating: 
      hp_box_animate.left = hp_box_cur.right 
      hp_box_animate.right = hp_box_cur.right 

      # now animate based on time elapsed 
      chunks = elapsed/self.animate_delay 

      # if elapsed is 200, chunks = 1 
      # elapsed 400, chunks = 2 , etc... 
      hp_box_animate.width = chunks * chunk_width  
      draw(hp_box_animate) 

draw()被重新計算3個rects,(或2如果動畫結束)。如果你想,cur_hp可以升級到@property,它將自動渲染和緩存更新的曲面。我使用類似的方法來緩存文本的渲染,並且只在數據導致髒布爾被設置時才重新渲染。