Okay, I've been tearing my hair out for a while now, and Stack Overflow, Google and my usual sources aren't getting me any further along.
I'm trying to load a sprite from a spritesheet, with alpha, and overlay it on top of a couple of background layers. The idea being that the 'charlayer' sprites will keep the transparency that already exists in the files, and therefore look nice. Ish.
Unfortunately, when I run the program I've currently got, the sprite loads fine...but the parts that should be transparent are black, and overwrite the background layers.
I'm obviously losing the alpha transparency somewhere along the line, but I can't figure out where.
Anybody got any ideas? Horrible code below (in spoiler). And yes, this is a practice attempt, not an actual program.
# INITIALISATION
import pygame, sys, os
FPS = 30
SCREEN_WIDTH = 1024
SCREEN_HEIGHT = 768
TILE_WIDTH = 32
TILE_HEIGHT = 32
TILES_WIDE = 21
TILES_HIGH = 17
VIEWPORT_WIDTH = TILE_WIDTH*TILES_WIDE
VIEWPORT_HEIGHT = TILE_HEIGHT*TILES_HIGH
if VIEWPORT_WIDTH>SCREEN_WIDTH: VIEWPORT_WIDTH = (SCREEN_WIDTH/TILE_WIDTH)*TILE_WIDTH
if VIEWPORT_HEIGHT>SCREEN_HEIGHT: VIEWPORT_HEIGHT = (SCREEN_HEIGHT/TILE_HEIGHT)*TILE_WIDTH
print("Viewport width: " + str(VIEWPORT_WIDTH))
print("Viewport height: " + str(VIEWPORT_HEIGHT))
class VisibleThing(pygame.sprite.Sprite):
def __init__(self,filename,tile_x,tile_y,tile_width,tile_height):
pygame.sprite.Sprite.__init__(self)
self.rect=pygame.Rect( 0 , 0 , tile_width,tile_height)
self.image=pygame.Surface((tile_width,tile_height)).convert_alpha()
sheet = pygame.image.load(os.path.join(filename)).convert_alpha()
targetRect=pygame.Rect( (tile_x-1)*tile_width , (tile_y-1)*tile_height ,tile_width,tile_height)
sprite = sheet.subsurface(targetRect)
self.image.blit(sprite,(0,0),self.rect)
self.rect = self.image.get_rect()
pygame.init()
# main screen on
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
#set up system icon
rect=pygame.Rect(0,0,32,32)
icon=pygame.Surface(rect.size).convert()
icon.blit(pygame.image.load(os.path.join("eeko.png")).convert(), (0,0), rect)
pygame.display.set_icon(icon)
#set up surfaces
background = pygame.Surface(screen.get_size())
background.fill((255,255,255))
background = background.convert()
groundlayer = pygame.Surface((VIEWPORT_WIDTH,VIEWPORT_HEIGHT))
groundlayer = groundlayer.convert_alpha()
charlayer = pygame.Surface((VIEWPORT_WIDTH,VIEWPORT_HEIGHT))
charlayer = charlayer.convert_alpha()
charlayer.fill((0,0,0,0))
#set up sprite groups
VisibleThingList = pygame.sprite.Group()
allSpritesList = pygame.sprite.Group()
#set up player
player = VisibleThing("rgba-compose.png",7,1,24,24)
VisibleThingList.add(player)
allSpritesList.add(player)
#set player starting position
player.rect.x = SCREEN_WIDTH/2
player.rect.y = SCREEN_HEIGHT/3
# initiate clock
clock = pygame.time.Clock()
playtime = 0.0
mainloop = True
# AND THEY'RE OFF!
while mainloop:
milliseconds = clock.tick(FPS)
playtime += milliseconds / 1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainloop = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
mainloop = False
# Update our caption
text = "FPS: {0:.1f} Playtime: {1:.2f}".format(clock.get_fps(),playtime)
pygame.display.set_caption(text)
# Draw our background
screen.blit(background,(0,0))
# Draw our ground layer
rect=pygame.Rect(0,0,TILE_HEIGHT,TILE_WIDTH)
aTile = pygame.Surface(rect.size).convert()
aTile.blit(pygame.image.load(os.path.join("floor.png")).convert(),(0,0),rect)
for y in range(0,TILES_HIGH):
for x in range (0,TILES_WIDE):
groundlayer.blit(aTile,(x*TILE_WIDTH,y*TILE_HEIGHT))
screen.blit(groundlayer, (0,0))
# Get mouse position
pos = pygame.mouse.get_pos()
# Update 'player' location
player.rect.x=pos[0]
player.rect.y=pos[1]
# Draw the sprites
charlayer.fill((0,0,0,0))
allSpritesList.draw(charlayer)
screen.blit(charlayer,(0,0))
pygame.display.update()
pygame.quit()
print("This game was played for {1:.2f} seconds.".format(clock.get_fps(),playtime))
def main():
pass
if __name__ == '__main__':
main()