Last active
January 19, 2018 03:44
-
-
Save wwj718/3ade708bc6e7f3cf669dc38a998000cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
import turtle | |
import time | |
# 处理python2/python3兼容性 | |
try: | |
input = raw_input | |
except NameError: | |
pass | |
def do_action(action): | |
if action == "f": | |
turtle.forward(25) | |
if action == "b": | |
turtle.backward(25) | |
if action == "l": | |
turtle.left(90) | |
if action == "r": | |
turtle.right(90) | |
time.sleep(1) # 休眠1秒 | |
def call_function(function_actions,call_num): | |
for i in range(call_num): | |
for action in function_actions: | |
do_action(action) | |
# 数字映射 | |
call_num_dict = { | |
"1":1, | |
"2":2, | |
"3":3, | |
"4":4 | |
} | |
while True: | |
# 获取移动指令输入,在硬件上为获取插槽的输入 | |
function_actions = input("请输入函数部分[F(前),B(后),L(左),R(右),#(退出)]:").lower() | |
command_actions = input("请输入移动指令[F(前),B(后),L(左),R(右),数字(1表示调用一次,4表示调用4次),#(退出)]:").lower() | |
if ("#" in command_actions) or ("#" in function_actions) : | |
break | |
for action in command_actions: | |
call_num = call_num_dict.get(action,None) | |
if call_num: | |
call_function(function_actions,call_num) #执行函数 | |
else: | |
do_action(action) #执行动作 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment