import turtle as t
t.up()
t.hideturtle()
t.tracer(False)
snake = {‘x’:10,‘y’:0}
#画蛇
def draw(x,y):
#移动画笔坐标
t.goto(x,y)
#绘制一节蛇
t.dot(30,‘red’)
#移动蛇
def move():
t.clear()
draw(snake[‘x’], snake[‘y’])
snake[‘x’] +=0
snake[‘y’] -= 5
t.ontimer(move,50)
snake = [{‘x’:10,‘y’:0},{‘x’:10,‘y’:32},{‘x’:10,‘y’:64}]
#画蛇
def draw(x,y):
#移动画笔坐标
t.goto(x,y)
#绘制一节蛇
t.dot(30,'red')
#移动蛇
def move():
t.clear()
# head = snake[0].copy()
# head[‘x’] += direction[‘x’]
# head[‘y’] += direction[‘y’]
#贪吃蛇增加一节
#贪吃蛇减少一节
#画出三个蛇
for body in snake:
draw(body['x'], body['y'])
for body in snake:
body['y'] -= 15
# snake['x'] +=0
t.ontimer(move,1000)
#控制蛇向右
def right():
snake[‘x’] += 30
#控制蛇向左
def left():
snake[‘x’] -= 30
#控制蛇向上
def up():
snake[‘y’] += 30
#控制蛇向下
def down():
snake[‘x’] += 30
move()
#绑定键盘事件
t.onkeypress(right,‘Right’)
t.onkeypress(left,‘Left’)
t.onkeypress(up,‘Up’)
t.onkeypress(down,‘Down’)
t.listen()
t.done()