希望在繁忙的工作过后给大家带来点滴快乐,以下代码已经在python3.6上测试通过。在windows上直接安装python3.6,记得勾选tcl/tk选项啊,enjoy!
心图
from turtle import *
from time import sleep
def go_to(x, y):
up()
goto(x, y)
down()
def big_circle(size): #函数用于绘制心的大圆
speed(10)
for i in range(150):
forward(size)
right(0.3)
def small_circle(size): #函数用于绘制心的小圆
for i in range(210):
right(0.786)
def line(size):
speed(1)
forward(51*size)
def heart( x, y, size):
go_to(x, y)
left(150)
begin_fill()
line(size)
big_circle(size)
small_circle(size)
left(120)
end_fill()
def arrow():
pensize(10)
setheading(0)
go_to(-400, 0)
left(15)
forward(150)
go_to(339, 178)
def arrowhead():
pensize(1)
speed(5)
color('red', 'red')
forward(20)
right(150)
forward(35)
right(120)
def main():
pensize(2)
color('red', 'pink')
#getscreen().tracer(30, 0) #取消注释后,快速显示图案
heart(200, 0, 1) #画出第一颗心,前面两个参数控制心的位置,函数最后一个参数可控制心的大小
setheading(0) #使画笔的方向朝向x轴正方向
heart(-80, -100, 1.5) #画出第二颗心
arrow() #画出穿过两颗心的直线
arrowhead() #画出箭的箭头
go_to(400, -300)
write(author:国才大叔的小屋, move=true, align=left, font=(宋体, 30, normal))
done()
main()
菠萝图
color('red', 'yellow')
while true:
forward(200)
left(170)
if abs(pos()) < 1:
break
西瓜图
import turtle
t = turtle.pen()
t.pensize(10)
def _dot_(x,y,size):
t.up()
t.goto(x,y)
t.down()
t.dot(size)
#瓜皮-绿色
t.goto(0,-210)
t.begin_fill()
t.fillcolor(green)
t.circle(400,extent = 30)
t.goto(0,190)
t.setheading(0)
t.circle(400,extent = -30)
t.end_fill()
#果肉-红色
t.goto(0,-160)
t.fillcolor(red)
t.circle(350,extent = 30)
t.circle(350,extent = -30)
#瓜子
_dot_(0,100,30)
_dot_(50,0,30)
_dot_(-50,0,30)
_dot_(0,-100,30)
_dot_(100,-100,30)
_dot_(-100,-100,30)
机器猫
t =turtle.pen()
t.pensize(5)
#blue&white face
t.fillcolor(blue)
t.circle(200)
t.fillcolor(white)
t.circle(160)
#nose
t.goto(0,200)
t.circle(20)
#eye
t.goto(-45,250)
t.circle(45)
t.goto(-20,250)
t.fillcolor(black)
t.circle(15)
t.goto(45,250)
t.goto(20,250)
#smile
t.goto(0,50)
t.circle(150,extent = -75)
print(t.pos())
t.circle(150,extent = 75)
t.left(15)
t.forward(150)
鱼
t.fillcolor(yellow)
t.goto(200,200)
t.goto(200,-200)
t.home()
t.goto(200,0)
t.goto(250,50)
t.goto(250,-50)
t.goto(50,-10)
t.circle(10)
红星闪闪放光彩
import time
def draw():
t.pencolor(red)
for x in range(5):
t.forward(200)
t.right(144)
draw()
time.sleep(2)
国旗
import math
def draw_polygon(aturtle, size=50, n=3):
''' 绘制正多边形
args:
aturtle: turtle对象实例
size: int类型,正多边形的边长
n: int类型,是几边形
'''
for i in range(n):
aturtle.forward(size)
aturtle.left(360.0/n)
def draw_n_angle(aturtle, size=50, num=5, color=none):
''' 绘制正n角形,默认为黄色
size: int类型,正多角形的边长
n: int类型,是几角形
color: str, 图形颜色,默认不填色
if color:
aturtle.begin_fill()
aturtle.fillcolor(color)
for i in range(num):
aturtle.left(360.0/num)
aturtle.right(2*360.0/num)
aturtle.end_fill()
def draw_5_angle(aturtle=none, start_pos=(0,0), end_pos=(0,10), radius=100, color=none):
''' 根据起始位置、结束位置和外接圆半径画五角星
start_pos: int的二元tuple,要画的五角星的外接圆圆心
end_pos: int的二元tuple,圆心指向的位置坐标点
radius: 五角星外接圆半径
aturtle = aturtle or turtle.turtle()
size = radius * math.sin(math.pi/5)/math.sin(math.pi*2/5)
aturtle.left(math.degrees(math.atan2(end_pos[1]-start_pos[1], end_pos[0]-start_pos[0])))
aturtle.penup()
aturtle.goto(start_pos)
aturtle.fd(radius)
aturtle.pendown()
aturtle.right(math.degrees(math.pi*9/10))
draw_n_angle(aturtle, size, 5, color)
def draw_5_star_flag(times=20.0):
''' 绘制五星红旗
times: 五星红旗的规格为30*20, times为倍数,默认大小为10倍, 即300*200
width, height = 30*times, 20*times
# 初始化屏幕和海龟
window = turtle.screen()
aturtle = turtle.turtle()
aturtle.hideturtle()
aturtle.speed(10)
# 画红旗
aturtle.goto(-width/2, height/2)
aturtle.fillcolor('red')
aturtle.fd(width)
aturtle.right(90)
aturtle.fd(height)
# 画大星星
draw_5_angle(aturtle, start_pos=(-10*times, 5*times), end_pos=(-10*times, 8*times), radius=3*times, color='yellow')
# 画四个小星星
stars_start_pos = [(-5, 8), (-3, 6), (-3, 3), (-5, 1)]
for pos in stars_start_pos:
draw_5_angle(aturtle, start_pos=(pos[0]*times, pos[1]*times), end_pos=(-10*times, 5*times), radius=1*times, color='yellow')
# 点击关闭窗口
window.exitonclick()
if __name__ == '__main__':
draw_5_star_flag()
奥迪车标
#t.circle(100)
#t.up()
#t.forward(150)
#t.down()
for x in range(4):
t.circle(100)
绿色的小海龟
t.color(green)
for x in range(12):
t.shape(turtle)
t.circle(100,360)
t.left(30)
最后已绿色的小海龟致敬python,end!