Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I always solved the problem like this: (a partial solution):</p> <pre><code>for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() if event.type == KEYDOWN: if event.key == K_ESCAPE: pygame.quit() sys.exit() if event.key == pygame.K_LEFT: x_speed=3 if event.key == pygame.K_RIGHT: player.moveright() x_speed=-3 if event.key == pygame.K_UP: y_speed=3 if event.key == pygame.K_DOWN: y_speed=-3 if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT and x_speed == 3: x_speed=0 if event.key == pygame.K_RIGHT and x_speed == -3: x_speed=0 if event.key == pygame.K_UP and y_speed == 3: y_speed=0 if event.key == pygame.K_DOWN and y_speed == -3: y_speed=0 </code></pre> <p>This doesn't stop movement when the previous key has been unpressed.</p> <p>EDIT: It is not impossible, its just not that useful. You could for example have 2 lists that would imitate a stack. When the list is empty your character will not move. With each keydown, you append the key to the end of list. Every keyup, you remove the key from the list. That way, the most recent key pressed will be at the end of the list, and if you unpress the last recent will also be at the end of the list.</p> <p>A small example:</p> <pre><code>keylist = [] for event in pygame.event.get(): if event.type == KEYDOWN: if event.key == pygame.K_LEFT: keylist.append(-1) if event.key == pygame.K_RIGHT: keylist.append(1) if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: keylist.reverse().remove(-1) if event.key == pygame.K_RIGHT: keylist.reverse().remove(1) </code></pre> <p>the in the move method you try this: You get the last element in the list if it exists, move the character in the right direction.</p> <pre><code>if(not keylist[-1:]): character.move(direction=keylist[-1]) else: character.move(direction=0) </code></pre>
    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.
 

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