简体   繁体   English

使用 PyGame 根据百分比将颜色值从绿色转换为红色

[英]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.

相关问题 python pandas数据框根据另一列的条件将列中的文本着色为红色、绿色或黑色 - python pandas dataframe color the text as red, green or black in column based on the condition from the other column 基于类别的单线图的不同颜色(绿色和红色) - Different color for single line plot in plotly based on category (Green & red) 如何使用pyds9通过python在ds9中将圆形区域的颜色从绿色更改为红色 - How to change the color of the circular region from green to red in ds9 through python using pyds9 在pygame中,如何使背景不断在循环中在红色,绿色,蓝色,黑色和白色之间改变颜色 - In pygame, how do you make the background continuously change color between red, green, blue, black , and white in a loop 简单的jes功能可以将图片的颜色从红色切换为绿色或蓝色? - Simple jes Function to switch a pictures color from red to green or blue? 如果移动验证状态发生变化,如何将颜色从红色变为绿色? - How to change color from red to green if mobile verification status changes? 我想根据通过或失败的Python将单元格的背景色设置为红色或绿色 - I want to set background color of a cell to red or green based on pass or fail in python 如何使用 python 将全彩色图像转换为仅三色图像(红、蓝、绿)? - How do convert a full color image into just three color image (red, blue, green) using python? 如何在 Seaborn/Matplotlib 中创建固定颜色 map(最小值 = 红色,最大值 = 绿色) - How do I create a fixed color map in Seaborn/Matplotlib (min value = Red, max value = Green) 两个间隔颜色 df panda red &lt;-3 和 green &gt;3 - two intervals color df panda red <-3 and green >3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM