首页 > 解决方案 > 如何在 python 中使用十六进制值而不是 RGB 值。蟒蛇 2.7

问题描述

我不喜欢 RGB,我用 HEX 代替。我是 Python 新手,这就是我的代码的样子;我如何使用十六进制。对不起我不是来自美国的英语。

import pygame

black = (0,0,0)
white = (255,255,255)
blue = ("#7ec0ee")

pygame.init()

size = 1024,768
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Code for Stack")

done = False
clock = pygame.time.Clock()

while not done:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        done = True

      screen.fill(blue)

      pygame.display.flip()
      clock.tick(60)

pygame.quit()

标签: pythonpygame

解决方案


pygame.Color支持十六进制参数。所以你可以这样做:

blue = pygame.Color("#7ec0ee")
screen.fill(blue)

这会自动将您的颜色转换为 RGBA 值。所以如果你去打印颜色,你会看到:

(126, 192, 238, 255)

推荐阅读