<分区> 分区>
例如,下面的函数移动 玩家根据方向给定数量 通过:
def move(player, direction, amount):
if direction == 'up':
player.y -= amount
elif direction == 'down':
player.y += amount
elif direction == 'left':
player.x -= amount
elif direction == 'right':
player.x += amount
不是将方向作为字符串传递,而是应该 该功能可以分解为四个更小的功能吗?
def move_up(player, amount):
player.y -= amount
def move_down(player, amount):
player.y += amount
def move_left(player, amount):
player.x -= amount
def move_right(player, amount):
player.x += amount
