Skip to content

Idea for multiple keys pressed #22

@borisreyesgrange

Description

@borisreyesgrange

Hi, I'm using your pygamge_function with my High School Computer Science students in Chile, and I want to share with you this idea... I take your keyPressed function and add a simple modification to add multiple keys detecting (for jumping with direction for example, or move in diagonal):

KeyCheck must be a list with the keys to check

def keysPressed(keyCheck):
global keydict
pygame.event.clear()
keys = pygame.key.get_pressed()
pressedKeys = 0
if sum(keys) > 0:
for key in keyCheck:
if key == "" or keys[keydict[key.lower()]]:
pressedKeys += 1
if pressedKeys == len(keyCheck):
return True
return False

When we use this, in the game loop, we must check the multiple keys pressed actions first, before of the single keystroke in the "if" that controls the movement.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions