我的Python学习之路(2)

此文转载自:https://blog.csdn.net/Mr_BigG/article/details/112394166

今日所学内容

1.学习了海龟(turtle)库的一些函数,掌握基本的线条绘制方法
2.根据视频所学内容,绘制一条蟒蛇,逐句理解代码并写出详细的注释,效果图如下:
在这里插入图片描述

代码如下:

#PythonDraw.py
import turtle as t #引入海龟turtle绘图库
t.setup(650, 350, 200, 200) #前两个参数设置窗口的宽高,后两个参数设置窗口左上角的位置
t.penup() #画笔控制函数,抬起海龟(画笔),不会在屏幕上留下轨迹,一般和pendown成对使用。可简写为t.pu()
t.fd(-250) #t.fd()是海龟的前进函数,参数为前进的像素距离,与t.forward()等价
t.pendown() #可简写为t.pd()
t.pensize(25) #画笔宽度,也就是海龟的腰围,别名t.width()
t.pencolor("purple") #画笔颜色函数,参数为颜色的字符串或者是R,G,B值(小数)
t.seth(-40) #控制海龟的运动方向,与t.setheading()等价,参数为绝对角度。相对角度可以用t.left()或t.right(),参数为角度
for i in range(4): #range(N)产生0到N-1的整数序列,共N个。range(M, N)产生M到N-1的整数序列,共N-M个
    t.circle(40, 80) #根据第一个参数为半径绘制第二个参数角度的弧形,圆心在海归左侧半径为第一个参数的位置
    t.circle(-40, 80)
t.circle(40, 80 / 2)
t.fd(40)
t.circle(16, 180)
t.fd(40 * 2 / 3)
t.done()

3.结合网上的相关例子,编写一段送给女朋友的小程序(专属程序员的浪漫,嘿嘿),效果图如下:
密码
大白

代码如下:

#Love.py
#表白神器
import turtle as t
import time

#密码部分
Str = t.textinput("给最爱的宝贝!", "小宝贝么么哒,这是猪猪给你的节日贺卡,需要输入特定密码才能打开哦")
if (Str != '2020-07-27'):
    Str = t.textinput("抱歉", "密码错误!温馨提示:8位数字,再来一次吧!")
    if (Str == '20200727'):
        Str = t.textinput("233333", "宝贝最棒啦,数字对了哦,但是格式不正确,密码格式为'XXXX-XX-XX'")
        while (Str != '2020-07-27'):
            Str = t.textinput("233333", "是我们在一起的日子呀,密码格式为'XXXX-XX-XX'")
    elif (Str != '2020-07-27'):
        Str = t.textinput("抱歉", "是一个对我们很有意义的日子哦")
        if (Str == '20200727'):
            Str = t.textinput("233333", "宝贝最棒啦,数字对了哦,但是格式不正确,密码格式为'XXXX-XX-XX'")
            while (Str != '2020-07-27'):
                Str = t.textinput("233333", "是我们在一起的日子呀,密码格式为'XXXX-XX-XX'")
        elif (Str != '2020-07-27'):
            Str = t.textinput("抱歉", "最后一次提示哦,是我们的纪念日啊")
            if (Str == '20200727'):
                Str = t.textinput("233333", "宝贝最棒啦,数字对了哦,但是格式不正确,密码格式为'XXXX-XX-XX'")
                while (Str != '2020-07-27'):
                    Str = t.textinput("233333", "是我们在一起的日子呀,密码格式为'XXXX-XX-XX'")
            else:
                while (Str != '2020-07-27'):
                    Str = t.textinput("233333", "是我们在一起的日子呀,密码格式为'XXXX-XX-XX'")

#画布的初始化
t.setup(800, 600)

time.sleep(3)

#上半部分的文字
t.hideturtle()
t.color("red")
t.penup()
t.goto(-300, 220)
loveWords1 = '当'
loveWords2 = ['送', '给', '小', '宝', '贝']
loveWords3 = ['每', '天', '都', '快', '乐', '!']

for i in range(4):
    t.write(loveWords1, font = ("华文彩云", 3 * i + 14, "bold"))
    t.fd(20 + 3 * i)
    time.sleep(1)

t.fd(8)
t.write("!", font = ("华文彩云", 23, "bold"))
t.fd(20)
time.sleep(1)

for word in loveWords2:
    t.write(word, font = ("华文彩云", 26, "bold"))
    t.fd(35)
    time.sleep(1)

t.write(":", font = ("华文彩云", 23, "bold"))
t.fd(35)

#大白部分
#脑袋
t.goto(-100, 150)
t.right(90)
t.color("black")
t.pensize(1)
t.pendown()
size = 0.6
a = 0.8 * size

