python - 我怎样才能 'tween'我的图形? (Python)

标签 python python-2.7 pygame

# imports
import pygame
import sys
import random

def randomcolor():

    blue = (0, 0, 255)
    red = (255, 0, 0)
    green = (0, 255, 0)
    white = (255, 255, 255)

    selection = random.randrange(1, 5)
    if selection == 1:
        return blue
    elif selection == 2:
        return red
    elif selection == 3:
        return green
    elif selection == 4:
        return white

def start(dx, dy):

    #initialize
    pygame.init()
    size = width, height = 640, 480
    x = width / 2
    y = height / 2
    randc = (0, 0, 255)

    #call window as ROOT
    root = pygame.display.set_mode(size)
    pygame.display.set_caption("Moving Object")

    #main loop
    while 1:
        root.fill(0)

        if x > 640:
            randc = randomcolor()
            dx = -dx
            x += dx
        elif x < 0:
            randc = randomcolor()
            dx = -dx
            x += dx
        else:
            x += dx

        if y > 480:
            randc = randomcolor()
            dy = -dy
            y += dy
        elif y < 0:
            randc = randomcolor()
            dy = -dy
            y += dy
        else:
            y += dy

        pygame.draw.circle(root, randc, (x, y), 20, 0)
        pygame.time.wait(10)
        pygame.display.flip()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit(0)


velx = input("Enter velocity in x: ")
vely = input("Enter velocity in y: ")
start(velx, vely)

由于缺乏更好的词,我基本上希望我的程序能够渲染两个“关键帧”之间的帧,以便我的程序中的动画在更高的速度下看起来更干净。我目前使用 pygame.time 的原因是因为即使在速度 1 下动画也非常快,所以这是我能想到的最好的方法来将其减慢到合理的速度。

最佳答案

所以,有几个提示:

1)使用时钟来控制帧率

 clock = pygame.time.Clock()

然后在循环中调用

clock.tick(fps)

2) 考虑根据更新之间耗时进行移动,以抵消更改帧速率的影响

time = pygame.time.get_ticks() # time in ms
timepassed = lastTime - time

## do distance traveled calculations with speed x timepassed

lastTime = time # set the time of this update so it can be used next loop

您所寻求的流畅度将来自于高稳定的帧率

关于python - 我怎样才能 'tween'我的图形? (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24861601/

相关文章:

pygame - 如何修复边界

python - 如何在一张背景图像上移动玩家?

python - 对应于所有唯一行的所有行的平均值

python - pandas 将字符串的类别转换为数字作为一个对象,但得到一组数字

python - 将总和行添加到数据框中的特定列

Python pygame 崩溃,修复似乎不起作用

python - 为什么我不能通过 admin/将 jpg 文件上传到我的 Django 应用程序?

python - 根据第二个数组的索引对数组进行矢量化和

mysql - Django AutoField 默认值错误

python - 值错误 : invalid literal for long() with base 10: '5B'