from turtle import *
import math
def Acircle(x0,y0,r):
tracer(0)
clear()
for i in range(360):
a = i/180*math.pi
x = r*math.sin(a)+x0
y = r*math.cos(a)+y0
goto(x,y)
tracer(1)
s = 10
b = 0
while True:
for i in range(10):
Acircle(b,0,20)
b = b + s
sleep(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 math
def Acircle(x0,y0,r):
tracer(0)
clear()
for i in range(360):
a = i/180*math.pi
x = r*math.sin(a)+x0
y = r*math.cos(a)+y0
goto(x,y)
tracer(1)
x=0
y=0
s=10
def moveUp_Acircle():
global x,y
y=y+s
Acircle(x,y,20)
onkeypress(moveUp_Acircle,"Up")
listen()
done()
注意这里要在moveUp_Acircle()函数里把变量y声明成全局变量。
现在已经能够上移键控制小球向上移动,继续丰富程序内容,实现上下左右移动控制。
from turtle import *
import math
def Acircle(x0,y0,r):
tracer(0)
clear()
for i in range(360):
a = i/180*math.pi
x = r*math.sin(a)+x0
y = r*math.cos(a)+y0
goto(x,y)
tracer(1)
x=0
y=0
s=10
def moveUp_Acircle():
global x,y
y=y+s
Acircle(x,y,20)
def moveDown_Acircle():
global x,y
y=y-s
Acircle(x,y,20)
def moveLeft_Acircle():
global x,y
x=x-s
Acircle(x,y,20)
def moveRight_Acircle():
global x,y
x=x+s
Acircle(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 math
def Acircle(x0,y0,r):
tracer(0)
clear()
for i in range(360):
a = i/180*math.pi
x = r*math.sin(a)+x0
y = r*math.cos(a)+y0
goto(x,y)
tracer(1)
x=0
y=0
s=10
def moveUp_Acircle():
global y
y=y+s
def moveDown_Acircle():
global y
y=y-s
def moveLeft_Acircle():
global x
x=x-s
def moveRight_Acircle():
global x
x=x+s
while 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 math
import random
from time import sleep
def foodBall(x0,y0,r):
tracer(0)
clear()
for i in range(360):
a = i/180*math.pi
x = r*math.sin(a)+x0
y = r*math.cos(a)+y0
goto(x,y)
tracer(1)
while True:
foodBall(random.randint(-300,300),random.randint(-300,300),5)
sleep(0.5)
程序运行结果如下,每次随机出现都会带有一个尾巴
程序修改如下:
from turtle import *
import math
import random
from time import sleep
def foodBall(x0,y0,r):
tracer(0)
penup()
goto(x0,y0+r)
pendown()
clear()
for i in range(360):
a = i/180*math.pi
x = r*math.sin(a)+x0
y = r*math.cos(a)+y0
goto(x,y)
tracer(1)
while True:
foodBall(random.randint(-300,300),random.randint(-300,300),5)
sleep(0.5)
对比发现,直线运动的小球和随机出现的小球的程序几乎是一样的,那么两个程序就可以合并如下:
from turtle import *
import math
import random
from time import sleep
last_x0=0
last_y0=0
def Ball(x0,y0,r):
ht()#隐藏箭头画笔
global last_x0,last_y0
penup()
goto(x0,y0+r)
tracer(0)
# clear()
for i in range(360):
a = i/180*math.pi
x = r*math.sin(a)+x0
y = r*math.cos(a)+y0
pendown()
goto(x,y)
tracer(1)
x=0
y=0
xr = random.randint(-200,200)
yr = random.randint(-200,200)
def moveUp():
global y
y=y+1
def moveDown():
global y
y=y-1
def moveLeft():
global x
x=x-1
def moveRight():
global x
x=x+1
def headBall():
listen()
onkeypress(moveUp,"Up")
onkeypress(moveDown,"Down")
onkeypress(moveLeft,"Left")
onkeypress(moveRight,"Right")
Ball(x,y,10)
def foodBall():
global xr,yr
Ball(xr,yr,5)
while True:
foodBall()
headBall()
print(xr,yr,x,y)