选择颜色在皮格姆画生命

2024-04-26 09:52:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用一个充满颜色的pygame.Surface来描绘玩家的生活: 绿色表示玩家的寿命接近玩家的最大寿命,红色表示玩家的寿命较低,我不知道如何选择颜色。 随着玩家生命的减少,绿色必须慢慢变为红色。你知道吗


Tags: 颜色玩家surfacepygame绿色红色寿命
1条回答
网友
1楼 · 发布于 2024-04-26 09:52:57

如果您正在寻找一个颜色变化的矩形(或任何其他形状),那么pygame有一些非常有用的draw命令

拿这个,pygame.draw.rect拿自the pygame docs

pygame.draw.rect()
    draw a rectangle shape
    rect(Surface, color, Rect, width=0) -> Rect

Draws a rectangular shape on the Surface. The given Rect is the area of the 
rectangle. The width argument is the thickness to draw the outer edge. If 
width is zero then the rectangle will be filled.

在本例中,color将是红、绿和蓝颜色值的3元素元组。这些都将在0和255之间。例如,(255,255,255)将是纯白色。你知道吗

如果跟踪一个health和一个max_health变量,那么可以找出矩形中有多少应该是红色的,有多少应该是绿色的。你知道吗

例如

green_value = 255 * (health / max_health)
red_value =   255 * ((max_health - health) / max_health)

假设您的生命值是最大值100中的20,那么绿色值将是255的20%,红色值将是255的80%,并且pygame.draw.rect函数将接受color参数(red_value, green_value, 0)

只要您记得更新green_valuered_value变量,就可以了。你知道吗

相关问题 更多 >