python/用turtle实现画图,弘扬传统文化,画一个可可爱爱的大汤圆
水水和小组伙伴做了一个课上的小任务,用turtle画图,弘扬传统文化,我们做了一个可可爱爱的大汤圆和一碗小汤圆。
话不多说,先看效果!!!
说实话,他或许不够美观,但是论搞笑还是可以的,不管是我们可爱的大汤圆绿帽子,红棉袄的搭配,还是我们那把奇奇怪怪的勺子,真的很好玩,水水在课上展示的时候也为我们的绿帽子,红棉袄做了很勉强的找补。。。
以下是我们的全部代码。
import turtle
import math
# 设置窗口大小和背景颜色
turtle.setup(1000, 800)
turtle.bgcolor("floralwhite")
turtle.pensize(3)
wan_a = 120 # 长半轴
wan_b= 110 # 短半轴
turtle.speed(1000)
turtle.penup()
turtle.goto(-wan_a, 0)#椭圆的最左边
turtle.pendown()
turtle.begin_fill()
for i in range(180,360):
x = wan_a * math.cos(math.radians(i))
y = wan_b * math.sin(math.radians(i))
turtle.goto(x, y)
turtle.fillcolor("red")
turtle.end_fill()
w_a = 120 # 长半轴
w_b = 50 # 短半轴
turtle.speed(1000)
turtle.penup()
turtle.goto(w_a, 0)
turtle.pendown()
turtle.begin_fill()
for i in range(360):
x =w_a * math.cos(math.radians(i))
y = w_b * math.sin(math.radians(i))
turtle.goto(x, y)
turtle.fillcolor("beige")
turtle.end_fill()
n_a = 40 # 长半轴
n_b = 5 # 短半轴
turtle.speed(1000)
turtle.penup()
turtle.goto(-n_a,-105)
turtle.pendown()
turtle.begin_fill()
for i in range(180,400):
x = n_a * math.cos(math.radians(i))
y = n_b * math.sin(math.radians(i))-110
turtle.goto(x, y)
turtle.fillcolor("red")
turtle.end_fill()
turtle.hideturtle()
#画勺子
t=turtle.Turtle()
t.pensize(3)
t.penup()
t.goto(150,100)
t.pendown()
t.circle(-50,-90)
t.goto(100,50)
t.circle(60,-60)
t.penup()
t.goto(150,100)
t.pendown()
t.goto(155,95)
t.right(20)
t.circle(-50,-90)
t.circle(60,-60)
t.circle(30,-80)
t.hideturtle()
#画汤圆
turtle.penup()
turtle.goto(100,-25)
turtle.pendown()
#turn_angle = 90
turtle.left(110)
turtle.begin_fill()
turtle.circle(30,180)
turtle.fillcolor("white")
turtle.end_fill()
turtle.right(210)
turtle.begin_fill()
turtle.circle(30,210)
turtle.fillcolor("white")
turtle.end_fill()
turtle.right(210)
turtle.begin_fill()
turtle.circle(30,180)
turtle.fillcolor("white")
turtle.end_fill()
turtle.penup()
turtle.goto(-50,-15)
turtle.pendown()
turtle.right(150)
turtle.begin_fill()
turtle.circle(-30,210)
turtle.fillcolor("white")
turtle.end_fill()
turtle.penup()
turtle.goto(-70,-30)
turtle.pendown()
turtle.left(270)
turtle.begin_fill()
turtle.circle(-30,225)
turtle.fillcolor("white")
turtle.end_fill()
turtle.penup()
turtle.goto(10,0)
turtle.pendown()
turtle.right(220)
turtle.begin_fill()
turtle.circle(-30,180)
turtle.fillcolor("white")
turtle.end_fill()
turtle.penup()
turtle.goto(0,13)
turtle.pendown()
turtle.left(220)
turtle.begin_fill()
turtle.circle(-30,225)
turtle.fillcolor("white")
turtle.end_fill()
turtle.hideturtle()
#大大大汤圆
t= 80 # 短半轴
turtle.penup()
turtle.goto(280, 200)
turtle.begin_fill()
turtle.pendown()
for i in range(90,400):
x = t * math.cos(math.radians(i))+280
y = t * math.sin(math.radians(i))+120
turtle.goto(x, y)
turtle.fillcolor("floralwhite")
turtle.end_fill()
turtle.pendown()
turtle.begin_fill()
m_a=40
m_b=50
#turtle.circle(30,140)
for i in range(0,160):
x = m_a * math.cos(math.radians(i))+310
y = m_b* math.sin(math.radians(i))+180
turtle.goto(x, y)
turtle.fillcolor("green")
turtle.end_fill()
turtle.penup()
turtle.goto(330,223)
turtle.pendown()
turtle.begin_fill()
turtle.circle(3,360)
turtle.fillcolor("black")
turtle.end_fill()
#眼睛
turtle.penup()
turtle.goto(250,150)
turtle.pendown()
turtle.circle(15,360)
turtle.penup()
turtle.goto(300,130)
turtle.pendown()
turtle.circle(15,360)
#眼珠
turtle.penup()
turtle.goto(300,130)
turtle.pendown()
turtle.begin_fill()
turtle.circle(6,360)
turtle.fillcolor("black")
turtle.end_fill()
turtle.penup()
turtle.goto(250,150)
turtle.pendown()
turtle.begin_fill()
turtle.circle(6,360)
turtle.fillcolor("black")
turtle.end_fill()
#高光
turtle.penup()
turtle.goto(305,130)
turtle.pendown()
turtle.begin_fill()
turtle.circle(3,360)
turtle.fillcolor("white")
turtle.end_fill()
turtle.penup()
turtle.goto(255,150)
turtle.pendown()
turtle.begin_fill()
turtle.circle(3,360)
turtle.fillcolor("white")
turtle.end_fill()
#嘴
turtle.penup()
turtle.goto(270,110)
turtle.pendown()
turtle.goto(260,105)
turtle.goto(280,100)
turtle.goto(270,110)
#衣服
turtle.penup()
turtle.goto(360, 120)
turtle.pendown()
turtle.begin_fill()
turtle.goto(340, 110)
turtle.goto(330, 80)
turtle.goto(350, 80)
turtle.fillcolor("red")
turtle.end_fill()
turtle.penup()
turtle.goto(200, 120)
turtle.pendown()
turtle.begin_fill()
turtle.goto(230, 100)
turtle.goto(220, 80)
turtle.goto(210, 80)
turtle.fillcolor("red")
turtle.end_fill()
# 隐藏画笔箭头(让画面更简洁)
turtle.hideturtle()
# 保持图形窗口显示
turtle.done()
一:首先是第一部分,背景设置,这个就是引入turtle库,之后设置屏幕大小,背景颜色
import turtle
import math
# 设置窗口大小和背景颜色
turtle.setup(1000, 800)
turtle.bgcolor("floralwhite")
turtle.pensize(3)
二:接着我们做了那个碗这一部分。
wan_a = 120 # 长半轴
wan_b= 110 # 短半轴
turtle.speed(1000)
turtle.penup()
turtle.goto(-wan_a, 0)#椭圆的最左边
turtle.pendown()
turtle.begin_fill()
for i in range(180,360):
x = wan_a * math.cos(math.radians(i))
y = wan_b * math.sin(math.radians(i))
turtle.goto(x, y)
turtle.fillcolor("red")
turtle.end_fill()
w_a = 120 # 长半轴
w_b = 50 # 短半轴
turtle.speed(1000)
turtle.penup()
turtle.goto(w_a, 0)
turtle.pendown()
turtle.begin_fill()
for i in range(360):
x =w_a * math.cos(math.radians(i))
y = w_b * math.sin(math.radians(i))
turtle.goto(x, y)
turtle.fillcolor("beige")
turtle.end_fill()
n_a = 40 # 长半轴
n_b = 5 # 短半轴
turtle.speed(1000)
turtle.penup()
turtle.goto(-n_a,-105)
turtle.pendown()
turtle.begin_fill()
for i in range(180,400):
x = n_a * math.cos(math.radians(i))
y = n_b * math.sin(math.radians(i))-110
turtle.goto(x, y)
turtle.fillcolor("red")
turtle.end_fill()
turtle.hideturtle()
这里就运用了一个数学知识,椭圆的基本公式,,把x,y用w_a,w_b表示,这个就表示椭圆的长轴和短轴,而这个碗就由几个这样的椭圆来构成,包括最后那个碗底,只不过是取得度数不一样。而那个speed没什么用了,忘记给注释掉了,因为这样子去表示椭圆的画就是在一个点一个点这样的画,所以他的速度会比较慢。最后就是把这个碗身填充为红色,这样子比较喜庆。
三:这部分其实就是那个勺子和碗中小汤圆了,还是比较抽象的。。。
#画勺子
t=turtle.Turtle()
t.pensize(3)
t.penup()
t.goto(150,100)
t.pendown()
t.circle(-50,-90)
t.goto(100,50)
t.circle(60,-60)
t.penup()
t.goto(150,100)
t.pendown()
t.goto(155,95)
t.right(20)
t.circle(-50,-90)
t.circle(60,-60)
t.circle(30,-80)
t.hideturtle()
#画汤圆
turtle.penup()
turtle.goto(100,-25)
turtle.pendown()
#turn_angle = 90
turtle.left(110)
turtle.begin_fill()
turtle.circle(30,180)
turtle.fillcolor("white")
turtle.end_fill()
turtle.right(210)
turtle.begin_fill()
turtle.circle(30,210)
turtle.fillcolor("white")
turtle.end_fill()
turtle.right(210)
turtle.begin_fill()
turtle.circle(30,180)
turtle.fillcolor("white")
turtle.end_fill()
turtle.penup()
turtle.goto(-50,-15)
turtle.pendown()
turtle.right(150)
turtle.begin_fill()
turtle.circle(-30,210)
turtle.fillcolor("white")
turtle.end_fill()
turtle.penup()
turtle.goto(-70,-30)
turtle.pendown()
turtle.left(270)
turtle.begin_fill()
turtle.circle(-30,225)
turtle.fillcolor("white")
turtle.end_fill()
turtle.penup()
turtle.goto(10,0)
turtle.pendown()
turtle.right(220)
turtle.begin_fill()
turtle.circle(-30,180)
turtle.fillcolor("white")
turtle.end_fill()
turtle.penup()
turtle.goto(0,13)
turtle.pendown()
turtle.left(220)
turtle.begin_fill()
turtle.circle(-30,225)
turtle.fillcolor("white")
turtle.end_fill()
turtle.hideturtle()
这一部分我们其实就是在重复这样一个抬笔,怎么这么画(向左或者向右)这样子去落笔,去画一下线条,来构成这样一个在用汤的碗中的小汤圆,毕竟,如果你只画一个完整的圆的画真的就很丑了,最后给汤圆填充成白色。
四:最后是我们的大大大大汤圆
#大大大汤圆
t= 80 # 短半轴
turtle.penup()
turtle.goto(280, 200)
turtle.begin_fill()
turtle.pendown()
for i in range(90,400):
x = t * math.cos(math.radians(i))+280
y = t * math.sin(math.radians(i))+120
turtle.goto(x, y)
turtle.fillcolor("floralwhite")
turtle.end_fill()
turtle.pendown()
turtle.begin_fill()
m_a=40
m_b=50
#turtle.circle(30,140)
for i in range(0,160):
x = m_a * math.cos(math.radians(i))+310
y = m_b* math.sin(math.radians(i))+180
turtle.goto(x, y)
turtle.fillcolor("green")
turtle.end_fill()
turtle.penup()
turtle.goto(330,223)
turtle.pendown()
turtle.begin_fill()
turtle.circle(3,360)
turtle.fillcolor("black")
turtle.end_fill()
#眼睛
turtle.penup()
turtle.goto(250,150)
turtle.pendown()
turtle.circle(15,360)
turtle.penup()
turtle.goto(300,130)
turtle.pendown()
turtle.circle(15,360)
#眼珠
turtle.penup()
turtle.goto(300,130)
turtle.pendown()
turtle.begin_fill()
turtle.circle(6,360)
turtle.fillcolor("black")
turtle.end_fill()
turtle.penup()
turtle.goto(250,150)
turtle.pendown()
turtle.begin_fill()
turtle.circle(6,360)
turtle.fillcolor("black")
turtle.end_fill()
#高光
turtle.penup()
turtle.goto(305,130)
turtle.pendown()
turtle.begin_fill()
turtle.circle(3,360)
turtle.fillcolor("white")
turtle.end_fill()
turtle.penup()
turtle.goto(255,150)
turtle.pendown()
turtle.begin_fill()
turtle.circle(3,360)
turtle.fillcolor("white")
turtle.end_fill()
#嘴
turtle.penup()
turtle.goto(270,110)
turtle.pendown()
turtle.goto(260,105)
turtle.goto(280,100)
turtle.goto(270,110)
#衣服
turtle.penup()
turtle.goto(360, 120)
turtle.pendown()
turtle.begin_fill()
turtle.goto(340, 110)
turtle.goto(330, 80)
turtle.goto(350, 80)
turtle.fillcolor("red")
turtle.end_fill()
turtle.penup()
turtle.goto(200, 120)
turtle.pendown()
turtle.begin_fill()
turtle.goto(230, 100)
turtle.goto(220, 80)
turtle.goto(210, 80)
turtle.fillcolor("red")
turtle.end_fill()
# 隐藏画笔箭头(让画面更简洁)
turtle.hideturtle()
# 保持图形窗口显示
turtle.done()
首先其实就是去画不太完整的圆,因为要留一部分去给他画帽子,所以就是一个有口的圆,接着直接把这一部分连接起来,继续画半圆填充绿色作为帽子,在给帽子上画一个小圆,看起来更好看一些,需要注意的是,在这里找圆心需要去找把笔先移动到那里,,接着就是画汤圆的眼睛,嘴巴,和他的小红棉袄,这一部分其实就是在重复找坐标,连接起来,这样一个过程。最后就是填充颜色,然后隐藏画笔,保持窗口显示,不然的话运行完会直接关掉。
以上
作者:氕氘氚不是水