我试图用pygame制作一个矩形的彩虹,但我遇到了一个问题,说“ValueError:无效的颜色参数”
import pygame
pygame.init()
width = 400
height = 400
window_size = (width , height)
screen = pygame.display.set_mode(window_size)
colour = pygame.color.Color('#646400')
row = 0
done = False
while not done:
increment = 255 / 100
while row <= height:
pygame.draw.rect(screen, colour, (0, row, width, row + increment))
pygame.display.flip()
if colour[2] + increment < 255:
colour[2] += increment
row += increment
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pygame.quit()
3条答案
按热度按时间zpf6vheq1#
最简单的方法是使用
tuple
的RGB值作为颜色:然后将它作为第二个参数传递给
draw.rect()
,就像你已经做过的那样:qc6wkl3g2#
你需要改变这个,
colour = pygame.color.Color('#646400')
,因为它将更容易去与RGB颜色在一个list
。改变它:或者任何你想要的颜色,也许混合它们得到彩虹的结果。这是我会做的。代码的结果是一个渐变的蓝色。但是正如我之前提到的,你可以改变它。但是记住你不能使用
tuple
!因为它们是不可变的。d7v8vwbk3#
正如许多其他人所建议的那样,PyGame不支持HTML符号颜色字符串。
但是转换这些是相当简单的:
通常不需要将它们转换为
Color
对象来使用它们。