1

I want to make a class "Player" that has a list containing the inputs I want as strings, like ['w','a','s','d'] , and using the pygame module to get all the pressed keys through pygame.key.get_pressed(), find a way to generalize the conditions:

keys_pressed[pygame.K_w]|keys_pressed[pygame.K_a]|keys_pressed[pygame.K_s]|keys_pressed[pygame.K_d] ,using the list of keys from the Player class.

pseudo-code:

player.list = ['w','a','s','d']

keys_pressed = pygame.key.get_pressed()

if keys_pressed[pygame.K_<player.list[0]>]:move up

if keys_pressed[pygame.K_<player.list[1]>]:move left

if keys_pressed[pygame.K_<player.list[2]>]:move down

if keys_pressed[pygame.K_<player.list[3]>]:move right

Is there a way I can implement this? If so, what's the right way to concatenate the "K_" + list[i] so that pygame accepts it?

Lima 713
  • 11
  • 1
  • Why do you want to do that. Why do not use a list `[pygame.K_w, pygame.K_a, pygame.K_s, pygame.K_d]`? – Rabbid76 Mar 23 '21 at 17:29
  • @Rabbid76 mainly because of the "pygame.K_" repetition. If that's the only way to implement, okay, but I wanted to see if a less repetitive way was possible :) – Lima 713 Mar 23 '21 at 18:02
  • There is no repetition. `K_a` is different to `K_w`. – Rabbid76 Mar 23 '21 at 18:10
  • *"A long descriptive name is better than a short enigmatic name. A long descriptive name is better than a long descriptive comment."* Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship – Rabbid76 Mar 23 '21 at 18:11
  • I got it working using the way you suggested, so thanks! – Lima 713 Mar 23 '21 at 18:45

0 Answers0