如何在图形中实现渐变效果?(Python)
# 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 个回答
2
这里有几个小建议:
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
你想要的流畅感会来自于一个高且稳定的帧率