pygame text input
1.0.0
该模块提供了两个实用程序类,可以使用PyGame简化输入文本。课程是:
TextInputVisualizer可用于管理和绘制文本输入。只需将pygame.event.get()返回的所有事件传递给每个帧,然后在屏幕上闪烁其surface属性即可。TextInputManager可用于管理输入的文本,而没有视觉方面。由TextInputVisualizer在幕后使用。最简单的方法是使用PYPI:
python3 -m pip install pygame-textinput
TextInputVisualizer对构造函数的所有论点都是可选的。构造后,将所有Pygame事件作为参数调用每个帧update ,然后将其surface字段闪烁到屏幕上,就像这样:
import pygame_textinput
import pygame
pygame . init ()
# Create TextInput-object
textinput = pygame_textinput . TextInputVisualizer ()
screen = pygame . display . set_mode (( 1000 , 200 ))
clock = pygame . time . Clock ()
while True :
screen . fill (( 225 , 225 , 225 ))
events = pygame . event . get ()
# Feed it with events every frame
textinput . update ( events )
# Blit its surface onto the screen
screen . blit ( textinput . surface , ( 10 , 10 ))
for event in events :
if event . type == pygame . QUIT :
exit ()
pygame . display . update ()
clock . tick ( 30 )所有参数都是可选的。
| 争论 | 描述 |
|---|---|
| 经理 | 用于管理输入的TextInputManager |
| font_object | pygame.font.Font对象用于渲染 |
| 反式 | 是否渲染字体 |
| font_color | 字体的颜色呈现 |
| cursor_blink_interval | 光标闪烁的间隔,在MS中 |
| CURSOR_WIDTH | 光标的宽度,以像素为单位 |
| CURSOR_COLOR | 光标的颜色 |
上面的所有参数也是可以随时访问和修改的字段,例如这样:
textinput . cursor_width = 12
textinput . value = "Hello, World!"
print ( textinput . font_color )| 场地 | 描述 |
|---|---|
| 价值 | (字符串)到目前为止输入的文本 |
| 表面 | pygame.Surface对象,带有其在其上输入的文本和光标以及透明的背景。无法设置。 |
| Cursor_visible | (布尔)光标目前是否可见。只要update连续调用,每个cursor_interval MS都会翻转。 |
| 方法 | 描述 |
|---|---|
| 更新(事件) | 只要要处理输入,并且应更新cursor_visible ,请调用此方法。 |
for event in events :
...
if event . type == pygame . KEYDOWN and event . key == pygame . K_RETURN :
print ( "Oooweee" )pygame中启用它们: pygame . key . set_repeat ( 200 , 25 ) # press every 50 ms after waiting 200 ms该新版本还进行了优化,以便您可以随时修改任何字段,并且实际的表面只有在使用textinput.surface访问时才能重新渲染 - 并且仅当您实际修改任何值时。
TextInputManager如果您希望自己绘制屏幕上的文本,则可以使用TextInputManager来管理到目前为止输入的字符串。
像TextInputVisualizer一样,您可以为其update方法喂食pygame.event.get()收到的所有事件,您希望它能处理。如果您将其传递给TextInputManager ,则TextInputVisualizer在其update方法中为您执行此操作。
| 争论 | 描述 |
|---|---|
| 最初的 | 初始值(文本) |
| 验证器 | 一个函数拿起string并返回bool 。每次输入修改值时,此函数都会以修改值作为参数调用;如果函数返回True ,则将接受输入,否则输入将被忽略。 |
因此,说您只想允许输入最多5个字母,您可以使用
manager = TextInputManager ( validator = lambda input : len ( input ) <= 5 )| 场地 | 描述 |
|---|---|
| 价值 | 到目前为止,插入的值。当更改时, cursor_pos会尽可能保留。 |
| CURSOR_POS | 光标的位置。 0是在第一个字符之前, len(manager.value)最后一个位置。该范围之外的值被夹紧。 |
import pygame
import pygame . locals as pl
pygame . init ()
# No arguments needed to get started
textinput = TextInputVisualizer ()
# But more customization possible: Pass your own font object
font = pygame . font . SysFont ( "Consolas" , 55 )
# Create own manager with custom input validator
manager = TextInputManager ( validator = lambda input : len ( input ) <= 5 )
# Pass these to constructor
textinput_custom = TextInputVisualizer ( manager = manager , font_object = font )
# Customize much more
textinput_custom . cursor_width = 4
textinput_custom . cursor_blink_interval = 400 # blinking interval in ms
textinput_custom . antialias = False
textinput_custom . font_color = ( 0 , 85 , 170 )
screen = pygame . display . set_mode (( 1000 , 200 ))
clock = pygame . time . Clock ()
# Pygame now allows natively to enable key repeat:
pygame . key . set_repeat ( 200 , 25 )
while True :
screen . fill (( 225 , 225 , 225 ))
events = pygame . event . get ()
# Feed it with events every frame
textinput . update ( events )
textinput_custom . update ( events )
# Get its surface to blit onto the screen
screen . blit ( textinput . surface , ( 10 , 10 ))
screen . blit ( textinput_custom . surface , ( 10 , 50 ))
# Modify attributes on the fly - the surface is only rerendered when .surface is accessed & if values changed
textinput_custom . font_color = [( c + 10 ) % 255 for c in textinput_custom . font_color ]
# Check if user is exiting or pressed return
for event in events :
if event . type == pygame . QUIT :
exit ()
if event . type == pygame . KEYDOWN and event . key == pygame . K_RETURN :
print ( f"User pressed enter! Input so far: { textinput . value } " )
pygame . display . update ()
clock . tick ( 30 )