Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import time
import copy
import serial
def main():
text = ""
ser = serial.Serial()
#Arudinoの場所
ser.port = '/dev/tty.usbmodem1411'
ser.baudrate = 9600
ser.open()
while (text != "q"):
print "start!"
text = raw_input()
#入力されたコマンドを判断しじっこうする
#終わったら軍が動いてる2秒間停止する
command(text,ser)
time.sleep(2.0)
def command(text,ser):
pata = '2'
pon = '8'
don = '7'
chaka = '4'
#出力用辞書
diction = {'2':'pata', '8':'pon', '7':'don', '4':'chaka'}
#命令コマンドリスト
go = [pata,pata,pata,pon]
back = [pon,pata,pon,pata]
atk = [pon,pon,pata,pon]
guard = [chaka,chaka,pata,pon]
charge = [pon,pon,chaka,chaka]
jump = [don,don,chaka,chaka]
lis_tmp = []
#タイポしても許容出来る様な感じの選択肢にした
if text == "go" or text == "g":
lis_tmp = copy.copy(go)
elif text == "back" or text == "b" or text == "ba" or text == "bac" or text == "bak":
lis_tmp = copy.copy(back)
elif text == "atk" or text == "at" or text == "a" or text == "ak" or text == "attack" or text == "atack":
lis_tmp = copy.copy(atk)
elif text == "guard" or text == "gu" or text == "def":
lis_tmp = copy.copy(guard)
elif text == "charge" or text == "c" or text == "ch" or text == "cha" :
lis_tmp = copy.copy(charge)
elif text == "jump" or text == "j" or text == "jp" or text == "ju" or text == "jm":
lis_tmp = copy.copy(jump)
#動作部
#1コマンド送って0.5秒止まる
while(len(lis_tmp) != 0):
tmp = lis_tmp.pop(0)
print diction[tmp]
ser.write(tmp)
time.sleep(0.5)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment