彩虹五角星
import turtle                                                          #引用turtle库
q = turtle.Pen()                                                       #构造画笔
turtle.bgcolor("black")                                                #画布的背景颜色为黑色
sides = 7                                                              #定义变量控制环绕程度
colors = ["red","orange","yellow","green","cyan","blue","purple"]
for x in range(360):                                                   #for循环控制画笔的走向与速度
    q.speed(35)
    q.pencolor(colors[x%sides])
    q.forward(x*3/sides+x)
    q.left(360/sides+1)
    q.width(x*sides/200)
导入turtle库:
import turtle
这行代码导入了Python的turtle图形库,允许我们在一个窗口中绘制图形。turtle图形库通过一个小海龟(pen)在屏幕上移动来绘制图形。
构造画笔:
q = turtle.Pen()
这里创建了一个Pen对象q,作为我们的画笔。默认情况下,画笔是黑色的,并且初始位置在画布(窗口)的中心。
设置画布背景颜色:
turtle.bgcolor("black")
这行代码将画布的背景颜色设置为黑色。
定义变量控制环绕程度:
sides = 7
这个变量sides用于控制螺旋的环绕程度或者说是每次旋转的角度。这里设置为7,意味着每次循环会旋转约51.43度(360度/7)。
定义颜色列表:
colors = ["red","orange","yellow","green","cyan","blue","purple"]
这是一个包含7种颜色的列表,与sides变量相匹配,用于控制螺旋线每次绘制时的颜色。
绘制螺旋图案:
for x in range(360):
    q.speed(35)
    q.pencolor(colors[x%sides])
    q.forward(x*3/sides+x)
    q.left(360/sides+1)
    q.width(x*sides/200)
循环遍历:通过for x in range(360):循环360次,x从0递增到359。
 画笔速度:q.speed(35)设置画笔的移动速度。这里的35是一个相对速度值,其中0表示最快,10表示最慢。
 画笔颜色:q.pencolor(colors[x%sides])设置画笔颜色。x%sides确保颜色索引在colors列表的范围内循环。
 向前移动:q.forward(x3/sides+x)控制画笔向前移动的距离。这个距离随着x的增加而增加,但增长的速度受sides影响。
 向左旋转:q.left(360/sides+1)控制画笔每次绘制后向左旋转的角度。360/sides确保螺旋的形状,而额外的+1导致螺旋线之间有一些轻微的交错。
 画笔宽度:q.width(xsides/200)改变画笔的宽度。随着x的增加,画笔宽度也逐渐增加,但增长的速度受到sides和分母200的调节。
超级爱心
# coding=utf-8
import turtle
import time
 
def draw_circle():
    for i in range(400):
        turtle.right(0.5)
        turtle.forward(1)
def draw_love():
#    turtle.color('red','darkred')
#    turtle.pensize(1)
    turtle.pen(fillcolor="black",pencolor="red",pensize=8)
    turtle.speed(2000)
    turtle.goto(0,0)
    turtle.begin_fill()
    turtle.left(140)
    turtle.forward(224)
    draw_circle()
    turtle.left(120)
    draw_circle()
    turtle.forward(224)
    turtle.end_fill()
    turtle.write("I Love you")
    time.sleep(2)
    turtle.up()
    turtle.goto(150,20)
    turtle.color('black')
    turtle.write('相夕妄想 来日方长 很喜欢你 初心永远',font=("微软雅黑",18,"normal"))
    time.sleep(2)
def draw_abc():
    turtle.fillcolor("black")
    turtle.pencolor("red")
    turtle.pensize(10)
    turtle.speed(1)
    turtle.up()
    turtle.goto(0,-50)
    turtle.down()
    turtle.begin_fill()
    turtle.circle(45)
    turtle.end_fill()
    time.sleep(2)
def word():
    turtle.up()
    turtle.goto(-100,200)
    turtle.color("red")
    turtle.pensize(4)
#   turtle.down()
    turtle.write('宝贝,宝贝你就是唯一',font=("隶书",18,"bold"))
    time.sleep(10)
draw_love()
draw_abc()
word()
导入模块
import turtle
import time
**turtle:Python的内置绘图库,用于在屏幕上绘制图形。
time:用于在程序中添加延时,以便更好地观察绘图过程。**
绘制圆形函数
def draw_circle():
    for i in range(400):
        turtle.right(0.5)
        turtle.forward(1)
这个函数通过逐步旋转和前进来绘制一个近似的圆形。每次前进1个单位,并向右旋转0.5度,总共进行400次迭代。
绘制心形函数
def draw_love():
    ...
    turtle.begin_fill()
    ...
    draw_circle()
    ...
    draw_circle()
    ...
    turtle.end_fill()
    ...
