以度为单位移动 pygame
Move in degrees pygame
我正在为自己制作一个实用程序,以便在我的游戏中轻松地将度数设置为 x 和 y 坐标,但我遇到了一个问题;试图在屏幕上以度数移动玩家。我发现了多个不起作用的公式,我需要一些帮助。这是我找到的代码:
def move(degrees, offset):
x = math.cos(degrees * 57.2957795) * offset # 57.2957795 Was supposed to be the
y = math.sin(degrees * 57.2957795) * offset # magic number but it won't work.
return [x, y]
然后我运行这个:
move(0, 300)
输出:
[300.0, 0.0]
它工作得很好,但是当我这样做时:
move(90, 300)
它输出了这个:
[-89.8549554331319, -286.22733444608303]
数字正确,但操作错误。为了将度数转换为弧度,您需要每半圆除以 180 度,然后乘以每半圆的 pi 弧度。这相当于除以除以你拥有的常数。
您的方法几乎是正确的。您应该对 sin/cos 函数使用弧度。这是我在 C++ 中常用的方法(移植到 python)进行 2D 移动。
import math
def move(degrees, offset)
rads = math.radians(degrees)
x = math.cos(rads) * offset
y = math.sin(rads) * offset
return x, y
您可以使用向量的 from_polar
method of the pygame.math.Vector2
class to set the polar coordinates。然后你可以使用这个向量来调整精灵或矩形的位置。
import pygame as pg
from pygame.math import Vector2
def move(offset, degrees):
vec = Vector2() # Create a zero vector.
vec.from_polar((offset, degrees)) # Set its polar coordinates.
return vec
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
BLUE = pg.Color('dodgerblue1')
rect = pg.Rect(300, 200, 30, 20)
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
# Use the vector that `move` returns to move the rect.
rect.move_ip(move(50, 90))
screen.fill(BG_COLOR)
pg.draw.rect(screen, BLUE, rect)
pg.display.flip()
clock.tick(30)
pg.quit()
我正在为自己制作一个实用程序,以便在我的游戏中轻松地将度数设置为 x 和 y 坐标,但我遇到了一个问题;试图在屏幕上以度数移动玩家。我发现了多个不起作用的公式,我需要一些帮助。这是我找到的代码:
def move(degrees, offset):
x = math.cos(degrees * 57.2957795) * offset # 57.2957795 Was supposed to be the
y = math.sin(degrees * 57.2957795) * offset # magic number but it won't work.
return [x, y]
然后我运行这个:
move(0, 300)
输出:
[300.0, 0.0]
它工作得很好,但是当我这样做时:
move(90, 300)
它输出了这个:
[-89.8549554331319, -286.22733444608303]
数字正确,但操作错误。为了将度数转换为弧度,您需要每半圆除以 180 度,然后乘以每半圆的 pi 弧度。这相当于除以除以你拥有的常数。
您的方法几乎是正确的。您应该对 sin/cos 函数使用弧度。这是我在 C++ 中常用的方法(移植到 python)进行 2D 移动。
import math
def move(degrees, offset)
rads = math.radians(degrees)
x = math.cos(rads) * offset
y = math.sin(rads) * offset
return x, y
您可以使用向量的 from_polar
method of the pygame.math.Vector2
class to set the polar coordinates。然后你可以使用这个向量来调整精灵或矩形的位置。
import pygame as pg
from pygame.math import Vector2
def move(offset, degrees):
vec = Vector2() # Create a zero vector.
vec.from_polar((offset, degrees)) # Set its polar coordinates.
return vec
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
BLUE = pg.Color('dodgerblue1')
rect = pg.Rect(300, 200, 30, 20)
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
# Use the vector that `move` returns to move the rect.
rect.move_ip(move(50, 90))
screen.fill(BG_COLOR)
pg.draw.rect(screen, BLUE, rect)
pg.display.flip()
clock.tick(30)
pg.quit()