Martin Jones
Martin Jones

Reputation: 123

Surface Drawing in Pygame

I have created a number of surfaces to act as placeholders for my UI elements, one of which is the 'Game Window'. My Player character is created using the Freetype Font module and is rendered to a target surface. The target surface is filled each loop to clear the 'frame' but the character won't display. If there is no fill the character displays but duplicates each 'frame'. I know the character is drawn on the correct surface as the origin is (0, 0) and is drawn offset as the surface is offset itself from the screen.

# Set up various surfaces for UI containers
GAME_SURFACE = pygame.Surface((1350, 755))
GAME_SURFACE_RECT = pygame.Surface.get_rect(GAME_SURFACE)
STATS_SURFACE = pygame.Surface((1350, 125))
CHAR_DETAILS_SURFACE = pygame.Surface((570, 540))
ENEMY_DETAILS_SURFACE = pygame.Surface((570, 540))
LOG_SURFACE = pygame.Surface((1350, 200))

# Initialise Objects
PLAYER = Entity(0, 0, PLAYER_CHAR, WHITE, GAME_SURFACE)

...

# --- Screen-clearing code goes here
SCREEN.fill(BLACK)
GAME_SURFACE.fill(BLACK)
STATS_SURFACE.fill(RED)
CHAR_DETAILS_SURFACE.fill(WHITE)
ENEMY_DETAILS_SURFACE.fill(GREEN)
LOG_SURFACE.fill(RED)

# --- Drawing code should go here
SCREEN.blit(STATS_SURFACE, (0, 0))
SCREEN.blit(CHAR_DETAILS_SURFACE, (1350, 0))
SCREEN.blit(ENEMY_DETAILS_SURFACE, (1350, 540))
SCREEN.blit(LOG_SURFACE, (0, 880))
SCREEN.blit(GAME_SURFACE, (0, 125))
PLAYER.draw()

Upvotes: 1

Views: 154

Answers (1)

sloth
sloth

Reputation: 101042

You didn't show all the relevant code but my educated guess is that nothing shows up is because your drawing logic works like this loop:

  1. clear screen
  2. clean game surface
  3. blit game surface to screen
  4. blit player image to game surface
  5. goto 1.

Hence when you draw your player image to the game surface it gets immediatly cleared before the updated game surface is blitted to the screen surface.

In this case, update the game surface before blitting it to the screen surface. Something like this:

# --- Screen-clearing code goes here
SCREEN.fill(BLACK)
GAME_SURFACE.fill(BLACK)
STATS_SURFACE.fill(RED)
CHAR_DETAILS_SURFACE.fill(WHITE)
ENEMY_DETAILS_SURFACE.fill(GREEN)
LOG_SURFACE.fill(RED)

# --- Drawing code should go here
PLAYER.draw()

SCREEN.blit(STATS_SURFACE, (0, 0))
SCREEN.blit(CHAR_DETAILS_SURFACE, (1350, 0))
SCREEN.blit(ENEMY_DETAILS_SURFACE, (1350, 540))
SCREEN.blit(LOG_SURFACE, (0, 880))
SCREEN.blit(GAME_SURFACE, (0, 125))

Upvotes: 2

Related Questions