Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can get the events from pygame and then watch out for the <code>KEYDOWN</code> event, instead of looking at the keys returned by <code>get_pressed()</code>(which gives you keys that are currently pressed down, whereas the <code>KEYDOWN</code> event shows you which keys were pressed down on <em>that frame</em>). </p> <p>What's happening with your code right now is that if your game is rendering at 30fps, and you hold down the left arrow key for half a second, you're updating the location 15 times.</p> <pre><code>events = pygame.event.get() for event in events: if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: location -= 1 if event.key == pygame.K_RIGHT: location += 1 </code></pre> <p>To support continuous movement while a key is being held down, you would have to establish some sort of limitation, either based on a forced maximum frame rate of the game loop or by a counter which only allows you to move every so many ticks of the loop.</p> <pre><code>move_ticker = 0 keys=pygame.key.get_pressed() if keys[K_LEFT]: if move_ticker == 0: move_ticker = 10 location -= 1 if location == -1: location = 0 if keys[K_RIGHT]: if move_ticker == 0: move_ticker = 10 location+=1 if location == 5: location = 4 </code></pre> <p>Then somewhere during the game loop you would do something like this:</p> <pre><code>if move_ticker &gt; 0: move_ticker -= 1 </code></pre> <p>This would only let you move once every 10 frames (so if you move, the ticker gets set to 10, and after 10 frames it will allow you to move again)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload