1. from turtle import *
    2. import math
    3. def Acircle(x0,y0,r):
    4. tracer(0)
    5. clear()
    6. for i in range(360):
    7. a = i/180*math.pi
    8. x = r*math.sin(a)+x0
    9. y = r*math.cos(a)+y0
    10. goto(x,y)
    11. tracer(1)
    12. s = 10
    13. b = 0
    14. while True:
    15. for i in range(10):
    16. Acircle(b,0,20)
    17. b = b + s
    18. sleep(0.05)
    19. s=-s

    运动的圆已经实现了,接下来使用键盘控制其运动方向。其中关键点就是控制圆的坐标,也就是上面程序的b=b+s;现在要实现的是当键盘按下时候再执行b=b+s;这里需要引入turtle库中的一个onkeypress()方法。

    1. from turtle import *
    2. def p():
    3. print("test")
    4. onkeypress(p,"space")
    5. listen()
    6. done()

    onkeypress()方法需要打开监听事件listen(),onkeypress(函数名,”键盘标识”)

    “space”—-空格键 “Return”—-回车键 “Up”、”Down”、”Left”、”Right”—-方向键 “a”、”b”、”c”、”d”——字母键 “1”、”2”、”3”、”.”、”。”、”\”、—-数字、符号键

    接下来我们就使用键盘监听事件来控制小球的运动:

    1. from turtle import *
    2. import math
    3. def Acircle(x0,y0,r):
    4. tracer(0)
    5. clear()
    6. for i in range(360):
    7. a = i/180*math.pi
    8. x = r*math.sin(a)+x0
    9. y = r*math.cos(a)+y0
    10. goto(x,y)
    11. tracer(1)
    12. x=0
    13. y=0
    14. s=10
    15. def moveUp_Acircle():
    16. global x,y
    17. y=y+s
    18. Acircle(x,y,20)
    19. onkeypress(moveUp_Acircle,"Up")
    20. listen()
    21. done()

    注意这里要在moveUp_Acircle()函数里把变量y声明成全局变量。
    现在已经能够上移键控制小球向上移动,继续丰富程序内容,实现上下左右移动控制。

    1. from turtle import *
    2. import math
    3. def Acircle(x0,y0,r):
    4. tracer(0)
    5. clear()
    6. for i in range(360):
    7. a = i/180*math.pi
    8. x = r*math.sin(a)+x0
    9. y = r*math.cos(a)+y0
    10. goto(x,y)
    11. tracer(1)
    12. x=0
    13. y=0
    14. s=10
    15. def moveUp_Acircle():
    16. global x,y
    17. y=y+s
    18. Acircle(x,y,20)
    19. def moveDown_Acircle():
    20. global x,y
    21. y=y-s
    22. Acircle(x,y,20)
    23. def moveLeft_Acircle():
    24. global x,y
    25. x=x-s
    26. Acircle(x,y,20)
    27. def moveRight_Acircle():
    28. global x,y
    29. x=x+s
    30. Acircle(x,y,20)
    31. onkeypress(moveUp_Acircle,"Up")
    32. onkeypress(moveDown_Acircle,"Down")
    33. onkeypress(moveLeft_Acircle,"Left")
    34. onkeypress(moveRight_Acircle,"Right")
    35. listen()
    36. done()

    程序优化:把Acircle()函数提出来,使用while True让程序一直循环执行起来。

    1. from turtle import *
    2. import math
    3. def Acircle(x0,y0,r):
    4. tracer(0)
    5. clear()
    6. for i in range(360):
    7. a = i/180*math.pi
    8. x = r*math.sin(a)+x0
    9. y = r*math.cos(a)+y0
    10. goto(x,y)
    11. tracer(1)
    12. x=0
    13. y=0
    14. s=10
    15. def moveUp_Acircle():
    16. global y
    17. y=y+s
    18. def moveDown_Acircle():
    19. global y
    20. y=y-s
    21. def moveLeft_Acircle():
    22. global x
    23. x=x-s
    24. def moveRight_Acircle():
    25. global x
    26. x=x+s
    27. while True:
    28. listen()
    29. onkeypress(moveUp_Acircle,"Up")
    30. onkeypress(moveDown_Acircle,"Down")
    31. onkeypress(moveLeft_Acircle,"Left")
    32. onkeypress(moveRight_Acircle,"Right")
    33. Acircle(x,y,20)

    注意这里onkeypress()方法中按键调用的函数不能带有参数,全局变量的声明必须紧邻该变量调用的地方。
    键盘控制移动的小球当作贪吃蛇的蛇头的话,接下来需要随机出现的食物。

    1. from turtle import *
    2. import math
    3. import random
    4. from time import sleep
    5. def foodBall(x0,y0,r):
    6. tracer(0)
    7. clear()
    8. for i in range(360):
    9. a = i/180*math.pi
    10. x = r*math.sin(a)+x0
    11. y = r*math.cos(a)+y0
    12. goto(x,y)
    13. tracer(1)
    14. while True:
    15. foodBall(random.randint(-300,300),random.randint(-300,300),5)
    16. sleep(0.5)

    程序运行结果如下,每次随机出现都会带有一个尾巴
    foodball.gif
    程序修改如下:

    1. from turtle import *
    2. import math
    3. import random
    4. from time import sleep
    5. def foodBall(x0,y0,r):
    6. tracer(0)
    7. penup()
    8. goto(x0,y0+r)
    9. pendown()
    10. clear()
    11. for i in range(360):
    12. a = i/180*math.pi
    13. x = r*math.sin(a)+x0
    14. y = r*math.cos(a)+y0
    15. goto(x,y)
    16. tracer(1)
    17. while True:
    18. foodBall(random.randint(-300,300),random.randint(-300,300),5)
    19. sleep(0.5)

    对比发现,直线运动的小球和随机出现的小球的程序几乎是一样的,那么两个程序就可以合并如下:

    1. from turtle import *
    2. import math
    3. import random
    4. from time import sleep
    5. last_x0=0
    6. last_y0=0
    7. def Ball(x0,y0,r):
    8. ht()#隐藏箭头画笔
    9. global last_x0,last_y0
    10. penup()
    11. goto(x0,y0+r)
    12. tracer(0)
    13. # clear()
    14. for i in range(360):
    15. a = i/180*math.pi
    16. x = r*math.sin(a)+x0
    17. y = r*math.cos(a)+y0
    18. pendown()
    19. goto(x,y)
    20. tracer(1)
    21. x=0
    22. y=0
    23. xr = random.randint(-200,200)
    24. yr = random.randint(-200,200)
    25. def moveUp():
    26. global y
    27. y=y+1
    28. def moveDown():
    29. global y
    30. y=y-1
    31. def moveLeft():
    32. global x
    33. x=x-1
    34. def moveRight():
    35. global x
    36. x=x+1
    37. def headBall():
    38. listen()
    39. onkeypress(moveUp,"Up")
    40. onkeypress(moveDown,"Down")
    41. onkeypress(moveLeft,"Left")
    42. onkeypress(moveRight,"Right")
    43. Ball(x,y,10)
    44. def foodBall():
    45. global xr,yr
    46. Ball(xr,yr,5)
    47. while True:
    48. foodBall()
    49. headBall()
    50. print(xr,yr,x,y)