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

[Python painting spider man] did your DNA move?

編輯:Python

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 Pinstaller(Python Packaging for exe file ) A little bit of Python File to exe, Send to not installed Python he / she .

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 : Draw spider man author : Ali Yiyang official account : The code of Ali Yiyang '''
import os
import pygame
import 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 . Call your friends who like challenges to complete the missing part , See who completes it first .

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 )


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