简体   繁体   English

pygame.Rect 对象一直卡在 top = 0 并且在使用 .move 或 .move_ip 时不移动

[英]pygame.Rect objects keep getting stuck at top = 0 and don't move when .move or .move_ip is used

This is a small part of some more complicated code but basically.这是一些更复杂的代码的一小部分,但基本上。 I've narrowed down the problem to this one bit of code.我已经将问题缩小到这一点代码。

I'm using the formula s = ut + 1/2 at^2 to get the distance traveled in one instance to time, in this case, 0.001 seconds which will then further be used to draw the rects on a window.我正在使用公式 s = ut + 1/2 at^2 来计算一个实例到时间的行进距离,在本例中为 0.001 秒,然后将进一步用于在窗口上绘制矩形。

import pygame


run = True
clock = pygame.time.Clock()

ex_s = pygame.Rect((500, 625, 50, 50))
start_height = ex_s.top
print(start_height, "m")

g = -9.8
v = 400
t = 0.001
while run:

    ex_s.top -= (v * t) + ((g / 2) * (t ** 2))

    print(ex_s.top - start_height)

    if (ex_s.top - start_height) * -1 <= -10:
        run = False
    clock.tick(30)

I want the rectangles to move upwards/downwards based on their velocity.我希望矩形根据它们的速度向上/向下移动。

The rectangles move up and get stuck whenever the top reaches y = 0. This doesn't happen if the value of "t" is increased to o.o1 or above but I need it to be 0.001 for the precision.矩形向上移动并在顶部到达 y = 0 时卡住。如果“t”的值增加到 o.o1 或更高,则不会发生这种情况,但我需要它为 0.001 以提高精度。 The problem is also solved when the value of v is increased by an order of magnitude, changing g does nothing.当 v 的值增加一个数量级时,问题也解决了,改变 g 没有任何作用。

I tried using both move and move_ip and the y coordinate did not change at all.我尝试同时使用 move 和 move_ip,y 坐标根本没有改变。

I've also tried manually pushing the objects above y = 0 when they hit it but it doesn't do anything.我也尝试过手动将对象推到 y = 0 以上,但它没有做任何事情。 The fact that the problem changes with decimal places has led me to believe it could be something to do with the integer coordinate system of pygame but I don't see how this would cause things to move first and then get stuck only at 0.问题随着小数位的变化而变化,这让我相信这可能与 pygame 的整数坐标系有关,但我不明白这将如何导致物体先移动然后卡在 0 处。

See Pygame doesn't let me use float for rect.move, but I need it .请参阅Pygame 不允许我将 float 用于 rect.move,但我需要它 The problem is that a pygame.Rect object can only store integer data.问题是pygame.Rect对象只能存储整数数据。 So when you add a value less than 1.0 to the coordinate of a rectangle, than the position stays the same.因此,当您将小于 1.0 的值添加到矩形的坐标时,位置将保持不变。 You need to store the position of the rectangle in separate variables and update the position of the rectangle using these variables.您需要将矩形的位置存储在单独的变量中,并使用这些变量更新矩形的位置。

ex_s = pygame.Rect((500, 625, 50, 50))
ex_s_y = ex_s.top

# [...]

while run:
    # [...]

    # change position
    ex_s_y = .....

    ex_s.top = round(ex_s_y)

You didn't change the time t variable, and your formula for the motion is wrong.你没有改变时间t变量,你的运动公式是错误的。 Also your initial time, initial velocity and g are wrong.您的初始时间、初始速度和g也是错误的。

If you want to use an exact solution for the position, do like this.如果您想对位置使用精确解,请这样做。

dt = 0.05
t = 0
g = 9.8 # It's positive by definition.
v = -100 # It's upward.
while run:
    y = 625 + v*t + 0.5*g*t**2 # The acceleration is +g, downward.
    t += dt
    ex_s.top = y
    ...

If you want to solve the equation of motion approximately and iteratively, do like this inside the loop.如果你想近似地和迭代地求解运动方程,在循环内这样做。

...
y = 625
while run:
    v += g * dt
    y += v * dt
    t += dt
    ex_s.top = y
    ...

The above method is called sympletic Euler method.上述方法称为辛欧拉法。 There're other more accurate methods, such as Runge-Kutta method .还有其他更准确的方法,例如Runge-Kutta 方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM