【发布时间】:2018-01-04 17:56:15
【问题描述】:
我想用填充了颜色的pygame.Surface 来绘制玩家的生活:
绿色表示玩家的生命接近玩家的最大生命,红色表示玩家的生命低且我不知道如何选择颜色。
随着玩家生命的减少,绿色必须慢慢变成红色。
【问题讨论】:
-
请添加您尝试过的代码示例,并使用this guide 了解如何在 StackOverflow 上提问。
我想用填充了颜色的pygame.Surface 来绘制玩家的生活:
绿色表示玩家的生命接近玩家的最大生命,红色表示玩家的生命低且我不知道如何选择颜色。
随着玩家生命的减少,绿色必须慢慢变成红色。
【问题讨论】:
如果你正在寻找一个变色矩形(或任何其他形状),那么 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)
假设您的健康状况为 20(满分为 100),那么您的绿色值为 255 的 20%,红色值为 255 的 80%,而您的 pygame.draw.rect 函数将采用 @987654330 (red_value, green_value, 0)的@参数
只要您记得更新 green_value 和 red_value 变量就可以了。
【讨论】: