如何使受控图像在Pygame中平稳移动?

发布于 2025-01-31 02:31:13 字数 1512 浏览 3 评论 0原文

我试图用Pygame在Python中制作一款游戏,当我使用箭头键移动图像时,我几乎可以做到这一点,有时会变得生干,它会跳动像素,而不是一次移动一个像素。

如果有人可以帮助我,我真的很感激。

这是我的代码。

import pygame, sys

clock = pygame.time.Clock()

#Variables
isPlaying = True

windowSize = [600, 400]

playerLocation = [50, 50]

movingUp = False
movingDown = False

#Window
window = pygame.display.set_mode(windowSize)
windowCaption = pygame.display.set_caption('Game')

display = pygame.Surface((300, 200))

#Images
playerImage = pygame.image.load('Data/Images/Player.png')

#Rects
playerRect = pygame.Rect(50, 50, playerImage.get_width(), playerImage.get_height())

#MainLoop
while isPlaying == True:
    display.fill((255, 255, 255))

    display.blit(playerImage, playerLocation)

    if movingUp == True:
        playerLocation[1] -= 4
    if movingDown == True:
        playerLocation[1] += 4

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                movingUp = True
            if event.key == pygame.K_DOWN:
                movingDown = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                movingUp = False
            if event.key == pygame.K_DOWN:
                movingDown = False

    surf = pygame.transform.scale(display, windowSize)
    window.blit(surf, (0, 0))

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

顺便说一句,我在覆盆子Pi上。

I am trying to make a game in python with pygame and I can pretty much do it exept when I move the image using the arrow keys it sometimes becomes jerky and it jumps pixels instead of moving one pixel at a time.

I would really appreciate it if someone could help me.

Here is my code.

import pygame, sys

clock = pygame.time.Clock()

#Variables
isPlaying = True

windowSize = [600, 400]

playerLocation = [50, 50]

movingUp = False
movingDown = False

#Window
window = pygame.display.set_mode(windowSize)
windowCaption = pygame.display.set_caption('Game')

display = pygame.Surface((300, 200))

#Images
playerImage = pygame.image.load('Data/Images/Player.png')

#Rects
playerRect = pygame.Rect(50, 50, playerImage.get_width(), playerImage.get_height())

#MainLoop
while isPlaying == True:
    display.fill((255, 255, 255))

    display.blit(playerImage, playerLocation)

    if movingUp == True:
        playerLocation[1] -= 4
    if movingDown == True:
        playerLocation[1] += 4

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                movingUp = True
            if event.key == pygame.K_DOWN:
                movingDown = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                movingUp = False
            if event.key == pygame.K_DOWN:
                movingDown = False

    surf = pygame.transform.scale(display, windowSize)
    window.blit(surf, (0, 0))

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

By the way I'm on raspberry pi.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我很坚强 2025-02-07 02:31:13

您可以尝试将其设置为一次仅移动一个像素。 (看起来您将其设置为4。)

使用时钟。您的PI可能无法运行60 fps,因此降低它可能会有所帮助。

You could try setting it to only move a single pixel at a time. (It looks like you have it set to 4.)

Fiddling with the clock.tick is also an option. Your pi might not be able to run 60 FPS, so lowering it could help.

━╋う一瞬間旳綻放 2025-02-07 02:31:13

您应该考虑使用速度*时间在屏幕上移动图像以进行更平滑的过渡。您的速度为4 +或 - 不管格兰特所说的屏幕上发生了多少重新粉刷。降低FPS可能会看起来光滑,但是您的对象四处走动是在忽略自上次循环以来实际上已经过去了多少时间,因此它永远是生干的。

您要做的是您要做的事情,从而减少了最后一个帧以来的时间或当前帧的时间减去以来的时间。在这种情况下,您只是上下移动,因此单个数字会很好。

数学在毫秒中就是这样。 (当前循环时间= 2000-最后一个循环时间= 1000) /一个秒1000 =以秒 *的速度使用的时间百分比。在这种情况下,您的速度为4,因此您将在刷新之间移动完整的4像素。希望您的帧速率不是那么糟糕,但是如果框架之间有两倍的时间,它应该将您的对象8像素移动。

我对游戏没有多大作用,但是我认为这通常是如何工作的。

You should look into using speed*time for moving images on a screen for smoother transitions. Your speed of 4 will translate 4 + or - regardless of how many repaints occurred on the screen like Grant is saying. Lowering the FPS will probably appear smooth but your object moving around is ignoring how much time has actually passed since the last loop so it will always be jerky.

Getting the DeltaTime or the time of the current frame minus the time since the last frame then multiplying that by a vector is what you want to do. In this case you're only moving up and down so a single number will be fine.

The math would be like this in milliseconds. (Current loop time = 2000 - Last loop time = 1000) / One second 1000 = Percentage of the time that's passed in a second * your speed. In this case your speed is 4 so you'll move a full 4 pixels between refreshes. Hopefully your frame rate isn't that bad but if there is twice as much time between frames it should move your object 8 pixels.

I don't do much with games but, I think that's how it usually works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文