How can I rotate my picture using Pygame?

I want to make the Dragon on the picture rotate softly to the left when he flies to the left and rotate to the right when he flies to the right and up and down also.

How can I do that with the WASD keys? The Dragon can fly already up, down, lef, and right....but he doesn't turn...

My Code:

import pygame, sys
from pygame.locals import *

pygame.init()

size = width, height = 1600, 848
speed = [2, 2]
black = (0, 0, 0)

pygame.mouse.set_cursor(*pygame.cursors.broken_x)

screen = pygame.display.set_mode(size)
x,y=0,0
clock=pygame.time.Clock()
speed=250
movex, movey=0,0

while True:
screen.blit(background, (0,0))
screen.blit(dragon1, (x,y))

for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()

if event.type==KEYDOWN:
if event.key==K_a:
movex=-4
elif event.key==K_d:
movex=+4
elif event.key==K_w:
movey=-4
elif event.key==K_s:
movey=+4
if event.type==KEYUP:
if event.key==K_a:
movex=0
elif event.key==K_d:
movex=0
elif event.key==K_w:
movey=0
elif event.key==K_s:
movey=0
x+=movex
y+=movey

pygame.display.flip()

• So that the dragon looks to the left when he flys to the left. Apr 29, 2014 at 17:56
• In the future, please do not delete and re-post your question. Just edit it.
– user1430
Apr 29, 2014 at 18:05
• but then no-one sees my question....can you help me now? Apr 29, 2014 at 18:12
• Everybody still sees your question, because you edited it, which moves it to the front page. Trying to circumvent the rules of the site, however, is a great way to guarantee your questions don't get viewed because they get closed. Please read the help center so you don't make the same mistake again.
– user1430
Apr 29, 2014 at 18:25
• ok sorry :) thx i never make this mistake again... Apr 29, 2014 at 18:26

Use pygame.transform.rotate function:

    screen.blit(background, (0,0))
angle = None
if movex == 0 and movey < 0:
angle = -180
elif movex > 0 and movey >= 0:
angle = 20
elif movex < 0 and movey >= 0:
angle = -20
elif movex > 0 and movey < 0:
angle = 160
elif movex < 0 and movey < 0:
angle = -160

if angle:
dragon = pygame.transform.rotate(dragon1, angle)
else:
dragon = dragon1

screen.blit(dragon, (x,y))
...


You can always keep the original surface dragon1 and transform it when you are going to blit it (like the example), or keep three variations pre-calculated and choose which one you want to blit.

• sorry i'm just beginner but i tried it an it doesn't work...like your example.. Apr 29, 2014 at 19:11
• and what when de dragon flys up and down? Apr 29, 2014 at 19:15
• now it works but the dragon turns all the time i hold the key down...:( Apr 29, 2014 at 19:24
• Isn't it good? That while you are going right the angle is always the same? I'll add code for up and down too. Apr 29, 2014 at 19:48
• sorry what? if the dragon fly's left, he turns 20* but all the time until im stop holding the key...but he should turn only once he flys on one side... Apr 29, 2014 at 19:52