2013-01-01 4 views
3

사용자로부터 파이 게임 인풋을 입력하는 방법 :파이 게임에서 사용자로부터 텍스트 입력을받는 방법?

사용자 유형을 가지고 있고 파이 게임이 화면에 프린트하려고합니다.

이 내 현재 프로그램입니다 : 나는 사용자 안타 입력 할 때 그렇게, 그것은 화면을 emptys 원하는

import pygame, pygame.font, pygame.event, pygame.draw, string 
from pygame.locals import * 

def get_key(): 
    while 1: 
    event = pygame.event.poll() 
    if event.type == KEYDOWN: 
     return event.key 
    else: 
     pass 

def display_box(screen, message): 
    "Print a message in a box in the middle of the screen" 
    fontobject = pygame.font.Font(None,18) 
    pygame.draw.rect(screen, (0,0,0), 
        ((screen.get_width()/2) - 100, 
        (screen.get_height()/2) - 10, 
        200,20), 0) 
    pygame.draw.rect(screen, (255,255,255), 
        ((screen.get_width()/2) - 102, 
        (screen.get_height()/2) - 12, 
        204,24), 1) 
    if len(message) != 0: 
    screen.blit(fontobject.render(message, 1, (255,255,255)), 
       ((screen.get_width()/2) - 100, (screen.get_height()/2) - 10)) 
    pygame.display.flip() 

def ask(screen, question): 
    "ask(screen, question) -> answer" 
    pygame.font.init() 
    current_string = [] 
    display_box(screen, question + ": " + string.join(current_string,"")) 
    while 1: 
    inkey = get_key() 
    if inkey == K_BACKSPACE: 
     current_string = current_string[0:-1] 
    elif inkey == K_RETURN: 
     break 
    elif inkey == K_MINUS: 
     current_string.append("_") 
    elif inkey <= 127: 
     current_string.append(chr(inkey)) 
    display_box(screen, question + ": " + string.join(current_string,"")) 
    return string.join(current_string,"") 

def main(): 
    screen = pygame.display.set_mode((320,240)) 
    print ask(screen, "Name") + " was entered" 

if __name__ == '__main__': main() 

.

도와주세요!

+0

입니까? – favoretti

답변

6

다음은 화면에 입력을 블릿 (blits)하는 예제 스크립트입니다. pygame 이벤트 큐를 반복하면서 name 문자열을 수정하는 방법을 보여줍니다. 각 프레임, 화면이 지워지고 이름 표면이 재구성되고 blit됩니다. 여기

import pygame 
from pygame.locals import * 

def name(): 
    pygame.init() 
    screen = pygame.display.set_mode((480, 360)) 
    name = "" 
    font = pygame.font.Font(None, 50) 
    while True: 
     for evt in pygame.event.get(): 
      if evt.type == KEYDOWN: 
       if evt.unicode.isalpha(): 
        name += evt.unicode 
       elif evt.key == K_BACKSPACE: 
        name = name[:-1] 
       elif evt.key == K_RETURN: 
        name = "" 
      elif evt.type == QUIT: 
       return 
     screen.fill((0, 0, 0)) 
     block = font.render(name, True, (255, 255, 255)) 
     rect = block.get_rect() 
     rect.center = screen.get_rect().center 
     screen.blit(block, rect) 
     pygame.display.flip() 

if __name__ == "__main__": 
    name() 
    pygame.quit() 

는 지금까지 시도 것을, 그래서 gist version

+0

입력란이 "이름"상자에 표시되도록 만들 수 있습니까? –

+0

차라리 먼저 시도해 보겠습니다. 이 예제에서는'Font.render' 표면을 입력 위에 "Name"으로 blit하는 방법을 보여주기에 충분합니다. 질문이 있으시면 답변 해 드리겠습니다. – Devourant

+0

사용자가 종료 할 때 오류가 발생하여이 게시물의 스크립트를 편집하고 있습니다. 그 오류가 동일하면 궁금하네요. (파이 게임 위키에서 언급했습니다.) (http://www.pygame.org/wiki/FrequentlyAskedQuestions#In%20IDLE%20why%20does%20the%20Pygame%20window%20not%20close% 20correctly?). 업데이트 된 버전을 사용해보고 문제가 해결되지 않으면 의견을 보내 문제가 무엇인지 파악하십시오. – Devourant

관련 문제