|
发表于 2022-5-29 16:06:18
|
显示全部楼层
- origin = (0, 0)
- legal_x = (-100, 100)
- legal_y = (-100, 100)
- def create(pos_x=0, pos_y=0):
- def moving(direction, step):
- nonlocal pos_x, pos_y
- new_x = pos_x + direction[0] * step
- new_y = pos_y + direction[1] * step
- if new_x < legal_x[0]:
- pos_x = legal_x[0] - (new_x - legal_x[0])
- elif new_x > legal_x[1]:
- pos_x = legal_x[1] - (new_x - legal_x[1])
- else:
- pos_x = new_x
- if new_y < legal_y[0]:
- pos_y = legal_y[0] - (new_y - legal_y[0])
- elif new_y > legal_y[1]:
- pos_y = legal_y[1] - (new_y - legal_y[1])
- else:
- pos_y = new_y
- return pos_x, pos_y
- return moving
- def isDirec(char):
- if char == "上":
- direc = [0, 1]
- elif char == "下":
- direc = [0, -1]
- elif char == "左":
- direc = [-1, 0]
- elif char == "右":
- direc = [1, 0]
- elif char == "左上":
- direc = [-1, 1]
- elif char == "左下":
- direc = [-1, -1]
- elif char == "右上":
- direc = [1, 1]
- elif char == "右下":
- direc = [1, -1]
- return direc
- sologan = True
- b = input("开始游戏(y/n):")
- while b != 'y' and b != 'n':
- b = input("开始游戏(y/n):")
- if b == 'n':
- sologan = False
- a = create()
- while sologan:
- char = input("请输入方向('0'退出):")
- while char == "" or char not in ["上", "下", "左", "右", "左上", "左下", "右上", "右下", '0']:
- char = input("请输入方向('0'退出):")
- if char == '0':
- break
- direc = isDirec(char)
- stepp = input("请输入步数(数字):")
- while not stepp.isdigit():
- stepp = input("请输入步数(数字):")
- stepp = int(stepp)
- print(f"向{char}移动{stepp}后,位置是{a(direc, stepp)}")
复制代码 |
|