from turtle import *import mathdef Acircle(x0,y0,r):tracer(0)clear()for i in range(360):a = i/180*math.pix = r*math.sin(a)+x0y = r*math.cos(a)+y0goto(x,y)tracer(1)s = 10b = 0while True:for i in range(10):Acircle(b,0,20)b = b + ssleep(0.05)s=-s
运动的圆已经实现了,接下来使用键盘控制其运动方向。其中关键点就是控制圆的坐标,也就是上面程序的b=b+s;现在要实现的是当键盘按下时候再执行b=b+s;这里需要引入turtle库中的一个onkeypress()方法。
from turtle import *def p():print("test")onkeypress(p,"space")listen()done()
onkeypress()方法需要打开监听事件listen(),onkeypress(函数名,”键盘标识”)
“space”—-空格键 “Return”—-回车键 “Up”、”Down”、”Left”、”Right”—-方向键 “a”、”b”、”c”、”d”——字母键 “1”、”2”、”3”、”.”、”。”、”\”、—-数字、符号键
接下来我们就使用键盘监听事件来控制小球的运动:
from turtle import *import mathdef Acircle(x0,y0,r):tracer(0)clear()for i in range(360):a = i/180*math.pix = r*math.sin(a)+x0y = r*math.cos(a)+y0goto(x,y)tracer(1)x=0y=0s=10def moveUp_Acircle():global x,yy=y+sAcircle(x,y,20)onkeypress(moveUp_Acircle,"Up")listen()done()
注意这里要在moveUp_Acircle()函数里把变量y声明成全局变量。
现在已经能够上移键控制小球向上移动,继续丰富程序内容,实现上下左右移动控制。
from turtle import *import mathdef Acircle(x0,y0,r):tracer(0)clear()for i in range(360):a = i/180*math.pix = r*math.sin(a)+x0y = r*math.cos(a)+y0goto(x,y)tracer(1)x=0y=0s=10def moveUp_Acircle():global x,yy=y+sAcircle(x,y,20)def moveDown_Acircle():global x,yy=y-sAcircle(x,y,20)def moveLeft_Acircle():global x,yx=x-sAcircle(x,y,20)def moveRight_Acircle():global x,yx=x+sAcircle(x,y,20)onkeypress(moveUp_Acircle,"Up")onkeypress(moveDown_Acircle,"Down")onkeypress(moveLeft_Acircle,"Left")onkeypress(moveRight_Acircle,"Right")listen()done()
程序优化:把Acircle()函数提出来,使用while True让程序一直循环执行起来。
from turtle import *import mathdef Acircle(x0,y0,r):tracer(0)clear()for i in range(360):a = i/180*math.pix = r*math.sin(a)+x0y = r*math.cos(a)+y0goto(x,y)tracer(1)x=0y=0s=10def moveUp_Acircle():global yy=y+sdef moveDown_Acircle():global yy=y-sdef moveLeft_Acircle():global xx=x-sdef moveRight_Acircle():global xx=x+swhile True:listen()onkeypress(moveUp_Acircle,"Up")onkeypress(moveDown_Acircle,"Down")onkeypress(moveLeft_Acircle,"Left")onkeypress(moveRight_Acircle,"Right")Acircle(x,y,20)
注意这里onkeypress()方法中按键调用的函数不能带有参数,全局变量的声明必须紧邻该变量调用的地方。
键盘控制移动的小球当作贪吃蛇的蛇头的话,接下来需要随机出现的食物。
from turtle import *import mathimport randomfrom time import sleepdef foodBall(x0,y0,r):tracer(0)clear()for i in range(360):a = i/180*math.pix = r*math.sin(a)+x0y = r*math.cos(a)+y0goto(x,y)tracer(1)while True:foodBall(random.randint(-300,300),random.randint(-300,300),5)sleep(0.5)
程序运行结果如下,每次随机出现都会带有一个尾巴
程序修改如下:
from turtle import *import mathimport randomfrom time import sleepdef foodBall(x0,y0,r):tracer(0)penup()goto(x0,y0+r)pendown()clear()for i in range(360):a = i/180*math.pix = r*math.sin(a)+x0y = r*math.cos(a)+y0goto(x,y)tracer(1)while True:foodBall(random.randint(-300,300),random.randint(-300,300),5)sleep(0.5)
对比发现,直线运动的小球和随机出现的小球的程序几乎是一样的,那么两个程序就可以合并如下:
from turtle import *import mathimport randomfrom time import sleeplast_x0=0last_y0=0def Ball(x0,y0,r):ht()#隐藏箭头画笔global last_x0,last_y0penup()goto(x0,y0+r)tracer(0)# clear()for i in range(360):a = i/180*math.pix = r*math.sin(a)+x0y = r*math.cos(a)+y0pendown()goto(x,y)tracer(1)x=0y=0xr = random.randint(-200,200)yr = random.randint(-200,200)def moveUp():global yy=y+1def moveDown():global yy=y-1def moveLeft():global xx=x-1def moveRight():global xx=x+1def headBall():listen()onkeypress(moveUp,"Up")onkeypress(moveDown,"Down")onkeypress(moveLeft,"Left")onkeypress(moveRight,"Right")Ball(x,y,10)def foodBall():global xr,yrBall(xr,yr,5)while True:foodBall()headBall()print(xr,yr,x,y)
