Python draws a small hamster

Meaty little animals are very cute. This article introduces the use of the turtle library control functions in Python to draw small hamsters.
  

  

1. Effect display

  
Before introducing the code, let's take a look at the implementation effect of this article.

You can refer to the article Pinstaller (Python is packaged into an exe file) to convert the Python file into an exe and send it to him/her who does not have Python installed.

  
  

2. Detailed code explanation

  
The principle of Python drawing a small hamster is: use the turtle library to draw different parts of the body.

  

1 import library

  
First import the libraries that need to be loaded in this article. If you have not installed some libraries and an error is reported when running the code, you can use the pip method to install them in Anaconda Prompt.

# -*- coding: UTF-8 -*-
'''
代码用途 :画小仓鼠
作者     :阿黎逸阳
博客     :  https://blog.csdn.net/qq_32532663/article/details/106176609
'''
import os
import pygame
import turtle as t 

There are fewer libraries used in this article, and only three libraries of os, pygame and turtle are used.
  
The os library can set where the file is read from.
  
The pygame library is to make the drawing process more interesting, adding background music during the drawing process.
  
The turtle library is a drawing library, which is equivalent to giving you a brush, and you can use the code controlled by mathematical logic to complete the drawing on the canvas.

  
  

2 play music

  
Then use the pygame library to play background music. The music in this article is "Eran - Spring の思い出".

#播放音乐
print('播放音乐')
pygame.mixer.init()
pygame.mixer.music.load(r"F:\公众号\64.小仓鼠\Eran - 春の思い出.mp3") 
pygame.mixer.music.set_volume(0.5) 
pygame.mixer.music.play(1, 10)

This part of the code is separated from the overall code, you can choose to put the code at the beginning, or you can delete it directly. If you choose to play music, you need to fill in the local storage address of the computer where you want to play music in the code music.load function. Some friends have doubts about this piece, the filling format can refer to the following picture:

picture

  
  

3 Define the function of drawing the head of the hamster

  
Then set the size of the artboard and define the function to draw the head of the hamster.

t.title('阿黎逸阳的代码公众号')
t.speed(10)
#t.screensize(1000, 800)
t.setup(startx=0, starty = 0, width=800, height = 600)
print('画右耳朵')
#画右耳朵
t.penup()
t.goto(100, 150)
t.pendown()
t.pensize(0.5)
t.color('cornsilk')
#t.color('cornsilk', 'cornsilk')
t.begin_fill()
t.setheading(40)
t.circle(40, 20)
t.circle(7, 180)
t.right(22)
t.circle(50, 12)
print('画头')
#画头
t.setheading(173)
t.circle(200, 14)
print('画左耳朵')
#画左耳朵
t.setheading(120)
t.circle(40, 20)
t.circle(7, 180)
t.right(22)
t.circle(50, 12)
print('画左脸')
#画左脸
t.setheading(230)
t.circle(200, 10)
t.setheading(190)
t.circle(20, 100)
print('画脸上的波浪线')
#画脸上的波浪线
t.setheading(60)
t.circle(-30, 100)
t.setheading(60)
t.circle(-10, 130)
t.setheading(70)
t.circle(-10, 130)
t.setheading(40)
t.circle(-38, 100)
t.setheading(56)
t.circle(20, 130)
t.setheading(110)
t.circle(80, 22)
t.end_fill()

Detailed explanation of key codes:
  
t.pensize(width): Set the size of the brush.
  
t.color(color): Set the color of the brush.
  
t.penup(): Lift up the brush, which is generally used to draw in another place.
  
t.goto(x,y): The brush goes to a certain position, the parameter is (x,y), corresponding to the abscissa and ordinate.
  
t.pendown(): Put down the brush, generally used in combination with penup.
  
t.left(degree): how many degrees the brush turns to the left, and the degrees are indicated in brackets.
  
t.right(degree): how many degrees the brush turns to the right, and the degrees are indicated in brackets.
  
t.circle(radius,extent,steps): radius refers to the radius, if it is positive, the radius is far away from the radius on the left side of the little turtle, if it is negative, the radius is far away from the radius on the right side of the little turtle; extent refers to radians; steps Refers to the order.
  
The key to drawing the outer contour is to adjust the radian of the curve by adjusting the radius and radian in the circle function, so that the outline of the little hamster is smoother.

  
  

4 Define the function to draw the left and right eyes

  
Then define the function to draw the left eye and the right eye.

print('画左眼睛')
#画左眼睛
t.penup()
t.goto(30, 115)
t.pendown()
t.color('black')
t.begin_fill()
t.setheading(5)
t.circle(-20, 40)
t.setheading(60)
t.circle(6, 205)
t.end_fill()
print('画右眼睛')
#画右眼睛
t.penup()
t.goto(90, 116)
t.pendown()
t.color('black')
t.begin_fill()
t.setheading(180)
t.circle(20, 40)
t.setheading(120)
t.circle(-6, 205)
t.end_fill()

  
  

5 Define the function of drawing mouth

  
Then define the function of drawing the mouth.

#画嘴巴
t.color('black', 'yellow')
t.begin_fill()
t.setheading(-20)
t.circle(10, 60)
t.setheading(290)
t.circle(-30, 30)
t.circle(-9, 180)
t.left(20)
t.circle(-50, 12)
t.setheading(-22)
t.circle(10, 60)
t.end_fill()

So far, the realization of the drawing logic of the little hamster in Python has been roughly explained. Call your friends who like challenges to complete the missing parts together.
  
You may be interested in:
Draw Pikachu with Python Draw a
word cloud map
with Python Draw 520 eternal heartbeats with
Python With sound and text) Use the py2neo library in Python to operate neo4j and build a relationship map Python romantic confession source code collection (love, rose, photo wall, confession under the stars)



Guess you like

Origin blog.csdn.net/qq_32532663/article/details/127950008