2016-09-20 7 views
0

私は最初の試合を進行中で、ユーザーに情報(名前など)を入力するように求めます。私は、私が使用しようとしている以下の関数を書いて、最後の3.5時間を過ごしました(私のゲームでは、アンダースコアカーソルを少し点滅させて少し修正しました)。pygameのユーザーキーボード入力、CAPS入力の取得方法

私のコードが現在書かれているので、私はそのような文字を許可していても、ユーザーからCAPS入力を受け取ることができません。どうすればいい?

他の提案も歓迎します。

コード:以下

import pygame 
from pygame.locals import * 
import sys 

def enter_text(max_length, lower = False, upper = False, title = False): 
    """ 
    returns user name input of max length "max length and with optional 
    string operation performed 
    """ 
    BLUE = (0,0,255) 
    pressed = "" 
    finished = False 
    # create list of allowed characters by converting ascii values 
    # numbers 1-9, letters a-z(lower/upper) 
    allowed_chars = [chr(i) for i in range(97, 123)] +\ 
        [chr(i) for i in range(48,58)] +\ 
        [chr(i) for i in range(65,90)] 


    while not finished: 
     screen.fill((0,0,0)) 
     pygame.draw.rect(screen, BLUE, (125,175,150,50)) 
     print_text(font, 125, 150, "Enter Name:") 

     for event in pygame.event.get(): 
      if event.type == QUIT: 
       pygame.quit() 
       sys.exit() 
      # if input is in list of allowed characters, add to variable 
      elif event.type == KEYUP and pygame.key.name(event.key) in \ 
       allowed_chars and len(pressed) < max_length: 
       pressed += pygame.key.name(event.key) 
      # otherwise, only the following are valid inputs 
      elif event.type == KEYUP: 
       if event.key == K_BACKSPACE: 
        pressed = pressed[:-1] 
       elif event.key == K_SPACE: 
        pressed += " " 
       elif event.key == K_RETURN: 
        finished = True 


     print_text(font, 130, 180, pressed) 
     pygame.display.update() 

    # perform any selected string operations 
    if lower: pressed = pressed.lower() 
    if upper: pressed = pressed.upper() 
    if title: pressed = pressed.title() 
    return pressed 


def print_text(font, x, y, text, color = (255,255,255)): 
    """Draws a text image to display surface""" 
    text_image = font.render(text, True, color) 
    screen.blit(text_image, (x,y)) 

pygame.init() 
screen = pygame.display.set_mode((400,400)) 
font = pygame.font.SysFont(None, 25) 
fpsclock = pygame.time.Clock() 
fps = 30 

BLUE = (0,0,255) 



# name entered? 
name = False 

while True: 
    fpsclock.tick(fps) 
    pressed = None 
    for event in pygame.event.get(): 
     if event.type == KEYUP: 
      print(pygame.key.name(event.key)) 
      print(ord(pygame.key.name(event.key))) 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 

    # key polling 
    keys = pygame.key.get_pressed() 

    screen.fill((0,0,0)) 

    if not name: 
     name = enter_text(4, title = True) 

    print_text(font, 130, 180, name) 
    pygame.display.update() 
+0

ちょうどあなたのプログラムは、キャップロックのための1つを取得し、あなたが 'allowed_chars' –

+0

が既に行わに追加することができ起動し、' allowed_chars'状態で、そしてからkeyup印刷上の 'event.key'を削除、http://stackoverflow.com/questions/23475858/how-to-allow-caps-in-this-input-box-program-for-pygame –

+0

ああ、ありがとう。私はその男が引用した元の投稿を見つけましたが、それはありません。 –

答えて

0

がため仕出し大文字入力に再加工コードです。私はまた、点滅するアンダースコアを入れました。それが最も効率的な方法であるかどうか不明ですが、そこにはあります。私はそれをやって楽しくした。

import pygame 
from pygame.locals import * 
import sys 
from itertools import cycle 

def enter_text(max_length, lower = False, upper = False, title = False): 
    """ 
    returns user name input of max length "max length and with optional 
    string operation performed 
    """ 
    BLUE = (0,0,255) 
    pressed = "" 
    finished = False 
    # create list of allowed characters using ascii values 
    # numbers 1-9, letters a-z 
    allowed_values = [i for i in range(97, 123)] +\ 
        [i for i in range(48,58)] 

    # create blinking underscore 
    BLINK_EVENT = pygame.USEREVENT + 0 
    pygame.time.set_timer(BLINK_EVENT, 800) 
    blinky = cycle(["_", " "]) 
    next_blink = next(blinky) 

    while not finished: 
     screen.fill((0,0,0)) 
     pygame.draw.rect(screen, BLUE, (125,175,150,50)) 
     print_text(font, 125, 150, "Enter Name:") 

     for event in pygame.event.get(): 
      if event.type == QUIT: 
       pygame.quit() 
       sys.exit() 
      if event.type == BLINK_EVENT: 
       next_blink = next(blinky) 
      # if input is in list of allowed characters, add to variable 
      elif event.type == KEYUP and event.key in allowed_values \ 
       and len(pressed) < max_length: 
       # caps entry? 
       if pygame.key.get_mods() & KMOD_SHIFT or pygame.key.get_mods()\ 
        & KMOD_CAPS: 
        pressed += chr(event.key).upper() 
       # lowercase entry 
       else: 
        pressed += chr(event.key) 
      # otherwise, only the following are valid inputs 
      elif event.type == KEYUP: 
       if event.key == K_BACKSPACE: 
        pressed = pressed[:-1] 
       elif event.key == K_SPACE: 
        pressed += " " 
       elif event.key == K_RETURN: 
        finished = True 
     # only draw underscore if input is not at max character length 
     if len(pressed) < max_length: 
      print_text(font, 130, 180, pressed + next_blink) 
     else: 
      print_text(font, 130, 180, pressed) 
     pygame.display.update() 

    # perform any selected string operations 
    if lower: pressed = pressed.lower() 
    if upper: pressed = pressed.upper() 
    if title: pressed = pressed.title() 

    return pressed 


def print_text(font, x, y, text, color = (255,255,255)): 
    """Draws a text image to display surface""" 
    text_image = font.render(text, True, color) 
    screen.blit(text_image, (x,y)) 

pygame.init() 
screen = pygame.display.set_mode((400,400)) 
font = pygame.font.SysFont(None, 25) 
fpsclock = pygame.time.Clock() 
fps = 30 
BLUE = (0,0,255) 
# name entered? 
name = False 

while True: 
    fpsclock.tick(fps) 
    pressed = None 
    for event in pygame.event.get(): 
     if event.type == KEYUP: 
      print(pygame.key.name(event.key)) 
      print(ord(pygame.key.name(event.key))) 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 

    # key polling 
    keys = pygame.key.get_pressed() 
    screen.fill((0,0,0)) 

    if not name: 
     name = enter_text(10) 

    print_text(font, 130, 180, name) 
    pygame.display.update() 
関連する問題