Refactor of player movement
jiwaszki opened this issue · comments
Lines 54 to 69 in 8ef6fc8
It can be done via creation of a class that will handle movements of a player:
ACTIONS = {
pygame.K_LEFT: {"axis": 0, "value": -playerSpeed},
pygame.K_RIGHT: {"axis": 0, "value": playerSpeed},
pygame.K_UP: {"axis": 1, "value": -playerSpeed},
pygame.K_DOWN: {"axis": 1, "value": playerSpeed},
}
class PlayerMoveVector():
def __init__(self):
self.vector = [0, 0]
def __getitem__(self, i):
return self.vector[i]
def do_action(self, key):
self.vector[ACTIONS[key]["axis"]] = ACTIONS[key]["value"]
def clear(self):
self.vector = [0, 0]
player_move_vector = PlayerMoveVector()
while carryOn:
# --- Main event loop
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
carryOn = False # Flag that we are done so we can exit the while loop
elif event.type == pygame.KEYDOWN:
player_move_vector.do_action(event.key)
elif event.type == pygame.KEYUP:
player_move_vector.clear() # This will populate whole vector with 0's
We can discuss benefits and details of this approach on the next meeting.