Donnx 2021-03-14 20:54 采纳率: 50%
浏览 911
已采纳

pygame时报错TypeError: invalid color argument

用pygame写长方形彩虹的时候,使用了如下代码:

import pygame
pygame.init()

height=300
width=400
windowSize=[400,300]
screen=pygame.display.set_mode(windowSize)
colour=pygame.color.Color('#FE12AD')

row=0
done=False
while not done:#while not ..的意思就是当done是False的时候执行下列语句,while done就是当done是True的时候执行下列语句
    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()

但出现了报错:

    colour[2] += increment
TypeError: invalid color argument

请问大佬们应该怎么解决呢

  • 写回答

1条回答 默认 最新

  • 幻灰龙 2021-03-15 09:19
    关注

    颜色必须是0-255之间的整数,这句代码的 increment 不是整数: `colour[2] += increment`

    取整下即可:`increment=round(255/100)`

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错