这是我的代码:
import pygame, pygame.font, pygame.event, pygame.draw, string
from pygame.locals import *
counter = 0
def get_key():
global counter
while 1:
event = pygame.event.poll()
if event.type == KEYDOWN:
return event.key
else:
pass
def display_box(screen, message):
global counter
"Print a message in a box in the middle of the screen"
fontobject = pygame.font.Font(None, 18)
pygame.draw.rect(screen, (255, 255, 255),
((screen.get_width() / 2) - 100,
(screen.get_height() / 2) - 10,
200, 20), 1)
pygame.draw.rect(screen, (0, 0, 0),
((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):
global counter
str_counter = str(counter)
"ask(screen, question) -> answer"
pygame.font.init()
current_string = []
display_box(screen, "comment" + ": " + 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_SPACE:
screen = screen + str_counter
counter = counter + 1
elif inkey <= 127:
current_string.append(chr(inkey))
display_box(screen, "comment" + ": " + string.join(current_string, ""))
return string.join(current_string, "")
def main():
global counter
screen = pygame.display.set_mode((300, 100))
print ask(screen)
if __name__ == '__main__': main()
答案 0 :(得分:1)
您收到的错误是第49行导致您正在执行操作screen = screen + str_counter,它正在尝试添加一个pygame.Surface对象和一个无法一起添加的字符串对象。
此外,pygames不是经常构建的,以便能够同时处理多个窗口的运行。如果你想在pygames中运行多个窗口,你需要使用多处理或其他可以运行多个窗口的模块,例如pyglet或cocos2d。