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

pygame - get current color of text (pygame.Surface type)

我想随机化颜色并每隔几秒更改一次文本颜色,因此我想确保我没有使用相同的颜色。我怎么知道当前文本的颜色?

您可以定义一个 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)