[英]Shifting the color value based on percentage from green to red using PyGame
I'm trying to make a health bar that scales the colors of GREEN to RED depending on the percentage of health.我正在尝试制作一个健康栏,根据健康百分比将绿色的 colors 缩放为红色。 Basically, i'm trying to make the value of 0.5 correspond to (255, 255, 0), and a value of 1.0 correspond to (0, 255, 0) and a value of 0.0 correspond to (255,0,0).基本上,我试图使0.5的值对应于(255,255,0),1.0的值对应于(0,255,0),0.0的值对应于(255,0,0) . I saw This Post , which has the same intent as I, but doesn't quite help code-wise.我看到了This Post ,它的意图与我相同,但在代码方面并没有多大帮助。 Same applies för This Post .这同样适用于这篇文章。
This is my current code:这是我当前的代码:
def draw_player_health_mana(surface, x, y, curr_value, start_value, type_bar, font):
#TRANSFORMS THE VALUES TO PERCENTAGE
pct = curr_value/start_value
if pct < 0:
pct = 0
#SIZE OF THE HEALTH BAR
BAR_LENGTH = 150
BAR_HEIGHT = 18
#INITIAL COLOR VALUES
red_color = 255
green_color = 255
#THE FILLED BAR TO DISPLAY VISUALLY OF CURRENT HP COMPARED TO MAX HP
fill = pct * BAR_LENGTH
#PYGAME RECTANGLES
outline_rect = pg.Rect(x, y, BAR_LENGTH, BAR_HEIGHT)
fill_rect = pg.Rect(x, y, fill, BAR_HEIGHT)
#CHECKS WHAT TYPE OF BAR IS CALLED FOR - THERES MANA AND HEALTH
if type_bar == 'health':
#THE DIFFERENCE IN PERCENTAGE, MEANT AS AN INCREASE
pct_diff = 1.0 - pct
#INTENT IS TO CHECK IF PERCENTAGE IS ABOVE 50%, THEN DECREASE THE GREEN SPECTRUM
#BUT AS SHOWN, IT NEVER DECREASES TO 0, BUT ONLY TO HALF OF 255...
#LIKEWISE FOR THE INCREASE OF RED_COLOR
if pct > 0.5:
red_color = 255 * pct #### Can't really figure out how to deal with this part
else:
green_color = 255*pct_diff
col = (red_color, green_color, 0)
print(col)
elif type_bar == 'mana':
col = BLUE
pg.draw.rect(surface, col, fill_rect)
pg.draw.rect(surface, BLACK, outline_rect, 2)
text = font.render(str(curr_value) + "/" + str(start_value), True, BLACK)
text_rect = text.get_rect(center=(BAR_LENGTH/2 + x, BAR_HEIGHT/2 + y))
surface.blit(text, text_rect)
As commented in the syntax above, the issue is the color conversion.如上面语法中所述,问题在于颜色转换。 Color works as (RED, GREEN, BLUE).颜色为(红色、绿色、蓝色)。 For a nice green color of having full health, it should be (0, 255, 0).对于具有完整健康的漂亮绿色,它应该是 (0, 255, 0)。 However, as the health shifts towards 50%, the red color must reach the value of 255 (Causing for (255, 255, 0), which i cannot figure out how to effectively do whilst transitioning the decrease in green to 0 to reach a more of a red color.. I feel really lost on this matter, and any type of help would be greatly appreciated.然而,随着健康向 50% 转变,红色必须达到 255 的值(导致 (255, 255, 0),我无法弄清楚如何有效地做到这一点,同时将绿色的减少过渡到 0 以达到更多的红色..我在这件事上真的很迷茫,任何类型的帮助都将不胜感激。
Thanks in advance.提前致谢。
0.5 correspond to (255, 255, 0), and a value of 1.0 correspond to (0, 255, 0) (255, 255, 0), and a value of 1.0 correspond to (0, 255, 0) 0.5 对应 (255, 255, 0),1.0 对应 (0, 255, 0) (255, 255, 0),1.0 对应 (0, 255, 0)
Therefore the green color component is min(255, pct_diff*2 * 255)
and the red color component is min(255, pct*2 * 255)
:因此,绿色分量为min(255, pct_diff*2 * 255)
,红色分量为min(255, pct*2 * 255)
:
pct_diff = 1.0 - pct
red_color = min(255, pct_diff*2 * 255)
green_color = min(255, pct*2 * 255)
col = (red_color, green_color, 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.