簡體   English   中英

Pygame 不允許我使用 float 進行 rect.move,但我需要它

[英]Pygame doesn't let me use float for rect.move, but I need it

我最近在 Python 3 和 Pygame 中重新創建了 月球着陸器的一個版本(你知道,舊的復古游戲):由於重力,我的着陸器沿 y 軸移動( rect.move )每一幀。\

問題:
在我達到 1 m/s 之前,添加到 rect.move 的 y 值是一個低於 1 的浮點數:我必須使用int()對其進行四舍五入,因為 pygame 不喜歡浮點數。
在之前的 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 object 只能存儲積分數據:

Rect 對象的坐標都是整數。 [...]

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並將其分配給矩形的位置(例如.topleft ):

x, y = # floating point coordinates
rect.topleft = round(x), round(y)

我自己沒有測試過,所以如果這不起作用,我很抱歉,但我認為 python 將數字作為字符串而不是int(variable)

你應該做float(variable)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM