pygame-获取文本的当前颜色(pygame.Surface类型)

时间:2018-07-20 22:24:05

标签: python python-3.x pygame pygame-surface

我想将颜色随机化,并每隔几秒钟更改文本的颜色,所以我想确保我不使用相同的颜色。我怎么知道当前的文字颜色?

1 个答案:

答案 0 :(得分:1)

您可以定义颜色的set,并使用组和当前颜色的差异来获得仅包含不同颜色的组。然后将其转换为列表,并使用random.choice选择一种新颜色。

import random

RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Define a set of the colors.
COLORS = {RED, GREEN, BLUE}
color = RED  # Current color.

for _ in range(50):
    # The difference of `COLORS` and the set `{color}` is
    # a set that doesn't contain `color`.
    difference = COLORS - {color}
    # Then you need to convert this set into a list in order
    # to use `random.choice`.
    color = random.choice(list(difference))
    print(color)