小程序6:贪吃蛇(字典加函数)
代码实现:
# -*- coding: utf-8 -*-
# !/user/bin/env python
x = 0
y = 0
def up(): # w
global y
y+=1
def down(): # s
global y
y-=1
def right(): #a
global x
x+=1
def left(): #d
global x
x-=1
mydict = {'w':up , 's':down , 'a':right , 'd':left}
while True:
action = raw_input()
try:
mydict[action]() # 函数加括号就是调用
except KeyError:
print('输入错了')
pass