当前位置:网站首页>80% of the people answered incorrectly. Does the leaf on the apple logo face left or right?
80% of the people answered incorrectly. Does the leaf on the apple logo face left or right?
2022-07-07 23:56:00 【Ali Yiyang】
Everyone should know about Apple Mobile , But you can tell what it really is logo Do you ? This paper mainly introduces the application of Python Medium turtle Library control function drawing Apple logo And similar patterns , It is said that 80% All the people chose the wrong .
One 、 Effect display
Before I introduce the code , Let's first look at the implementation effect of this paper .
Choose what you think is right from the following options logo, It is said that there is 80% All the people chose the wrong , See if you can choose the right .
Two 、 Code details
Python Draw true and false logo The principle is : application turtle The library draws the outer frame and the inner frame in turn logo.
1 Import library
First, import the library to be loaded in this article , If you don't have some libraries installed , Cause the running code to report an error , Can be in Anaconda Prompt of use pip Method installation .
# -*- coding: UTF-8 -*-
''' Code usage : Draw apples logo author : Ali Yiyang official account : The code of Ali Yiyang '''
import os
import time
import pygame
import turtle as t
This article applies to fewer libraries , Only os、time、pygame and turtle Four Libraries .
os The library can set the location where files are read .
time Library is used to set the code pause time .
pygame The library is designed to make the drawing process more interesting , Added background music during drawing .
turtle Library is a drawing library , It's equivalent to giving you a brush , You can draw on the canvas with code controlled by mathematical logic .
2 Play music
Then apply pygame Library playing background music , The music of this article is 《 All I think are stars 》.
os.chdir(r'F:\ official account \58. True and false logo\ Apple ')
# Play music
print(' Play music ')
pygame.mixer.init()
pygame.mixer.music.load("CMJ - All I think are stars .mp3")
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(1, 10)
This part of the code is separated from the whole code , You can choose to put the code at the beginning , You can also delete . If you choose to play music , Need to be in code music.load Function to fill in the local storage address of the computer where you want to play music . Some friends have questions about this piece , For filling format, please refer to the following pictures :

