美文网首页
Day018|给大爷笑一个

Day018|给大爷笑一个

作者: 龙渊的宝藏 | 来源:发表于2021-03-12 00:09 被阅读0次

今天是Andy学Python的第18天哦! 

大家好,我是Andy。

今天学习了《教孩子学编程》第7章第2节用函数画笑脸。

01.小海龟走路有痕迹

笑脸图如下所示:

import random

import turtle as t

t.bgcolor("black")

t.speed(1)

# 定义笑脸函数

def draw_smiley(x, y):

    t.penup()

    t.goto(x,y)    

    t.pendown()

    # head

    t.pencolor("red")

    t.fillcolor("yellow")

    t.begin_fill()

    t.circle(50)

    t.end_fill()

    # left eye

    t.goto(x-15,y+60)

    t.fillcolor("blue")

    t.begin_fill()

    t.circle(10)

    t.end_fill()

    # right eye

    t.goto(x+15,y+60)

    t.fillcolor("blue")

    t.begin_fill()

    t.circle(10)

    t.end_fill()

    # mouth

    t.goto(x-30,y+40)

    t.pencolor("black")

    t.width(10)

    t.goto(x-10,y+10)

    t.goto(x+10,y+10)

    t.goto(x+30,y+40)

    t.width(1)

#画笑脸10个

for i in range(10):

    x = random.randrange(-t.window_width()//2,t.window_width()//2)

    y = random.randrange(-t.window_width()//2,t.window_width()//2)

    draw_smiley(x,y)

02.更改无痕迹

书上讲goto()与setpos()一样,但实际验证不一样,goto()函数带有海龟走路痕迹,而setpos()走过去无痕迹。

个人理解goto()是前往的意思,是携带画笔颜色,而setpos()是定位的意思,直接一键定位,不含路途,画笔可直接跳转。(其实我理解错了,请看下篇笔记)

import random

import turtle as t

t.bgcolor("black")

t.speed(5)

# 定义笑脸函数

def draw_smiley(x, y):

    t.penup()

    t.setpos(x,y)    

    t.pendown()

    # head

    t.pencolor("yellow")

    t.fillcolor("yellow")

    t.begin_fill()

    t.circle(50)

    t.end_fill()

    # left eye

    t.setpos(x-15,y+60)

    t.fillcolor("blue")

    t.begin_fill()

    t.circle(10)

    t.end_fill()

    # right eye

    t.setpos(x+15,y+60)

    t.fillcolor("blue")

    t.begin_fill()

    t.circle(10)

    t.end_fill()

    # mouth

    t.setpos(x-30,y+40)

    t.pencolor("black")

    t.width(10)

    t.goto(x-10,y+10)

    t.goto(x+10,y+10)

    t.goto(x+30,y+40)

    t.width(1)

#画笑脸10个

for i in range(10):

    x = random.randrange(-t.window_width()//2,t.window_width()//2)

    y = random.randrange(-t.window_width()//2,t.window_width()//2)

    draw_smiley(x,y)

End

不会Python,将成为人工智能时代的新“文盲”!

欢迎加入零基础自学Python计划,未来100天,Andy会把自学Python学习笔记持续输出公众号、视频号,不定期更新,邀您观战、加油、评论,亦或围观Andy被打脸。

人到中年,你一定要努力,但千万别着急。

我是Andy,一个终身学习者。

欢迎关注公众号与视频号:Andy学Python。

相关文章

  • Day018|给大爷笑一个

    今天是Andy学Python的第18天哦! 大家好,我是Andy。 今天学习了《教孩子学编程》第7章第2节用函数画...

  • 小段子里的深意

    今天听到两个笑话,女儿讲了一个,老大爷讲了一个,现分享给大家,如果不好笑,以后接着讲,讲到大家都笑为止。 晚上陪着...

  • 第九篇姐姐的新书包

    今天大爷给姐姐买个新书包,非常好看,我也想叫大爷给我买一个,可是大爷说是姐姐学习好才给姐姐买的,还说你啥时候考得好...

  • 第九篇姐姐的新书包

    今天大爷给姐姐买个新书包,非常好看,我也想叫大爷给我买一个,可是大爷说是姐姐学习好才给姐姐买的,还说你啥时候考得好...

  • Day018

    In the season of joy, I present my sincere wishes and kin...

  • Day018

    I am on my way back. Shenzhen See you tomorrow afternoon.

  • 晚熟的女人害自己 晚熟的男人害全家

    有一次到村子里面做调研,和一个老大爷交流,我问他“大爷,您知道为什么近亲不能结婚吧?” 大爷嘿嘿一笑,说“太熟了,...

  • 每次死亡都会重生,可每次重生都是同一种死法..

    第1章 龙形玉佩 “小妞,给大爷乐一个,要不大爷给你乐一个也成!” “什么不依,看大爷拿银票扇你!” 在白杨镇,翡...

  • 搞笑大爷

    来个搞笑“打野”,不对,是大爷! 一 路人甲:大爷,听说你会武术啊? 大爷:啊!对啊! 路人甲:那你给咱们武一个呗...

  • 来!给爷笑一个 -038

    生活就是你大爷 笑或不笑 你来定

网友评论

      本文标题:Day018|给大爷笑一个

      本文链接:https://www.haomeiwen.com/subject/alzbqltx.html