选择颜色在pygame中绘制生命

问题描述 投票:0回答:1

我想用填充颜色的pygame.Surface画出玩家的生命:绿色意味着玩家的生命接近玩家的最大生命,当玩家的生命很低并且我不知道如何选择颜色时红色。随着玩家生命的减少,果岭必须慢慢变红。

python colors pygame
1个回答
1
投票

如果你正在寻找一个颜色变化的矩形(或任何其他形状),那么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)将是纯白色。

如果你跟踪healthmax_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变量你应该没问题。

© www.soinside.com 2019 - 2024. All rights reserved.