2

一个月前我开始使用 Pygame,我的第一款游戏在高分表和玩家姓名输入框中运行良好。我的问题是当玩家输入他们的名字时让 shift 键工作。我没有弄乱我的游戏,而是创建了一个小型测试程序。每当我按下任何修饰键或键盘时,我都会收到错误“文件“C:\PythonProjects\NameBox\namebox.py”,第 61 行,询问 current_string += chr(inkey) ValueError: chr() arg not in range( 256)"

任何人都可以帮我完成这项工作。我已经尝试了很多我在网上找到的建议,但无法让它们中的任何一个起作用。也许有更好的方法来做到这一点,但至少这给了我一个名字(所有都是小写),并将它与分数和日期一起输入到正确位置的高分表中。

这是我的测试程序。希望我正确输入了代码。

import pygame, sys, os
os.environ['SDL_VIDEO_CENTERED'] = '1'  # centers Pygame SCREEN on desktop

from pygame.locals import *
pygame.font.init()

SCREENWIDTH = 640
SCREENHEIGHT = 480 
SCREEN = pygame.display.set_mode((SCREENWIDTH+9, SCREENHEIGHT+9))
BASICFONT = pygame.font.Font('freesansbold.ttf', 18, bold = False, italic=False)
WHITE     = (255, 255, 255)
BLACK     = (  0,   0,   0)
GREEN     = (  0, 255,   0)
DARKGREEN = (  0, 128,   0)

def main():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
            elif event.type == KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    terminate()
                elif event.key == pygame.K_n:
                    getname()

        spaceSurf = BASICFONT.render('Press n to enter name', False, WHITE)
        spaceRect = spaceSurf.get_rect()
        spaceRect.midtop = (SCREENWIDTH / 2, SCREENHEIGHT - 30)
        SCREEN.blit(spaceSurf, spaceRect)
        pygame.display.update()

def getname():
    name = ask("Your name")
    SCREEN.fill(BLACK)
    nSurf = BASICFONT.render('Press n to enter name', False, WHITE)
    nRect = nSurf.get_rect()
    nRect.midtop = (SCREENWIDTH / 2, SCREENHEIGHT - 30)
    SCREEN.blit(nSurf, nRect)

    nameSurf = BASICFONT.render('Your name is '+ name, True, WHITE)
    nameRect = nameSurf.get_rect()
    nameRect.midtop = (SCREENWIDTH / 2, SCREENHEIGHT / 2)
    SCREEN.blit(nameSurf, nameRect)

    pygame.display.update()

def ask(question):
    "ask(question) -> answer"
    current_string = ""
    display_box(question + ": " + current_string)
    while 1:
        inkey = get_key()
        if inkey == K_BACKSPACE:
            current_string -= 1
        elif inkey == K_RETURN or inkey == K_KP_ENTER:
            break
        elif inkey == pygame.K_ESCAPE:
            terminate()
        else:
            current_string += chr(inkey)

        display_box(question + ": " + current_string.capitalize())

    return current_string.capitalize() # this is the answer    

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

def display_box(message):
  "Print a message in a box in the middle of the screen"
  SCREEN.fill(BLACK)
  left = (SCREENWIDTH / 2) - 156
  top = (SCREENHEIGHT / 2) -100
  pygame.draw.rect(SCREEN, DARKGREEN, (left, top, 320, 200)) 
  SCREEN.blit(BASICFONT.render("New High Score!", True, GREEN),(left + 90, top + 35)) 
  SCREEN.blit(BASICFONT.render("Press Enter when done.", True, GREEN),
          (left + 51, top + 160)) 

  pygame.draw.rect(SCREEN, BLACK, (left + 39, top + 110, 240, 20))
  pygame.draw.rect(SCREEN, WHITE, (left + 38, top + 108, 244, 24), 1)

  if len(message) != 0:
      SCREEN.blit(BASICFONT.render(message, True, WHITE), (left+42, top + 111))

  pygame.display.update()

def terminate():
    pygame.quit()
    sys.exit()

if __name__ == '__main__':
    main()
4

2 回答 2

4

更新:在仔细阅读了链接的问题文档之后,我想我找到了一种更好的方法来完成你想要的。

KEYDOWN事件有一个附加属性,unicode。引用文档:

pygame.KEYDOWN 事件有一个额外的属性 unicode 和 scancode。unicode 表示单个字符串,它是输入的完全翻译的字符。这考虑了 shift 和 composition 键。scancode 代表平台特定的密钥代码。这可能因键盘而异,但对于像多媒体键这样的奇怪键的键选择很有用。

