程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python+turtle sample code for drawing spider man

編輯:Python

Catalog

One 、 Effect display

Two 、 Code details

1. Import library

2. Play music

3. Define the function that draws spider man's upper body

4. Define functions that draw left and right hands

5. Define the function for drawing spiders

6. Call a function to draw a graph

spider-man (Spider-Man) Peter · Parke (Peter Parker), It's a marvel superhero .

By screenwriter Stan · Lee and painter Steve · Ditco co co created , First appeared in 《 Amazing fantasy 》(Amazing Fantasy) The first 15 period (1962 year 8 month ).

Because it is very popular , After a few months , He began to have a comic strip with himself as the protagonist .

The comments on the Spider Man Theme song in the Netease cloud that have been commented by tens of thousands say .

Why spider man became one of the most popular superheroes , Because of the people under this mask , Regardless of color 、 race 、 Gender ...

He / She could be you , Is my , It's the people you live with .

Anyone can wear this mask , You can also do , If you haven't thought about it before , I hope it's ok now ~

This paper mainly introduces the application of python Medium turtle Library control function to draw spider man .

One 、 Effect display

Before I introduce the code , Let's first look at the implementation effect of this paper .

You can refer to the following steps to Python File to exe, Send to not installed Python he / she .

Pinstaller(Python Packaging for exe file )

Before I put Python The files are packed into exe When , It's been a long time , This article will describe in detail how to quickly build without installing Python Files that can also be executed on your computer

1. stay prompt Run in pip install pyinstaller , install  pyinstaller  library

2.  stay prompt Run in where pyinstaller 

3.  Find to be pack Path of file storage

Put the files to be packaged in the path found  

C:\Users\Administrator\Anaconda3\Scripts in  ( My path is this , You follow the path of step two )

4.  call cmd window

Put the files to be packaged in

C:\Users\Administrator\Anaconda3 \Scripts Under the table of contents , Press... In this folder shift+ Right mouse button , Click on Open command window here call cmd 

5.  stay cmd Input in pyinstaller -F   file name

Example : pack Python Draw Picchu's video , stay cmd Input in pyinstaller -F  pkq_1.py

To generate a common icon exe Executable file .

6.  Generate exe file

Can be in the path

C:\Users\Administrator\Anaconda3\Scripts Under the dist Found the packed... In the folder exe file ( That is, there is no need to install Python You can also run the file ).

The file icon thus generated is in a standard fixed format , If you want to generate an icon with a specific shape, you need to use the 7 Click the statement in .

7.   Generate custom shape icons , stay cmd Input in :pyinstaller -i  ico route -F xxxxx.py

Example :  pack   Python Draw Picchu video py file , stay cmd Input in ( notes : I put ico The icon and the files to be packaged are placed in a folder , So I directly input ico Name )

pyinstaller -i  pikaqiu2.ico -F pkq_1.py

The generated icon is Picchu shaped exe file .

Two 、 Code details

Python The principle of drawing spider man is : application turtle The library draws different parts of the body .

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 : The author of spider man : Ali Yiyang blog : https://blog.csdn.net/qq_32532663/article/details/106176609'''import osimport pygameimport turtle as t

This article applies to fewer libraries , Only os、pygame and turtle Three libraries .

os The library can set the location where files are read .

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 《Sunflower》.

os.chdir(r'F:\ official account \56. spider-man ')# Play music print(' Play music ')pygame.mixer.init()pygame.mixer.music.load("Cope - Sunflower (Original Version).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 the function that draws spider man's upper body

