Instead of doing a long if..elif
, is there a way to store code in a list
or dictionary to be run? Something like:
my_dict = {'up': 'y=y+1', 'down': 'y=y-1', 'left': 'x=x-1', 'right': x=x+1'}
Then you match up the choice to the dict
and run the result?
Or is the next best thing to a traditional if..elif
chain a conditional expression:
x = x+1 if choice == 'right' else x-1 if choice == 'left' else x
y = y+1 if choice == 'up' else y-1 if choice == 'down' else y
Part of the reason I am asking, apart from sheer curiosity, is that I am writing a game in Pygame to help learn Python coding. I have 12 keys I am checking for being pressed, and I am using a very long and ungainly if..elif
structure that makes me think there has to be a more beautiful way of doing it. Basically what I have looks like this:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_t:
red += 10
elif event.key == pygame.K_g:
red -= 10
elif event.key == pygame.K_5:
red += 1
elif event.key == pygame.K_b:
red -= 1
elif event.key == pygame.K_y:
green += 10
elif event.key == pygame.K_h:
green -= 10
elif event.key == pygame.K_6:
green += 1
elif event.key == pygame.K_n:
green -= 1
elif event.key == pygame.K_u:
blue += 10
elif event.key == pygame.K_j:
blue -= 10
elif event.key == pygame.K_7:
blue += 1
elif event.key == pygame.K_m:
blue -= 1
Is that what my Python code is supposed to look like? Or is there a more Pythonic way to code this?
Yes, there is a way but by using functions:
def up():
y=y+1
def down():
y=y-1
def left():
x=x-1
def right():
x=x+1
d = {'up': up, 'down': down, 'left': left, 'right': right};
then call it like d['up']()
.
To avoid using global variables, you may use OOP approach:
class player:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def up(self):
self.y += 1
def down(self):
self.y -= 1
def left(self):
self.x -= 1
def right(self):
self.x += 1
def move(self, direction):
# call the appropriate method that corresponds to given direction
getattr(self, direction)()
Example use:
p = player(0,0)
p.move('up') # and so on..
For this situation, one way to do it is to store the values you want to add/subtract in the dictionary, then retrieve them and add them. Something like this:
offsets = {
'up': (0, 10),
'down': (0, -10),
'left': (-10, 0),
'right': (10, 0)
}
xOffset, yOffset = offsets[choice]
x += xOffset
y += yOffset
You can store functions in a dictionary, but the functions won't be able to modify variables of their calling context. If you find that the "code snippets" you are storing all have the same structure (as here where each is "add something to x" or "add something to y"), what you may be able to do is factor out that code into one place, and only store the parameters in the dictionary (as here where you can store the values you want to add).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With