2017-09-06 4 views
2

J'ai une fenêtre pygame (par exemple mainWindow),
est-il possible d'avoir un terminal dans le jeu dans lequel l'utilisateur peut donner une entrée textuelle pour le programme?
Par exemple il y a un cercle en coordonnées x = 100 y = 300 et lorsque le type d'utilisateur[Python 2.7] Shell d'entrée dans la fenêtre utilisant pygame?

Left 

déplacer le cercle gauche va x = 150 y = 300.
Je sais déjà qu'il est possible pour utiliser pygame.event.get() pour gérer les entrées au clavier, mais j'ai besoin d'une application commandée par une ligne de commande.
Mais le terminal devrait être intégré à mainWindow. Est-ce possible? Si oui, comment? P.: J'utilise la version Linux de pygame pour python 2.7 avant que quelqu'un ne demande, et désolé pour le mauvais anglais.

+0

Si vous avez juste besoin d'une zone de saisie de texte, je viens juste de poster un exemple [ici] (https://stackoverflow.com/a/46133578/6220679). Cela vous permet également d'ajouter des majuscules. – skrx

+0

Je vais jeter un coup d'oeil, merci !!! –

Répondre

2

Ici, vous avez besoin pygame.key, here est la documentation

Et je fait une démonstration pour vous, en plus pygame.key semble pas soutenir casquettes, alors que la « gauche » que vous vouliez, ici ne peut montrer que la « gauche '

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() 
+0

C'est exactement ce que j'essayais de faire ... Merci! –