I'm doing an interactive map and after reading on the Internet, I'm trying to develop my learn towards an efficient way of programming. I wonder, is there a way to write more efficient this kinds of if_elif tree's ? I've read that one way is to make every conditions a def
and store the conditions in a tuple ( 'cause is more efficiente than a list, and the values won't change). I've considered using ternary operator but don't know if is gonna be more efficient.
if direction == "w":
point[POS_Y] -= 1
elif direction == "s":
point[POS_Y] += 1
elif direction == "a":
point[POS_X] -= 1
elif direction == "d":
point[POS_X] += 1
Here's the code:
POS_X = 0
POS_Y = 1
MAP_WIDTH = 20
MAP_HEIGHT = 15
point = [3,7]
while 1: # i've read that Python is more efficient using a 1 instead of a True
# def player_movement(point):
print('+' + '-' * MAP_WIDTH * 3 + '+')
for coordinate_y in range(MAP_HEIGHT):
print('|',end='')
for coordinate_x in range(MAP_WIDTH):
if point[POS_X] == coordinate_x and point[POS_Y] == coordinate_y:
print(' @',end='') # it works with ' @ ' as well, but in Visual Code doesn't see though
else:
print(' ',end='')
print('|')
print('+' + '-' * MAP_WIDTH * 3 + '+')
# player_movement([3,8])
direction = readchar.readchar().decode()
if direction == "w":
point[POS_Y] -= 1
elif direction == "s":
point[POS_Y] += 1
elif direction == "a":
point[POS_X] -= 1
elif direction == "d":
point[POS_X] += 1