Pygame: key.get_pressed() does not coincide with the event queue

前端 未结 4 457
心在旅途
心在旅途 2020-11-30 13:25

I\'m attempting to work out simple controls for an application using pygame in Python. I have got the basics working, but I\'m hitting a weird wall: I am using the arrow key

4条回答
  •  时光取名叫无心
    2020-11-30 13:32

    You are using event-based input , but in this case you want polling-based input. Then you don't mess with key-repeats.

    import pygame
    from pygame.locals import *
    
    done = False    
    player.pos = Rect(0,0,10,10)
    
    while not done:
        for event in pygame.event.get():
            # any other key event input
            if event.type == QUIT:
                done = True        
            elif event.type == KEYDOWN:
                if event.key == K_ESC:
                    done = True
                elif event.key == K_F1:
                    print "hi world mode"
    
        # get key current state
        keys = pygame.key.get_pressed()
        if keys[K_LEFT]:
            player.pos.left -= 10
        if keys[K_RIGHT]:
            player.pos.left += 10
        if keys[K_UP]:
            player.pos.top -= 10
        if keys[K_DOWN]:
            player.pos.left += 10
        if keys[K_SPACE]: 
            print 'firing repeated gun'
    

提交回复
热议问题