Python急转弯-5-面试篇

2019-07-18 15:53:05 浏览数 (2)

在前几篇中,都是枯燥无味,还要动动脑筋的算法题,现在可以放松下,来完成画一只 粉红 Pig。 随着函数出现的拼图,来猜猜这只会是什么样的 Pig?

大部分python安装环境下都包含turtle这个绘图模块

  • 切换RGB色彩模式
  • 画笔控制函数
  • 运动控制函数
  • 方向控制函数

① 画鼻子

  • turtle.penup()

画笔抬起,不留下痕迹

  • turtle.pendown()

画笔落下,留下痕迹

  • turtle.setheading(angle)

改变行进方向

angle:改变方向的角度(绝对坐标下,绝对角度)

  • turtle.goto(100,100)

指从当前的点指向括号内所给坐标

代码语言:javascript复制
def nose(x,y):
    turtle.penup()
    turtle.goto(x,y)
    turtle.pendown()
    turtle.setheading(-30)
    turtle.begin_fill()
    a = 0.4
    for i in range(120):
        if 0<=i<30 or 60<=i<90:
            a = a   0.08
            turtle.left(3)
            turtle.forward(a)
        else:
            a = a - 0.08
            turtle.left(3)
            turtle.forward(a)
    turtle.end_fill()

    turtle.penup()
    turtle.setheading(90)
    turtle.forward(25)
    turtle.setheading(0)
    turtle.forward(10)
    turtle.pendown()
    turtle.pencolor("deeppink")
    turtle.setheading(10)
    turtle.begin_fill()
    turtle.circle(5)
    turtle.color("pink")
    turtle.end_fill()

    turtle.penup()
    turtle.setheading(0)
    turtle.forward(20)
    turtle.pendown()
    turtle.pencolor("deeppink")
    turtle.setheading(10)
    turtle.begin_fill()
    turtle.circle(5)
    turtle.color("pink")
    turtle.end_fill()

② 画头部

  • turtle.circle(r,angle)

指沿着海龟左侧的某一点做圆运动

根据半径r,绘制一个extent角度的弧度

r:默认圆心在海龟左侧r距离的位置

  • turtle.forword(d)

向前行进 d:行进距离,可以为负数

代码语言:javascript复制
def head(x,y):
    turtle.color("pink")
    turtle.penup()
    turtle.goto(x,y)
    turtle.setheading(180)
    turtle.circle(300,-30)
    turtle.circle(100,-60)
    turtle.circle(80,-100)
    turtle.circle(150,-20)
    turtle.circle(60,-95)
    turtle.setheading(161)
    turtle.circle(-300,15)
    turtle.penup()
    turtle.goto(-100,100)
    turtle.pendown()
    turtle.setheading(-30)
    a = 0.4
    for i in range(60):
        if 0<=i<30 or 60<=i<90:
            a = a   0.08
            turtle.left(3)
            turtle.forward(a)
        else:
            a = a - 0.08
            turtle.left(3)
            turtle.forward(a)
    turtle.end_fill()

③ 画耳朵

代码语言:javascript复制
def ears(x,y):
    turtle.color("pink")
    turtle.penup()
    turtle.goto(x,y)
    turtle.pendown()
    turtle.begin_fill()
    turtle.setheading(100)
    turtle.circle(-50,50)
    turtle.circle(-10,120)
    turtle.circle(-50,54)
    turtle.end_fill()

    turtle.penup()
    turtle.setheading(90)
    turtle.forward(-12)
    turtle.setheading(0)
    turtle.forward(30)
    turtle.pendown()
    turtle.begin_fill()
    turtle.setheading(100)
    turtle.circle(-50,50)
    turtle.circle(-10,120)
    turtle.circle(-50,56)
    turtle.end_fill()

④ 画眼睛

代码语言:javascript复制
def eyes(x,y):
    turtle.color("white")
    turtle.penup()
    turtle.setheading(90)
    turtle.forward(-20)
    turtle.setheading(0)
    turtle.forward(-95)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(15)
    turtle.end_fill()

    turtle.color("black")
    turtle.penup()
    turtle.setheading(90)
    turtle.forward(12)
    turtle.setheading(0)
    turtle.forward(-3)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(3)
    turtle.end_fill()

    turtle.color("white")
    turtle.penup()
    turtle.setheading(90)
    turtle.forward(-25)
    turtle.setheading(0)
    turtle.forward(40)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(15)
    turtle.end_fill()

    turtle.color("black")
    turtle.penup()
    turtle.setheading(90)
    turtle.forward(12)
    turtle.setheading(0)
    turtle.forward(-3)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(3)
    turtle.end_fill()

⑤ 画脸颊

代码语言:javascript复制
def cheek(x,y):
    turtle.color("pink")
    turtle.penup()
    turtle.goto(x,y)
    turtle.pendown()
    turtle.setheading(0)
    turtle.begin_fill()
    turtle.circle(30)
    turtle.end_fill()

⑥ 画嘴巴

代码语言:javascript复制
def mouth(x,y):
    turtle.color("pink")
    turtle.penup()
    turtle.goto(x,y)
    turtle.pendown()
    turtle.setheading(-80)
    turtle.circle(30,40)
    turtle.circle(40,80)

⑦ 全局设置

  • turtle.colormode(mode)

1.0:RGB小数模式

255:RGB整数模式

  • turtle.pensize(width)

画笔宽度

  • turtle.setup(width,height,startx,starty)

-setup() 设置窗体的位置和大小 相对于桌面的起始点的坐标以及窗口的宽度高度,若不写窗口的起始点,则默认在桌面的正中心,窗体的坐标原点默认在窗口的中心。

代码语言:javascript复制
def setting():
    turtle.pensize(4)
    turtle.hideturtle()
    turtle.colormode(255)
    turtle.color("pink")
    turtle.setup(840,500)
    turtle.speed(10)

⑧ 主函数

代码语言:javascript复制
def main():
    setting()
    nose(-100,100)
    head(-69,167)
    ears(0,160)
    eyes(0,140)
    cheek(80,10)
    mouth(-20,30)
    turtle.done()

来欣赏下,辛苦那么久,敲了那么多码,结果会是什么呢?

0 人点赞