Pygame走向旋转



我想让圆圈朝着它看起来的样子移动,当我输入0时,它会向0移动但当我输入90时,由于某种原因,它会向200或其他方向移动

import pygame
import math
import random
from random import randint
pygame.init()

screen = pygame.display.set_mode([500, 500])
""""""

def rad_to_offset(radians, offset): 
x = math.cos(radians) * offset
y = math.sin(radians) * offset
return [x, y]

X = 250
Y = 250
""""""
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
""" if i put 90 it doesnt go towards 90 """
xy = rad_to_offset(90, 1)
X += xy[0]
Y += xy[1]
print(X, Y)
screen.fill((255, 255, 255))
pygame.draw.circle(screen, (0, 0, 255), (X, Y), 20)
pygame.display.flip()
pygame.quit()

三角函数中角度的单位是弧度而不是度。使用math.radians将角度转换为弧度:

def rad_to_offset(degrees, offset): 
x = math.cos(math.radians(degrees)) * offset
y = math.sin(math.radians(degrees)) * offset
return [x, y]

最新更新