1
\$\begingroup\$

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.

enter image description here

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)
background = pygame.image.load("background.PNG")
dragon1 = pygame.image.load("dragon1.gif")
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()
\$\endgroup\$
6
  • \$\begingroup\$ So that the dragon looks to the left when he flys to the left. \$\endgroup\$ Apr 29, 2014 at 17:56
  • \$\begingroup\$ In the future, please do not delete and re-post your question. Just edit it. \$\endgroup\$
    – user1430
    Apr 29, 2014 at 18:05
  • \$\begingroup\$ but then no-one sees my question....can you help me now? \$\endgroup\$ Apr 29, 2014 at 18:12
  • \$\begingroup\$ 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. \$\endgroup\$
    – user1430
    Apr 29, 2014 at 18:25
  • \$\begingroup\$ ok sorry :) thx i never make this mistake again... \$\endgroup\$ Apr 29, 2014 at 18:26

1 Answer 1

3
\$\begingroup\$

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.

\$\endgroup\$
15
  • \$\begingroup\$ sorry i'm just beginner but i tried it an it doesn't work...like your example.. \$\endgroup\$ Apr 29, 2014 at 19:11
  • \$\begingroup\$ and what when de dragon flys up and down? \$\endgroup\$ Apr 29, 2014 at 19:15
  • \$\begingroup\$ now it works but the dragon turns all the time i hold the key down...:( \$\endgroup\$ Apr 29, 2014 at 19:24
  • \$\begingroup\$ 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. \$\endgroup\$
    – pmoleri
    Apr 29, 2014 at 19:48
  • \$\begingroup\$ 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... \$\endgroup\$ Apr 29, 2014 at 19:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .