亲宝软件园·资讯

展开

Python月饼

猿童学 人气:0

一、效果展示

话不多说先上效果

为了更有意境我加了个完美的背景来衬托出月饼的好看

我的月饼画的不圆的原因是我故意的,为什么呢?

因为月有阴晴圆缺啊!哈哈!

上面是直接画的,没有填充,接下来我们画一个有填充的。

也可以添加好看的背景噢。

二、程序代码

代码将在下面解释,可以看着代码对应下面的函数解释。

第一种:

import turtle
import math
turtle.title("照彻乾坤,印透山河")
turtle.bgpic("yueliang/1.png")
 
p = turtle.Turtle()
p.pensize(4)
p.speed(3)
 
#做月饼的面
def ellipse(a,b,n=500,c='#CD950C'):
    p.penup()
    p.setpos(a,0)
 
    color = p.color(c)
    p.pendown()
 
    for i in range(n):
        radian = 2 * math.pi /n
        theta = (i+1) * radian
        next_point = (a * math.cos(theta)),b * math.sin(theta)
        p.setpos(next_point)
 
ellipse(300,200,c='#CD950C')
ellipse(296,196,c='#FFB90F')
ellipse(292,192,c='#FFD700')
 
#做月饼的底
turtle.penup()
turtle.goto(-10,-300)
turtle.color("#CD950C")
turtle.pensize(6)
turtle.pendown()
 
turtle.circle(430,42)#33
turtle.circle(420,-84)#-60
turtle.goto(-296,-40)
turtle.penup()
turtle.goto(296,-40)
turtle.pendown()
turtle.right(54)
turtle.forward(151)
turtle.penup()
turtle.goto(0,0)
turtle.pendown()
 
 
 
def draw(a1,b1,a2,b2,f='#CD950C'):#边角的2个点,分别是(a1,b1),(a2,b2)
    turtle.penup()
    turtle.goto(a1,b1)
    turtle.pensize(6)
    turtle.color(f)
    turtle.pendown()
    turtle.goto(a2,b1)
    turtle.goto(a2,b2)
    turtle.penup()
    return()
 
#下面是4个角图案
draw(260,78,130,170,'#FFD700')
draw(250,90,150,160)
draw(240,102,172,150)
draw(230,114,194,140)
 
draw(-260,-78,-130,-170,'#FFD700')
draw(-250,-90,-150,-160)
draw(-240,-102,-172,-150)
draw(-230,-114,-194,-140)
 
draw(260,-78,130,-170,'#FFD700')
draw(250,-90,150,-160)
draw(240,-102,172,-150)
draw(230,-114,194,-140)
 
draw(-262,78,-130,170,'#FFD700')
draw(-250,90,-150,160)
draw(-240,102,-172,150)
draw(-230,114,-194,140)
 
 
 
 
turtle.penup()
turtle.pensize(6)
turtle.color("#CD950C")
turtle.goto(-270,66)
turtle.pendown()
turtle.goto(-150,66)
turtle.goto(-150,-66)
turtle.goto(-270,-66)
turtle.goto(-270,-54)
turtle.goto(-170,-54)
turtle.goto(-170,54)
turtle.goto(-276,54)
turtle.goto(-276,42)
turtle.goto(-190,42)
turtle.goto(-190,-42)
turtle.goto(-276,-42)
turtle.goto(-276,-30)
turtle.goto(-210,-30)
turtle.goto(-210,30)
turtle.goto(-279,30)
turtle.goto(-279,18)
turtle.goto(-230,18)
turtle.goto(-230,-18)
turtle.goto(-282,-18)
turtle.goto(-282,-2)
turtle.goto(-258,-2)
turtle.penup()
 
turtle.penup()
turtle.goto(-128,-2)
turtle.pendown()
turtle.color("#FFD700")
turtle.pensize(2)
turtle.goto(126,-2)
turtle.penup()
 
turtle.penup()
turtle.pensize(6)
turtle.color("#CD950C")
turtle.goto(270,66)
turtle.pendown()
turtle.goto(150,66)
turtle.goto(150,-66)
turtle.goto(270,-66)
turtle.goto(270,-54)
turtle.goto(170,-54)
turtle.goto(170,54)
turtle.goto(276,54)
turtle.goto(276,42)
turtle.goto(190,42)
turtle.goto(190,-42)
turtle.goto(276,-42)
turtle.goto(276,-30)
turtle.goto(210,-30)
turtle.goto(210,30)
turtle.goto(279,30)
turtle.goto(279,18)
turtle.goto(230,18)
turtle.goto(230,-18)
turtle.goto(282,-18)
turtle.goto(282,-2)
turtle.goto(258,-2)
turtle.penup()
 
turtle.penup()
turtle.pensize(6)
turtle.color("#CD950C")
turtle.goto(-110,174)
turtle.pendown()
turtle.goto(-110,90)
turtle.goto(110,90)
turtle.goto(110,174)
turtle.goto(90,174)
turtle.goto(90,102)
turtle.goto(-90,102)
turtle.goto(-90,176)
turtle.goto(-70,176)
turtle.goto(-70,114)
turtle.goto(70,114)
turtle.goto(70,176)
turtle.goto(50,176)
turtle.goto(50,126)
turtle.goto(-50,126)
turtle.goto(-50,180)
turtle.goto(-30,180)
turtle.goto(-30,138)
turtle.goto(30,138)
turtle.goto(30,182)
turtle.goto(10,182)
turtle.goto(-10,182)
turtle.goto(-10,150)
turtle.goto(10,150)
turtle.goto(10,166)
turtle.penup()
 
turtle.penup()
turtle.goto(10,76)
turtle.pendown()
turtle.pensize(2)
turtle.color("#FFD700")
turtle.goto(10,-75)
turtle.penup()
 
 
turtle.penup()
turtle.pensize(6)
turtle.color("#CD950C")
turtle.goto(-110,-174)
turtle.pendown()
turtle.goto(-110,-90)
turtle.goto(110,-90)
turtle.goto(110,-174)
turtle.goto(90,-174)
turtle.goto(90,-102)
turtle.goto(-90,-102)
turtle.goto(-90,-176)
turtle.goto(-70,-176)
turtle.goto(-70,-114)
turtle.goto(70,-114)
turtle.goto(70,-176)
turtle.goto(50,-176)
turtle.goto(50,-126)
turtle.goto(-50,-126)
turtle.goto(-50,-180)
turtle.goto(-30,-180)
turtle.goto(-30,-138)
turtle.goto(30,-138)
turtle.goto(30,-182)
turtle.goto(10,-182)
turtle.goto(-10,-182)
turtle.goto(-10,-150)
turtle.goto(10,-150)
turtle.goto(10,-166)
turtle.penup()
 
 
#中间金色边框
turtle.penup()
turtle.goto(-130,170)
draw(-130,78,130,-78,'#FFD700')
draw(130,-78,-130,78,'#FFD700')
turtle.penup()
 
#中间金色字体
turtle.penup()
turtle.speed(1)
turtle.penup()
turtle.goto(-100,0)
turtle.pendown()
turtle.color('#D2691E')
turtle.write("花",font=("隶书",60,"bold"))
turtle.penup()
turtle.goto(30,0)
turtle.pendown()
turtle.write("好",font=("隶书",60,"bold"))
turtle.penup()
turtle.goto(-100,-80)
turtle.pendown()
turtle.write("月",font=("隶书",60,"bold"))
turtle.penup()
turtle.goto(30,-80)
turtle.pendown()
turtle.write("圆",font=("隶书",60,"bold"))
turtle.penup()
 
 
 
 
p.hideturtle()
turtle.mainloop()

第二种:

import turtle
turtle.title("祝大家中秋节快乐!")
# turtle.bgpic("hua/1.png")
 
turtle.speed(100)
turtle.color("#F5E16F")
for i in range(20):
 # 顺时针移动18度
 turtle.right(18)
 turtle.begin_fill()
 # 向前移动的距离
 turtle.forward(220)
 # 画半径为 40 的半圆
 turtle.circle(40, 180)
 # 画完半圆之后回到(0,0)
 turtle.goto(0, 0)
 turtle.right(360)
 turtle.end_fill()
# 设置画笔粗细
turtle.pensize(20)
# 填充颜色(外部、内部)
turtle.color("#F5E16F", "#FF9933")
turtle.goto(0, -200)
# 准备开始填充
turtle.begin_fill()
turtle.circle(200)
# 填充结束
turtle.end_fill()
turtle.right(360)
turtle.color('#F5E16F')
turtle.goto(0, -180)
for i in range(12):
 turtle.begin_fill()
 turtle.circle(60, 120)
 turtle.left(180)
 turtle.circle(60, 120)
 turtle.end_fill()
 
 
turtle.penup()
turtle.goto(-80,-40)
turtle.pendown()
turtle.color("#F5E16F")
turtle.write("团圆", font=("隶书", 60, "bold"))
 
 
turtle.mainloop()

三、素材背景

将图片放在文件夹中

本文将1.png放在yueliang文件夹中

1.png

2.png

3.png

 更多的背景可以在网上搜索获得。

四、原理解释

1、概念

turtle(海龟)是Python重要的标准库之一,它能够进行基本的图形绘制,其概念诞生于1969年。turtle是最有价值的程序设计入门实践库,它是程序设计入门层面最常用的基本绘图库。

turtle的绘图原理:

有一只海龟处于画布正中心,由程序控制在画布上游走;

海龟走过的轨迹形成了绘制的图形

海龟由程序控制,可改变其大小,颜色等

使用 import 保留字对 turtle 库的引用以下方式:

import turtle

2、函数

窗体函数

turtle.setup()函数与窗体有关,定义如下:

turtle.setup(width,height,startx,starty)

作用:设置主窗体的大小和位置

参数:

 画笔状态函数

函数描述
pendown( )放下画笔
penup()提起画笔
pensize(width)设置画笔线条的粗细为指定大小
pencolor()设置画笔的颜色
color( a,b)同时设置画笔和填充颜色
begin_fill()填充恩形前,调用该方法
end_fill( )填充图形结束
filling()返回填充的状态,True为填充。False为未填充
clear( )清空当前窗口,但不改变当前画笔的位置
reset()清空当前窗口,并重置位置等状态为默认值
screensize( )设置画布窗口的宽度、高度和背昱颜色
hideturtle()隐藏画笔的turtle形状
showturtle( )显示画笔的turtle形状
isvisible( )如果turtle可见,则返回True
write(str,font=None)输出font字体的字符串

画笔运动函数

函数描述
fd(distance)沿着当前方向前进指定距离
bk(distance)沿着当前相反方向后退指定距离
right(angle)向右旋转angle角度
left(angle)向左旋转angle角度
goto(x,y)移动到绝对坐标xy)处
sex(x)修改画笔的横坐标到次,纵坐标不变
sety(y)修改画笔的纵坐标到y,横坐标不变
seth(angle)设置当前朝向为angle角度
home()设置当前画笔位置为原点,朝向东
circle(radius,e)绘制一个指定半径r和角度e的圆或新形
dot(r.color)绘制一个指定半径r和预色color的圆点
undo( )撤销画笔的最后一步动作
speed()设置画笔的绘制速度,参数为0~10之间

颜色代码大全

可以选择自己喜欢的颜色画。

加载全部内容

相关教程
猜你喜欢
用户评论