S
S
Sam Pie2017-12-11 11:24:47
Python
Sam Pie, 2017-12-11 11:24:47

How to handle simultaneous pressing of 2 or more keys in pygame?

For example, so that when you press the up and right arrows, the player moves obliquely.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xdgadd, 2017-12-11
@piesam

Take and process:

import pygame as pg
...
for e in pg.event.get():
    ...
    if e.type == pg.KEYDOWN:
        if e.key == pg.K_w:
            move_up()
        elif e.key == pg.K_s:
            move_down()
        elif ...
...

V
Vlad, 2017-12-11
@d1skort

Lord, come on. First link on google!
First option:

keys = pygame.keys.get_pressed()

if keys[pygame.K_RIGHT] and keys[pygame.K_LEFT]:
    move_fullcube = left

Second option:
right_pressed = False
one_pressed = False
for event in pygame.event.get():
    if event.type==KEYDOWN:
        if event.key==K_RIGHT:
            right_pressed = True
        if event.key==K_1:
            right_pressed = True

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question