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 handsome Pikachu with python

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you how to use python to draw a handsome Pikachu. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

In order to access the Python library, you need to import it into your Python environment, and import it into your Python script using the following command.

Import turtle

First, let's create an instance of Screen.

Wn = turtle.Screen ()

Now, create an instance of Turtle.

Self.t = turtle.Turtle ()

Let's set the speed to 3 and use the speed method, which means that Pikachu will not just appear on the screen, but the drawing will also have some animation. If you want to change the background color, you can use this bgcolor method, which defaults to white. You can use the pensize method to adjust the thickness of the pen, which is slightly thicker. Ondrag dragging the Turtle will move it across the screen, resulting in a hand-drawn (if the pen has been put down).

T.pensize (3) t.speed (9) t.ondrag (getPosition)

Now let's take a look at some of the common methods used in this program:

Turtle (): used to create and return a new turtle object.

Forward (value): the turtle moves forward relative to the specified value.

Backward (value): the turtle moves backward relative to the specified value.

Right (angle): the turtle rotates clockwise.

Left (angle): the turtle rotates counterclockwise.

Penup (): pick up the tortoise pen.

Pendown (): put the tortoise pen down.

Up (): same as penup ().

Down (): same as pendown ().

Color (color name): the color of the Turtle pen changes.

Fillcolor (color name): the color used to fill a specific shape has changed.

Title (): returns the current title.

Position (): returns the current position.

Goto (x, y): move the position of the turtle to coordinates x, y.

End_fill (): fills the current fill color when polygons are turned off.

Begin_fill (): remember the starting point of the fill polygon.

Dot (): the point remains in its current position.

Stamp (): the impression of the shape of the turtle remains in its current position.

Shape (): should be-'turtle',' classic', 'arrow' or' circle'.

The above code:

Import turtledef getPosition (x, y): turtle.setx (x) turtle.sety (y) print (x, y) class Pikachu: def _ init__ (self): self.t = turtle.Turtle () t = self.t t.pensize (3) t.speed (9) t.ondrag (getPosition) def noTrace_goto (self, x Y): self.t.penup () self.t.goto (x, y) self.t.pendown () def leftEye (self, x, y): self.noTrace_goto (x Y) t = self.t t.seth (0) t.fillcolor ('# 333333') t.begin_fill () t.circle (22) t.end_fill () self.noTrace_goto (x Y + 10) t.fillcolor ('# 000000') t.begin_fill () t.circle (10) t.end_fill () self.noTrace_goto (x + 6, y + 22) t.fillcolor ('# ffffff') t.begin_fill () t.circle (10) t.end_fill () def rightEye (self, x Y): self.noTrace_goto (x, y) t = self.t t.seth (0) t.fillcolor ('# 333333') t.begin_fill () t.circle (22) t.end_fill () self.noTrace_goto (x Y + 10) t.fillcolor ('# 000000') t.begin_fill () t.circle (10) t.end_fill () self.noTrace_goto (x-6, y + 22) t.fillcolor ('# ffffff') t.begin_fill () t.circle (10) t.end_fill () def mouth (self, x Y): self.noTrace_goto (x Y) t = self.t t.fillcolor ('# 88141D') t.begin_fill () # Lower Lip L1 = [] L2 = [] t.seth (190a) a = 0.7for i in range (28): a + = 0.1t.right (3) t.fd (a) L1.append (t.position ()) self.noTrace_goto Y) t.seth (10) a = 0.7 for i in range (28): a + = 0.1 t.left (3) t.fd (a) l2.append (t.position ()) # Upper Lip t.seth (10) t.circle (50,15) t.left (180) t.circle (- 50) 15) t.circle (- 50,40) t.seth (233) t.circle (- 50,55) t.left (180) t.circle (50,12.1) t.end_fill () # Tongue self.noTrace_goto (17 54) t.fillcolor ('# DD716F') t.begin_fill () t.seth (145) t.circle (40,86) t.penup () for pos in reversed (L1 [: 20]): t.goto (pos [0], pos [1] + 1.5) for pos in L2 [: 20]: t.goto (pos [0]) Pos [1] + 1.5) t.pendown () t.end_fill () # Nose self.noTrace_goto (- 17,94) t.seth (8) t.fd (4) t.back (8) # Red Cheeks def leftCheek (self, x, y): turtle.tracer (False) t = self.t self.noTrace_goto (x Y) t.seth (300) t.fillcolor ('# DD4D28') t.begin_fill () a = 2.3 for i in range (120): if 0

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: 0

*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

Wechat

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

12
Report