我正在制作一个带有菜单的游戏,该菜单使用向左和向右箭头键进行导航。问题是,当您按下箭头之一时,它会在两个箭头之间快速切换。我假设我需要使用pygame.KEYUP
,但是我不确定如何使用它。现在,它只是在“播放”和“退出”之间闪烁。这是我的代码。我想要它,所以如果您向左或向右按它可以选择播放或退出,并且不会在两者之间闪烁。
'''
Mega Maro Bois
Zan3yGameZ 2018
'''
import pygame, time
pygame.init()
def createWindow():
global WIDTH, HEIGHT, TITLE, W
WIDTH, HEIGHT = 800, 450
TITLE = "Mega Maro Bois"
pygame.display.set_caption(TITLE)
W = pygame.display.set_mode( (WIDTH, HEIGHT), pygame.HWSURFACE|pygame.DOUBLEBUF )
createWindow()
menu = pygame.image.load("Graphics\\Menu.png")
############################################################# PLAY PICTURES
play = pygame.image.load("Graphics\\Play.png")
play_sel = pygame.image.load("Graphics\\Play_Selected.png")
############################################################# QUIT PICTURES
quit = pygame.image.load("Graphics\\Quit.png")
quit_sel = pygame.image.load("Graphics\\Quit_Selected.png")
Event = "Menu"
loop = True
Button_Sel = "Play"
Button_Sel2 = 1
if Event == "Menu":
W.blit(menu, (0, 0))
W.blit(play, (25, 250))
W.blit(quit, (435, 250))
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if Event == "Menu":
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
Button_Sel = "Play"
if event.key == pygame.K_2:
Button_Sel = "Quit"
if event.key == pygame.K_RIGHT:
Button_Sel2 += 1
if event.key == pygame.K_LEFT:
Button_Sel2 -= 1
#################################################################ENTER SELECT
if Button_Sel == "Play" and event.key == pygame.K_RETURN:
Event = "Play"
if Button_Sel == "Quit" and event.key == pygame.K_RETURN:
pygame.quit()
exit()
#################################################################SPACE SELECT
if Button_Sel == "Play" and event.key == pygame.K_SPACE:
Event = "Play"
if Button_Sel == "Quit" and event.key == pygame.K_SPACE:
pygame.quit()
exit()
###################################################IF USER SELECTS PLAY
if Button_Sel == "Play":
W.blit(play_sel, (25, 250))
W.blit(quit, (435, 250))
###################################################IF USER SELECTS QUIT
if Button_Sel == "Quit":
W.blit(play, (25, 250))
W.blit(quit_sel, (435, 250))
if Button_Sel2 == 1:
Button_Sel = "Play"
if Button_Sel2 == 2:
Button_Sel = "Quit"
pygame.display.update()
pygame.quit()
exit()
在这种情况下,我基本上只需要知道如何使用pygame.KEYUP
。
答案 0 :(得分:0)
您的if event.type == pygame.KEYDOWN:
在事件处理循环for event in pygame.event.get():
之外
这意味着您一遍又一遍地处理同一事件
将事件检查放入循环中。