我正在使用pygame制作一个绘画程序,这需要调色板。
我的代码基本上是:
from pygame import *
screen = display.set_mode((1152,864))
color = (0,0,0)
palette = image.load("images/palette.jpg")
paletteRect=Rect(0,675,150,200)
running = True
while running:
finish = False
for e in event.get():
if e.type == QUIT:
running = False
screen.blit(palette,paletteRect)
if mb[0] == 1 and paletteRect.collidepoint(mx,my):
color = spectrum.get_at((mx,my))
发生的情况是:颜色 = palette.get_at((mx,my)) 索引错误:像素索引超出范围
但是,如果我将其更改为 paletteRect = palette.get_rect(),我的调色板工作得很好。有人知道如何解决这个问题吗?谢谢。
我想说你的问题是(mx, my)
代表一个屏幕坐标(虽然很难说,因为你没有包含这部分代码),所以当点击与你的paletteRect
发生冲突时,它的坐标将是类似于(10, 700)
的东西,这超出了你的palette
图像大小。
您需要从鼠标 x 和 y 坐标中减去调色板在屏幕上的位置:
color = palette.get_at((mx-paletteRect.x, my-paletteRect.y))