Ask coding questions

← Back to all posts
Move a sprite at an angle in pygame
Dunce (45)

So if I had a rotated sprite in pygame, and I wanted to move it in the direction it was facing, how would I do that?
I don't want the movement to have any velocity or anything for now, just simple movement in the direction it's facing.

Answered by notGilbert (73) [earned 5 cycles]
View Answer
Comments
hotnewtop
notGilbert (73)

I'm not a pygame expert, but you can use trigonometry, and simple xy movement to move it in a direction.

from math import sin, cos

direction = 0 # in radians!!!
speed = 20

def move():
  player.x += cos(direction) * speed
  player.y += sin(direction) * speed

you might need to tweak it a little (change the signs) since it will assume the player is facing horizontally