Search code examples
pythoncoordinatespygamedegrees

python convert degrees to change in x and change in y


I am making a snake game in python with pygame and for moving the character I have a integer which is the degrees of the angle it should move. Is there any way I can get the change in x and y to move it based on the degrees? For example: func(90) # [0, 5] or func(0) # [5, 0]


Solution

  • import math
    
    speed = 5
    angle = math.radians(90)    # Remember to convert to radians!
    change = [speed * math.cos(angle), speed * math.sin(angle)]