我正在制作游戏,但我在为这个游戏制作的 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。
每当我给我的损失方法一个值时,它会不断迭代从起始位置丢失的相同数量的块;这让我很难在前一个之后调用第二个甚至第三个损失方法时扣除扣除的值,无论如何我可以阻止这个吗?另一方面,这些块可以随着时间的推移对损失/增益进行动画处理,所有这些都显示在我的 hp bar 图像上;我尝试在代码的不同区域使用 break 语句,但它们不会生成预期的输出,而是限制了整体块值丢失
如果你能帮助我,那将不胜感激。如果我没有正确解释这一点,谢谢和道歉。