Difference between revisions of "Drawing images"
(Created page with "==Method One== One method is to simply load the image and then draw it to the screen in a given position. When you alter the position and redraw the screen the image will move...") |
|||
Line 4: | Line 4: | ||
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
pos = [100,100] # starting position of player image | pos = [100,100] # starting position of player image | ||
− | player = pygame.image.load(os.path.join("hero.png")). | + | player = pygame.image.load(os.path.join("hero.png")).convert_alpha(); # load in player image, convert_alpha will keep transparent background |
player = pygame.transform.scale(player, (20, 30)) # resize player image | player = pygame.transform.scale(player, (20, 30)) # resize player image | ||
Line 29: | Line 29: | ||
pos = [100,100] # position for image | pos = [100,100] # position for image | ||
playerSurface = pygame.Surface((20,30)) # surface to draw image onto | playerSurface = pygame.Surface((20,30)) # surface to draw image onto | ||
− | player = pygame.image.load(os.path.join("hero.png")). | + | player = pygame.image.load(os.path.join("hero.png")).convert_alpha(); # load in player image, convert_alpha will keep transparent background |
player = pygame.transform.scale(player, (20, 30)) # resize image | player = pygame.transform.scale(player, (20, 30)) # resize image | ||
Latest revision as of 13:58, 1 March 2018
Method One
One method is to simply load the image and then draw it to the screen in a given position. When you alter the position and redraw the screen the image will move:
pos = [100,100] # starting position of player image
player = pygame.image.load(os.path.join("hero.png")).convert_alpha(); # load in player image, convert_alpha will keep transparent background
player = pygame.transform.scale(player, (20, 30)) # resize player image
#game loop
while True:
SCREEN.fill(BLACK)
SCREEN.blit(player,pos) # blit will draw the image to the screen
pygame.display.update()
for events in pygame.event.get(): #get all pygame events
if events.type == pygame.KEYDOWN:
if events.key == pygame.K_LEFT:
pos[0]=pos[0]-10
elif events.key == pygame.K_RIGHT:
pos[0]=pos[0]+10
elif events.key == pygame.K_UP:
pos[1]=pos[1]-10
elif events.key == pygame.K_DOWN:
pos[1]=pos[1]+10
Method 2
You could alternatively create a surface, then load the image onto the surface. The user input can move the surface instead of the image:
pos = [100,100] # position for image
playerSurface = pygame.Surface((20,30)) # surface to draw image onto
player = pygame.image.load(os.path.join("hero.png")).convert_alpha(); # load in player image, convert_alpha will keep transparent background
player = pygame.transform.scale(player, (20, 30)) # resize image
#game loop
while True:
SCREEN.fill(BLACK)
playerSurface.blit(player, (0,0)) # draw player on the surface at position 0,0
SCREEN.blit(playerSurface,pos) # draw the surface onto the screen at the position of pos
pygame.display.update()
for events in pygame.event.get(): #get all pygame events
if events.type == pygame.KEYDOWN:
if events.key == pygame.K_LEFT:
pos[0]=pos[0]-10
elif events.key == pygame.K_RIGHT:
pos[0]=pos[0]+10
elif events.key == pygame.K_UP:
pos[1]=pos[1]-10
elif events.key == pygame.K_DOWN:
pos[1]=pos[1]+10