我有一个蓝色背景上的红色球的图像另存为BMP文件。图像中只使用了两种颜色:红色(255,0,0)和蓝色(0,0,255)。我写了下面的Pygame测试程序来隔离这个问题。它只是加载一个图像,将左上角像素的颜色设置为透明颜色,然后将其显示在屏幕上。它很简单,但我不能让它工作。
import pygame
if __name__ == "__main__":
pygame.init()
size = (640, 400)
screenSurface = pygame.display.set_mode(size)
ballSurface = pygame.image.load("ball.bmp")
transparentColor = ballSurface.get_at((0, 0))
ballSurface.set_colorkey(transparentColor)
screenSurface.blit(ballSurface, ballSurface.get_rect())
pygame.display.update()
pygame.time.wait(30000)
我已经尝试将每种颜色手动传递到set_colorkey()
中,但都无济于事。我做错了什么?谢谢你的帮助!
发布于 2013-07-29 01:46:55
啊!在纠结了很长一段时间之后,我终于弄明白了。我所要做的就是把load(...)
改成load(...).convert()
!现在我的头发都被扯掉了。
发布于 2013-07-29 01:45:04
import pygame
if __name__ == "__main__":
pygame.init()
size = (640, 400)
screenSurface = pygame.display.set_mode(size)
ballSurface = pygame.image.load("ball.bmp")
//You need to loop through each pixel.
for each x in range (0, 640)
for each y in range (o, 400)
pixel =getPixel (x,y)
//algorithm follows
//get each pixel
//find it's color
//change the individual color of each pixel
transparentColor = ballSurface.get_at((0, 0))
pixel.set_colorkey(transparentColor)
screenSurface.blit(ballSurface, ballSurface.get_rect())
pygame.display.update()
pygame.time.wait(30000)
https://stackoverflow.com/questions/17914793
复制相似问题