用Python的turtle画一只小猪乔治【刚学python两天,纪念一下】

本人是新人小白,欢迎各位大佬指正,本文介绍的是用Python的turtle画一只小猪乔治【手动比心 by zwx lvmm】
【女朋友很喜欢小猪乔治,我在学习python,她说,那你会画小猪乔治了吗?于是我花了四个小时画了一个丑丑的乔治…】
我画的很丑的乔治
首先参考了一个网上的案例:
用Python画小猪佩奇

但是,这个代码有一个小问题,直接复制之后运行就会出现内核死亡的bug,开始不知道,后来在学习的过程中发现tkinter中创建对话框后再结尾要加一行mainloop()来保证对话框可以动,所以就尝试了一下,发现加上这个之后就不会出现之前的bug了。
还有本人画画基础超级差= =没天赋,画参考度娘上的一个图片画的。
具体的turtle内容可以参考这个博客的内容:
turtle库

先上总代码,然后说说我写的思路,希望大家看了之后可以画出来哄女朋友开心,【嘿嘿嘿】

import turtle as t
'''
t.pu()  提起画笔
t.pd()  移动时绘制图形,缺省时也为绘制
t.seth  设置当前朝向为angle角度
t.begin_fill()  准备开始填充图形
t.color      同时设置pencolor=color1, fillcolor=color2
t.goto       设置笔的坐标
t.circle(70,20) 半径 度数
15,124,215  乔治裤子颜色外面
66,163,242  乔治裤子颜色里面

134  196  247  天空的颜色
123,245,95 草地的颜色
253,6,6  鞋子外面
253,70,70  鞋子里面
130,119,100 泥坑


'''
r_a=0.8
wight = 1100
height = 700
#t.pensize(4)
t.hideturtle()
t.colormode(255)
t.color((255,155,192),"pink")
t.setup(wight,height)
t.speed(10)
def move_pen(x,y):
    t.pu()
    t.goto(x-wight/2+50,y-height/2+50)
    t.pd()
def pen_set(size,r1,g1,b1,r2=0,g2=0,b2=0):
    t.pensize(size)
    t.color((r1,g1,b1),(r2,g2,b2))
def draw_grid():
    pen_set(1,0,0,0,0,0,0)
    for i in range(20):
        move_pen(0+i*50,0)
        t.seth(90)
        t.fd(600)
    for i in range(12):
        move_pen(0,0+i*50)
        t.seth(0)
        t.fd(1000)

def draw_bg():
    #画草地
    
    move_pen(0,350)
    pen_set(4,123,245,95, 123,245,95)
    t.begin_fill()
    t.seth(-90)
    t.fd(350)
    t.seth(0)
    t.fd(1000)
    t.seth(90)
    t.fd(350)
    t.end_fill()
    #画天空
    move_pen(0,350)
    pen_set(4,134,196,247, 134,196,247)
    t.begin_fill()
    t.seth(90)
    t.fd(250)
    t.seth(0)
    t.fd(1000)
    t.seth(-90)
    t.fd(250)
    a=-180 + r_a
    for i in range(50):
        a = a - r_a/50
        t.seth(a)
        t.fd(500/50)
    a =180
    for i in range(50):
        a = a - r_a/50
        t.seth(a)
        t.fd(500/50)
    t.end_fill()
    
def draw_mud_pit():
    #画泥坑
    pen_set(5,130,119,100, 130,119,100)
    move_pen(350,150)
    t.begin_fill()
    t.seth(-180)
    t.circle(50,125)
    t.seth(-20)
    t.circle(350,60)
    t.seth(20)
    t.circle(50,30)
    t.seth(10)
    t.circle(50,30)
    t.seth(0)
    t.circle(50,30)
    t.seth(40)
    t.circle(50,90)
    t.seth(170)
    t.circle(500,45)
    t.end_fill()
    
def draw_shoes():
    pen_set(3,253,6,6, 253,70,70)
    move_pen(400,100)
    t.begin_fill()
    t.seth(0)
    t.fd(50)
    t.seth(87)
    t.fd(50)
    t.seth(180)
    t.fd(25)
    t.seth(-93)
    t.fd(20)
    t.seth(-180)
    t.fd(25)
    t.seth(-120)
    t.circle(45,38)
    t.end_fill()
    move_pen(470,100)
    t.begin_fill()
    t.seth(0)
    t.fd(50)
    t.seth(87)
    t.fd(50)
    t.seth(180)
    t.fd(25)
    t.seth(-93)
    t.fd(20)
    t.seth(-180)
    t.fd(25)
    t.seth(-120)
    t.circle(45,38)
    t.end_fill()
def draw_leg(): 
    pen_set(6,255,155,192, 255,155,192)
    move_pen(440,140)
    t.seth(90)
    t.fd(20)
    move_pen(510,140)
    t.seth(90)
    t.fd(20)
def draw_trousers():
    move_pen(400,300)
    pen_set(6,15,124,215, 66,163,242)
    t.begin_fill()
    d_a = 100
    a=-130 
    for i in range(60):
        a = a + 2
        t.seth(a)
        t.fd(3)
    for i in range(14):
        a = a + 0.02
        t.seth(a)
        t.fd(2)
    a = 0-a
    for i in range(14):
        a = a + 0.02
        t.seth(a)
        t.fd(2)
    for i in range(60):
        a = a + 2.2
        t.seth(a)
        t.fd(3)
    t.end_fill()
def draw_tile():
    move_pen(550,177)
    pen_set(6,255,155,192, 255,155,192)
    a=-60 
    for i in range(25):
        a = a + 4
        t.seth(a)
        t.fd(1)
    t.circle(5)
    a = -a
    for i in range(30):
        a = a + 4
        t.seth(a)
        t.fd(1)
