我有一个 pygame 窗口(例如 mainWindow),
是否可以有一个游戏内终端,用户可以在其中为程序提供文本输入?
例如,在坐标 x = 100 y = 300 处有一个圆,当用户键入
Left
圆圈向左移动到 x = 150 y = 300。
我已经知道可以使用 pygame.event.get() 来处理键盘输入,但我需要一个命令行控制的应用程序。
但终端应该与 mainWindow 集成。 是否可以?如果是,如何?
P.s:在有人询问之前,我使用的是适用于 python 2.7 的 pygame 的 Linux 版本,很抱歉英语不好。
请您参考如下方法:
这里需要pygame.key, here是文档
我给你做了一个demo,另外pygame.key好像不支持大写,所以你要的'Left',这里只能显示'left'
import pygame
from pygame.locals import *
from sys import exit
#initializing variables
pygame.init()
screen=pygame.display.set_mode((640,480),0,24)
FONT=pygame.font.SysFont("comicsansms",24)
# 26 letters, pygame_key not support for caps
LETTERS = [chr(i) for i in range(97,123)]
#text input in terminal
text = ""
start_input = False
#just use color for show if the input start or end
terminal_color = (240,240,240)
circle_position = [100,300]
def on_event(event):
global text, terminal_color, start_input,circle_position
if event.type==QUIT:
exit()
elif event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
if mouse_in_terminal_window(pos):
# input start
start_input = True
terminal_color = (0,0,0)
if start_input and pygame.key.get_focused():
press=pygame.key.get_pressed()
for i in xrange(0,len(press)):
if press[i]==1:
name=pygame.key.name(i)
if name == 'return':
if text == "left":
#change the circle postion from (100, 300) to (150, 300)
circle_position[0] += 50
#input end
start_input = False
terminal_color = (240,240,240)
text= ""
elif name in LETTERS:
text += name
elif name == 'backspace':
text = text[:-1] if len(text)>0 else text
def on_render():
screen.fill((255,255,255))
render_terminal()
render_circle()
pygame.display.update()
def render_terminal():
pygame.draw.rect(screen, terminal_color,(0,50,640,50),0)
t=FONT.render(text,True,(255,255,255))
screen.blit(t,(10,60))
def render_circle():
pygame.draw.circle(screen, (0,0,0), circle_position, 20, 0)
def mouse_in_terminal_window(pos):
if 0<=pos[0]<=640 and 50<=pos[1]<=100:
return True
return False
while True:
for event in pygame.event.get():
on_event(event)
on_render()
