Pygame 不允许我为 rect.move 使用浮动,但我需要它 [英] Pygame doesn't let me use float for rect.move, but I need it
问题描述
我最近重新创建了一个版本的 Lunar Lander(你知道,旧的复古游戏)在 Python 3 和 Pygame 中:我的着陆器由于重力沿 y 轴移动(̀̀̀rect.move
)每一帧.\
I've recently recreated a version of Lunar Lander (you know, the old retro game) in Python 3 and Pygame: my lander moves (̀̀̀rect.move
) each frame along the y axis because of gravity.\
问题:
在我达到 1 m/s 之前,添加到 rect.move 的 y 值是小于 1 的浮点数:我必须使用 int()
将其四舍五入,因为 pygame 不喜欢浮点数.
在以前的 Tkinter 版本中,着陆器的 y 坐标是这样的:
Problem:
Until I hit 1 m/s, the y value added to rect.move is a float under 1: I must use int()
to round it up, as pygame doesn't like floats.
In a previous version with Tkinter, the y coord of the lander was like this:
0.01
0.02
...
0.765
1.03
1.45
...
在 pygame 中是
In pygame it's
0
0
0
...
1
1
1
2
2
...
这真的很烦人,因为运动不流畅.有人知道如何解决这个问题吗?比如,输入一个浮点数到 rect.move
?提前致谢!
This is really annoying, as the movement isn't fluid.
Does someone know how to solve this? Like, input a float to rect.move
?
Thanks in advance!
推荐答案
自从 pygame.Rect
应该代表屏幕上的一个区域,一个 pygame.Rect
对象只能存储整数数据:
Since pygame.Rect
is supposed to represent an area on the screen, a pygame.Rect
object can only store integral data:
Rect 对象的坐标都是整数.[...]
The coordinates for Rect objects are all integers. [...]
如果要以浮点精度存储对象位置,则必须将对象的位置分别存储在单独的变量和属性中,并同步 pygame.Rect
对象.round
坐标并将其分配给矩形的位置(例如 .topleft
):
If you want to store object positions with floating point accuracy, you have to store the location of the object in separate variables respectively attributes and to synchronize the pygame.Rect
object. round
the coordinates and assign it to the location (e.g. .topleft
) of the rectangle:
x, y = # floating point coordinates
rect.topleft = round(x), round(y)
这篇关于Pygame 不允许我为 rect.move 使用浮动,但我需要它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!