敌人的健康栏不会耗尽 pygame [英] Enemy health bar ain't draining pygame

查看:60
本文介绍了敌人的健康栏不会耗尽 pygame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我试图为我的敌人类制作一个健康栏,但只有一部分有效,我的意思是每次我击中我的敌人时,我希望它的健康栏从绿色变为红色,然后,麦芽酒失去了 50% 的生命值,但这并没有真正起作用,它需要 2 次命中才能杀死我的敌人的部分正在起作用,但是生命值消耗和从绿色变为红色的部分不起作用.我的问题出在第二个主循环中.

显然,如果您的目标具有 RPG 风格的生命值",则百分比始终可以传递为:

health_percentage = round( current_hp/maximum_hp * 100 )

参考代码:

导入pygame# 窗口大小WINDOW_WIDTH = 400WINDOW_HEIGHT = 400蓝色 = ( 3, 5, 54 )红色 = ( 200, 0, 0 )绿色 = ( 0, 200, 0 )def drawHealthBar( window, position, health_percent ):"在给定位置绘制一个健康条,显示健康水平为绿色部分,红色部分为100%"BAR_WIDTH = 64BAR_HEIGHT = 8# 从完全健康开始bar = pygame.Surface( ( BAR_WIDTH, BAR_HEIGHT ) )bar.fill( 绿色 )# 计算出相对柱尺寸如果( health_percent <100 ):health_lost = BAR_WIDTH - 回合(BAR_WIDTH * health_percent/100)pygame.draw.rect( bar, RED, ( BAR_WIDTH - health_lost, 0, BAR_WIDTH, BAR_HEIGHT ) )# 绘制结果条window.blit( bar, position )### 初始化pygame.init()pygame.mixer.init()window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )pygame.display.set_caption(健康栏")font = pygame.font.Font(无,16)### 主循环时钟 = pygame.time.Clock()完成 = 错误虽然没有完成:# 处理用户输入对于 pygame.event.get() 中的事件:如果( event.type == pygame.QUIT ):完成 = 真elif ( event.type == pygame.MOUSEBUTTONUP ):# 鼠标点击经过elif ( event.type == pygame.KEYUP ):# 在 Key-released 上经过# 移动键#keys = pygame.key.get_pressed()#if (keys[pygame.K_UP]):# 打印(向上")# 更新窗口window.fill( 蓝色 )x = WINDOW_WIDTH//2y = WINDOW_HEIGHT//7# 从 100 - 0 % 绘制健康条对于范围内的 health_remaining ( 100, -10, -10 ):# 健康作为一个数字text = font.render( str( health_remaining ) + '%' , True, (200, 200, 200) )window.blit( 文本, ( x-50, y) )# 酒吧drawHealthBar( window, ( x, y ), health_remaining )y += 25pygame.display.flip()# 钳制 FPS时钟滴答(3)pygame.quit()

Okay so I was trying to make a health bar for my enemy class and only a part of it is working, what I mean is that every time I hit my enemy, I want its health bar to turn green to red and ,ale is lose 50 percent of it health, but that's not really working, the part of it needing 2 hits to kill my enemy is working but the part of the health draining and turning from green to red is not working. My problem is in the second main loop.

https://gyazo.com/9ab3f871afb9d3bcdd0fea0a0eadec87

when I shot at the player, the heath did not go down at all.

this is were I drew my color red and green.

pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 20, 80, 10))
pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 20, 80 - (5 * (10 - self.health)), 10))
self.hitbox = (self.x + 17, self.y + 2, 31, 57)

this is were I told it to take away health

if Enemy.health > -10:
    Enemy.health -= 5
else:
    del enemys[one]

my full code

解决方案

I can't see anything immediately wrong with the code sections. I suspect it's not working because of the "health units" being used.

It may help to change you code to express these units as a percentage. That way no matter what kind of enemy it is, 0 health means destroyed, and removes potentially confusing comparisons, e.g.: -8 health = "somehow OK".

I've created a simple function to draw a red/green health bar. I reasoned that for some larger time the health is at 100%, so only draw the negative-health part when necessary.

def drawHealthBar( window, position, health_percent ):
    """ Draw a health bar at the given position, showing the
        health level as a green part and a red part of 100% """
    BAR_WIDTH = 64
    BAR_HEIGHT= 8

    # Start with full health
    bar = pygame.Surface( ( BAR_WIDTH, BAR_HEIGHT ) )
    bar.fill( GREEN )

    # Work out the relative bar size
    if ( health_percent < 100 ):
        health_lost = BAR_WIDTH - round( BAR_WIDTH * health_percent / 100 )
        pygame.draw.rect( bar, RED, ( BAR_WIDTH - health_lost, 0, BAR_WIDTH, BAR_HEIGHT ) )

    # Draw the resultant bar
    window.blit( bar, position )

Obviously if your targets have RPG-style "hit points", the percentage can always be passed as:

health_percentage = round( current_hp / maximum_hp * 100 )

Reference Code:

import pygame

# Window size
WINDOW_WIDTH    = 400
WINDOW_HEIGHT   = 400

BLUE  = (   3,   5,  54 )
RED   = ( 200,   0,   0 )
GREEN = ( 0,   200,   0 )


def drawHealthBar( window, position, health_percent ):
    """ Draw a health bar at the given position, showing the
        health level as a green part and a red part of 100% """
    BAR_WIDTH = 64
    BAR_HEIGHT= 8

    # Start with full health
    bar = pygame.Surface( ( BAR_WIDTH, BAR_HEIGHT ) )
    bar.fill( GREEN )

    # Work out the relative bar size
    if ( health_percent < 100 ):
        health_lost = BAR_WIDTH - round( BAR_WIDTH * health_percent / 100 )
        pygame.draw.rect( bar, RED, ( BAR_WIDTH - health_lost, 0, BAR_WIDTH, BAR_HEIGHT ) )

    # Draw the resultant bar
    window.blit( bar, position )




### initialisation
pygame.init()
pygame.mixer.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
pygame.display.set_caption("Health Bar")
font = pygame.font.Font( None, 16 )

### Main Loop
clock = pygame.time.Clock()
done = False
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True
        elif ( event.type == pygame.MOUSEBUTTONUP ):
            # On mouse-click
            pass
        elif ( event.type == pygame.KEYUP ):
            # on Key-released
            pass

    # Movement keys
    #keys = pygame.key.get_pressed()
    #if ( keys[pygame.K_UP] ):
    #    print("up")


    # Update the window
    window.fill( BLUE )

    x = WINDOW_WIDTH  // 2
    y = WINDOW_HEIGHT // 7 

    # Draw health bars from 100 - 0 %
    for health_remaining in range( 100, -10, -10 ):
        # The health as a number
        text = font.render( str( health_remaining ) + '%' , True, (200, 200, 200) )
        window.blit( text, ( x-50, y) )
        # The bar
        drawHealthBar( window, ( x, y ), health_remaining )
        y += 25

    pygame.display.flip()

    # Clamp FPS
    clock.tick( 3 )


pygame.quit()

这篇关于敌人的健康栏不会耗尽 pygame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