【问题标题】:How can I 'tween' my graphics? (Python)我怎样才能“补间”我的图形? (Python)
【发布时间】:2014-07-21 09:22:06
【问题描述】:
# 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 下,动画也非常快,所以这是我能想到的最好的办法,将其减慢到合理的速度。

【问题讨论】:

    标签: python python-2.7 pygame


    【解决方案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
    

    您正在寻找的平滑度将来自高稳定帧率

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 2023-01-28
      • 1970-01-01
      相关资源
      最近更新 更多