python pygame更新文本标签

时间:2016-01-18 21:15:35

标签: python pygame

我希望在按下按键时更新文本标签,但没有任何反应。它可能与循环有关,但我不知道如何解决它。

我正在使用pygame

import pygame

pygame.init()

display_width = 800
display_height = 600

screen = pygame.display.set_mode((display_width, display_height))
done = False

pygame.display.set_caption('UTTER')

bg = pygame.image.load("wheel.bmp")

digitalFont = pygame.font.Font("digital-7.ttf",26)

mixValue = "03"

def updateLED (dial, value):
    if dial == "mix":
        mixLED = digitalFont.render(value, 1, (255,255,0))
        screen.blit(mixLED, (362, 166))


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

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                updateLED("mix", "01")
                print("down")

    # Add this somewhere after the event pumping and before the display.flip()
    screen.blit(bg, (0, 0))

    updateLED ("mix", "03")

    pygame.display.update()

pygame.quit()
quit()

1 个答案:

答案 0 :(得分:3)

按键时更改标签

updateLED("mix", "01")

但你有另一个updateLED在每个循环中将文本更改为旧文本

updateLED("mix", "03")

所以你看不到新的文字。

blit绘制缓冲区(以防止闪烁屏幕),pygame.display.update()将缓冲区发送到屏幕。

但是还有其他问题 - KEYDOWN是非常短的事件 - 当关键状态从“UP”变为“DOWN”时 - 而不是当按下按键时。

# default text
text = "03"

while not done:


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

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                # new text
                text = "01"
                print("down")

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_DOWN:
                # again old text
                text = "03"
                print("up")

    screen.blit(bg, (0, 0))

    updateLED("mix", text)

    pygame.display.update()