for i in range(120):
    if 0 <= i < 30 or 60 <= i < 90:
        a += 0.2 * size
        t.left(3)
        t.fd(a)
    else:
        a -= 0.2 * size
        t.left(3)
        t.fd(a)

t.penup()

#眼睛
time.sleep(1)
t.goto(-70, 150)
t.dot(14)
time.sleep(1)
t.goto(-24, 150)
t.dot(14)
time.sleep(1)
t.right(60)
t.pendown()
t.speed(2)
t.circle(-50, 50)

#身体
t.penup()
t.goto(-89.85, 131.47)
t.left(65)
t.pendown()
t.circle(250, 60)
t.penup()
t.goto(-2.64, 133.09)
t.left(30)
t.pendown()
t.circle(-250, 60)
t.right(20)
t.circle(-134,110)

#腿
t.penup()
t.left(135)
t.goto(-120, -145)
t.pendown()
t.circle(120, 45)
t.left(15)
t.circle(42, 80)
t.left(50)
t.fd(70)
t.penup()
t.bk(70)
t.pendown()
t.right(130)
t.circle(42, 80)
t.left(15)
t.circle(115, 48)
t.penup()

#胳膊
t.goto(-125, 89)
t.pendown()
t.left(112)
t.circle(250, 50)
t.circle(50, 45)
t.circle(20, 80)
t.circle(50, 45)
t.circle(180, 16)
t.penup()
t.goto(31, 90)
t.pendown()
t.right(126)
t.circle(-250, 50)
t.circle(-50, 45)
t.circle(-20, 80)
t.circle(-50, 45)
t.circle(-180, 14)

#手
t.penup()
t.goto(-208, -90)
t.pendown()
t.left(150)
t.right(180)
t.circle(-30, 20)
t.penup()
t.goto(-208, -90)
t.right(160)
t.pendown()
t.circle(30, 40)
t.circle(5, 120)
t.circle(40, 40)

#玫瑰
t.penup()
t.goto(-270, 60)
t.pendown()

#玫瑰花的角度
t.right(60)

#玫瑰花的尺寸
size = 0.35

#玫瑰花的花瓣
t.fillcolor("red")
t.begin_fill()
t.circle(10 * size, 180)
t.circle(25 * size, 110)
t.left(50)
t.circle(60 * size, 45)
t.circle(20 * size, 170)
t.right(24)
t.fd(30 * size)
t.left(10)
t.circle(30 * size, 110)
t.fd(20 * size)
t.left(40)
t.circle(90 * size, 70)
t.circle(30 * size, 150)
t.right(30)
t.fd(15 * size)
t.circle(80 * size, 90)
t.left(15)
t.fd(45 * size)
t.right(165)
t.fd(20 * size)
t.left(155)
t.circle(150 * size, 80)
t.left(50)
t.circle(150 * size, 90)
t.end_fill()

# 花瓣1
t.left(150)
t.circle(-90*size, 70)
t.left(20)
t.circle(75*size, 105)
t.seth(80)
t.circle(80*size, 98)
t.circle(-90*size, 40)

# 花瓣2
t.left(180)
t.circle(90 * size, 40)
t.circle(-80 * size, 98)
t.seth(-63.5)

# 叶子1
t.fd(30 * size)
t.left(90)
t.fd(25 * size)
t.left(45)
t.fillcolor("green")
t.begin_fill()
t.circle(-80 * size, 90)
t.right(90)
t.circle(-80 * size, 90)
t.end_fill()
t.right(135)
t.fd(60 * size)
t.left(180)
t.penup()
t.fd(85 * size)
t.pendown()
t.left(90)
t.fd(80 * size)

# 叶子2
t.right(90)
t.right(45)
t.fillcolor("green")
t.begin_fill()
t.circle(80 * size, 90)
t.left(90)
t.circle(80 * size, 90)
t.end_fill()
t.left(135)
t.fd(60 * size)
t.left(180)
t.fd(60 * size)
t.right(90)
t.circle(-1000 * size, 8.6)
t.penup()
t.goto(-200, -110)
t.pendown()
t.circle(-1000 * size, 10)

#文字
t.penup()
t.color("red")
t.goto(30, 220)
t.seth(0)
for word in loveWords3:
    t.write(word, font=("华文彩云", 26, "bold"))
    t.fd(35)
    time.sleep(1)

t.done()

注:本文是博主本人学习的日常记录,不进行任何商用所以不支持转载请理解!(大白的例子如有侵权请联系博主删除)如果你也对Python有一定的兴趣和理解,欢迎随时找博主交流~

   

更多内容详见微信公众号:Python测试和开发

Python测试和开发

原文地址:https://www.cnblogs.com/phyger/p/14262003.html