2016-05-16 70 views
0

請幫我,我已經開始在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() 

回答

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 
相關問題