pygame - 文本框呈现在pygame的主代码之上?
问题描述
我已经在我的模拟中实现了文本框代码,但现在它没有渲染主代码,或者在我的主代码之上渲染,所以除了框之外什么都没有显示:(
这是我的文本框代码:
def textbox(font20):
input_box = pygame.Rect(300,225,400,100)
color_inactive =(TURQUOISE)
color_active = (0,255,249)
color = color_inactive
active = False
text = ""
done = False
font = font20
while not done:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if input_box.collidepoint(event.pos):
active = not active
else:
active = False
color = color_active if active else color_inactive
if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
print(text)
text = ""
done = True
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
# Render the current text.
text_surface = font.render(text, True, color)
# Resize the box if the text is too long.
width = max(200, text_surface.get_width()+10)
input_box.w = width
# Blit the text.
screen.blit(text_surface, (input_box.x+5, input_box.y+5))
# Blit the input_box rect.
pygame.draw.rect(screen, color, input_box, 2)
pygame.display.flip()
我还在screen.blit
我绘制屏幕并翻转它的底部添加了绿松石色。
解决方案
你记得点击框吗?;)
还有一堆小问题:
- 该代码没有
TURQUOISE
. - 循环没有处理该
pygame.QUIT
事件。- 它还需要将此事件传播到外循环
- 随着盒子的增长,它变得一团糟,我添加了一个黑色矩形来擦除旧的。
- 它没有返回
text
值。
修复了所有这些非常轻微的错误,它工作得很好。但是请记住先单击该框!
import pygame
# Window size
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
### initialisation
pygame.init()
screen = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
pygame.display.set_caption("Text Box Test")
BLACK = (0,0,0)
TURQUOISE = (0,154,196)
CYAN = (0,255,249)
def textbox(font20):
#input_box = pygame.Rect(300,225,400,100)
input_box = pygame.Rect(10,10,400,100)
color_background = BLACK # for erasing
color_inactive = TURQUOISE
color_active = CYAN
color = color_inactive
active = False
text = ""
done = False
font = font20
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.event.post( pygame.event.Event( pygame.QUIT ) ) # re-send the quit event to the next loop
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
if input_box.collidepoint(event.pos):
active = not active
else:
active = False
color = color_active if active else color_inactive
elif event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
print(text)
#text = ""
done = True
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
print( "DEBUG: text is now [%s]" % ( text ) )
# Render the current text.
text_surface = font.render(text, True, color)
# Resize the box if the text is too long.
width = max(200, text_surface.get_width()+10)
input_box.w = width
# Blit the input_box rect.
pygame.draw.rect(screen, color_background, input_box) # filled box to clear the old one
pygame.draw.rect(screen, color, input_box, 2)
# Blit the text.
screen.blit(text_surface, (input_box.x+5, input_box.y+5))
pygame.display.flip()
return text
font = pygame.font.SysFont('', 24)
textbox( font )
pygame.quit()
推荐阅读
- random - 如何在 vhdl 中获取 -1024 和 1024 之间的随机数
- python - Python 数三
- javascript - 如何在 JavaScript 正则表达式中替换冒号和星号特殊字符
- google-cloud-dataflow - Apache Beam/Dataflow:在转换之间传递属性
- reactjs - ReactJS:映射组件的构造函数调用了多少次?
- html - 对它应用填充后如何将背景颜色拉伸为父容器的宽度-CSS
- pyspark - 如何将外部配置文件传递给 pyspark(Spark 2.x) 程序?
- windows - 从 Win32::API 调用返回一个宽字符串
- python - 如何从我们放入的 alist 中找到均值和中位数?
- ssl - 对云端有点困惑,为什么我的 CNAME 证书不起作用