I have a simple program that blits a background image onto the display area, and then puts a sprite in the upper left corner. The sprite uses colorkey transparency, but when I blit the sprite, the areas that should be transparent are black, instead. The color used for the colorkey is a greenish color, so the colorkey is doing something, at least.
Here's my main module, which has most of the display code:
import pygame
from pygame.locals import *
import sprites
try:
import psyco
psyco.full()
except:
print "psyco failed to import"
class PatchCon(object): # main game class
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((832, 768))
pygame.display.set_caption('PatchCon')
self.setBackground()
self.sprites = pygame.sprite.RenderPlain((sprites.Actor("Reimu")))
def refresh(self):
self.sprites.update()
self.screen.blit(self.background, (0,0))
self.sprites.draw(self.screen)
pygame.display.update()
def setBackground(self):
self.background = pygame.Surface(self.screen.get_size())
backgrounds = sprites.loadImage('patchconbackgrounds.png')
self.background.blit(backgrounds[0], (0,0), sprites.bgcoords[3])
self.background = self.background.convert()
def mainLoop(self):
while True:
for event in pygame.event.get():
if event.type == QUIT: return
self.refresh()
if __name__ == '__main__':
game = PatchCon()
game.mainLoop()
My other module, 'sprites.py', deals with loading images and constructing sprite objects:
import os
import pygame
from pygame.locals import *
# there are four different backgrounds compiled into one big image.
bgcoords = ((0,0,832,768),
(0,769,832,1537),
(833,0,1664,768),
(833,769,1664,1537))
# the top left pixels of characters' sprites in the sprite sheet
charCoords = { "Reimu" : (1, 10),
"Marisa" : (334, 10)}
def loadImage(name, colorkey=None):
fullname = os.path.join('images', name)
try:
image = pygame.image.load(fullname)
except pygame.error, message:
print 'Cannot load image:', fullname
raise SystemExit, message
image = image.convert_alpha()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
class SpriteDict(object):
"""SpriteDict has a list that holds all of the different images
the sprites have. It's in charge of finding character's sprites on a big
sprite sheet and loading them into that list."""
def __init__(self):
self.spritesheet = loadImage("patchconsprites.png", pygame.Color('#007888'))[0]
# more code here
def choose(self, dir, step, color):
"""This function returns an image from the sprite list."""
class Actor(pygame.sprite.Sprite):
def __init__(self, name):
pygame.sprite.Sprite.__init__(self)
self.sprites = SpriteDict(name)
self.image = self.sprites.choose('N', 0, 1)
self.rect = self.image.get_rect()
All the tutorials I've found lead me to believe this code is correct. Anyone see what I'm doing wrong?
EDIT: searching around the related questions, I found that image.convert() doesn't preserve alpha pixels, so as suggested in another question, I changed it to image.convert_alpha(). Now, however, instead of black areas, I just get the color of the colorkey. I've triple-checked the color value, and I'm certain its correct. Anything else I could be missing?