Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw a lovely Mickey Mouse with Python

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to draw a lovely Mickey Mouse with Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, the effect display second, the code detailed explanation

The principle of drawing Mickey Mouse with python is that the turtle library is used to draw the outline of the head first, and then draw different modules such as ears, hands, clothes, pants, feet, shoes and so on.

1. Import library

First of all, import the libraries that need to be loaded in this article. If you have some libraries that have not been installed, which leads to errors in running code, you can install them in Anaconda Prompt using the pip method.

Import osimport pygameimport turtle as t

In this paper, only three libraries, os, pygame and turtle, are applied. The os library can set the location where the file is read. The pygame library is designed to make the drawing process more interesting by adding background music to the drawing process. The turtle library is a drawing library, which is equivalent to giving you a paintbrush, and you can complete the drawing on the canvas with code controlled by mathematical logic.

two。 Play music

Then use the pygame library to play background music, the music of this article is about "Please give more advice in the rest of my life" song.

# play music print ('play music') pygame.mixer.init () pygame.mixer.music.load (r "F:\ official account\ 49. Please give more advice to Yang Zi for the rest of your life, and (Live) .mp3 for the rest of your life) pygame.mixer.music.set_volume (0.5) pygame.mixer.music.play (1,10)

This part of the code and the overall code are stripped, 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 address where you want to play music in the code music.load function.  

3. Draw the outline of Mickey Mouse's head

Then enter the formal drawing process of Mickey Mouse, starting with the outline of the head.

T.title ('code official account of Ali Yiyang') t.speed (10) # t.screensize (1000) t.setup (startx=0, starty = 0, width=800, height = 600) # # external outline # head print ('picture head') t.penup () t.goto (20,100) t.begin_fill () t.left (90) t.pendown () t.color ('black') t.pensize (2) t.circle (60,190) t.left (150) t.circle T.left (170,170) t.circle (- 35,100) t.circle (- 15,100) t.left (140,140) t.circle (- 15,100) t.circle (- 35,95) t.left (160,160) t.circle (- 20,72) t.end_fill () t.left (20) t.circle (- 10,80) t.begin_fill () t.circle (- 60) 55) t.left (60) t.forward (20) t.left (130) t.forward (130) t.left (120) t.circle (- 60,30) t.left (95) t.forward (65) t.end_fill () t.penup () t.goto () t.pendown () t.left (30) t.circle (20,60) t.right (15) t.circle (60) 30) t.begin_fill () # chin print ('draw chin') # t.right (30) t.circle (60,20) t.right (30) t.circle (33,110)

Key codes are explained in detail:

T.pensize (width): sets the size of the brush.

T.color (color): sets the color of the brush.

T.penup (): lift the brush, usually used for drawing from another place.

T.goto (XMagol y): the brush goes to a certain position, and the parameter is (xPermine y), corresponding to the Abscissa and Abscissa to which it goes.

T.pendown (): put down the brush, usually used in combination with penup.

T.left (degree): how many degrees does the brush turn to the left? degrees are indicated in parentheses.

T.right (degree): how many degrees does the brush turn to the right? degrees are indicated in parentheses.

T.circle (radius,extent,steps): radius refers to the radius, if positive, the radius is far from radius on the left side of the tortoise, if negative, the radius is far away from radius on the right side of the tortoise; extent refers to radians; steps refers to the number of degrees.

The key to the out-of-picture outline is to adjust the Radian of the curve by adjusting the radius and Radian in the circle function, so as to make the outline of Mickey Mouse smooth.

4. Draw clothes and ears

After drawing the outline of the head, you can draw other parts in modules. This section draws clothes and ears.

