python - Pygame 不允许我为 rect.move 使用 float ,但我需要它

标签 python pygame

我最近重新创建了一个版本的 Lunar Lander (你知道,旧的复古游戏)在 Python 3 和 Pygame 中:我的着陆器由于重力沿 y 轴每帧移动 (̀̀̀ rect.move )。\
问题:
在我达到 1 m/s 之前,添加到 rect.move 的 y 值是小于 1 的浮点数:我必须使用 int()将其四舍五入,因为 pygame 不喜欢 float 。
在以前的 Tkinter 版本中,着陆器的 y 坐标是这样的:

0.01
0.02
...
0.765
1.03
1.45
...
在 pygame 中它是
0
0
0
...
1
1
1
2
2
...
这真的很烦人,因为运动不流畅。
有人知道如何解决这个问题吗?比如,输入一个浮点数到 rect.move ?
提前致谢!

最佳答案

pygame.Rect 应该代表屏幕上的一个区域,一个 pygame.Rect对象只能存储整数数据:

The coordinates for Rect objects are all integers. [...]


如果要以浮点精度存储对象位置,则必须将对象的位置分别存储在单独的变量和属性中,并同步 pygame.Rect目的。 round 坐标并将其分配给矩形的位置(例如 .topleft ):
x, y = # floating point coordinates
rect.topleft = round(x), round(y)

关于python - Pygame 不允许我为 rect.move 使用 float ,但我需要它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63468413/

相关文章:

python - Django ORM 优化 - 按计数以及注释过滤器的最后一个条目进行注释

python - 在 Django 中导入 python 模块 : __init__. py 类未加载

python 使用 w 模式

python - Pygame : Is color readable from a class variable in function pygame. 绘制.矩形?

python - Pygame 模块不播放音频

python - 不能改变 python 中的原点..?

python - 可以将变量发送到 CherryPy 中的索引页吗?

python - win.blit()后台pygame时出现滞后

python - USB Controller 在 Pygame 中不起作用

python - 如何使用 PyGame 而不是 GLUT 初始化 OpenGL 上下文