繁体   English   中英

Python 中的 pygame.draw.rect() 的 colors 有什么不同?

[英]What are the different colors for pygame.draw.rect() in Python?

我正在学习使用 PyGame 制作蛇游戏。 现在这里是代码看起来到目前为止的样子(它不完整,请不要给出制作蛇游戏的实际代码:):

# Setting up PyGame and a window
import pygame
pygame.init()
dis = pygame.display.set_mode((400, 300))
pygame.display.update()
pygame.display.set_caption('Snake game by snoopstick')
game_over = False

# Creating Colors
blue = (0, 0, 225)
red = (255, 0, 0)

while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
    pygame.draw.rect(dis, blue, [200, 150, 10, 10])
    pygame.display.update()

pygame.quit()
quit()

你可以看到我已经可以制作红色和蓝色,但我想知道如何制作更多的colors或更多不同类型的游戏。 go 可以用什么不同的参数代替蓝色和红色。

这些是RGB colors ,在提供的链接中有一个选择器!

Colors 是红色、绿色和蓝色的混合色。 就像油漆一样。 您可以使用 0 到 255(含)之间的值来控制混合中每种的量。

You can make any color you want, with the Pygame Color class - https://www.pygame.org/docs/ref/color.html

查看Color class 构造函数,了解各种创建 colors 的方法。

如果您想尝试一些颜色混合,请使用HTML 颜色选择器页面

pygame 有大量名为 colors 的预定义,非常有用。 您可以从 pygames 预定义列表中获取红色和蓝色 colors ,而不是自己重新定义它们:

red = pygame.Color('red')
blue = pygame.Color('blue')

You can see all the colors directly by looking at the dict pygame.color.THECOLORS, but that is a lot of colors to go through. 是我对类似问题的先前答案的回答的链接。 也许我应该把它留给答案的链接? 不确定,所以我将包含足够的内容以供使用。

我发现这个代码片段很容易在交互式 session 中执行,然后我可以在我的实际代码中使用我想要的那个。

import pygame
from pprint import  pprint

def print_colors(color_name):
    color_list = [(c, v) for c, v in pygame.color.THECOLORS.items() if color_name in c]
    pprint(color_list)

print_colors('slategrey')

输出:

[('darkslategrey', (47, 79, 79, 255)),
 ('slategrey', (112, 128, 144, 255))
 ('lightslategrey', (119, 136, 153, 255))]

我在交互式 session 中执行此操作以获取包含“slategrey”的所有名称,然后在我的实际代码中我可以使用我想要的这样的名称:

darkslategrey = pygame.Color('darkslategrey')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM