简体   繁体   中英

Manage keydown in pygame

I am developing a program that will show a letter from an array. If the user press the up arrow this will move in the array, also if the user press the down arrow. The problem is that when I press any of these keys the letter moves very fast, for example if I am in the A the program moves till D or something like that. Here is my code

def HighScore(event):
    pygame.init()
    nameString=""
    nameStringFinal=""
    index=0
    ABC=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
    font=pygame.font.SysFont("Arial",30,True,False)
    screen=pygame.display.set_mode((700, 700))
    pygame.display.set_caption("New High Score" )
    font=pygame.image.load("imagenes/paisaje.jpg").convert()
    escape=False        
    while escape==False:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.blit(fondo,(0,0))
        show=fuente.render(nombreString,0,(0,0,0))

        if event.type ==  pygame.KEYDOWN:
            if control==True:
                if event.key == pygame.K_UP:  
                    if index==25:
                        index=25
                    else:
                        index=index+1
                    nameString=ABC[index]
                elif event.key == pygame.K_DOWN:
                    if index==0:
                        index=0
                    else:
                        index=index-1
                    nameString=ABC[index]
                elif event.key == pygame.K_RETURN:
                    nameStringFinal=nameStringFinal+nameString
        screen.blit(despliegue,(200,200))
        pygame.display.flip()

You need to make sure all the event processing is indented properly, so it's part of the for loop

The way you have it now, the last even will be processed over and over until a new event arrives

For example:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()

    if event.type ==  pygame.KEYDOWN:
        if event.key == pygame.K_UP:  
            if index==25:
                index=25
            else:
                index=index+1

            nameString=ABC[index]
        elif event.key == pygame.K_DOWN:
            if index==0:
                index=0
            else:
                index=index-1
            nameString=ABC[index]
        elif event.key == pygame.K_RETURN:
            nameStringFinal=nameStringFinal+nameString

screen.blit(fondo,(0,0))
show=fuente.render(nombreString,0,(0,0,0))
screen.blit(despliegue,(200,200))

In the shell, you can see how a for loop over the empty list, just leaves the variable with the value of the previous loop

>>> for i in [1,2,3]:   # imagine this is the event list
...     if i == 4:
...         sys,exit()
... 
>>> if i == 3:
...     print "i==3"
... 
i==3
>>> for i in []:        # next time round the while loop, the event list is empty
...     if i == 4:
...         sys,exit()
... 
>>> if i == 3:          # but you're still processing this event again!
...     print "i==3"
... 
i==3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM