当前位置:网站首页>80%的人答错,苹果logo上的叶子到底朝左还是朝右?
80%的人答错,苹果logo上的叶子到底朝左还是朝右?
2022-07-07 21:54:00 【阿黎逸阳】
苹果手机大家应该都有了解,但是你能区分出它真正的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
本文应用到的库较少,只应用了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)
这一部分的代码和整体代码是剥离的,可以选泽在最开始放上该代码,也可以直接删除。如果选择播放音乐,需要在代码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()
关键代码详解:
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'))
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()
至此,在Python中实现真假苹果logo的绘制逻辑已大致讲解完毕,感兴趣的朋友可以动手实现一遍。
你可能感兴趣:
用Python绘制皮卡丘
用Python绘制词云图
用Python绘制520永恒心动
Python人脸识别—我的眼里只有你
Python画好看的星空图(唯美的背景)
【Python】情人节表白烟花(带声音和文字)
用Python中的py2neo库操作neo4j,搭建关联图谱
Python浪漫表白源码合集(爱心、玫瑰花、照片墙、星空下的告白)

边栏推荐
- C - linear table
- Gorm Association summary
- ASP. Net query implementation
- @Detailed introduction of configuration annotation
- 第四期SFO销毁,Starfish OS如何对SFO价值赋能?
- One click installation with fishros in blue bridge ROS
- Pypharm uses, and the third-party library has errors due to version problems
- LinkedBlockingQueue源码分析-新增和删除
- Oracle string sorting
- Codeworks 5 questions per day (average 1500) - day 8
猜你喜欢
Learn about scratch
Problems faced when connecting to sqlserver after downloading (I)
At the age of 35, I made a decision to face unemployment
如何衡量产品是否“刚需、高频、痛点”
The function is really powerful!
Navicat connects Oracle
Benchmarking Detection Transfer Learning with Vision Transformers(2021-11)
MySQL Architecture
95. (cesium chapter) cesium dynamic monomer-3d building (building)
蓝桥ROS中使用fishros一键安装
随机推荐
一鍵免費翻譯300多頁的pdf文檔
redis缓存工具类,值得拥有~
数据库面试题+解析
C - linear table
Rock-paper-scissors
Ora-02437 failed to verify the primary key violation
[path planning] use the vertical distance limit method and Bessel to optimize the path of a star
C language greedy snake
关于CH32库函数与STM32库函数的区别
Pigsty: out of the box database distribution
One of the anti climbing methods
35岁那年,我做了一个面临失业的决定
gorm 关联关系小结
Installing gradle
Restricted linear table
The function is really powerful!
平衡二叉樹【AVL樹】——插入、删除
Ping error: unknown name or service
Balanced binary tree [AVL tree] - insert, delete
Pycharm essential plug-in, change the background (self use, continuous update) | CSDN creation punch in