利用turtle库写的python程序《各站停靠》
灵感来源于苏打绿同名单曲(三年之约快回归吧)
python的turtle库系统介绍可参考以下链接,百度搜索也有很多大大们写了教程
https://docs.python.org/2/library/turtle.html
1. 设计思路:
这个程序名字叫做《各站停靠》
灵感来自苏打绿的同名歌曲,讲的是庄周梦蝶的故事
场景一:“我们到底在追求些什么?
需要蝴蝶(小乌龟)来寻找前世的自己
首先在弹框中输入1-10中的lucky number
这个数字会乘上一个随机数作为寻找半径(寻找点是随机生成的)
为了方便找对位置,后台输出四个数,分别是:
蝴蝶所在横纵坐标,随机生成的横纵坐标
鼠标点击让蝴蝶运动,到达范围内后跳转界面(出现提示框,需要输入1)
场景二:“念念不忘必有回响”
生成前世的树,洒落落叶(这个时候不要点鼠标,因为之前的程序还在,哭了我有空改改)
最后小乌龟会再出来
2.部分代码与最终结果
#初始化部分
import turtle
import random
from time import sleep
screen=turtle.Screen()
screen.setup(640,480)
turtle.bgpic(r'backflower.gif')#下面放了这个图,注意一定要用gif
#不知道为什么向tk和turtle都只能用gif图片作为背景,而且插入进去也不动
butterfly=turtle.Turtle(shape='turtle')
butterfly.fillcolor("yellow")
butterfly.pensize(4)
turtle.penup()
butterfly.penup()
turtle.goto(-300,60)
butterfly.goto(-300,60)
turtle.pencolor("pink")
turtle.write("每一只蝴蝶\n""都是从前一朵花的鬼魂\n""回来\n""寻找她自己\n",font=("STXinwei",18))
def cursorxy(event):
x=event.x-360
y=240-event.y
def IPLN():
guess=turtle.simpledialog.askinteger("请输入你的幸运数字","your lucky number is: ")
return guess*random.randint(5,10)
def TTNP():
gono=turtle.simpledialog.askinteger("蝴蝶找到了花的位置!", "input '1'to go on: ")
if gono==1:
print(1)
turtle.clear()
butterfly.hideturtle()
turtle.bgpic(picname='nopic')
turtle.bgcolor("pink")
turtle.pencolor("white")
turtle.goto(-300,110)
turtle.write("念念不忘,必有回响\n""你所想的\n""终有一天\n""会成为你所成为的\n",font=("STXinwei", 18))
turtle.penup()
turtle.goto(0,-200)
turtle.hideturtle()
turtle.left(90)
turtle.pendown()
tree(60)
downflower(100)
butterfly.goto(250,150)
butterfly.left(160)
turtle.setx(-200)
butterfly.showturtle()
butterfly.color("snow")
turtle.color("snow")
turtle.write("庄周为蝶,你我亦为蝶",font=("STXinwei", 33))
#生成树的递归函数设计(这个是借鉴的网上的)
def tree(branchLen):
sleep(0.0005)
if branchLen > 3:
if 8 <= branchLen <= 12:
if random.randint(0, 2) == 0:
turtle.color('snow')
else:
turtle.color('lightcoral')
turtle.pensize(branchLen / 3)
elif branchLen < 8:
if random.randint(0, 1) == 0:
turtle.color('snow')
else:
turtle.color('lightcoral')
turtle.pensize(branchLen / 2)
else:
turtle.color('sienna')
turtle.pensize(branchLen / 10)
turtle.forward(branchLen)
a = 1.5 * random.random()
turtle.right(20 * a)
b = 1.5 * random.random()
tree(branchLen - 10 * b)
turtle.left(40 * a)
tree(branchLen - 10 * b)
turtle.right(20 * a)
turtle.penup()
turtle.backward(branchLen)
turtle.pendown()
#随机落叶
def downflower(m):
for i in range(m):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
turtle.up()
turtle.forward(b)
turtle.left(90)
turtle.forward(a)
turtle.down()
turtle.color('lightcoral') # 淡珊瑚色
turtle.circle(1)
turtle.up()
turtle.backward(a)
turtle.right(90)
turtle.backward(b)
def Right():
butterfly.right(random.randint(0, 90))
def creat_the_place():
x0,y0=random.randint(-320,320),random.randint(-240,240)
return x0,y0
def bg(x,y):
butterfly.goto(x,y)
print(x,y,x0,y0)
if (x - x0) ** 2 + (y - y0) ** 2 <= maxm ** 2:
TTNP()
def connection():
global maxm,x0,y0
maxm = IPLN()
x0,y0=creat_the_place()
screen.onkey(Right, "space")
screen.onclick(bg)
screen.cv.bind("<Motion>",cursorxy)
butterfly.pencolor("pink")
butterfly.pendown()
connection()
screen.listen()
screen.mainloop()
turtle.penup()
turtle.goto(230,-250)
turtle.pencolor("lightgreen")
turtle.write("by 阽旿",font=("STXinwei",10))