简体   繁体   English

pygame 增量时间导致运动不一致

[英]pygame delta time causes inconsistent movement

I am trying to understand delta time and wrote this simple example:我试图了解增量时间并编写了这个简单的示例:

import pygame, sys, time

pygame.init()
screen = pygame.display.set_mode((1280,720))
clock = pygame.time.Clock()

rect1 = pygame.Rect(0,150,100,100)
rect2 = pygame.Rect(1180,500,100,100)
speed = 300

last_time = time.time()

while True:
    dt = time.time() - last_time
    last_time = time.time()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill('white')
    rect1.x += speed * dt
    rect2.x -= speed * dt

    pygame.draw.rect(screen,'red',rect1)
    pygame.draw.rect(screen,'green',rect2)

    pygame.display.update()
    clock.tick(60)

The problem I have with it is that the left movement is faster than the right movement.我遇到的问题是向左移动比向右移动快。 So in the code snippet the green rectangle (rect2) reaches the end of the screen noticeably faster than the red rectangle.因此,在代码片段中,绿色矩形 (rect2) 到达屏幕末端的速度明显快于红色矩形。

I also tried to use pygame.clock to get delta time:我还尝试使用 pygame.clock 来获取增量时间:

import pygame, sys, time

pygame.init()
screen = pygame.display.set_mode((1280,720))
clock = pygame.time.Clock()

rect1 = pygame.Rect(0,150,100,100)
rect2 = pygame.Rect(1180,500,100,100)
speed = 300

while True:

    dt = clock.tick(60) / 1000

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill('white')
    rect1.x += speed * dt
    rect2.x -= speed * dt

    pygame.draw.rect(screen,'red',rect1)
    pygame.draw.rect(screen,'green',rect2)

    pygame.display.update()

But the result remains the same.但结果还是一样。

I am really confused by this, am I doing something wrong?我真的很困惑,我做错了什么吗?

Edit: Someone closed this and linked to deltatime not working at higher framerates.编辑:有人关闭了它并链接到 deltatime 在更高帧速率下不工作。 The framerate here is a constant 60fps.这里的帧率是恒定的 60fps。

Since pygame.Rect is supposed to represent an area on the screen, a pygame.Rect object can only store integral data.由于pygame.Rect应该代表屏幕上的一个区域,所以pygame.Rect object 只能存储整数数据。

The coordinates for Rect objects are all integers. Rect 对象的坐标都是整数。 [...] [...]

The fraction part of the coordinates gets lost when the new position of the object is assigned to the Rect object. If this is done every frame, the position error will accumulate over time.当 object 的新 position 分配给Rect object 时,坐标的小数部分会丢失。如果每帧都这样做,position 错误将随着时间累积。

If you want to store object positions with floating point accuracy, you have to store the location of the object in separate variables and you have to synchronize the pygame.Rect object. round the coordinate and assign it to the position of the rectangle:如果要以浮点精度存储 object 位置,则必须将 object 的位置存储在单独的变量中,并且必须同步pygame.Rect object. round入坐标并将其分配给矩形的 position:

rect1 = pygame.Rect(0,150,100,100)
rect2 = pygame.Rect(1180,500,100,100)
pos1_x = rect1.x
pos2_x = rect2.x  
while True:
    # [...]

    pos1_x += speed * dt
    pos2_x -= speed * dt
    rect1.x = round(pos1_x)
    rect2.x = round(pos2_x)
 
    # [...]

See also Pygame doesn't let me use float for rect.move, but I need it另见Pygame 不允许我将 float 用于 rect.move,但我需要它

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

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