gpt4 book ai didi

python - Blit 用户文本输入到屏幕

转载 作者:行者123 更新时间:2023-12-01 02:41:18 26 4
gpt4 key购买 nike

我想将用户输入到屏幕上的文本传输到屏幕上。每次用户按回车键时,键入的文本都会被传输到屏幕上。对于文本输入,我使用这个 [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 后显示下面的文本。

谢谢。

最佳答案

如果我理解正确的话,输入字段中的文本应该被清除,并且应该在屏幕的主要区域中进行位图传输。如果用户按 Enter 键,我会将文本分配给 user_input 变量,然后创建一个新的 pygame_textinput.TextInput() 实例来清除输入字段。

我试图简化你的代码,因为两个 while 循环有点令人困惑,我不确定它们的目的是什么。游戏中通常应该只有一个 while 循环。

import pygame
import pygame_textinput


pygame.init()

screen = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
font = pygame.font.SysFont("arial", 20)

textinput = pygame_textinput.TextInput()
user_input = ''

done = False

while not done:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
done = True

if textinput.update(events):
user_input = textinput.get_text()
textinput = pygame_textinput.TextInput()

# Draw everything.
screen.fill((225, 225, 225))

screen.blit(textinput.get_surface(), (10, 10))

user_input_surface = font.render(user_input, True, (30, 80, 100))
screen.blit(user_input_surface, (10, 50))

pygame.display.update()
clock.tick(30)

pygame.quit()

编辑:在此版本中,我将渲染的文本表面附加到列表中,并使用偏移量对它们进行 blit。

import pygame
import pygame_textinput


pygame.init()

screen = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
font = pygame.font.SysFont("arial", 20)

textinput = pygame_textinput.TextInput()
user_inputs = []

done = False

while not done:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
done = True

if textinput.update(events):
user_inputs.append(
font.render(textinput.get_text(), True, (30, 80, 100)))
textinput = pygame_textinput.TextInput()

screen.fill((225, 225, 225))

screen.blit(textinput.get_surface(), (10, 10))

for y, text_surf in enumerate(user_inputs):
screen.blit(text_surf, (10, 50+30*y))

pygame.display.update()
clock.tick(30)

pygame.quit()

Edit2:要获取表格,您可以对行偏移使用模数,对列偏移使用底除法。此示例的问题是,如果文本表面太宽,它们可能会重叠。

for n, text_surf in enumerate(user_inputs):
# 5 rows. Offset = 30 pixels.
y_pos = 50 + (n%5) * 30
# After 5 rows add a new column. Offset = 100 pixels.
x_pos = 10 + n // 5 * 100
screen.blit(text_surf, (x_pos, y_pos))

关于python - Blit 用户文本输入到屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45694116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com