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

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

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

最佳答案

您可以定义 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/

相关文章:

python - 使用 python 检查 pytesseract 版本

python - 如何打印想要的职业的平均值/中位数?

python - 如何正确替换 QScintilla 小部件上的某些匹配项?

python-3.x - 将 Pandas 数据帧转换为 utf8

python - Pygame 中的平滑键盘移动

python - 有没有办法通过只调用类的名称来调用函数?

python - 如何明确指定 Firefox for Selenium 的路径?

python - 使用python的Youtube API的KeyError

python - 如何排除 Pandas 标题中的第一个单词?

python - 为 mac 制作 pygame/SDL 安装程序