停止游戏中的循环扣血显示

停止游戏中循环扣血并显示的具体实现方式会依赖于你的代码结构和游戏的逻辑。通常情况下,你可以通过以下方式来实现停止循环扣血和显示:

在这里插入图片描述

1、问题背景

在使用 Python 代码为游戏开发一个生命值条时,遇到了一个问题。代码使用了循环来减少生命值,但当扣除生命值后再次调用扣血方法时,生命值会继续从初始状态开始减少,而不是从当前生命值开始扣除。这使得生命值条无法正确反映当前的生命值。

2、解决方案

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)
        self.current_hp = self.unit_w

    def loss(self, loss_val):
        self.loss_val = loss_val
        total_chunk = 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.current_hp - total_chunk                           
        total_chunk = 0                                                     # first iterative value
        stop_val = chunkPosx - total_chunk
        self.current_hp -= total_chunk

        for lossx in range(self.current_hp, 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()


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()

    # test, using hp bar instance
    newbar.loss(3)
    pygame.display.flip()

修改了 loss 方法,并且引入了 current_hp 属性,当调用 loss 方法时,首先计算出要扣除的生命值数量,然后从当前生命值中减去此数量,接着计算新的生命值范围,并使用循环绘制生命值条。这样,即使多次调用 loss 方法,生命值也会从当前值开始减少,并且可以正确反映当前的生命值。

无论我们最终选择哪种方法,确保在游戏逻辑中合理地处理扣血和显示,以及适时地结束循环,这样可以保证游戏的流程和用户体验。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值