【问题标题】:Pygame walk towards rotationPygame走向旋转
【发布时间】:2023-01-21 17:56:09
【问题描述】:

我试图让一个圆圈朝着它看起来的方向走,当我把 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()

【问题讨论】:

    标签: python pygame geometry rotation degrees


    【解决方案1】:

    三角函数中角度的单位是弧度而不是度。使用 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]
    

    【讨论】: