Skip to content

Instantly share code, notes, and snippets.

@hortonew
Last active December 10, 2015 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hortonew/4507687 to your computer and use it in GitHub Desktop.
Save hortonew/4507687 to your computer and use it in GitHub Desktop.
Pygame - Sprite image and Text label
import pygame, sys, os
running = True
pygame.init()
screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
#calculate current path + location of player image. Image must be on same level as this file
mypath = os.path.dirname( os.path.realpath( __file__) )
p_path = os.path.join(mypath, 'player.png')
#create player image, move to 400,300
player_image = pygame.sprite.Sprite()
player_image.image = pygame.image.load(p_path).convert()
player_image.rect = player_image.image.get_rect().move(400,300)
#create text label
font = pygame.font.Font(None, 24)
font_color = (255,255,255)
font_background = (0,0,0)
t = font.render("Hello World", True, font_color, font_background)
t_rect = t.get_rect()
t_rect.centerx, t_rect.centery = 100, 100
def update():
#draw player to screen
screen.blit(player_image.image, player_image.rect)
#draw text to screen
screen.blit(t, t_rect)
if __name__ == "__main__":
while running:
update()
pygame.display.flip()
clock.tick(30)
pygame.quit()
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment