python turtle 例子 海归绘图

本文提供了一系列使用Python的Turtle模块绘制各种图形的代码示例,包括太阳花、五角星、彩色螺旋线、方形蜘蛛网等,适合初学者实践和学习。
   



 

太阳花


1# coding=utf-8
2import turtle
3import time
4 
5# 同时设置pencolor="red", fillcolor="yellow"
6turtle.color("red", "yellow")
7 
8# 开始填充
9turtle.begin_fill()
10for _ in range(50):        # 循环50次, 从0到49
11    turtle.forward(200)    # 前行200
12    turtle.left(170)       # 左转170°
13# 结束填充
14turtle.end_fill()
15 
16# 不会退出, 而是等待
17turtle.mainloop()

五角星


# coding=utf-8
import turtle
import time

turtle.pensize(5)          # 线宽
turtle.pencolor("yellow")  # 线的眼神
turtle.fillcolor("red")    # 填充颜色

def draw_5AnglesShape():
    turtle.begin_fill()
    for _ in range(5):
        turtle.forward(200)
        turtle.right(144)
    turtle.end_fill()
    time.sleep(2)

def draw_word():           # 写文字
    turtle.penup()
    turtle.goto(-150, -120)
    turtle.color("violet")
    turtle.write("五角星绘制完毕", font=('Arial', 40, 'normal'))

if __name__ == "__main__":
    draw_5AnglesShape()
    draw_word()
    turtle.mainloop()
 

彩色螺旋线


1# coding=utf-8
2import turtle
3 
4from datetime import *
5import time
6 
7def doWork():
8    turtle.pensize(2)
9    turtle.bgcolor("black")
10    colors = ["red","yellow","purple","blue"]
11    #turtle.tracer(False)
12    for x in range(400):
13        turtle.forward(2*x)
14        turtle.color(colors[x % 4])
15        turtle.left(91)
16    #turtle.tracer(True)
17    # input()   可以有效解决闪退问题,或者下面的方法    
18 
19if __name__ == "__main__":
20    doWork()
21    turtle.done();
22 

方形蜘蛛网


# coding=utf-8
import turtle

from datetime import *
import time

def doWork(t):
    for x in range(100):
        t.forward(x)
        t.left(90)
 
if __name__ == "__main__":
    t = turtle.Pen()
    doWork(t)
    turtle.done()

旋转的海龟


# coding=utf-8
import turtle

from datetime import *
import time

def doWork(t):
    for x in range(100):
        t.forward(x)
        t.left(91)
        
if __name__ == "__main__":
    t = turtle.Pen()
    doWork(t)

彩色的旋转的海龟


# coding=utf-8
import turtle

from datetime import *
import time

def doWork(t):
    colors = ["red", "yellow", "blue", "green"]
    for x in range(100):
        t.pencolor(colors[x%4])
        t.forward(x)
        t.left(91)
        
if __name__ == "__main__":
    t = turtle.Pen()
    doWork(t)

彩色的旋转的海龟2


# coding=utf-8
import turtle

from datetime import *
import time

def doWork(t):
    turtle.bgcolor("black")   # 修改背景颜色
    sides = 6
    colors = ["red", "yellow", "blue", "green"]
    for x in range(100):
        t.pencolor(colors[x%4])
        t.forward(x * 3/sides + x)
        t.left(360/sides + 1)
        t.width(x*sides/200)   
 
if __name__ == "__main__":
    t = turtle.Pen()
    doWork(t)

 

蟒蛇绘制


1# coding=utf-8
2import turtle
3from datetime import *
4import time
5   
6if __name__ == "__main__":
7    # 屏幕大小为(650,300)
8    turtle.setup(650,300)
9    turtle.penup()
10    turtle.fd(-250)
11    turtle.pendown()
12    turtle.pensize(10)
13    turtle.pencolor("yellow")
14    turtle.seth(-40)
15    for i in range(4):
16        turtle.circle(40,80)
17        turtle.circle(-40,80)
18    turtle.circle(40,80/2)
19    turtle.fd(40)
20    turtle.circle(16,180)
21    turtle.fd(40 * 2/3)
22    turtle.done()

