使用Python为中秋节绘制一块美味的月饼

每逢佳节...

5847426-acecd21ad4ad6fcf.png
在这里插入图片描述

对于在外的游子,每逢佳节倍思亲。而对于996ICU的苦逼程序猿们,最期待的莫过于各种节假日能把自己丢在床上好好休息一下了。这几天各公司都陆续开始发中秋礼品了。朋友圈各种秀高颜值的月饼,所以今天我也提前给大家送去一份中秋的 美味月饼吧!

python & turtle

python的turtle库,最早还是在小甲鱼的【零基础入门学习Python】中接触的,好久没用了有些生疏,带大家一起回顾下模块的使用吧。
如果你是想认真学习这个库,推荐去官网仔细学习 https://docs.python.org/3.7/library/turtle.html
但如果你只是跟我一样玩票性质的,那么你简单看看就好。我帮你整理了一些常用的方法:

参数说明
turtle.setup(width,height,startx,starty)起始点坐标:左上角相对于屏幕的坐标,默认在屏幕中央
turtle.begin_fill()准备开始填充图形
turtle.end_fill()填充完成
turtle.goto(x,y)将海龟走到该坐标位置 绝对坐标
turtle.bk(d)海龟后退 海龟坐标
turtle.fd(d)海龟前进
turtle.circle(r,angle)海龟左侧某一点为圆心曲线运行
turtle.seth(angle)海龟转向,绝对坐标
turtle.left(angle)左转
turtle.right(angle)右转
penup()抬起画笔
pendown()落下画笔
pencolor()笔的颜色
pensize()笔的大小
turtle.colormode(mode)改变RGB模式,mode=1.0小数值,mode=255整数值 RGB颜色,如white的RGB整数值为:255.255.255,修改数值可以改变颜色
turtle.done()画完之后不关闭窗口

就以上这些完全够你强势装13了...

练习画月饼

话不多说,先来一波!
然后,还没开团呢,先送一血了...为什么pycharm会告警呢?


5847426-9603904c5ba90ee8
Pycharm告警.png

但是使用又是正常的好奇怪,网上搜了一下解决办法。从import turtle进入源码,把142行开始的all注释掉,手动引用下turtle的所有方法即可。

# __all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +
#            _tg_utilities + ['Terminator']) # + _math_functions)

__all__ = ['ScrolledCanvas', 'TurtleScreen', 'Screen', 'RawTurtle', 'Turtle', 'RawPen', 'Pen', 'Shape', 'Vec2D', 'back',
           'backward', 'begin_fill', 'begin_poly', 'bk', 'addshape', 'bgcolor', 'bgpic', 'bye', 'clearscreen',
           'colormode', 'delay', 'exitonclick', 'getcanvas', 'getshapes', 'listen', 'mainloop', 'mode', 'numinput',
           'onkey', 'onkeypress', 'onkeyrelease', 'onscreenclick', 'ontimer', 'register_shape', 'resetscreen',
           'screensize', 'setup', 'Terminator', 'setworldcoordinates', 'textinput', 'title', 'tracer', 'turtles',
           'update', 'window_height', 'window_width', 'write_docstringdict', 'done', 'circle', 'clear', 'clearstamp',
           'clearstamps', 'clone', 'color', 'degrees', 'distance', 'dot', 'down', 'end_fill', 'end_poly', 'fd',
           'fillcolor', 'filling', 'forward', 'get_poly', 'getpen', 'getscreen', 'get_shapepoly', 'getturtle', 'goto',
           'heading', 'hideturtle', 'home', 'ht', 'isdown', 'isvisible', 'left', 'lt', 'onclick', 'ondrag', 'onrelease',
           'pd', 'pen', 'pencolor', 'pendown', 'pensize', 'penup', 'pos', 'position', 'pu', 'radians', 'right', 'reset',
           'resizemode', 'rt', 'seth', 'setheading', 'setpos', 'setposition', 'settiltangle', 'setundobuffer', 'setx',
           'sety', 'shape', 'shapesize', 'shapetransform', 'shearfactor', 'showturtle', 'speed', 'st', 'stamp', 'tilt',
           'tiltangle', 'towards', 'turtlesize', 'undo', 'undobufferentries', 'up', 'width', 'write', 'xcor', 'ycor']

问题解决了,开始写吧:

# -*- coding: utf-8 -*-
# @Author   : 王翔
# @WeChat   : King_Uranus
# @公众号    : 清风Python
# @Date     : 2019/9/8 23:08
# @Software : PyCharm
# @version  :Python 3.7.3
# @File     : Mid-Autumn_Festival.py

import turtle


def goto(x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()


def yuan():
    turtle.color("#D1C185", "#839F26")
    goto(0, -200)
    turtle.begin_fill()
    turtle.circle(200)
    turtle.end_fill()


def huabian():
    goto(0, 0)
    turtle.color("#839F26")
    for _ in range(20):
        turtle.right(18)
        turtle.begin_fill()
        turtle.forward(220)
        turtle.circle(40, 180)
        turtle.goto(0, 0)
        turtle.right(180)
        turtle.end_fill()


def neitu():
    turtle.color('#D1C185')
    goto(0, -25)
    for _ in range(12):
        turtle.begin_fill()
        turtle.circle(150, 60)
        turtle.left(90)
        turtle.circle(150, 60)
        turtle.end_fill()


def wirte():
    goto(-40, 10)
    turtle.color("red")
    turtle.write("中秋快乐", font=("Time", 18, "bold"))
    turtle.done()


if __name__ == '__main__':
    turtle.speed(10)
    huabian()
    yuan()
    neitu()
    wirte()

turtle.done()


看看效果:

5847426-cddb5ffdd6ed1eb9.gif
Python画月饼
The End

OK,今天的内容就到这里,如果觉得内容对你有所帮助,欢迎点击文章右下角的“在看”。
期待你关注我的公众号 清风Python,如果觉得不错,希望能动动手指转发给你身边的朋友们。
希望每周一至五清晨的7点10分,都能让清风Python的知识文章叫醒大家!谢谢……

5847426-c8d3e152cfe54f53
清风Python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值