我建议不要从您的 中返回一个值,而是一个元组get_key,这样您就可以访问这两个值:

if event.type == pygame.KEYDOWN:
    return (event.key, event.unicode)

在您的调用代码中,而不是使用chr(inkey)使用unicode值:

(inkey, unichr) = get_key()
...
else:
    current_string += unichr

这样,正确的字符将被附加到字符串中,所有的小细节(修饰键、死键等)都已经处理好了。

PS 不要使用string.capitalize(),否则它会弄乱你的字符串。引用文档

返回字符串的副本,其第一个字符大写,其余小写。

这意味着任何字母都是您的字符串将始终遵循此模式:如果它是第一个字符,则大写,否则小写。其他字符(没有大小写)将保持不变。一些例子:

>>> "aBcDeF".capitalize()
"Abcdef"
>>> "a123B".capitalize()
"A123b"
>>> "@#$%".capitalize()
"@#$%"
>>> "qq2@3#~`?/|\\".capitalize()
"Qq2@3#~`?/|\\"
于 2012-06-03T02:54:34.960 回答
1

这是我最终的工作解决方案。希望它会帮助别人。

" This demonstrates how to show an input box on screen and get the player name."
" By Barberic http://web.aanet.com.au/~barberic/"
" with thanks for help from mgibsonbr on stackoverflow.com"

import pygame, sys, os
os.environ['SDL_VIDEO_CENTERED'] = '1'  # centers Pygame SCREEN on desktop

from pygame.locals import *
pygame.font.init()

SCREENWIDTH = 640
SCREENHEIGHT = 480 
SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
BASICFONT = pygame.font.Font('freesansbold.ttf', 18, bold = False, italic=False)
WHITE     = (255, 255, 255)
BLACK     = (  0,   0,   0)
GREEN     = (  0, 255,   0)
DARKGREEN = (  0, 128,   0)

def main():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
            elif event.type == KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    terminate()
                elif event.key == pygame.K_n:
                    getname()

        spaceRect = spaceSurf.get_rect()
        spaceRect.midtop = (SCREENWIDTH / 2, SCREENHEIGHT - 30)
        SCREEN.blit(spaceSurf, spaceRect)
        pygame.display.update()

def getname():
    name = ask("Your name")
    SCREEN.fill(BLACK)
    nSurf = BASICFONT.render('Press n to enter name', False, WHITE)
    nRect = nSurf.get_rect()
    nRect.midtop = (SCREENWIDTH / 2, SCREENHEIGHT - 30)
    SCREEN.blit(nSurf, nRect)

    # display the name on screen
    nameSurf = BASICFONT.render('Your name is '+ name, True, WHITE)
    nameRect = nameSurf.get_rect()
    nameRect.midtop = (SCREENWIDTH / 2, SCREENHEIGHT / 2)
    SCREEN.blit(nameSurf, nameRect)

    pygame.display.update()

def ask(question):
    "ask(question) -> answer"
    current_string = ""
    display_box(question + ": " + current_string)
    while 1:
        (inkey, unichr) = get_key()

        if inkey == K_BACKSPACE:  # remove last char
            current_string = current_string[:-1]
        elif inkey == K_RETURN or inkey == K_KP_ENTER:
            break   # break out of the while loop to return current_string
        elif inkey == pygame.K_ESCAPE:
            terminate()
        else:  
           current_string += unichr  # add a new char

        # limit the name length to 12 characters 
        current_string = current_string[:12]
        # show the current name during typing
        display_box(question + ": " + current_string)

    return current_string # this is the answer    

def get_key():
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                return (event.key, event.unicode)

def display_box(message):
  "Print a message in a box in the middle of the screen"
  SCREEN.fill(BLACK)
  left = (SCREENWIDTH / 2) - 156
  top = (SCREENHEIGHT / 2) -100
  pygame.draw.rect(SCREEN, DARKGREEN, (left, top, 320, 200)) 
  SCREEN.blit(BASICFONT.render("New High Score!", True, GREEN),
              (left + 90, top + 35)) 
  SCREEN.blit(BASICFONT.render("Press Enter when done.", True, GREEN),
              (left + 51, top + 160)) 

  pygame.draw.rect(SCREEN, BLACK, (left + 39, top + 110, 240, 20))
  pygame.draw.rect(SCREEN, WHITE, (left + 38, top + 108, 244, 24), 1)

  if len(message) != 0:
    SCREEN.blit(BASICFONT.render(message, True, WHITE), (left+42, top + 111))

  pygame.display.update()

def terminate():
    pygame.quit()
    sys.exit()

if __name__ == '__main__':
    main()
于 2012-06-04T05:47:56.660 回答