def draw_hands():
    move_pen(550,250)
    pen_set(6,255,155,192, 255,155,192)
    t.seth(20)
    t.fd(70)
    move_pen(600,270)
    t.seth(60)
    t.fd(20)
    move_pen(600,270)
    t.seth(-20)
    t.fd(20)
    
    move_pen(380,250)
    t.seth(160)
    t.fd(50)
    move_pen(350,260)
    t.seth(100)
    t.fd(20)
    move_pen(350,260)
    t.seth(-140)
    t.fd(20)
    
def draw_face():
    move_pen(400,360)
    pen_set(4,255,155,192, 255,196,218)
    t.begin_fill()
    a=-120 
    for i in range(20):
        a = a + 2.5
        t.seth(a)
        t.fd(2.2)
    for i in range(130):
        a = a + 1.3
        t.seth(a)
        t.fd(1.8)
    for i in range(35):
        a = a + 1.4
        t.seth(a)
        t.fd(2)
    for i in range(50):
        a = a + 0.35
        t.seth(a)
        t.fd(2)
    for i in range(50):
        a = a + 0.2
        t.seth(a)
        t.fd(2)
    n=0.4
    for i in range(180):
        if 0<=i<30 or 60<=i<90 or 120<=i<150 :
            n=n+0.08
            t.lt(3) #向左转3度
            t.fd(n) #向前走a的步长
        else:
            n=n-0.08
            t.lt(3)
            t.fd(n)
    a=-50
    for i in range(20):
        a = a + 2.8
        t.seth(a)
        t.fd(5)
    t.end_fill()
def draw_other():
    move_pen(310,440)
    pen_set(6,255,145,192, 255,145,192)
    t.begin_fill()
    t.circle(3)
    t.end_fill()
    move_pen(330,430)
    t.begin_fill()
    t.circle(3)
    t.end_fill()
    
    pen_set(6,255,145,192, 255,255,255)
    move_pen(410,425)
    t.begin_fill()
    t.circle(10)
    t.end_fill()
    move_pen(460,395)
    t.begin_fill()
    t.circle(10)
    t.end_fill()
    
    pen_set(6,0,0,0,0,0,0)
    move_pen(405,429)
    t.begin_fill()
    t.circle(3)
    t.end_fill()
    move_pen(455,399)
    t.begin_fill()
    t.circle(3)
    t.end_fill()
    
    move_pen(510,310)
    pen_set(6,255,155,192, 255,155,192)
    t.begin_fill()
    t.circle(25)
    t.end_fill()
    
    move_pen(410,340)
    pen_set(6,255,145,192, 255,145,192)
    a=-80 
    for i in range(20):
        a = a + 6
        t.seth(a)
        t.fd(3)
    move_pen(430,445)
    pen_set(4,255,155,192, 255,196,218)
    t.begin_fill()
    a=120 
    for i in range(40):
        a = a - 2
        t.seth(a)
        t.fd(1.2)
    a=-a
    for i in range(45):
        a = a - 2
        t.seth(a)
        t.fd(1.2)
    t.end_fill()
    move_pen(480,430)
    t.begin_fill()
    a=70 
    for i in range(40):
        a = a -1.5
        t.seth(a)
        t.fd(1.5)
    a=-80
    for i in range(45):
        a = a -1.5
        t.seth(a)
        t.fd(1.5)
    t.end_fill()
draw_bg()
draw_mud_pit()
#draw_grid()
draw_leg()
draw_shoes()
draw_trousers()
draw_tile()
draw_hands()
draw_face()
draw_other()
t.mainloop()

说说我的代码思路。
再尝试画图之前,发现总是需要移动画笔,抬起画笔放下画笔啥的,感觉操作重复太多了,代码不好看,于是自己写了一个函数:

def move_pen(x,y):
    t.pu()
    t.goto(x-wight/2+50,y-height/2+50)
    t.pd()

这个函数的意思就是先抬起画笔,再移动画笔,最后放下画笔准备接下来的绘制。函数里面第二行画笔移动的位置是我人为的规定了左下角是画笔的坐标原点,坐标后面加的东西类似坐标系的平移,方便后面坐标的计算。
还有一个就是设置画笔属性的函数,每次画线之前都要用这两个操作,所以我封装了下面这个函数

def pen_set(size,r1,g1,b1,r2=0,g2=0,b2=0):
    t.pensize(size)
    t.color((r1,g1,b1),(r2,g2,b2))

我先画了天空和草地,然后发现我画画的功底极差,没什么直觉,画一个弧线啊都要式很久,所以我自己画了一个坐标网表,这个感觉超级有用,对于确定一些细节的东西(比如眼睛嘴巴手臂尾巴什么的)的位置的调节帮助很大~建议大家画别的东西的时候也可以用一用这个方法。
绘制的网表
利用网表画的猪头

def draw_grid():
    pen_set(1,0,0,0,0,0,0)
    for i in range(21):
        move_pen(0+i*50,0)
        t.seth(90)
        t.fd(600)
    for i in range(13):
        move_pen(0,0+i*50)
        t.seth(0)
        t.fd(1000)

嗯就是这样,剩下的每一个函数都是画一个身体部分的,都是手动一点一点调的参数,因为自己的画画水平不行,不知道什么时候画什么样的弧线,都是按照感觉来的,调参数的时间花了不少。
嗯,就是这么多了!
第一次写的python代码,还有一些c的感觉在里面,没有完全转换好,有不合理的地方欢迎指出,不胜感激
加油,为自己的py【手动斜眼笑*3】

猜你喜欢

转载自blog.csdn.net/qq_14823403/article/details/81390575