Pygame键盘输入事件滞后

时间:2014-01-30 08:00:53

标签: python animation pygame lag keyboard-input

在用 pygame 编写一个小程序后,我遇到了一个问题。该程序采用拆分.GIF动画并通过以下方式加载图像(gif的帧):

pygame.image.load(filename)

这将返回一个pygame曲面对象,然后将其附加到数组中。该程序将 15帧加载到一个数组中,总共使用 6个数组

我遇到的问题是通过我的 while循环接受输入。循环播放其空闲动画并运行动画,然而,接受来自键盘的输入(从pygame的事件列表中获取输入...)

for event in pygame.event.get():通过pygame.KEYDOWN

存在非常明显的延迟,导致无响应的动画集切换。如果我要使用这种方法制作游戏,则必须修复它。我确定我的代码效率低下,但是不应该创建暂停。任何帮助都会很棒。

我的猜测? pygame.clock.tick()正在创建某种事件延迟,但如果事件滞后甚至是这种情况,我不确定如何解决这个问题。

这是我怀疑存在问题的循环:

while running == 2:
pygame.display.flip()
#mouse = pygame.mouse.get_pos()
#events = pygame.event.get()
#(pygame.QUIT, pygame.KEYDOWN, pygame.KEYUP)
for event in pygame.event.get():
#event = pygame.event.wait()
    if event.type == pygame.QUIT:
        sys.exit(0)
    elif event.type == pygame.KEYDOWN:
        print event.key
        wait = 0
        if event.key == pygame.K_d:
            tmpcache = wr
            lastkey = "wr"
        elif event.key == pygame.K_a:
            tmpcache = wl
            lastkey = "wl"
    elif event.type == pygame.KEYUP:
        wait = 1
        if lastkey == "wr":
            tmpcache = sr
        elif lastkey == "wl":
            tmpcache = sl

if wait == 1:           
    for frame in tmpcache:
        screen.blit(test, (0,0))
        screen.blit(frame, (currentchar.posx, currentchar.posy))
        pygame.display.flip()
        clock.tick(charfps)

else:
    for frame in tmpcache:
        screen.blit(test, (0,0))
        screen.blit(frame, (currentchar.posx, currentchar.posy))
        pygame.display.flip()
        clock.tick(charfps)

此处未显示的一些变量,但已使用:

charfps = 30
currentchar.posx, currentchar.posy 元组都设置为(300, 240)

1 个答案:

答案 0 :(得分:3)

您的问题是您在主循环中创建子循环:

while running == 2:
    pygame.display.flip()
    for event in pygame.event.get():
        ...
    for frame in tmpcache:
        screen.blit(test, (0,0))
        screen.blit(frame, (currentchar.posx, currentchar.posy))
        pygame.display.flip()
        clock.tick(charfps)

因此,如果tmpcache中有15个元素,那么您每帧15次调用clock.tick() ,而代码在此子循环内运行,则不会继续活动。

每帧只需调用pygame.display.flip()clock.tick(charfps) 一次即可解决您的问题。

这是一个简单的例子,每秒三次更改动画图像,同时以60 FPS运行:

import pygame
from collections import deque

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

# just some colored squares for our animation
def get_cache(colors):
    tmp=[]
    for c in colors:
        s = pygame.surface.Surface((50,50))
        s.fill(pygame.color.Color(c))
        tmp.append(s)
    return tmp

walk_left, walk_right = get_cache(('red', 'yellow', 'blue')), get_cache(('black', 'white', 'grey'))

rect = walk_left[0].get_rect(top=100, right=100)
cachedeque = deque(walk_left)
state = None
quit = False

# a simple variable to keep track of time
timer = 0

# a dict of {key: (animation, direction)}
moves = {pygame.K_LEFT:  (walk_left,  (-2, 0)),
         pygame.K_RIGHT: (walk_right, ( 2, 0))}

while not quit:
    quit = pygame.event.get(pygame.QUIT)
    pygame.event.poll()

    # state of the keys
    keys = pygame.key.get_pressed()

    # filter for the keys we're interessted in
    pressed = ((key, _) for (key, _) in moves.iteritems() if keys[key])
    key, (cache, dir) = next(pressed, (None, (None, None)))

    # if a key of the 'moves' dict is pressed:
    if key:
        # if we change the direction, we need another animation
        if state != key: 
            cachedeque = deque(cache)
            state = key
        # move the square                
        rect.move_ip(dir) 
    else:
        state = None

    screen.fill(pygame.color.Color('green'))

    # display first image in cachedeque    
    screen.blit(cachedeque[0], rect)

    # rotate cachedeque to the left, so the second image becomes the first
    # do this three times a second:
    if state and timer >= 1000./3:
        cachedeque.rotate(-1)
        timer = 0

    # call flip() and tick() only once per frame
    pygame.display.flip()

    # keep track of how long it took to draw this frame
    timer += clock.tick(60)

enter image description here