欢迎加入QQ学习交流群,与我们一起学习,一起进步吧!
群号:225361733
可以QQ扫一扫加入群聊哦!
花,是一种很美丽的东西,所以人们去画花,去赏花……想不想开设你自己的花卉展?你可能会说 啥?不可能吧!呵呵…… 不不不!我没有骗你!你只需要一台电脑……我们用Python来画一些花!!!
我们要写个库,各位知道吧,我在这里说一下,这个库如果要使用的话必须和用到这个库的PY文件放到一起,就像这样:
而且使用库的话记得import flowers
,如:
好的,我们废话不多说!开始编写你的库吧!
由于这些花朵都是在网上选的材,用到的库不一,所以我们一共要输导入6个库的代码(其实是4个库,turtle库的导入方式不一):
import turtle as T
import math
import random
import time
from turtle import *
import turtle
def rose():
window = turtle.Screen()
window.bgcolor("white")
window.title("draw")
tt = turtle.Turtle()
tt.penup()
tt.left(90)
tt.fd(200)
tt.pendown()
tt.right(90)
tt.fillcolor("red")
tt.begin_fill()
tt.circle(10,180)
tt.circle(25,110)
tt.left(50)
tt.circle(60,45)
tt.circle(20,170)
tt.right(24)
tt.fd(30)
tt.left(10)
tt.circle(30,110)
tt.fd(20)
tt.left(40)
tt.circle(90,70)
tt.circle(30,150)
tt.right(30)
tt.fd(15)
tt.circle(80,90)
tt.left(15)
tt.fd(45)
tt.right(165)
tt.fd(20)
tt.left(155)
tt.circle(150,80)
tt.left(50)
tt.circle(150,90)
tt.end_fill()
tt.left(150)
tt.circle(-90,70)
tt.left(20)
tt.circle(75,105)
tt.setheading(60)
tt.circle(80,98)
tt.circle(-90,40)
tt.left(180)
tt.circle(90,40)
tt.circle(-80,98)
tt.setheading(-83)
tt.fd(30)
tt.left(90)
tt.fd(25)
tt.left(45)
tt.fillcolor("green")
tt.begin_fill()
tt.circle(-80,90)
tt.right(90)
tt.circle(-80,90)
tt.end_fill()
tt.right(135)
tt.fd(60)
tt.left(180)
tt.fd(85)
tt.left(90)
tt.fd(80)
tt.right(90)
tt.right(45)
tt.fillcolor("green")
tt.begin_fill()
tt.circle(80,90)
tt.left(90)
tt.circle(80,90)
tt.end_fill()
tt.left(135)
tt.fd(60)
tt.left(180)
tt.fd(60)
tt.right(90)
tt.circle(200,60)
tt.ht()
time.sleep(1)
clearscreen()
运行方法:
flowers.rose()
def flower():
def p_line(t, n, length, angle):
for i in range(n):
t.fd(length)
t.lt(angle)
def polygon(t, n, length):
angle = 360/n
p_line(t, n, length, angle)
def arc(t, r, angle):
arc_length = 2 * math.pi * r * abs(angle) / 360
n = int(arc_length / 4) + 1
step_length = arc_length / n
step_angle = float(angle) / n
t.lt(step_angle/2)
p_line(t, n, step_length, step_angle)
t.rt(step_angle/2)
def petal(t, r, angle):
for i in range(2):
arc(t, r, angle)
t.lt(180-angle)
def flower(t, n, r, angle, p):
for i in range(n):
petal(t, r, angle)
t.lt(p/n)
def leaf(t, r, angle, p):
t.begin_fill()
t.down()
flower(t, 1, r, angle, p)
t.end_fill()
def main():
window= turtle.Screen()
window.bgcolor("white")
window.title("draw")
lucy= turtle.Turtle()
lucy.shape("turtle")
lucy.color("red")
lucy.width(3)
flower(lucy, 7, 60, 100, 360)
lucy.color("brown")
lucy.rt(90)
lucy.fd(200)
lucy.width(1)
lucy.rt(270)
lucy.color("green")
leaf(lucy, 40, 80, 180)
lucy.rt(140)
lucy.color("black")
lucy.fd(30)
lucy.lt(180)
lucy.fd(30)
lucy.rt(120)
lucy.color("green")
leaf(lucy, 40, 80, 180)
lucy.color("black")
lucy.rt(140)
lucy.fd(30)
lucy.ht()
main()
time.sleep(1)
clearscreen()
使用本函数方法:
flowers.flower()
我发现网络上的奇人太多啦,这效果……我还是收录了
你确定不是小树?
代码还是挺多的哈
def broccoli():
window = turtle.Screen()
window.bgcolor("white")
window.title(u"draw")
def tree(plist,l,a,f):
if l>5:
lst=[]
for p in plist:
p.forward(l)
q = p.clone()
p.left(a)
q.right(a)
lst.append(p)
lst.append(q)
tree(lst,l*f,a,f)
def maketree(x,y,clr):
p = turtle.Turtle()
p.pensize(5)
p.color(clr)
p.hideturtle()
p.getscreen().tracer(30,0)
p.left(90)
p.penup()
p.goto(x,y)
p.pendown()
t=tree([p],200,20,0.6375)
def main():
maketree(0,-300,"green")
main()
time.sleep(1)
clearscreen()
使用函数方法:
flowers.broccoli()
def beautifulflower():
title("draw")
pensize(5)
begin_fill()
color("yellow")
dot(100)
fd(50)
color("red")
circle(25,231)
for i in range(6):
right(180)
circle(25,231)
end_fill()
color("black")
right(270)
penup()
goto(0,-50)
pendown()
circle(500,10)
begin_fill()
color("green")
right(180)
circle(50,90)
left(90)
circle(50,90)
end_fill()
right(90)
color("black")
circle(500,10)
begin_fill()
color("green")
left(90)
circle(50,90)
left(90)
circle(50,90)
end_fill()
color("black")
circle(500,10)
time.sleep(1)
clearscreen()
使用函数:
flowers.beautifulflower()
这个是最最最最最最帅的了!而且每次画出来的不一样哦!!!
代码:
def cherrytree():
title("draw")
def Tree(branch, t):
time.sleep(0.0005)
if branch > 3:
if 8 <= branch <= 12:
if random.randint(0, 2) == 0:
t.color('snow')
else:
t.color('lightcoral')
t.pensize(branch / 3)
elif branch < 8:
if random.randint(0, 1) == 0:
t.color('snow')
else:
t.color('lightcoral')
t.pensize(branch / 2)
else:
t.color('sienna')
t.pensize(branch / 10)
t.forward(branch)
a = 1.5 * random.random()
t.right(20 * a)
b = 1.5 * random.random()
Tree(branch - 10 * b, t)
t.left(40 * a)
Tree(branch - 10 * b, t)
t.right(20 * a)
t.up()
t.backward(branch)
t.down()
def Petal(m, t):
for i in range(m):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
t.up()
t.forward(b)
t.left(90)
t.forward(a)
t.down()
t.color('lightcoral')
t.circle(1)
t.up()
t.backward(a)
t.right(90)
t.backward(b)
t = T.Turtle()
w = T.Screen()
t.hideturtle()
t.getscreen().tracer(5, 0)
w.screensize(bg='wheat')
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')
Tree(60, t)
Petal(200, t)
time.sleep(1)
使用函数:
flowers.cherrytree()
def sunflower():
title("draw")
color('red','yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos())<1:
break
end_fill()
time.sleep(1)
clearscreen()
使用:
flowers.sunflower()
def unrealisticflower():
title("draw")
def draw_diamond(turt):
for i in range(1,3):
turt.forward(100)
turt.right(45)
turt.forward(100)
turt.right(135)
def draw_art():
window = turtle.Screen()
window.bgcolor("blue")
brad = turtle.Turtle()
brad.shape("turtle")
brad.color("red")
brad.speed('fast')
for i in range(1,37):
draw_diamond(brad)
brad.right(10)
brad.right(90)
brad.forward(260)
draw_art()
time.sleep(1)
clearscreen()
使用:
flowers.unrealisticflower()
我们编写完了花卉的代码,接下来写一下功能代码
def exit():
exitonclick()
作用是点击绘画框就关闭
使用方法:
flowers.exit()
呼!弄完啦!什么?你闲麻烦?好吧……这是全部的代码:
import turtle as T
import math
import random
import time
from turtle import *
import turtle
def broccoli():
window = turtle.Screen()
window.bgcolor("white")
window.title(u"draw")
def tree(plist,l,a,f):
if l>5:
lst=[]
for p in plist:
p.forward(l)
q = p.clone()
p.left(a)
q.right(a)
lst.append(p)
lst.append(q)
tree(lst,l*f,a,f)
def maketree(x,y,clr):
p = turtle.Turtle()
p.pensize(5)
p.color(clr)
p.hideturtle()
p.getscreen().tracer(30,0)
p.left(90)
p.penup()
p.goto(x,y)
p.pendown()
t=tree([p],200,20,0.6375)
def main():
maketree(0,-300,"green")
main()
time.sleep(1)
clearscreen()
def flower():
def p_line(t, n, length, angle):
for i in range(n):
t.fd(length)
t.lt(angle)
def polygon(t, n, length):
angle = 360/n
p_line(t, n, length, angle)
def arc(t, r, angle):
arc_length = 2 * math.pi * r * abs(angle) / 360
n = int(arc_length / 4) + 1
step_length = arc_length / n
step_angle = float(angle) / n
t.lt(step_angle/2)
p_line(t, n, step_length, step_angle)
t.rt(step_angle/2)
def petal(t, r, angle):
for i in range(2):
arc(t, r, angle)
t.lt(180-angle)
def flower(t, n, r, angle, p):
for i in range(n):
petal(t, r, angle)
t.lt(p/n)
def leaf(t, r, angle, p):
t.begin_fill()
t.down()
flower(t, 1, r, angle, p)
t.end_fill()
def main():
window= turtle.Screen()
window.bgcolor("white")
window.title("draw")
lucy= turtle.Turtle()
lucy.shape("turtle")
lucy.color("red")
lucy.width(3)
flower(lucy, 7, 60, 100, 360)
lucy.color("brown")
lucy.rt(90)
lucy.fd(200)
lucy.width(1)
lucy.rt(270)
lucy.color("green")
leaf(lucy, 40, 80, 180)
lucy.rt(140)
lucy.color("black")
lucy.fd(30)
lucy.lt(180)
lucy.fd(30)
lucy.rt(120)
lucy.color("green")
leaf(lucy, 40, 80, 180)
lucy.color("black")
lucy.rt(140)
lucy.fd(30)
lucy.ht()
main()
time.sleep(1)
clearscreen()
def rose():
window = turtle.Screen()
window.bgcolor("white")
window.title("draw")
tt = turtle.Turtle()
tt.penup()
tt.left(90)
tt.fd(200)
tt.pendown()
tt.right(90)
tt.fillcolor("red")
tt.begin_fill()
tt.circle(10,180)
tt.circle(25,110)
tt.left(50)
tt.circle(60,45)
tt.circle(20,170)
tt.right(24)
tt.fd(30)
tt.left(10)
tt.circle(30,110)
tt.fd(20)
tt.left(40)
tt.circle(90,70)
tt.circle(30,150)
tt.right(30)
tt.fd(15)
tt.circle(80,90)
tt.left(15)
tt.fd(45)
tt.right(165)
tt.fd(20)
tt.left(155)
tt.circle(150,80)
tt.left(50)
tt.circle(150,90)
tt.end_fill()
tt.left(150)
tt.circle(-90,70)
tt.left(20)
tt.circle(75,105)
tt.setheading(60)
tt.circle(80,98)
tt.circle(-90,40)
tt.left(180)
tt.circle(90,40)
tt.circle(-80,98)
tt.setheading(-83)
tt.fd(30)
tt.left(90)
tt.fd(25)
tt.left(45)
tt.fillcolor("green")
tt.begin_fill()
tt.circle(-80,90)
tt.right(90)
tt.circle(-80,90)
tt.end_fill()
tt.right(135)
tt.fd(60)
tt.left(180)
tt.fd(85)
tt.left(90)
tt.fd(80)
tt.right(90)
tt.right(45)
tt.fillcolor("green")
tt.begin_fill()
tt.circle(80,90)
tt.left(90)
tt.circle(80,90)
tt.end_fill()
tt.left(135)
tt.fd(60)
tt.left(180)
tt.fd(60)
tt.right(90)
tt.circle(200,60)
tt.ht()
time.sleep(1)
clearscreen()
def cherrytree():
title("draw")
def Tree(branch, t):
time.sleep(0.0005)
if branch > 3:
if 8 <= branch <= 12:
if random.randint(0, 2) == 0:
t.color('snow')
else:
t.color('lightcoral')
t.pensize(branch / 3)
elif branch < 8:
if random.randint(0, 1) == 0:
t.color('snow')
else:
t.color('lightcoral')
t.pensize(branch / 2)
else:
t.color('sienna')
t.pensize(branch / 10)
t.forward(branch)
a = 1.5 * random.random()
t.right(20 * a)
b = 1.5 * random.random()
Tree(branch - 10 * b, t)
t.left(40 * a)
Tree(branch - 10 * b, t)
t.right(20 * a)
t.up()
t.backward(branch)
t.down()
def Petal(m, t):
for i in range(m):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
t.up()
t.forward(b)
t.left(90)
t.forward(a)
t.down()
t.color('lightcoral')
t.circle(1)
t.up()
t.backward(a)
t.right(90)
t.backward(b)
t = T.Turtle()
w = T.Screen()
t.hideturtle()
t.getscreen().tracer(5, 0)
w.screensize(bg='wheat')
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')
Tree(60, t)
Petal(200, t)
time.sleep(1)
clearscreen()
def sunflower():
title("draw")
color('red','yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos())<1:
break
end_fill()
time.sleep(1)
clearscreen()
def unrealisticflower():
title("draw")
def draw_diamond(turt):
for i in range(1,3):
turt.forward(100)
turt.right(45)
turt.forward(100)
turt.right(135)
def draw_art():
window = turtle.Screen()
window.bgcolor("blue")
brad = turtle.Turtle()
brad.shape("turtle")
brad.color("red")
brad.speed('fast')
for i in range(1,37):
draw_diamond(brad)
brad.right(10)
brad.right(90)
brad.forward(260)
draw_art()
time.sleep(1)
clearscreen()
def beautifulflower():
title("draw")
pensize(5)
begin_fill()
color("yellow")
dot(100)
fd(50)
color("red")
circle(25,231)
for i in range(6):
right(180)
circle(25,231)
end_fill()
color("black")
right(270)
penup()
goto(0,-50)
pendown()
circle(500,10)
begin_fill()
color("green")
right(180)
circle(50,90)
left(90)
circle(50,90)
end_fill()
right(90)
color("black")
circle(500,10)
begin_fill()
color("green")
left(90)
circle(50,90)
left(90)
circle(50,90)
end_fill()
color("black")
circle(500,10)
time.sleep(1)
clearscreen()
def exit():
exitonclick()
各位看官老爷们如果有新的点子,可以在下方评论区发表代码,也可以私信我代码,或者发一下代表你点子的网址!如果很nice的话我会收录进去!