好吧,所以我试图为我的敌人阶级建立一个健康专栏,而其中只有一部分是有效的,我的意思是每次我击中敌人时,我都希望它的健康专栏从绿色变为红色,然后,麦芽酒会失去50%的生命值,但这并不是真正有效,它需要2次击中才能杀死我的敌人,但部分生命值却在起作用,但从绿色变为红色的生命值却无效。我的问题出在第二个主循环中。
https://gyazo.com/9ab3f871afb9d3bcdd0fea0a0eadec87
当我向玩家射击时,健康完全没有下降。
这是我用红色和绿色绘制的颜色。
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)
这是我被告知要夺走健康
if Enemy.health > -10:
Enemy.health -= 5
else:
del enemys[one]
答案 0 :(得分:0)
我看不到代码部分立即出现任何错误。我怀疑,由于使用了“卫生单位”,因此无法正常工作。
更改代码以百分比表示这些单位可能会有所帮助。这样一来,无论是哪种敌人,0
健康都被摧毁了,并删除了可能引起混淆的比较,例如:-8
health =“某种程度上还可以”。
我创建了一个简单的功能来绘制红色/绿色健康栏。我认为,在更长的时间内,健康状况会达到100%,因此仅在必要时绘制负面健康状况部分。
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 )
很明显,如果您的目标具有RPG风格的“命中点”,则百分比始终可以按以下方式传递:
health_percentage = round( current_hp / maximum_hp * 100 )
参考代码:
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()