Python Turtle 系列

Python Turtle 系列

国宝大熊猫代码

import turtle as t
# 二维坐标轴
import turtle

# 初始化turtle屏幕
screen = turtle.Screen()
screen.title("二维坐标轴")
screen.setup(width=800, height=600)

# 创建turtle对象
pen = turtle.Turtle()

# 函数:绘制坐标轴
def draw_axes(pen):
    pen.speed(0)
    pen.color("black")
    pen.pensize(2)

    # 绘制X轴
    pen.penup()
    pen.goto(-390, 0)
    pen.pendown()
    pen.goto(390, 0)
    pen.penup()
    pen.goto(380, -10)
    pen.write("X", font=("Arial", 12, "normal"))
    pen.goto(0, 0)

    # 绘制Y轴
    pen.penup()
    pen.goto(0, -290)
    pen.pendown()
    pen.goto(0, 290)
    pen.penup()
    pen.goto(10, 280)
    pen.write("Y", font=("Arial", 12, "normal"))
    pen.goto(0, 0)

# 函数:标记刻度
def draw_ticks(pen):
    pen.speed(0)
    pen.color("black")
    pen.pensize(1)

    # X轴刻度
    for i in range(-380, 390, 20):
        pen.penup()
        pen.goto(i, -5)
        pen.pendown()
        pen.goto(i, 5)
        if i != 0:
            pen.penup()
            pen.goto(i, -20)
            pen.write(f"{i}", align="center", font=("Arial", 8, "normal"))

    # Y轴刻度
    for i in range(-280, 290, 20):
        pen.penup()
        pen.goto(-5, i)
        pen.pendown()
        pen.goto(5, i)
        if i != 0:
            pen.penup()
            pen.goto(-20, i-5)
            pen.write(f"{i}", align="center", font=("Arial", 8, "normal"))

# 绘制坐标轴和刻度
draw_axes(pen)
draw_ticks(pen)

# 停留窗口
# screen.mainloop()

# 全局属性
t.pensize(3)
# 绘制头部
t.penup()
t.goto(0,0)
t.pendown()
t.circle(100)

# 绘制左耳朵
t.penup()
t.goto(-50,185)
t.setheading(90)
t.pendown()
t.fillcolor('black')
t.begin_fill()
t.circle(30,260) # 绘制半径为30 范围仅到260 默认逆时针
t.end_fill()

# 绘制右耳朵
t.penup()
t.goto(50,185)
t.setheading(90)
t.pendown()
t.fillcolor('black')
t.begin_fill()
t.circle(-30,260) # 添加‘-’改变绘制时默认的逆时针为顺时针
t.end_fill()

# 绘制左眼睛外
t.penup()
t.goto(-45,100)
t.setheading(0)
t.pendown()
t.fillcolor('black')
t.begin_fill()
t.circle(30)
t.end_fill()

# 绘制左眼睛内
t.penup()
t.goto(-35,130)
t.setheading(0)
t.pendown()
t.fillcolor('white')
t.begin_fill()
t.circle(10)
t.end_fill()

# 绘制右眼睛外
t.penup()
t.goto(45,100)
t.setheading(0)
t.pendown()
t.fillcolor('black')
t.begin_fill()
t.circle(30)
t.end_fill()

# 绘制右眼睛内
t.penup()
t.goto(35,130)
t.setheading(0)
t.pendown()
t.fillcolor('white')
t.begin_fill()
t.circle(10)
t.end_fill()

# 绘制鼻子
t.penup()
t.goto(0,80)
t.setheading(0)
t.pendown()
t.circle(10)

# 绘制鼻中线
t.penup()
t.goto(0,80)
t.setheading(-90)
t.pendown()
t.goto(0,40)

# 绘制嘴巴
t.penup()
t.goto(-45,80)
t.setheading(-90)
t.pendown()
t.circle(45,180)

t.hideturtle()
t.done()

太极图代码

import turtle as t

# 初始化turtle屏幕
screen = t.Screen()
screen.title("太极图")
screen.setup(width=1000, height=1000)
radius = int(input('请输入太极半径'))
bgcolor = input('请输入背景颜色:建议(lightgrey)')
t.bgcolor(bgcolor)

# 绘制阴
t.penup()
t.goto(0,0)
t.fillcolor('black')
t.begin_fill()
t.setheading(0)
t.circle(radius,180)
t.end_fill()

# 绘制阳
t.fillcolor('white')
t.begin_fill()
t.circle(radius,180)
t.end_fill()

# 绘制阴蚪
t.penup()
t.goto(0,0)
t.fillcolor('black')
t.begin_fill()
t.circle(radius/2,360)
t.end_fill()

# 绘制阳蚪
t.penup()
t.goto(0,radius)
t.fillcolor('white')
t.begin_fill()
t.circle(radius/2,360)
t.end_fill()

# 绘制阴中阳
t.penup()
t.goto(0,radius/2/2)
t.fillcolor('white')
t.begin_fill()
t.circle(radius/2/2,360)
t.end_fill()

# 绘制阳中阴
t.penup()
t.goto(0,radius+radius/2/2)
t.fillcolor('black')
t.begin_fill()
t.circle(radius/2/2,360)
t.end_fill()

t.done()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值