在鼠标单击pygame上预览键盘输入和blit文本

时间:2018-02-16 04:04:51

标签: python pygame

我正在尝试将用户在屏幕上键入的内容显示为预览,当用鼠标左键单击时,它会将文本显示在屏幕上。

from pygame import *

init()
screen = display.set_mode((640, 480))
clock = time.Clock()
running = True

font = font.SysFont("comicsansms", 72) # Default font
textC = "a" # Stores user input
text = font.render(textC, True, (255,0,0)) # Render Text

while running:

    mx, my = mouse.get_pos()
    mb = mouse.get_pressed()
    for e in event.get():
        if e.type == QUIT:
            running = False

        if e.type == KEYDOWN:
            print(textC)
            if key.get_pressed()[K_BACKSPACE]:
                textC = textC[:-1] # removes last letter
            else:
                textC += e.unicode # adds letter

        screen.fill((255, 255, 255)) #fill screen white
        screen.blit(text, (mx, my)) # display text

    display.flip()
    clock.tick(60)

quit()

2 个答案:

答案 0 :(得分:2)

我修改了您的示例,将存储的文本放在您单击的位置。更改text后,您需要更新textC表面。我也会在鼠标单击时清除textC缓冲区。

from pygame import *

init()
screen = display.set_mode((640, 480))
clock = time.Clock()
running = True

font = font.SysFont("comicsansms", 72) # Default font
textC = "a" # Stores user input
text = font.render(textC, True, (255,0,0)) # Render Text
pos = None # store mouse click location

while running:
    mx, my = mouse.get_pos()
    mb = mouse.get_pressed()
    for e in event.get():
        if e.type == QUIT:
            running = False

        if e.type == KEYDOWN:
            print(textC)
            if key.get_pressed()[K_BACKSPACE]:
                textC = textC[:-1] # removes last letter
            else:
                textC += e.unicode # adds letter
            # need to update the text surface:
            text = font.render(textC, True, (255,0,0)) # Render Text

        elif e.type == MOUSEBUTTONDOWN:
            pos = mouse.get_pos() 
            locked_text = font.render(textC, True, (0,255,0))
            textC = ""
            text = font.render(textC, True, (255,0,0)) # Clear text

        screen.fill((255, 255, 255)) #fill screen white
        if pos:
            screen.blit(locked_text, pos)
        screen.blit(text, (mx, my)) # display text

    display.flip()
    clock.tick(60)

quit()

如果您希望文本保持不变,那么您需要创建文本表面及其位置列表,然后每帧迭代它们。在这种情况下,可能值得考虑Sprites

答案 1 :(得分:2)

首先,您没有正确捕获鼠标事件。您可以使用MOUSEBUTTONDOWN事件与使用KEYDOWN进行键事件相同的方式,然后使用mouse.get_pressed来了解正在按下哪个鼠标按钮。

其次,问题在于每次执行screen.fill时,所有曲面都将被删除,因此必须以某种方式跟踪文本和渲染文本的位置。它可以是一个列表。

所以你的代码看起来像这样

from pygame import *

init()
screen = display.set_mode((640, 480))
clock = time.Clock()
running = True

font = font.SysFont("comicsansms", 72) # Default font
textC = "a" # Stores user input
text = font.render(textC, True, (255,0,0)) # Render Text

text_rendered = []

def rerender_text():
    for surface, pos in text_rendered:
        screen.blit(surface, pos)  # display text

while running:
    screen.fill((255, 255, 255))  # fill screen white
    mx, my = mouse.get_pos()
    for e in event.get():
        if e.type == QUIT:
            running = False

        if e.type == KEYDOWN:
            keys = list(key.get_pressed())
            index = keys.index(1)
            if key.get_pressed()[K_BACKSPACE]:
                textC = textC[:-1] # removes last letter
            else:
                textC += e.unicode # adds letter

        if e.type == MOUSEBUTTONDOWN:
            if mouse.get_pressed()[0]: # Left click
                screen.blit(text, (mx, my))  # display text
                # save the text and the position for rerendering
                text_rendered.append((text,(mx,my))) 


    text = font.render(textC, True, (255, 0, 0))  # Render Text
    rerender_text()
    screen.blit(text, (mx, my))  # display text
    display.flip()
    clock.tick(60)

quit()