这个函数首先设置画笔的填充颜色和笔的颜色,然后移动到原点开始绘制。它首先向左旋转140度,前进一段距离,然后调用draw_circle()函数绘制心形的上半部分。接着,向左旋转120度,再次调用draw_circle()函数绘制心形的下半部分。最后,使用end_fill()完成心形的填充,并在心形内部写上“I Love you”。
绘制圆形图案函数
def draw_abc():
    ...
    turtle.begin_fill()
    turtle.circle(45)
    turtle.end_fill()
    ...
这个函数绘制一个填充的圆形,半径为45个单位。
绘制文本函数
def word():
    ...
    turtle.write('宝贝,宝贝你就是唯一',font=("隶书",18,"bold"))
    ...
** …
 这个函数在屏幕上指定位置写上文本“宝贝,宝贝你就是唯一”,并设置了文本的字体样式。**
主执行部分
draw_love()
draw_abc()
word()
最后,这三个函数被依次调用,首先绘制心形图案,然后绘制圆形图案,最后在屏幕上写上文本。
带帽的皮卡丘

完整代码
import turtle
def getPosition(x, y):
    turtle.setx(x)
    turtle.sety(y)
    print(x, y)
class Pikachu:
    def __init__(self):
        self.t = turtle.Turtle()
        t = self.t
        t.pensize(3)
        t.speed(9)
        t.ondrag(getPosition)
    def noTrace_goto(self, x, y):
        self.t.penup()
        self.t.goto(x, y)
        self.t.pendown()
    def leftEye(self, x, y):
        self.noTrace_goto(x, y)
        t = self.t
        t.seth(0)
        t.fillcolor('#333333')
        t.begin_fill()
        t.circle(22)
        t.end_fill()
        self.noTrace_goto(x, y + 10)
        t.fillcolor('#000000')
        t.begin_fill()
        t.circle(10)
        t.end_fill()
        self.noTrace_goto(x + 6, y + 22)
        t.fillcolor('#ffffff')
        t.begin_fill()
        t.circle(10)
        t.end_fill()
    def rightEye(self, x, y):
        self.noTrace_goto(x, y)
        t = self.t
        t.seth(0)
        t.fillcolor('#333333')
        t.begin_fill()
        t.circle(22)
        t.end_fill()
        self.noTrace_goto(x, y + 10)
        t.fillcolor('#000000')
        t.begin_fill()
        t.circle(10)
        t.end_fill()
        self.noTrace_goto(x - 6, y + 22)
        t.fillcolor('#ffffff')
        t.begin_fill()
        t.circle(10)
        t.end_fill()
    def mouth(self, x, y):
        self.noTrace_goto(x, y)
        t = self.t
        t.fillcolor('#88141D')
        t.begin_fill()
        # 下嘴唇
        l1 = []
        l2 = []
        t.seth(190)
        a = 0.7
        for i in range(28):
            a += 0.1
            t.right(3)
            t.fd(a)
            l1.append(t.position())
        self.noTrace_goto(x, y)
        t.seth(10)
        a = 0.7
        for i in range(28):
            a += 0.1
            t.left(3)
            t.fd(a)
            l2.append(t.position())
        # 上嘴唇
        t.seth(10)
        t.circle(50, 15)
        t.left(180)
        t.circle(-50, 15)
        t.circle(-50, 40)
        t.seth(233)
        t.circle(-50, 55)
        t.left(180)
        t.circle(50, 12.1)
        t.end_fill()
        # 舌头
        self.noTrace_goto(17, 54)
        t.fillcolor('#DD716F')
        t.begin_fill()
        t.seth(145)
        t.circle(40, 86)
        t.penup()
        for pos in reversed(l1[:20]):
            t.goto(pos[0], pos[1] + 1.5)
        for pos in l2[:20]:
            t.goto(pos[0], pos[1] + 1.5)
        t.pendown()
        t.end_fill()
        # 鼻子
        self.noTrace_goto(-17, 94)
        t.seth(8)
        t.fd(4)
        t.back(8)
    # 红脸颊
    def leftCheek(self, x, y):
        turtle.tracer(False)
        t = self.t
        self.noTrace_goto(x, y)
        t.seth(300)
        t.fillcolor('#DD4D28')
        t.begin_fill()
        a = 2.3
        for i in range(120):
            if 0 <= i < 30 or 60 <= i < 90:
                a -= 0.05
                t.lt(3)
                t.fd(a)
            else:
                a += 0.05
                t.lt(3)
                t.fd(a)
        t.end_fill()
        turtle.tracer(True)
    def rightCheek(self, x, y):
        t = self.t
        turtle.tracer(False)
        self.noTrace_goto(x, y)
        t.seth(60)
        t.fillcolor('#DD4D28')
        t.begin_fill()
        a = 2.3
        for i in range(120):
            if 0 <= i < 30 or 60 <= i < 90:
                a -= 0.05
                t.lt(3)
                t.fd(a)
            else:
                a += 0.05
                t.lt(3)
                t.fd(a)
        t.end_fill()
        turtle.tracer(True)
    def colorLeftEar(self, x, y):
        t = self.t
        self.noTrace_goto(x, y)
        t.fillcolor('#000000')
        t.begin_fill()
        t.seth(330)
        t.circle(100, 35)
        t.seth(219)
        t.circle(-300, 19)
        t.seth(110)
        t.circle(-30, 50)
        t.circle(-300, 10)
        t.end_fill()
    def colorRightEar(self, x, y):
        t = self.t
        self.noTrace_goto(x, y)
        t.fillcolor('#000000')
        t.begin_fill()
        t.seth(300)
        t.circle(-100, 30)
        t.seth(35)
        t.circle(300, 15)
        t.circle(30, 50)
        t.seth(190)
        t.circle(300, 17)
        t.end_fill()
    def body(self):
        t = self.t
        t.fillcolor('#F6D02F')
        t.begin_fill()
        # 右脸轮廓
        t.penup()
        t.circle(130, 40)
        t.pendown()
        t.circle(100, 105)
        t.left(180)
        t.circle(-100, 5)
        # 右耳朵
        t.seth(20)
        t.circle(300, 30)
        t.circle(30, 50)
        t.seth(190)
        t.circle(300, 36)
        # 上轮廓
        t.seth(150)
        t.circle(150, 70)
        # 左耳朵
        t.seth(200)
        t.circle(300, 40)
        t.circle(30, 50)
        t.seth(20)
        t.circle(300, 35)
        # print(t.pos())
        # 左脸轮廓
        t.seth(240)
        t.circle(105, 95)
        t.left(180)
        t.circle(-105, 5)
        # 左手
        t.seth(210)
        t.circle(500, 18)
        t.seth(200)
        t.fd(10)
        t.seth(280)
        t.fd(7)
        t.seth(210)
        t.fd(10)
        t.seth(300)
        t.circle(10, 80)
        t.seth(220)
        t.fd(10)
        t.seth(300)
        t.circle(10, 80)
        t.seth(240)
        t.fd(12)
        t.seth(0)
        t.fd(13)
        t.seth(240)
        t.circle(10, 70)
        t.seth(10)
        t.circle(10, 70)
        t.seth(10)
        t.circle(300, 18)
        t.seth(75)
        t.circle(500, 8)
        t.left(180)
        t.circle(-500, 15)
        t.seth(250)
        t.circle(100, 65)
        # 左脚
        t.seth(320)
        t.circle(100, 5)
        t.left(180)
        t.circle(-100, 5)
        t.seth(220)
        t.circle(200, 20)
        t.circle(20, 70)
        t.seth(60)
        t.circle(-100, 20)
        t.left(180)
        t.circle(100, 20)
        t.seth(300)
        t.circle(10, 70)
        t.seth(60)
        t.circle(-100, 20)
        t.left(180)
        t.circle(100, 20)
        t.seth(10)
        t.circle(100, 60)
        # 横向
        t.seth(180)
        t.circle(-100, 10)
        t.left(180)
        t.circle(100, 10)
        t.seth(5)
        t.circle(100, 10)
        t.circle(-100, 40)
        t.circle(100, 35)
        t.left(180)
        t.circle(-100, 10)
        # 右脚
        t.seth(290)
        t.circle(100, 55)
        t.circle(10, 50)
        t.seth(120)
        t.circle(100, 20)
        t.left(180)
        t.circle(-100, 20)
        t.seth(0)
        t.circle(10, 50)
        t.seth(110)
        t.circle(100, 20)
        t.left(180)
        t.circle(-100, 20)
        t.seth(30)
        t.circle(20, 50)
        t.seth(100)
        t.circle(100, 40)
        # 右侧身体轮廓
        t.seth(200)
        t.circle(-100, 5)
        t.left(180)
        t.circle(100, 5)
        t.left(30)
        t.circle(100, 75)
        t.right(15)
        t.circle(-300, 21)
        t.left(180)
        t.circle(300, 3)
        # 右手
        t.seth(43)
        t.circle(200, 60)
        t.right(10)
        t.fd(10)
        t.circle(5, 160)
        t.seth(90)
        t.circle(5, 160)
        t.seth(90)
        t.fd(10)
        t.seth(90)
        t.circle(5, 180)
        t.fd(10)
        t.left(180)
        t.left(20)
        t.fd(10)
        t.circle(5, 170)
        t.fd(10)
        t.seth(240)
        t.circle(50, 30)
        t.end_fill()
        self.noTrace_goto(130, 125)
        t.seth(-20)
        t.fd(5)
        t.circle(-5, 160)
        t.fd(5)
        # 手指纹
        self.noTrace_goto(166, 130)
        t.seth(-90)
        t.fd(3)
        t.circle(-4, 180)
        t.fd(3)
        t.seth(-90)
        t.fd(3)
        t.circle(-4, 180)
        t.fd(3)
        # 尾巴
        self.noTrace_goto(168, 134)
        t.fillcolor('#F6D02F')
        t.begin_fill()
        t.seth(40)
        t.fd(200)
        t.seth(-80)
        t.fd(150)
        t.seth(210)
        t.fd(150)
        t.left(90)
        t.fd(100)
        t.right(95)
        t.fd(100)
        t.left(110)
        t.fd(70)
        t.right(110)
        t.fd(80)
        t.left(110)
        t.fd(30)
        t.right(110)
        t.fd(32)
        t.right(106)
        t.circle(100, 25)
        t.right(15)
        t.circle(-300, 2)
        ##############
        # print(t.pos())
        t.seth(30)
        t.fd(40)
        t.left(100)
        t.fd(70)
        t.right(100)
        t.fd(80)
        t.left(100)
        t.fd(46)
        t.seth(66)
        t.circle(200, 38)
        t.right(10)
        t.fd(10)
        t.end_fill()
        # 尾巴花纹
        t.fillcolor('#923E24')
        self.noTrace_goto(126.82, -156.84)
        t.begin_fill()
        t.seth(30)
        t.fd(40)
        t.left(100)
        t.fd(40)
        t.pencolor('#923e24')
        t.seth(-30)
        t.fd(30)
        t.left(140)
        t.fd(20)
        t.right(150)
        t.fd(20)
        t.left(150)
        t.fd(20)
        t.right(150)
        t.fd(20)
        t.left(130)
        t.fd(18)
        t.pencolor('#000000')
        t.seth(-45)
        t.fd(67)
        t.right(110)
        t.fd(80)
        t.left(110)
        t.fd(30)
        t.right(110)
        t.fd(32)
        t.right(106)
        t.circle(100, 25)
        t.right(15)
        t.circle(-300, 2)
        t.end_fill()
        # 帽子、眼睛、嘴巴、脸颊
        self.cap(-134.07, 147.81)
        self.mouth(-5, 25)
        self.leftCheek(-126, 32)
        self.rightCheek(107, 63)
        self.colorLeftEar(-250, 100)
        self.colorRightEar(140, 270)
        self.leftEye(-85, 90)
        self.rightEye(50, 110)
        t.hideturtle()
    def cap(self, x, y):
        self.noTrace_goto(x, y)
        t = self.t
        t.fillcolor('#CD0000')
        t.begin_fill()
        t.seth(200)
        t.circle(400, 7)
        t.left(180)
        t.circle(-400, 30)
        t.circle(30, 60)
        t.fd(50)
        t.circle(30, 45)
        t.fd(60)
        t.left(5)
        t.circle(30, 70)
        t.right(20)
        t.circle(200, 70)
        t.circle(30, 60)
        t.fd(70)
        # print(t.pos())
        t.right(35)
        t.fd(50)
        t.circle(8, 100)
        t.end_fill()
        self.noTrace_goto(-168.47, 185.52)
        t.seth(36)
        t.circle(-270, 54)
        t.left(180)
        t.circle(270, 27)
        t.circle(-80, 98)
        t.fillcolor('#444444')
        t.begin_fill()
        t.left(180)
        t.circle(80, 197)
        t.left(58)
        t.circle(200, 45)
        t.end_fill()
        self.noTrace_goto(-58, 270)
        t.pencolor('#228B22')
        t.dot(35)
        self.noTrace_goto(-30, 280)
        t.fillcolor('#228B22')
        t.begin_fill()
        t.seth(100)
        t.circle(30, 180)
        t.seth(190)
        t.fd(15)
        t.seth(100)
        t.circle(-45, 180)
        t.right(90)
        t.fd(15)
        t.end_fill()
        t.pencolor('#000000')
    def start(self):
        self.body()
def main():
    print('Painting the Pikachu... ')
    turtle.screensize(800, 600)
    turtle.title('Pikachu')
    pikachu = Pikachu()
    pikachu.start()
    turtle.mainloop()
if __name__ == '__main__':
    main()



















