python 将度数转换为 x 的变化和 y 的变化

发布于 2024-10-09 08:54:22 字数 152 浏览 3 评论 0原文

我正在用 pygame 用 python 制作一个蛇游戏,为了移动角色,我有一个整数,它是它应该移动的角度。有什么方法可以改变 x 和 y 来根据度数移动它吗?例如:func(90) # [0, 5]func(0) # [5, 0]

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三人与歌 2024-10-16 08:54:22
import math

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

speed = 5
angle = math.radians(90)    # Remember to convert to radians!
change = [speed * math.cos(angle), speed * math.sin(angle)]
甜中书 2024-10-16 08:54:22

角度的正弦和余弦乘以移动总量,即可得出 X 和 Y 的变化。

import math
def func(degrees, magnitude):
    return magnitude * math.cos(math.radians(degrees)), magnitude * math.sin(math.radians(degrees))

>>> func(90,5)
(3.0616169978683831e-16, 5.0)
>>> func(0,5)
(5.0, 0.0)

The sine and cosine of the angle, multiplied by the total amount of the move, will give you the X and Y changes.

import math
def func(degrees, magnitude):
    return magnitude * math.cos(math.radians(degrees)), magnitude * math.sin(math.radians(degrees))

>>> func(90,5)
(3.0616169978683831e-16, 5.0)
>>> func(0,5)
(5.0, 0.0)
流绪微梦 2024-10-16 08:54:22

如果蛇只能以特定角度(例如 90 或 45 度)移动(这在此类游戏中很常见),那么您只能走 4 或 8 个方向。您只需将角度除以允许的增量即可获得方向索引,然后可以使用该方向索引索引 X/Y 偏移表。这比使用三角学要快得多。

x, y = 100, 100   # starting position of the snake

direction = angle / 90 % 4   # convert angle to direction

directions = [(0,-1), (1, 0), (0, 1), (-1, 0)]   # up, right, down, left

# convert the direction to x and y offsets for the next move
xoffset, yoffset = directions[direction]

# calculate the next move
x, y = x + xoffset, y + yoffset

更好的是,完全放弃角度概念,只使用方向变量。然后旋转蛇只是增加或减少方向的简单问题。

# rotate counter-clockwise
direction = (direction - 1) % 4

# rotate clockwise
direction = (direction + 1) % 4

如果需要,可以轻松扩展到 8 个方向(以 45 度增量移动)。

If the snake can only move at certain angles (e.g. 90 or 45 degrees), which is typical in such a game, there are only 4 or 8 directions you can go. You can just divide your angle by the permitted increment and get a direction index, which can then be used to index into a table of X/Y offsets. This will be a lot faster than using trigonometry.

x, y = 100, 100   # starting position of the snake

direction = angle / 90 % 4   # convert angle to direction

directions = [(0,-1), (1, 0), (0, 1), (-1, 0)]   # up, right, down, left

# convert the direction to x and y offsets for the next move
xoffset, yoffset = directions[direction]

# calculate the next move
x, y = x + xoffset, y + yoffset

Better yet, dispense with the angle concept entirely and just use a direction variable. Then rotating the snake is a simple matter of incrementing or decrementing the direction.

# rotate counter-clockwise
direction = (direction - 1) % 4

# rotate clockwise
direction = (direction + 1) % 4

This can easily be extended to 8 directions (moving at 45 degree increments) if desired.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文