2013-07-29 113 views
1

我在保存爲BMP文件的藍色背景上有一個紅球的圖像。圖像中只使用兩種顏色:紅色(255,0,0)和藍色(0,0,255)。我寫了下面的Pygame測試程序來隔離問題。它只是加載一個圖像,將左上角像素的顏色設置爲透明顏色,並將其粘貼到屏幕上。這個過程很簡單,但我無法實現。在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()無濟於事。我究竟做錯了什麼?謝謝你的幫助!

+1

做了一個紅色圓圈與藍色BG,它工作得很好,它只是顯示紅色圓圈沒有藍色 – Serial

回答

0
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) 
2

唉!經過很長一段時間,我終於搞清楚了。我所要做的就是將load(...)更改爲load(...).convert()!現在我的頭髮都被撕掉了。

相關問題