3 Define drawing logo Function of
Then set the size of the drawing board , And define drawing logo Function of , Due to the different orientation of leaves , Two functions are defined according to the orientation .
t.title(' The official account of Ali ESEY code ')
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)
# Arc on the left
t.left(10)
t.circle(50, 70)
# Lower small radian
t.circle(20, 60)
# The depression below
t.setheading(20)
t.circle(-40, 40)
# Lower small radian
t.circle(18, 60)
t.left(5)
t.circle(50, 76)
t.left(8)
t.circle(31, 91)
t.end_fill()
# Draw leaves
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)
# Arc on the left
t.left(10)
t.circle(50, 70)
# Lower small radian
t.circle(20, 60)
# The depression below
t.setheading(20)
t.circle(-40, 40)
# Lower small radian
t.circle(18, 60)
t.left(5)
t.circle(50, 76)
t.left(8)
t.circle(31, 91)
t.end_fill()
# Draw leaves
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()
Key code details :
t.pensize(width): Sets the size of the brush .
t.color(color): Set the color of the brush .
t.penup(): Lift up the brush. , It is generally used for drawing in another place .
t.goto(x,y): The brush goes to a certain position , Parameter is (x,y), Corresponding abscissa and ordinate .
t.pendown(): Put down the paintbrush , In general, and penup Use a combination of .
t.left(degree): How many degrees does the brush turn left , Degrees in parentheses .
t.right(degree): How many degrees does the brush turn right , Degrees in parentheses .
t.circle(radius,extent,steps):radius Finger radius , If it is positive , The radius is on the left side of the little turtle radius Far away , If it's negative , The radius is on the right side of the little turtle radius Far away ;extent Refers to radian ;steps Finger order .
draw logo The key is : By adjusting the circle Function to adjust the radian of the curve , Thus making logo The outline of is relatively smooth .
4 Define functions for writing text
Then define the function of writing text , It is convenient to write the title and the serial number corresponding to the picture in the specified position .
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: The starting abscissa of the text .
y: The starting ordinate of the text .
size: Size of text .
ss: written words .
5 Draw the outer border and call the function to draw logo
Finally, draw the outer border and call function in turn logo, And write the serial number and title .
# Outside frame
print(' Outside frame ')
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)
# Draw two lines inside
print(' Draw two lines inside ')
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)
# draw logo
print(' draw logo')
#A
draw_logo(-100, 130)
#B
draw_logo(100, 130)
#C
draw_logo(-100, -70)
#D
draw_logo2(100, -70)
# Draw bite marks B
print(' Draw bite marks ')
t.penup()
t.goto(60, 128)
t.pendown()
t.setheading(-35)
t.color('white')
t.begin_fill()
t.circle(-32, 140)
t.end_fill()
# Draw bite marks 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()
# Draw bite marks 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()
# Bid No
print(' Bid No ')
write_1(-190, 10, 25, 'A')
write_1(10, 10, 25, 'B')
write_1(-190, -190, 25, 'C')
write_1(10, -190, 25, 'D')
# Write the title
print(' Write the title ')
while 1:
write_1(-160, 230, 18, ' Which option in the figure below is apple logo?')
time.sleep(1)
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
t.undo()
thus , stay Python Realize true and false apples logo The drawing logic of has been roughly explained , Interested friends can do it again .
You may be interested in :
use Python Drawing Picasso
use Python Draw a cloud of words
use Python draw 520 Eternal heart
Python Face recognition — You are the only one in my eyes
Python Draw a nice picture of the starry sky ( Aesthetic background )
【Python】 Valentine's Day confession fireworks ( With sound and text )
use Python Medium py2neo Library operation neo4j, Building the association map
Python Romantic confession source collection ( love 、 The roses 、 Photo wall 、 Advertising under the stars )

边栏推荐
- webflux - webclient Connect reset by peer Error
- SQL connection problem after downloading (2)
- Basic learning of SQL Server -- creating databases and tables with the mouse
- QT and OpenGL: loading 3D models using the open asset import library (assimp) - Part 2
- Seven years' experience of a test engineer -- to you who walk alone all the way (don't give up)
- Gorm Association summary
- Anti climbing means cracking the second
- go time包常用函数
- Data analysis series 3 σ Rule / eliminate outliers according to laida criterion
- Uic564-2 Appendix 4 - flame retardant fire test: flame diffusion
猜你喜欢

SQL connection problem after downloading (2)

Introduction to programming hardware

激光slam学习(2D/3D、偏实践)

Kubectl's handy command line tool: Oh my Zsh tips and tricks

Aitm3.0005 smoke toxicity test

How did a fake offer steal $540million from "axie infinity"?

Set up personal network disk with nextcloud

【推荐系统基础】正负样本采样和构造

Chisel tutorial - 05 Sequential logic in chisel (including explicit multi clock, explicit synchronous reset and explicit asynchronous reset)

Problems faced when connecting to sqlserver after downloading (I)
随机推荐
Anti climbing means cracking the second
Restricted linear table
Robomaster visual tutorial (0) Introduction
QT creator add custom new file / Project Template Wizard
Install sqlserver2019
【编程题】【Scratch二级】2019.12 绘制十个正方形
C - minute number V3
C - Fibonacci sequence again
QT creator add JSON based Wizard
BSS 7230 flame retardant performance test of aviation interior materials
快速回复二极管整流特性
@Detailed introduction of configuration annotation
FFA与ICGA造影
在网页中打开展示pdf文件
串联二极管,提高耐压
ROS从入门到精通(九) 可视化仿真初体验之TurtleBot3
2022.7.7-----leetcode.648
Reverse output three digit and arithmetic sequence
第四期SFO销毁,Starfish OS如何对SFO价值赋能?
One click free translation of more than 300 pages of PDF documents