当我只按一次键时,如何使代码显示新的背景?

时间:2016-05-16 22:29:47

标签: python keyboard pygame

请帮助我,我最近开始在pygame中编写程序,我可以通过这个障碍,我无法在线找到任何帮助。因此,更具体的是我需要的东西,当我按下也许只有一次tab键因为如果我把KEYDOWN我将不得不我总是按下键我会离开起始背景然后会出现一条消息。谢谢你的时间。

    import pygame
    import random
    perguntaa = random.randint(1,2)
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    GREEN = (0, 255, 0)
    RED = (255, 0, 0)
    BLUE = (0, 0, 255)
    ORANGE = (255, 154, 23)
    pygame.init()
    size = (700, 500)
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("Quem quer ser Milionário")
    clock = pygame.time.Clock()
    done = False
    while not done:
        for event in pygame.event.get():
        screen.fill(ORANGE)
        pygame.draw.rect(screen, BLACK, [225, 200, 250, 100])
        font = pygame.font.SysFont('cambria', 100, False, False)
        text = font.render("Play", True, WHITE)
        screen.blit(text, [225, 200])
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
            screen.fill(ORANGE)
            pygame.draw.rect(screen, BLACK, [0, 50, 700, 100])
        pygame.display.flip()
        clock.tick(30)
pygame.quit()

1 个答案:

答案 0 :(得分:0)

我相信这就是你要找的东西

bgs = [Orange, Black, Green]
current_bg = 0
if event.key == pygame.K_Q:
    current_bg += 1
    if current_bg > len(bgs)-1:
        current_bg = 0
screen.fill(bgs[current_bg] #alternately screen.blit(bgs[current_bg]) if bgs indexes are instead sprites
screen.update()#flip and update are interchangeable however update is more commonly used