动画计时问题

时间:2016-06-16 03:48:30

标签: python pygame

问题

我有两张图片,我希望每半秒显示一次。 我用计时器做这个,但每当图像弹出它看起来真的很奇怪,因为看起来第一个图像显示然后消失所以你看到背景,然后另一个图像快速显示并消失并显示背景。

代码

#Importing files/modules
import pygame
import random

#Colors

BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
ORANGe = (255, 115, 0)
YELLOW = (242, 255, 0)
BROWN = (115, 87, 39)
PURPLE = ( 298, 0, 246)
GRAY = ( 168, 168, 168)
BLUE = ( 0, 0, 255)
GROUND = (156,168,91)
pygame.init()

#Screen
screenx = 1000
screeny = 700
screentotal = [screenx,screeny]
screen = pygame.display.set_mode(screentotal)

#Caption
pygame.display.set_caption("Animation attempt one")

#Variables
x = 500
y = 1000
a = 0
b = 500

#Booleans
go = True
#Graphics
one = pygame.image.load("1.jpg").convert()
two = pygame.image.load("2.jpg").convert()
one = pygame.transform.scale(one,(200,200))
two = pygame.transform.scale(two,(200,200))
#Position
position = [0,0]
#Time management
clock = pygame.time.Clock()
FPS = 60
animation_timer = pygame.time.Clock()
animation_time = 0

#MAIN LOOP _________________________________________________________

done = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    screen.fill(GREEN)
    if go:
        animation_timer.tick()
        animation_time += animation_timer.get_time()
        if animation_time < x and  animation_time > a:
            screen.blit(one,position)
            x += 1000
            a += 1000
        if animation_time < y and animation_time> b:
            screen.blit(two,position)
            y += 1000
            b += 1000






    #Flip the display
    pygame.display.flip()
    clock.tick(FPS)
#Quit
pygame.quit()

我尝试了什么

我尝试通过每1秒更换一次动画而不是半秒来减慢动画速度。这使得它看起来更糟,因为你只看了很短的时间图像,你在第二张图像之前看到的背景更长。我试图降低帧速率,但无济于事。感谢您提供的任何帮助:)

1 个答案:

答案 0 :(得分:2)

您需要在每次动画更新之前添加screen.fill,并且不要在每次循环运行时填充屏幕。如果您这样做,您将在它们出现后立即覆盖已加载的图像。