# upper body t.backward (5) t.right (150) t.forward (18) # t.left (10) t.circle (- 100,25) # clothes under arc print ('draw under clothes') t.right (50) t.circle (- 75,63) t.left (60) t.circle (100,30) t.right (80) t.circle (- 30,70) t.circle (- 20) 55) t.forward (70) t.end_fill () t.penup () t.goto (- 100,10) t.pendown () t.pensize (1.2t.left) # t.pencolor ('red') t.pencolor (' white') t.circle (- 30,30) # Line # 1t.penup () t.goto (- 81) at the armpit -3) t.pendown () t.pensize (1.3) t.setheading (30) # t.pencolor ('red') t.pencolor (' white') t.forward (13) # 2t.penup () t.goto (- 81,-3) t.pendown () t.pensize (1.3) t.setheading (- 18) # t.pencolor ('red') t.pencolor (' white') t.circle (20) 32) # # draw the ear # draw the right ear print ('draw the right ear') t.penup () t.goto (8140) t.pendown () t.begin_fill () t.setheading (- 10) t.color ('black') t.circle (30,160) t.circle (60,20) t.circle (30,160) t.end_fill () # draw the left ear print (' draw the left ear) t.penup () t.goto (- 90 ) t.pendown () t.begin_fill () t.setheading (40) t.color ('black') t.circle (30,160) t.circle (60,20) t.circle (30,160) t.circle (60,20) t.end_fill () 5. Draw eyes, nose, mouth

This section introduces the code for drawing eyes, nose and mouth. in order to look better, you need to pay attention to the symmetry of the eyes.

# draw print ('draw eyes') # Line below the eyes t.penup () t.goto (- 48,105) t.pendown () t.pensize (1.5) t.right (17) t.circle (- 40,42) # left eye t.penup () t.goto (- 42,106) t.pendown () t.left (160) t.circle (- 30,50) t.circle (- 7180) t.left (30) t.circle (- 30) 44) # left eye t.penup () t.goto (- 42,106) t.pendown () t.begin_fill () t.right (30,20) t.circle (- 4180) # t.left (25) t.circle (- 15,51) t.end_fill () # right eye t.penup () t.goto (- 29,107) t.pendown () t.right (160) t.circle (- 50,28) t.circle (- 7 T.left (17) t.circle (- 30,46) # right eyeball t.penup () t.goto (- 29,107) t.pendown () t.begin_fill () t.right (140,140) t.circle (30,20) t.circle (- 4180) # t.left (25) t.circle (- 15,51) t.end_fill () # nose drawing print ('draw nose') t.penup () t.goto (- 42) T.pendown () t.begin_fill () t.setheading (15) t.circle (- 40,22) t.circle (- 7180) t.circle (40,20) t.right (43) t.circle (- 7180) t.end_fill () # Arc t.penup () t.goto (- 80,85) t.pendown () t.pensize (1.7) t.setheading (- 45) t.circle (60 90) # mouth t.begin_fill () t.penup () t.goto (- 67,73) t.pendown () t.setheading (- 70) t.circle (60,30) t.circle (20,100) t.right (10) t.circle (60,25) t.setheading (210) t.circle (- 60,55) t.end_fill () # draw tongue print ('draw tongue') t.penup () t.goto (- 60) 57) t.pendown () t.begin_fill () t.setheading (40) t.color ('black','pink') t.circle (- 18,90) t.setheading (61) t.circle (- 16,90) t.setheading (- 122,20) t.setheading (- 60,20) t.circle (- 50,20) t.setheading (150) t.circle (- 60,20) t.end_fill () # draw smiley face Radian # t.penup () t.goto (- 86) 77) t.pendown () t.pensize (1.7) t.setheading (70) t.circle (- 18,60) # right Radian t.penup () t.goto (- 5,86) t.pendown () t.pensize (1.7) # t.setheading (10) t.circle (- 18,60) print ('chin painting') # chin painting t.penup () t.goto (- 58,40) t.pendown () t.setheading (140) t.circle (- 60) 10) # right squeak t.penup () t.goto (- 2,40) t.pendown () t.pencolor ('white') t.pensize (1.2) t.setheading (- 90) t.forward (11) "how to draw a cute Mickey Mouse with Python" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 241

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

  • How to realize paging in PHP+ajax

    This article is to share with you about how PHP+ajax implements paging. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look. Details are as follows: HTML code is as follows: title

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report