当前位置:网站首页>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 :
  

 Insert picture description here

  
  

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 )

Long press ( scan ) Identify the QR code above and learn more Python And modeling knowledge , Make your study and work more brilliant
原网站

版权声明
本文为[Ali Yiyang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207072148581831.html