苹果手机大家应该都有了解,但是你能区分出它真正的logo吗?

本文主要介绍运用Python中的turtle库控制函数绘制苹果logo和相似图案,据说80%的人都选错了。

本文目录

效果展示代码详解

2.1 导入库

2.2 播放音乐

2.3 定义绘制logo的函数

2.4 定义写文字的函数

2.5 画外边框并调用函数画logo一、效果展示

http://mpvideo.qpic.cn/0bc3heaasaaaneabsigb5rrfaoodbe4qacia.f10002.mp4?

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

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

答案见本文末尾。

二、代码详解

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

1 导入库

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

代码语言:javascript代码运行次数:0运行复制# -*- coding: UTF-8 -*-

'''

代码用途 :画苹果logo

作者 :阿黎逸阳

博客 : https://blog.csdn.net/qq_32532663/article/details/106176609

'''

import os

import time

import pygame

import turtle as t 本文应用到的库较少,只应用了os、time、pygame和turtle四个库。

os库可以设置文件读取的位置。

time库用来设置代码暂停的时间。

pygame库是为了绘制过程更有趣,在绘图过程中添加了背景音乐。

turtle库是绘图库,相当于给你一支画笔,你可以在画布上用数学逻辑控制的代码完成绘图。

2 播放音乐

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

代码语言:javascript代码运行次数:0运行复制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)这一部分的代码和整体代码是剥离的,可以选泽在最开始放上该代码,也可以直接删除。

如果选择播放音乐,需要在代码music.load函数中把你想放音乐的电脑本地存放地址填进去。

有部分朋友对这一块有疑问,填充格式可参考如下图片:

3 定义绘制logo的函数

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

代码语言:javascript代码运行次数:0运行复制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()关键代码详解:

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 定义写文字的函数

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

代码语言:javascript代码运行次数:0运行复制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'))x:文字的起始横坐标。

y:文字的起始纵坐标。

size:文字的大小。

ss:文字。

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

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

代码语言:javascript代码运行次数:0运行复制#画外边框

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()至此,在Python中实现真假苹果logo的绘制逻辑已大致讲解完毕,感兴趣的朋友可以动手实现一遍。

正确答案为:C。