首页 > 解决方案 > 文本框呈现在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我绘制屏幕并翻转它的底部添加了绿松石色。

标签: pygamepygame-surface

解决方案


你记得点击框吗?;)

还有一堆小问题:

  • 该代码没有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()

推荐阅读