在pygame中在屏幕上显示输入的数字

时间:2015-10-28 17:09:07

标签: python string numbers pygame

我有这段代码:

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

def display_box(screen, message):
  fontobject = pygame.font.Font(None,40)
  if len(message) != 0:
    screen.blit(fontobject.render(message, 1, (0,255,0)),((screen.get_width() / 2) - 300, (screen.get_height() / 2)-50 ))
  pygame.display.flip()

def ask(screen, question):
  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 int(string.join(current_string,""))

此代码应显示用户在键盘上输入屏幕的内容。虽然它适用于字母,但退格不适用于数字。我认为它与

有关
string.join(current_string,"")

in

def ask(screen, question)

,但我不知道如何解决这个问题。

请帮忙

1 个答案:

答案 0 :(得分:1)

I don't see where you actually clean up the screen. There should be some form of fill() command to set the screen to a background color before blitting the newest message to it. Otherwise, you're blitting a message with one fewer character to the screen exactly on top of the last message.