Home > Software engineering >  How do I store a pygame keyboard input value in a varibale
How do I store a pygame keyboard input value in a varibale

Time:05-28

How do I store a pygame keyboard input value in a variable. In the place pygame.K_r how can I use my key variable to assign to the key input.

import pygame
import random
from sys import exit

pygame.init()


screen = pygame.display.set_mode((300, 500))
clock = pygame.time.Clock()


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        **key = "r"**
        key_input = pygame.key.get_pressed()

        if key_input[pygame.**K_r**]:
            pass


    pygame.display.update()
    clock.tick(60)

CodePudding user response:

Just store the enumerator constant in a variable:

key = pygame.K_r

and that variable for subscription and get status from sequence:

key_input = pygame.key.get_pressed()
if key_input[key]:

CodePudding user response:

Try it Out,

import random
from sys import exit
import pygame

pygame.init()


screen = pygame.display.set_mode((300, 500))
clock = pygame.time.Clock()


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        
        R = pygame.K_r    # variable R is used to assign the pygame keypress K_r
        key_input = pygame.key.get_pressed()

        if key_input[R]:   #use varibale R to check if r is pressed
            print('Pressed')


    pygame.display.update()
    clock.tick(60)
  • Related