• 80%的人答错,苹果logo上的叶子到底朝左还是朝右?


    苹果手机大家应该都有了解,但是你能区分出它真正的logo吗?本文主要介绍运用Python中的turtle库控制函数绘制苹果logo和相似图案,据说80%的人都选错了。
      
      

    一、效果展示

      
    在介绍代码之前,先来看下本文的实现效果。
      

      
    在下面的选项中选出你认为正确的logo,据说有80%的人都选错了,看看你能不能选对。
      
      

    二、代码详解

      
    Python绘制真假logo的原理是:应用turtle库依次绘制外框和里面的相似logo。

      

    1 导入库

      
    首先导入本文需要加载的库,如果你有些库还没有安装,导致运行代码时报错,可以在Anaconda Prompt中用pip方法安装。

    # -*- coding: UTF-8 -*-
    '''
    代码用途 :画苹果logo
    作者     :阿黎逸阳
    公众号     :  阿黎逸阳的代码
    '''
    import os
    import time
    import pygame
    import turtle as t 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    本文应用到的库较少,只应用了os、time、pygame和turtle四个库。
      
    os库可以设置文件读取的位置。
      
    time库用来设置代码暂停的时间。
      
    pygame库是为了绘制过程更有趣,在绘图过程中添加了背景音乐。
      
    turtle库是绘图库,相当于给你一支画笔,你可以在画布上用数学逻辑控制的代码完成绘图。

      

    2 播放音乐

      

    接着应用pygame库播放背景音乐,本文的音乐是《所念皆星河》。

    os.chdir(r'F:\公众号\58.真假logo\苹果')
    #播放音乐
    print('播放音乐')
    pygame.mixer.init()
    pygame.mixer.music.load("CMJ - 所念皆星河.mp3") 
    pygame.mixer.music.set_volume(0.5) 
    pygame.mixer.music.play(1, 10)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    这一部分的代码和整体代码是剥离的,可以选泽在最开始放上该代码,也可以直接删除。如果选择播放音乐,需要在代码music.load函数中把你想放音乐的电脑本地存放地址填进去。有部分朋友对这一块有疑问,填充格式可参考如下图片:
      

    在这里插入图片描述

      
      

    3 定义绘制logo的函数

      
    然后设置画板的大小,并定义绘制logo的函数,由于叶子的朝向不同,根据朝向定义了两个函数。

    t.title('阿黎逸阳的代码公众号')
    t.speed(10)
    #t.screensize(1000, 800)
    t.setup(startx=0, starty = 0, width=800, height = 600)
    def draw_logo(x, y):
        t.penup()
        t.goto(x, y)
        t.pendown()
        t.color('gray')
        t.begin_fill()
        t.setheading(150)
        t.circle(30, 90)
        #左边的弧度
        t.left(10)
        t.circle(50, 70)
        #下方小弧度
        t.circle(20, 60)
        #下方凹陷的地方
        t.setheading(20)
        t.circle(-40, 40)
        #下方小弧度
        t.circle(18, 60)
        t.left(5)
        t.circle(50, 76)
        t.left(8)
        t.circle(31, 91)
        t.end_fill()
        #画叶子
        t.penup()
        t.goto(x, y+9)
        t.pendown()
        t.color('gray')
        t.begin_fill()
        t.setheading(90)
        t.circle(-20, 90)
        t.setheading(-90)
        t.circle(-20, 90)
        t.end_fill()
    def draw_logo2(x, y):
        t.penup()
        t.goto(x, y)
        t.pendown()
        t.color('gray')
        t.begin_fill()
        t.setheading(150)
        t.circle(30, 90)
        #左边的弧度
        t.left(10)
        t.circle(50, 70)
        #下方小弧度
        t.circle(20, 60)
        #下方凹陷的地方
        t.setheading(20)
        t.circle(-40, 40)
        #下方小弧度
        t.circle(18, 60)
        t.left(5)
        t.circle(50, 76)
        t.left(8)
        t.circle(31, 91)
        t.end_fill()
        #画叶子
        t.penup()
        t.goto(x, y+9)
        t.pendown()
        t.color('gray')
        t.begin_fill()
        t.setheading(180)
        t.circle(-20, 90)
        t.setheading(0)
        t.circle(-20, 90)
        t.end_fill()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    关键代码详解:
      
    t.pensize(width):设置画笔的尺寸。
      
    t.color(color):设置画笔的颜色。
      
    t.penup():抬起画笔,一般用于另起一个地方绘图使用。
      
    t.goto(x,y):画笔去到某个位置,参数为(x,y),对应去到的横坐标和纵坐标。
      
    t.pendown():放下画笔,一般和penup组合使用。
      
    t.left(degree):画笔向左转多少度,括号里表示度数。
      
    t.right(degree):画笔向右转多少度,括号里表示度数。
      
    t.circle(radius,extent,steps):radius指半径,若为正,半径在小乌龟左侧radius远的地方,若为负,半径在小乌龟右侧radius远的地方;extent指弧度;steps指阶数。
      
    画logo的关键是:通过调节circle函数中的半径和弧度来调节曲线的弧度,从而使得logo的轮廓比较流畅。

      

    4 定义写文字的函数

      
    接着定义写文字的函数,方便在指定的位置写上标题和图片对应的序号。

    def write_1(x, y, size, ss):
        t.hideturtle()
        t.penup()
        t.goto(x, y)
        t.pendown()
        t.pensize(30)
        t.pencolor('black')
        t.write(ss, font=('YouYuan', size, 'normal'))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    x:文字的起始横坐标。

    y:文字的起始纵坐标。

    size:文字的大小。

    ss:文字。

      

    5 画外边框并调用函数画logo

      
    最后依次画外边框和调用函数画logo,并写好序号和标题。

    #画外边框
    print('画外边框')
    t.penup()
    t.goto(200, -200)
    t.pendown()
    t.setheading(90)
    t.color('gray')
    t.forward(400)
    t.setheading(180)
    t.forward(400)
    t.setheading(-90)
    t.forward(400)
    t.setheading(0)
    t.forward(400)
    #画里面的两条线
    print('画里面的两条线')
    t.penup()
    t.goto(0, 200)
    t.pendown()
    t.setheading(270)
    t.forward(400)
    t.penup()
    t.goto(-200, 0)
    t.pendown()
    t.setheading(0)
    t.forward(400)
    #画logo
    print('画logo')
    #A
    draw_logo(-100, 130)
    #B
    draw_logo(100, 130)
    #C
    draw_logo(-100, -70)
    #D
    draw_logo2(100, -70)
    #画咬痕B
    print('画咬痕')
    t.penup()
    t.goto(60, 128)
    t.pendown()
    t.setheading(-35)
    t.color('white')
    t.begin_fill()
    t.circle(-32, 140)
    t.end_fill()
    #画咬痕D
    t.penup()
    t.goto(60, -72)
    t.pendown()
    t.setheading(-35)
    t.color('white')
    t.begin_fill()
    t.circle(-32, 140)
    t.end_fill()
    #画咬痕C
    t.penup()
    t.goto(-60, -72)
    t.pendown()
    t.setheading(-125)
    t.color('white')
    t.begin_fill()
    t.circle(32, 360)
    t.end_fill()
    #标序号
    print('标序号')
    write_1(-190, 10, 25, 'A')
    write_1(10, 10, 25, 'B')
    write_1(-190, -190, 25, 'C')
    write_1(10, -190, 25, 'D')
    #写标题
    print('写标题')
    while 1:
        write_1(-160, 230, 18, '下图中哪个选项才是苹果logo?')
        time.sleep(1)
        t.undo()
        t.undo()
        t.undo()
        t.undo()
        t.undo()
        t.undo()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    至此,在Python中实现真假苹果logo的绘制逻辑已大致讲解完毕,感兴趣的朋友可以动手实现一遍。
      
    你可能感兴趣:
    用Python绘制皮卡丘
    用Python绘制词云图
    用Python绘制520永恒心动
    Python人脸识别—我的眼里只有你
    Python画好看的星空图(唯美的背景)
    【Python】情人节表白烟花(带声音和文字)
    用Python中的py2neo库操作neo4j,搭建关联图谱
    Python浪漫表白源码合集(爱心、玫瑰花、照片墙、星空下的告白)

    长按(扫一扫)识别上方二维码学习更多Python和建模知识,让你的学习和工作更出彩
  • 相关阅读:
    JavaScript 迈入 AI 新纪元
    一文带你梳理Python的中级知识
    Vue学习——props(23)
    6 种方式读取 Springboot 的配置,老鸟都这么玩(原理+实战)
    ANSYS中如何手动为装配体添加接触约束教程
    .NET 数据库大数据操作方案(插入、更新、删除、查询 、插入或更新)
    Emacs有什么优点,用Emacs写程序真的比IDE更方便吗?
    vue中动态引入图片为什么要是require, 你不知道的那些事
    Java中JVM虚拟机详解
    【linux】虚拟化
  • 原文地址:https://blog.csdn.net/qq_32532663/article/details/125588611