ユーザーが画面に入力したテキストをblitします。ユーザーがReturnキーを押すたびに、入力されたテキストを画面に合わせる必要があります。テキスト入力の場合は、この[text_inputモジュール](https://github.com/Nearoo/pygame-text-input)を使用します。ここで画面へのユーザーテキストの入力を
は、私がこれまでに思い付いたコードは次のとおりです。
import pygame_textinput
import pygame
pygame.init()
# Set some parameters
duration = 5.0
time = pygame.time.get_ticks()/1000
screen = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
yoffset = 5
# Function that positions user input rects on screen
def renderInput(text, xoffset, yoffset):
font = pygame.font.SysFont("arial", 20)
renderText = font.render(text, False, (0, 0, 0))
rectText = renderText.get_rect()
rectText = rectText.move((0 + xoffset), (screen.get_height()/2 + yoffset))
return renderText, rectText
# Fills the screen once at the beginning
screen.fill((225, 225, 225))
while (pygame.time.get_ticks()/1000) < time + duration:
# creat new text input object on every trial
textinput = pygame_textinput.TextInput()
while True:
# Fills the surface after each keypress
screen.fill((225, 225, 225))
# Check events
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
# Feed with events every frame
# This evaluates to True once Return is pressed
if textinput.update(events):
userInput = textinput.get_text()
yoffset += 20
break
# Blit surface onto the screen
screen.blit(textinput.get_surface(), (10, 10))
# Update screen
pygame.display.update()
clock.tick(30)
# Blits user input to screen each time "Return" is pressed
# First get input text and the rectangle of the text
text, textrect = renderInput(userInput, 5, yoffset)
# Then blit it to the screen
screen.blit(text, textrect)
pygame.display.update()
私の問題は、私は、入力を処理するwhileループ内の各キーの押下後に画面を埋めていない場合はブリッティングにのみ動作すること、です。これを行うと、テキスト入力は、ユーザーがReturnキーを押すたびにクリアされません。
キーを押すたびに再描画し、Returnキーを押すたびにテキストを下に表示する方法があります。
ありがとうございました。
ありがとうございました。ユーザーがReturnを押すと新しいインスタンスを作成することは、それを解決するための素晴らしい方法です。しかし、私は十分に私の質問を特定しなかったと思う。私が最初のwhileループとスクリーンにレンダリングする関数を持つ理由は次のとおりです:ユーザ入力を(実装したように)下に表示したいが、ユーザが別のタイプを入力してReturnキーを押すと、その入力はすでに表示されている入力の下に表示されるべきです。このプロセスは、タイマーがなくなるまで続けられます(最初のwhileループ)。 – Ivan
その場合、私はおそらくテキストの表面をレンダリングし、それをリストに追加します。次に、このサーフェスのリストを反復して、メインループのオフセットを使ってblitします。 – skrx