【问题标题】:Pygame keyboard input event lagPygame 键盘输入事件延迟
【发布时间】:2014-01-31 16:21:57
【问题描述】:

我在用 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)

【问题讨论】:

  • 这个问题似乎是题外话,因为它属于gamedev.stackexchange.com
  • 在游戏开发方面会更好,但仍然是关于编程。

标签: python animation pygame lag keyboard-input


【解决方案1】:

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

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 个元素,您将调用clock.tick() 15 次每帧,并且当代码在此子循环中运行时,您不会继续事件。

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

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

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)

【讨论】:

  • 我明白你在说什么,更新显示并每帧只打​​一次时钟。然而,你给我的根本不起作用。当按下相应的键时,它只会给我动画中的第二帧。我希望动画在循环中以 30 fps 的角色行走(总共 15 帧左右)播放,具体取决于是否按住键而不实际影响输入时间(在等待动画来完成它的循环)。
  • 这绝对帮助我理解了哪里去了,我以前从未使用过双端队列,我相信这将有助于我在未来的编程项目中。我用您向我展示的相同技术更新了我的代码,现在它工作正常,谢谢!
  • 我知道这还不到十年,但我登录只是为了支持您的评论
猜你喜欢
  • 2012-12-27
  • 2011-02-08
  • 1970-01-01
  • 2014-05-03
  • 2015-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多