Python绘图,圣诞树,花,爱心 | Turtle篇

Python基础语法 - 专栏链接

1.画圣诞树

Python绘图,圣诞树,花,爱心 | Turtle篇_第1张图片

import turtle
screen = turtle.Screen()
screen.setup(800,600)
circle = turtle.Turtle()
circle.shape('circle')
circle.color('red')
circle.speed('fastest')
circle.up()
square = turtle.Turtle()
square.shape('square')
square.color('green')
square.speed('fastest')
square.up()
circle.goto(0,280)
circle.stamp()
k = 0
for i in range(1, 17):
    y = 30*i
    for j in range(i-k):
        x = 30*j
        square.goto(x,-y+280)
        square.stamp()
        square.goto(-x,-y+280)
        square.stamp()
    if i % 4 == 0:
        x = 30*(j+1)
        circle.color('red')
        circle.goto(-x,-y+280)
        circle.stamp()
        circle.goto(x,-y+280)
        circle.stamp()
        k += 2
    if i % 4 == 3:
        x = 30*(j+1)
        circle.color('yellow')
        circle.goto(-x,-y+280)
        circle.stamp()
        circle.goto(x,-y+280)
        circle.stamp()
square.color('brown')
for i in range(17,20):
    y = 30*i
    for j in range(3):
        x = 30*j
        square.goto(x,-y+280)
        square.stamp()
        square.goto(-x,-y+280)
        square.stamp()
turtle.exitonclick()

2.画樱花

Python绘图,圣诞树,花,爱心 | Turtle篇_第2张图片

import turtle as T
import random
import time
​
# 画樱花的躯干(60,t)
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')  # 赭(zhě)色
            t.pensize(branch / 10)  # 6
        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='white')  # wheat小麦
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')
​
# 画樱花的躯干
Tree(60, t)
# 掉落的花瓣
Petal(200, t)
w.exitonclick()

3.画一箭穿心

Python绘图,圣诞树,花,爱心 | Turtle篇_第3张图片

from turtle import*
color ("black","red")
pensize(5)
begin_fill()
penup()
goto(50,50)
pendown()
right(45)
goto(100,0)
left(90)
fd(120)
circle(50,225)
penup()
goto(0,0)
pendown()
left(135)
fd(120)
circle(50,225)
seth(90)
circle(50,225)
fd(121)
end_fill()
left(56)
penup()
goto(-210,40)
pendown()
goto(0,80)
penup()
goto(160,110)
pendown()
goto(320,140)
done()

4.画爱心

Python绘图,圣诞树,花,爱心 | Turtle篇_第4张图片

# -*- coding:utf-8 -*- 
import turtle
import time
​
# 画心形圆弧
​
def hart_arc():
    for i in range(200):
        turtle.right(1)
        turtle.forward(2)
​
def move_pen_position(x, y):
​
    turtle.hideturtle()     # 隐藏画笔(先)
​
    turtle.up()     # 提笔
​
    turtle.goto(x, y)    # 移动画笔到指定起始坐标(窗口中心为0,0)
​
    turtle.down()   # 下笔
​
    turtle.showturtle()     # 显示画笔
​
# 初始化
​
turtle.setup(width=800, height=500)     # 窗口(画布)大小
​
turtle.color('red', 'pink')     # 画笔颜色
​
turtle.pensize(2)       # 画笔粗细
​
turtle.speed(0.5)     # 描绘速度
​
# 初始化画笔起始坐标
​
move_pen_position(x=0,y=-180)   # 移动画笔位置
​
turtle.left(140)    # 向左旋转140度
​
turtle.begin_fill()     # 标记背景填充位置
​
# 画心形直线( 左下方 )
​
turtle.forward(224)    # 向前移动画笔,长度为224
​
# 画爱心圆弧
​
hart_arc()      # 左侧圆弧
​
turtle.left(120)    # 调整画笔角度
​
hart_arc()      # 右侧圆弧
​
# 画心形直线( 右下方 )
​
turtle.forward(224)
​
turtle.end_fill()       # 标记背景填充结束位置
  
# 点击窗口关闭程序
​
window = turtle.Screen()
​
window.exitonclick()

5.画玫瑰花

Python绘图,圣诞树,花,爱心 | Turtle篇_第5张图片


import turtle
 
# 设置初始位置
turtle.penup()
turtle.left(90)
turtle.fd(200)
turtle.pendown()
turtle.right(90)
 
