我正在使用Pygame来获得操纵杆事件。我也想从控制台获得用户输入。我不想使用Pygame窗口,因为我正在使用Spyder并不断打印并打印到控制台。输入用户输入后,程序将卡在控制台中。如何离开控制台?
作为测试,我现在输入一个字符,但随后被卡住。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 17 11:09:49 2019
@author: GJW
"""
import sys
import string
import pygame
pygame.init()
def get_input_char():
while True:
char = str.lower(input("Input a letter: "))
if len(char) == 1 and char in string.ascii_letters:
break
else:
print("Character '%s' not valid. Try again.\n" % char)
return char
def interrupt():
pygame.event.pump()
for event in pygame.event.get():
print("PyGameEvent: ",event)
if event.type == pygame.KEYDOWN :
if event.key == pygame.K_SPACE :
print("Space bar pressed down.")
elif event.key == pygame.K_ESCAPE :
print("Escape key pressed down.")
elif event.type == pygame.KEYUP :
if event.key == pygame.K_SPACE :
print("Space bar released.")
mychar = get_input_char()
print("mychar = ",mychar)
elif event.key == pygame.K_ESCAPE :
print("Escape key released.")
sys.exit()
while True:
interrupt()