我想随机化颜色并每隔几秒更改文本颜色,所以我想确保我不使用相同的颜色。我如何知道当前文本的颜色?
最佳答案
您可以定义 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)
关于python - pygame - 获取文本的当前颜色(pygame.Surface 类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51451106/