Then set the size of the drawing board , And define the function to draw spider man's upper body .

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 up_body(): # Picture head t.penup() t.goto(60, 200) t.pendown() t.pensize(1) t.color('black', 'red') t.begin_fill() t.setheading(60) t.circle(60, 30) t.left(4) t.circle(40, 173) t.left(4) t.circle(60, 30) # Draw the neck t.setheading(260) t.circle(30, 29) # Draw shoulders t.setheading(220) t.forward(30) # Draw the hand muscles t.setheading(150) t.circle(30, 130) # Draw the inner line of the chest t.setheading(30) t.circle(-100, 13) t.setheading(270) t.circle(50, 40) t.setheading(255) t.circle(55, 40) t.circle(-40, 50) # Draw the outer horizontal line of the waist t.setheading(0) t.forward(-7) t.setheading(270) t.forward(18) # Draw a waist line t.setheading(-30) t.forward(50) t.setheading(15) t.forward(80) t.setheading(90) t.forward(22) # Where it repeats # Draw the inner outline of the clothes t.setheading(190) t.forward(20) t.setheading(103) t.circle(-160, 41) # Draw the inner contour of the hand t.setheading(5) t.circle(-80, 30) t.setheading(20) t.circle(30, 30) # Where it repeats # Arm muscles t.setheading(70) t.circle(22, 150) t.setheading(150) t.forward(30) t.setheading(120) t.forward(15) 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 .

The key to the outline is : By adjusting the circle Function to adjust the radian of the curve , So that the outline of Spider-Man is more smooth .

4. Define functions that draw left and right hands

Then define the functions that draw the left hand and the right hand .

def left_hand(): # Draw the left arm # Draw the inner line of the chest t.penup() t.goto(-69, 134) t.color('black', 'blue') t.pendown() t.begin_fill() t.setheading(30) t.circle(-100, 13) t.setheading(270) t.circle(50, 40) t.setheading(255) t.circle(55, 40) t.circle(-40, 50) # Draw the outer horizontal line of the waist t.setheading(0) t.forward(-8) t.setheading(90) t.circle(220, 18) t.setheading(-90) t.circle(-40, 50) t.setheading(-85) t.circle(-50, 50) t.setheading(135) t.circle(30, 40) t.setheading(95) t.circle(-50, 50) t.setheading(98) t.circle(-60, 51) t.end_fill()def right_hand(): # Draw the right arm # Draw the inner outline of the clothes t.penup() t.goto(80, 39) t.color('black', 'blue') t.pendown() t.begin_fill() t.setheading(190) t.forward(20) t.setheading(103) t.circle(-160, 41) # Draw the inner contour of the hand t.setheading(5) t.circle(-80, 30) t.setheading(20) t.circle(30, 30) t.setheading(-20) t.circle(-55, 65) t.setheading(-30) t.circle(-50, 60) t.setheading(180) t.circle(30, 40) t.setheading(154) t.circle(-48, 60) t.setheading(164) t.circle(-50, 60) t.setheading(-90) t.circle(-40, 60) t.left(40) t.circle(150, 23) t.end_fill()def left_wrist(): # Draw the left wrist t.penup() t.goto(-81, 37) t.color('black', 'red') t.pendown() t.begin_fill() t.setheading(135) t.circle(30, 40) t.setheading(-90) t.circle(-60, 30) t.setheading(-90) t.forward(20) t.setheading(-45) t.forward(12) t.circle(6, 180) t.setheading(-50) t.circle(5, 160) t.setheading(95) t.forward(10) t.setheading(135) t.forward(8) t.setheading(95) t.forward(6) t.setheading(35) t.circle(30, 10) t.left(10) t.circle(30, 27) t.end_fill() # Draw a line on your wrist # Horizontal line # The first horizontal line t.penup() t.goto(-84, 30) t.color('black') t.pendown() t.setheading(145) t.circle(30, 36) # The second horizontal line t.penup() t.goto(-90, 22) t.color('black') t.pendown() t.setheading(185) t.circle(-30, 31) # The third horizontal line t.penup() t.goto(-83, 10) t.color('black') t.pendown() t.setheading(210) t.circle(-50, 31) # The fourth horizontal line t.penup() t.goto(-102, -10) t.color('black') t.pendown() t.setheading(50) t.circle(-20, 41) t.setheading(55) t.circle(-90, 8) # The first vertical line t.penup() t.goto(-105, 24) t.color('black') t.pendown() t.setheading(-95) t.circle(100, 20) # The second vertical line t.penup() t.goto(-87, 42) t.color('black') t.pendown() t.setheading(-110) t.forward(22) t.setheading(-63) t.circle(-50, 40)def right_wrist(): # Draw the right wrist t.penup() t.goto(189, 57) t.color('black', 'red') t.pendown() t.begin_fill() t.setheading(180) t.circle(30, 40) t.setheading(-55) t.circle(-100, 10) t.circle(-20, 70) t.setheading(-90) t.forward(10) t.setheading(-0) t.forward(5) t.setheading(-85) t.forward(8) t.setheading(-20) t.circle(8, 60) t.setheading(-35) t.circle(8, 70) t.setheading(-15) t.circle(6, 70) t.setheading(60) t.circle(20, 80) t.setheading(115) t.circle(-100, 20) t.end_fill() # Draw the first horizontal line t.goto(191, 45) t.color('black') t.pendown() t.setheading(215) t.circle(-30, 34) # Draw the second horizontal line t.penup() t.goto(197, 29) t.color('black') t.pendown() t.setheading(215) t.circle(-30, 37) # Draw the third horizontal line t.penup() t.goto(174, 11) t.color('black') t.pendown() t.setheading(-0) t.circle(-30, 27) t.setheading(20) t.circle(-20, 27) t.setheading(40) t.circle(-30, 23) # Draw the first vertical line t.penup() t.goto(178, 55) t.color('black') t.pendown() t.setheading(-70) t.circle(-200, 9) t.setheading(-82) t.circle(-100, 18) # Draw the second vertical line t.penup() t.goto(185, 55) t.color('black') t.pendown() t.setheading(-70) t.circle(-200, 8) t.setheading(-68) t.circle(-80, 25)5. Define the function for drawing spiders

