本文介绍了Pygame 密钥只运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def walk():
    x = 0
    y = 0
    dist = 5
    frame = 0
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            key = pygame.key.get_pressed()
            if key[pygame.K_UP]:
                x = x - 5

这是使我的精灵移动的一些代码的一部分.但是它试图让它在我由于某种原因按下按钮时打印它,它只打印一次yay",直到我再次按下该键.有人请帮忙.

This is part of some code to make my sprite move. But it tried to get it to print it while i was pressing the button for some reason it only print "yay" once until i press the key again. Someone please help.

推荐答案

您必须删除 key = pygame.key.get_pressed(): 行和下面的两行.它们在事件队列中的每个事件只执行一次,因为它们处于事件循环中(for event in pygame.event.get():),但它们应该在 while 中执行 循环每一帧.

You have to dedent the line key = pygame.key.get_pressed(): and the two lines below. They are executed only once per event in the event queue because they are in the event loop (for event in pygame.event.get():), but they should be executed in the while loop every frame.

def walk():
    x = 0
    y = 0
    dist = 5
    frame = 0
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        key = pygame.key.get_pressed()
        if key[pygame.K_UP]:
            x = x - 5

这篇关于Pygame 密钥只运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 05:28