如何在 Pygame 中连续平滑地移动图像? [英] How to continuously move an image smoothly in Pygame?

查看:33
本文介绍了如何在 Pygame 中连续平滑地移动图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道那里有类似的问题,我已经通读了它们,但它们对我没有帮助(或者很可能我只是不够了解它们而无法使用它们).

仅供参考:我不使用类或类似的东西.

问题

所以我想做的是在我的游戏中制作安全摄像头.它向左移动/平移图像直到图像到达边缘,等待 1 秒或其他时间,然后向右移动/平移图像直到图像到达边缘,等待 1 秒或其他时间,重复.我已经做到了,但由此产生的动画看起来生涩/滞后.我希望使动画更流畅,更少生涩/滞后.

如果可能的话,我也想保持相同的动画速度

这是我的代码;

如果 var_bounce == 0:如果 var_x <= -207:如果 var_x_timer == 10:var_x = var_x + 10var_bounce = 1var_x_timer = 0别的:var_x_timer = var_x_timer + 1别的:var_x = var_x - 10如果 var_bounce == 1:如果 var_x >= 0:如果 var_x_timer == 10:var_x = var_x - 10var_bounce = 0var_x_timer = 0别的:var_x_timer = var_x_timer + 1别的:var_x = var_x + 10

解释一些变量和值

  • var_x 是我刚放在 x 值位置的 x 值当我使用 blit 函数时.

    例如screen.blit(image, (var_x, 0))

  • var_x_timer 是需要等待 1第二个什么的.

    我使用程序的帧率而不是使用的原因计时器功能,因为 1) 我不想睡觉/延迟/停止程序只是为了这个,2)我不需要它是精确的等待.

  • var_bounce 只是让程序知道它有到了边缘,需要往相反的方向走.

  • 最小和最大 x 值分别为 0-207 并且这是为了使图像仍然覆盖整个屏幕.我得到了这些值通过反复试验,因此无需更改.

其他/附加信息

  • 我的安全摄像头图像尺寸为 1600 x 718

  • 屏幕尺寸为1366 x 718

我的代码实战

(忽略其他内容)

(循环)

解决方案

不要移动"将图像移动 10,但将其移动 1.

方法

导入pygamepygame.init()窗口 = pygame.display.set_mode((400, 300))时钟 = pygame.time.Clock()背景 = pygame.Surface((window.get_width()+200, window.get_height()))ts, w, h, c1, c2 = 100, *background.get_size(), (64, 64, 64), (127, 64, 64)瓷砖 = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) fory 在范围内((h+ts-1)//ts)]对于矩形,瓷砖中的颜色:pygame.draw.rect(背景,颜色,矩形)var_x, var_bounce, speed_x = 0, 0, 2定义滚动():全局 var_x, var_bounce如果 var_bounce == 0:如果 var_x >-200:var_x = var_x - speed_x别的:var_bounce = 1elif var_bounce == 1:如果 var_x <0:var_x = var_x + speed_x别的:var_bounce = 0运行 = 错误不运行时:时钟滴答(60)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 真滚动()window.blit(背景, (var_x, 0))pygame.display.flip()pygame.quit()出口()

Yes, I know that there are similar questions out there, and I have read through them, but they do not help me (or most likely I just don't understand them enough to use them).

FYI: I do not use classes, or anything like that.

The Problem

So what I am trying to do is make security cameras in my game. It moves/pans the image left until the image reaches the edge, waits like 1 second or something, then it moves/pans the image right until the image reaches the edge, wait 1 second or something, repeats. I have accomplished that, but the resulting animation looks jerky/laggy. I wish to make the animation smoother and less jerky/laggy.

EDIT: I also want to keep the same speed of the animation if possible

Here is my code;

if var_bounce == 0:
    if var_x <= -207:
        if var_x_timer == 10:
            var_x = var_x + 10
            var_bounce = 1
            var_x_timer = 0
        else:
            var_x_timer = var_x_timer + 1
    else:
        var_x = var_x - 10
if var_bounce == 1:
    if var_x >= 0:
        if var_x_timer == 10:
            var_x = var_x - 10
            var_bounce = 0
            var_x_timer = 0
        else:
            var_x_timer = var_x_timer + 1
    else:
        var_x = var_x + 10

Some Variables and Values Explained

  • The var_x is the x value which I just place in the x value spot when I use the blit function.

    E.g. screen.blit(image, (var_x, 0))

  • The var_x_timer is the timer for when it needs to wait for the 1 second or something.

    The reason why I am using the framerate of my program rather than use a timer function because 1) I did not want to sleep/delay/stop the program just for this, and 2) I don't need it to be precise when waiting.

  • The var_bounce is just a way for the program to know that it has reached the edge and needs to go in the opposite direction.

  • The minimum and maximum x values are 0 and -207respectively and this is so that the image still covers the entire screen. I got these values through trial and error, so no need to change those.

Other/Additional Information

  • The size of my images for the security cameras is 1600 x 718

  • The screen size is 1366 x 718

My Code in Action

(Ignore the other stuff)

(Looped)

解决方案

Do not "move" the image by 10, but move it by 1.

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

This method should be called once per frame.

That means that the loop:

var_x, var_bounce, speed_x = 0, 0, 2

# application loop
clock = pygame.time.Clock()
run = True
while run:
    clock.tick(60)

    if var_bounce == 0:
        if var_x > -207:
            var_x = var_x - speed_x 
        else:
            var_bounce = 1
    elif var_bounce == 1:
        if var_x < 0:
            var_x = var_x + speed_x 
        else:
            var_bounce = 0

    # [...]


Minimal example:

import pygame

pygame.init()
window = pygame.display.set_mode((400, 300))
clock = pygame.time.Clock()

background = pygame.Surface((window.get_width()+200, window.get_height()))
ts, w, h, c1, c2 = 100, *background.get_size(), (64, 64, 64), (127, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
for rect, color in tiles:
    pygame.draw.rect(background, color, rect)

var_x, var_bounce, speed_x = 0, 0, 2

def scroll():
    global var_x, var_bounce
    if var_bounce == 0:
        if var_x > -200:
            var_x = var_x - speed_x
        else:
            var_bounce = 1
    elif var_bounce == 1:
        if var_x < 0:
            var_x = var_x + speed_x
        else:
            var_bounce = 0

run = False
while not run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = True

    scroll()

    window.blit(background, (var_x, 0))
    pygame.display.flip()

pygame.quit()
exit()

这篇关于如何在 Pygame 中连续平滑地移动图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