Then define the function for drawing spiders .

def spider(): # Painting spiders t.penup() t.goto(8, 146) t.color('black') t.pendown() t.begin_fill() t.setheading(-120) t.circle(40, 60) t.setheading(60) t.circle(40,60) t.end_fill() # Draw the spider's feet # The right foot 1 t.penup() t.goto(13, 129) t.color('black') t.pendown() t.setheading(30) t.forward(10) t.setheading(90) t.forward(15) # The right foot 2 t.penup() t.goto(14, 125) t.color('black') t.pendown() t.setheading(30) t.forward(16) t.setheading(90) t.forward(17) # The right foot 3 t.penup() t.goto(14, 124) t.color('black') t.pendown() t.setheading(-20) t.forward(16) t.setheading(-90) t.forward(17) # The right foot 4 t.penup() t.goto(14, 120) t.color('black') t.pendown() t.setheading(-20) t.forward(10) t.setheading(-90) t.forward(15) # Draw the spider's feet # The left foot 1 t.penup() t.goto(3, 129) t.color('black') t.pendown() t.setheading(150) t.forward(10) t.setheading(90) t.forward(15) # The right foot 2 t.penup() t.goto(2, 125) t.color('black') t.pendown() t.setheading(150) t.forward(16) t.setheading(90) t.forward(17) # The right foot 3 t.penup() t.goto(2, 124) t.color('black') t.pendown() t.setheading(-170) t.forward(16) t.setheading(-99) t.forward(17) # The right foot 4 t.penup() t.goto(3, 120) t.color('black') t.pendown() t.setheading(-170) t.forward(10) t.setheading(-90) t.forward(15)6. Call a function to draw a graph

Finally, call the function to draw the graph .

print(' Draw the outline of the upper body ')up_body()print(' Draw the right hand ')right_hand()print(' Draw the left hand ')left_hand()print(' Draw the left fist ')left_wrist()print(' Draw the right fist ')right_wrist()print(' Draw spiders ')spider()

thus , stay Python The drawing logic of Spider-Man has been roughly explained in .

That's all Python+Turtle Draw the details of Spider-Man's sample code , More about Python Turtle Drawing Spider-Man information, please pay attention to other relevant articles on the software development website !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved