我正在 pygame 中制作一个简单的输入框,您应该可以在其中输入任何内容,按退格键,然后按回车键添加换行符。我似乎无法换行,因为我不知道怎么做。我希望能够在按回车键时添加一个换行符并将其他文本保留在它上面。
我试过使用 text = text + "/n" 但没有用。
这是我的代码:
import pygame
pygame.init()
winheight = 600
winwidth = 1200
font = pygame.font.Font(None, 32)
input_box = pygame.Rect(50, 50, winwidth - 100, winheight - 100)
blue = (0, 0, 255)
Text = ''
Writing = True
win = pygame.display.set_mode((winwidth, winheight))
pygame.display.set_caption("Clogging Cuesheet Writer")
while Writing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Writing = False
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
print ("replace this with the command to make Text plus a newline")
elif event.key == pygame.K_BACKSPACE:
Text = Text[:-1]
else:
Text += event.unicode
#clear screen, draw text, draw inputbox, then update
win.fill((0, 0, 0))
txt_surface = font.render(Text, True, blue)
win.blit(txt_surface, (input_box.x+15, input_box.y+15))
pygame.draw.rect(win, blue, input_box, 2)
pygame.display.update()
pygame.quit()
请帮忙
请您参考如下方法:
那是因为你的“/n”有错别字。你真正想要的是“\n”:
if event.key == pygame.K_RETURN:
print (text = text + '\n')
