无法让pygame键盘输入工作 [英] Can't get pygame keyboard input to work

查看:78
本文介绍了无法让pygame键盘输入工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始为一个需要键盘输入的项目学习python.从我所读到的,pygame 是做到这一点的最佳方式.不幸的是,我一直无法让 KEYDOWN 工作.pygame.event.get() 返回的唯一事件是 MOUSEMOTION.我需要的代码是这样的:

I am just starting to learn python for a project, which requires keyboard input. From what I've read, pygame is the best way to do this. Unfortunately I've been unable to get KEYDOWN to work. The only event pygame.event.get() returns is MOUSEMOTION. The code I need would go something like this:

import pygame
def main():
    pygame.init()
    while True:
        for event in pygame.event.get()
            if(event.type == pygame.KEYDOWN)
                 if(event.key==pygame.K_LEFT)
                     cry_because_it_worked()

我不确定我做错了什么.我已经阅读了一些关于键盘输入的其他问题,并且所有这些问题都建议使用据我所知是相同的代码.比如这个问题的第一个答案的第一个代码块:如何获取pygame 中的键盘输入?pygame.init() 返回 (6,0),我相信这意味着所有模块都已正确初始化.我正在使用 python 3.4.3 和 pygame 1.9.2.任何帮助将不胜感激.

I'm not sure what I'm doing wrong. I've read a few other questions about keyboard input, and all of them suggest using code that as far as I can tell is identical. For example, the first code block of the first answer to this question: How to get keyboard input in pygame? pygame.init() returns (6,0), which I believe means that all modules were initialized correctly. I'm using python 3.4.3 and pygame 1.9.2. Any help would be greatly appreciated.

这是我代码中的实际循环:

Here is the actual loop in my code:

import pygame
...
def handleKeyInput(enigma):
    pygame.init()
    print("before loop")
    while True:

        for event in pygame.event.get():
            print(event.type) #Always prints 4, which is the value of MOUSEMOTION 
            print(event.type == pygame.KEYDOWN) #Always prints false
            if event.type == pygame.KEYDOWN:
                #and then a bunch of if statements for all letters

推荐答案

使用 pygame.event.get 的正确代码如下所示:

A correct code for using pygame.event.get would look like this:

import pygame
from pygame.locals import *

for event in pygame.event.get():
    if event.type == QUIT: # if closing application
        pygame.quit()
        sys.exit()
    elif event.type == KEYDOWN:
        if event.key == K_LEFT:
            cry_because_it_worked()

Pygame 事件不仅仅是按键.它们也可以是鼠标移动和其他东西.首先检查事件类型很重要.这就是我们提出以下内容的原因:

Pygame events are not only key presses. They can be mouse movements and other stuff as well. It is important to first check the event type first. This is why we put the following:

if event.type == KEYDOWN:

接下来您需要做的是检查正在按下的键.您可以通过读取 event 对象的 key 属性来完成此操作:

The next thing you need to do is to check the key that is being pressed. You do this by reading the key attribute of the event object:

if event.key == K_LEFT:

因此,在您的程序中,您检查 event.type 以确保事件是按键按下,然后您检查 event.key 以查看按下了什么键.

So in your program you check event.type to make sure the event is a key press, then you check event.key to see what key was pressed.

这篇关于无法让pygame键盘输入工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