图形绘制


1# coding=utf-8
2import turtle
3from datetime import *
4import time
5 
6if __name__ == "__main__":
7    turtle.pensize(3)
8    turtle.penup()
9    turtle.goto(-200,-50)
10    turtle.pendown()
11    turtle.begin_fill()
12    turtle.color("red")
13    turtle.circle(40, steps=3)
14    turtle.end_fill()
15 
16 
17    turtle.penup()
18    turtle.goto(-100,-50)
19    turtle.pendown()
20    turtle.begin_fill()
21    turtle.color("blue")
22    turtle.circle(40, steps=4)
23    turtle.end_fill()
24 
25    turtle.penup()
26    turtle.goto(0,-50)
27    turtle.pendown()
28    turtle.begin_fill()
29    turtle.color("green")
30    turtle.circle(40, steps=5)
31    turtle.end_fill()
32 
33    turtle.penup()
34    turtle.goto(100,-50)
35    turtle.pendown()
36    turtle.begin_fill()
37    turtle.color("yellow")
38    turtle.circle(40, steps=6)
39    turtle.end_fill()
40 
41    turtle.penup()
42    turtle.goto(200,-50)
43    turtle.pendown()
44    turtle.begin_fill()
45    turtle.color("purple")
46    turtle.circle(40)
47    turtle.end_fill()
48 
49    turtle.color("green")
50    turtle.penup()
51    turtle.goto(-100,50)
52    turtle.pendown()
53    turtle.write( u"彩色简单图形".encode("utf-8"),
54                  font = ("Times", 18, "bold") )
55    turtle.hideturtle()
56 
57    turtle.done()
58 

三角塔的绘制


#encoding: utf8



import turtle



stepSize = 30



def draw1GreenTriangle():

	""" 画有一个绿色的小小三角形

	从图片上看, 整个图形就是由27个小三角形组成的

	"""

	global stepSize

	turtle.color("black", "green")    # 笔的颜色的黑色, 填充是绿色 

	turtle.begin_fill()               # 开始填充

	turtle.setheading(240)            # 头向左下

	turtle.forward(stepSize)          # 移动指定个单位

	turtle.left(120)                  # 逆时针旋转120度

	turtle.forward(stepSize)          # 移动10个单位

	turtle.left(120)                  # 逆时针旋转120度

	turtle.forward(stepSize)          # 移动10个单位

	turtle.end_fill()                 # 结束填充

	

def draw3GreenTriangle():

	""" 就是画三个小三角形

	原图片可以看做是9个这样的3个三角形组成的

	"""

	draw1GreenTriangle();

	turtle.left(120)                  # 逆时针旋转120度

	turtle.forward(stepSize)          # 移动指定单位

	draw1GreenTriangle();             # 画第二个三角形

	turtle.setheading(0)              # 头向左

	turtle.forward(stepSize)          # 移动指定单位

	draw1GreenTriangle();             # 画第三个三角形

	turtle.forward(stepSize)          # 移动指定个单位

	

def draw9GreenTriangle():

	""" 就是画九个三角形

	原图片可以看做是3个这样的9个三角形组成的

	"""

	draw3GreenTriangle()              # 画第一个三个小三角形

	turtle.left(120)                  # 逆时针旋转120度

	turtle.forward(stepSize*2)        # 移动2个指定单位

	draw3GreenTriangle()              # 画第二个三个小三角形

	turtle.setheading(0)              # 头向左

	turtle.forward(stepSize*2)        # 移动2个指定单位

	draw3GreenTriangle()              # 画第三个三个小三角形

	turtle.forward(stepSize*2)        # 移动2个指定单位

	

	

def draw27GreenTriangle():

	""" 画出最终图像, 就是27个小三角形, 

	其由三个draw9GreenTriangle()的结果组成

	"""

	draw9GreenTriangle()

	turtle.left(120)                  # 逆时针旋转120度

	turtle.forward(stepSize*4)        # 移动4个指定单位

	draw9GreenTriangle()

	turtle.setheading(0)              

转载于:https://www.cnblogs.com/wutaotaosin/p/10000654.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值