# 花蕊
turtle.fillcolor("red")
turtle.begin_fill()
turtle.circle(10,180)
turtle.circle(25,110)
turtle.left(50)
turtle.circle(60,45)
turtle.circle(20,170)
turtle.right(24)
turtle.fd(30)
turtle.left(10)
turtle.circle(30,110)
turtle.fd(20)
turtle.left(40)
turtle.circle(90,70)
turtle.circle(30,150)
turtle.right(30)
turtle.fd(15)
turtle.circle(80,90)
turtle.left(15)
turtle.fd(45)
turtle.right(165)
turtle.fd(20)
turtle.left(155)
turtle.circle(150,80)
turtle.left(50)
turtle.circle(150,90)
turtle.end_fill()
​
 
# 花瓣1
turtle.left(150)
turtle.circle(-90,70)
turtle.left(20)
turtle.circle(75,105)
turtle.setheading(60)
turtle.circle(80,98)
turtle.circle(-90,40)
 
# 花瓣2
turtle.left(180)
turtle.circle(90,40)
turtle.circle(-80,98)
turtle.setheading(-83)
​
 
# 叶子1
turtle.fd(30)
turtle.left(90)
turtle.fd(25)
turtle.left(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(-80,90)
turtle.right(90)
turtle.circle(-80,90)
turtle.end_fill()
 
turtle.right(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(85)
turtle.left(90)
turtle.fd(80)
​
 
# 叶子2
turtle.right(90)
turtle.right(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(80,90)
turtle.left(90)
turtle.circle(80,90)
turtle.end_fill()
 
turtle.left(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(60)
turtle.right(90)
turtle.circle(200,60)
# time.sleep(3)

6.画太极

#引入turtle函数库
​
from turtle import * 
​
#定义画半个太极图的函数,第一个参数radius是大圆的半径,
​
#color1,color2分别是两种填充颜色,对应图形中的黑白填充
​
def draw(radius, color1,color2):
​
    #设置画笔粗细
    width(3)
​
    #设置画笔颜色和填充颜色
    color("black",color1)
​
    #准备开始填充图形
    begin_fill()
​
    #首先画一个半径为radius/2,弧度为180的半圆,画的是红线所示半圆
    circle(radius/2,180)
​
    #画一个半径为radius,弧度为180的半圆,画的是黄线所示半圆
    circle(radius,180)
​
    #将画笔方向旋转180度
    left(180)
​
    #画一个半径为radius/2,弧度为180的半圆,此时半径值为负,
​
    #圆心在画笔的右边,画的是绿线所示半圆
    circle(-radius/2,180)
​
    #结束填充
    end_fill()
​
    #画笔向左旋转90度,正好指向画板上方
​
    left(90)
​
    #抬起画笔,再运动时不会留下痕迹
    up()
​
    #向前移动radius*0.35,这样小圆边线距离大圆边线上下各radius*0.35,
​
    #小圆的半径就为radius*0.15
    forward(radius*0.35)
​
    #画笔向右旋转90度,指向画板右侧
    right(90)
​
    #放下画笔
    down()
​
    color(color2,color2)
​
    #开始画内嵌小圆,如蓝线所示
    begin_fill()
​
    circle(radius*0.15)
​
    end_fill()
​
    #旋转画笔90度,指向画板上方
    left(90)
​
    up()
​
    #后退radius*0.35
    backward(radius*0.35)
​
    down()
​
    #旋转画笔90度,指向画板左方
    left(90)
​
#定义主函数
def main():
​
    #设置窗口或者画板大小
    setup(500,500)
​
    #绘制黑色一半,白色内圆
    draw(200,"black","white")
​
    #绘制白色一半,黑色内圆
    draw(200,"white","black")
​
    #隐藏画笔
    ht()
​
main()

 

 

秒变大牛,就是这么简单~



关注我们CSDN吧,每周每日,分享Python实战代码,入门资料,进阶资料,基础语法,爬虫,数据分析,web网站,机器学习,深度学习等等。


Python绘图,圣诞树,花,爱心 | Turtle篇_第6张图片​微信群(关注「Python家庭」一起轻松学Python吧)

Python绘图,圣诞树,花,爱心 | Turtle篇_第7张图片

Python绘图,圣诞树,花,爱心 | Turtle篇_第8张图片​QQ ②群(983031854

Python绘图,圣诞树,花,爱心 | Turtle篇_第9张图片

你可能感兴趣的:(Turtle入门到进阶)