Python Pikachu

Time:2024-1-23

preamble

Implementation of three loving Pokémon in python!

baby turtle

turtle in Python is an easy-to-use drawing library that allows us to programmatically draw a variety of shapes. Its internal implementation of a turtle (turtle), we can control the direction of movement of the turtle and length and other parameters to draw, very suitable for beginners to start using. In this article, we will introduce the basic drawing functions and examples of turtle to help beginners get started quickly.

1.Install turtle

Turtle is a standard library that comes with Python, so we don’t need to install anything, just import the turtle library in a Python terminal or editor to get started.
import turtle

2. turtle’s basic drawing functions

There are many basic plotting functions in turtle, here are some common ones:
  • turtle.forward(distance): move the turtle the specified distance in the current direction.
  • turtle.backwards(distance): move the turtle the specified distance in the opposite direction.
  • turtle.right(angle): rotate the current direction to the right by the specified angle.
  • turtle.left(angle): rotate the current direction to the left by the specified angle.
  • turtle.penup(): lifts the brush up and no longer draws the turtle’s track.
  • turtle.pendown(): puts the brush down and continues to draw the turtle’s trajectory.
  • turtle.pensize(width): set the width of the brush to the specified size.
  • turtle.pencolor(color): set the color of the brush to the specified color.
  • turtle.fillcolor(color): set the fill color to the specified color.
  • turtle.begin_fill(): start filling.
  • turtle.end_fill(): end fill.
  • turtle.circle(radius): draw a circle with specified radius.

3. Examples of turtle

Here are some turtle examples to help us better understand the basic plotting functions above. 3.1 Simple mapping Let’s start by drawing a simple graphic that has the turtle move forward 100 pixels, then rotate 90 degrees to the left, then move forward another 100 pixels, and finally rotate 90 degrees to the left to form a square graphic.
import turtle

turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)

turtle.done() # Indicates the end of the drawing
3.2 Drawing colored spirals Let’s draw a colorful spiral figure, let the turtle keep moving forward and rotate, each time the rotation angle and color is different, the final formation of a beautiful spiral figure.
import turtle

turtle.speed(10)
for i in range(200):
    turtle.forward(i)
    turtle.right(98)
    turtle.pencolor('red')
    turtle.pencolor('orange')
    turtle.pencolor('yellow')
    turtle.pencolor('green')
    turtle.pencolor('cyan')
    turtle.pencolor('blue')
    turtle.pencolor('purple')

turtle.done()
3.3 Drawing Pentagrams Let’s draw a pentagram by having the turtle move forward 100 pixels, rotate it 72 degrees to the left, and repeat 5 times to create a pentagram.
import turtle

turtle.pensize(5)
turtle.pencolor('purple')
turtle.fillcolor('yellow')
turtle.begin_fill()

for i in range(5):
    turtle.forward(100)
    turtle.right(72)
    turtle.forward(100)
    turtle.left(144)

turtle.end_fill()
turtle.done()
3.4 Drawing Circles and Squares Let’s draw a circle and a square, starting with a circle and then a square with the center of the circle as the starting point.
import turtle

turtle.circle(50) # draw a circle

turtle.penup() # lift the brush up
turtle.goto(0, -50) # move the turtle below the center of the circle
turtle.pendown() # put the brush down

for i in range(4):
    turtle.forward(100)
    turtle.left(90)

turtle.done()
Through the above examples, we can see that the turtle library is very suitable for beginners to start using, its easy-to-use interface and real-time drawing effect, can let us quickly understand and master the basic ideas and methods of Python programming.

Pikachu

mini version

Python Pikachu

computer programming
import turtle as t
import time
# Basic settings
t.setup(500, 500) # set the canvas size
t.bgcolor("pink")
t.title("pikachu")
t.pensize(1) # set the size of the brush
# Draw left-skewed curve functions
def left(ang, dis, step, n):
    for i in range(n):
        dis += step # Increase dis by step
        t.lt(ang) # turn ang degrees to the left
        t.fd(dis) # step forward dis
def right(ang, dis, step, n):
    for i in range(n):
        dis += step
        t.rt(ang) # turn ang degrees to the left
        t.fd(dis) # step forward dis
# Drawing ears
def Ears():
    t.color("black", "yellow")
    # Left ear curve
    t.pu() # lift the pen
    t.goto(-50, 100) # Initial position of the pen tip
    t.pd() # write
    t.setheading(110) # brush angle
    t.begin_fill()
    left(1.2, 0.4, 0.1, 40)
    t.setheading(270) # brush angle
    left(1.2, 0.4, 0.1, 40)
    t.setheading(44) # brush angle
    t.forward(32)
    t.end_fill()
    # Right ear curve
    t.pu() # lift the pen
    t.goto(50, 100) # Initial position of the pen tip
    t.pd() # write
    t.setheading(70) # brush angle
    t.begin_fill()
    right(1.2, 0.4, 0.1, 40)
    t.setheading(270) # brush angle
    right(1.2, 0.4, 0.1, 40)
    t.setheading(136) # brush angle
    t.forward(32)
    t.end_fill()
    # Black ears
    t.begin_fill()
    t.fillcolor("black")
    t.pu() # lift the pen
    t.goto(88, 141) # Initial position of the pen tip
    t.pd() # write
    t.setheading(35) # brush angle
    right(1.2, 1.6, 0.1, 16)
    t.setheading(270) # brush angle
    right(1.2, 0.4, 0.1, 25)
    t.setheading(132) # brush angle
    t.forward(31)
    t.end_fill()
    t.begin_fill()
    t.fillcolor("black")
    t.pu() # lift the pen
    t.goto(-88, 141) # Initial position of the pen tip
    t.pd() # write
    t.setheading(145) # brush angle
    left(1.2, 1.6, 0.1, 16)
    t.setheading(270) # brush angle
    left(1.2, 0.4, 0.1, 25)
    t.setheading(48) # brush angle
    t.forward(31)
    t.end_fill()
# Gao Bae
def Tail():
    # Oba
    t.begin_fill()
    t.fillcolor("yellow")
    t.pu() # lift the pen
    t.goto(64, -140) # Initial position of the pen tip
    t.pd() # write
    t.setheading(10) # brush angle
    t.forward(20)
    t.setheading(90) # brush angle
    t.forward(20)
    t.setheading(10) # brush angle
    t.forward(10)
    t.setheading(80) # brush angle
    t.forward(100)
    t.setheading(35) # brush angle
    t.forward(80)
    t.setheading(260) # brush angle
    t.forward(100)
    t.setheading(205) # brush angle
    t.forward(40)
    t.setheading(260) # brush angle
    t.forward(37)
    t.setheading(205) # brush angle
    t.forward(20)
    t.setheading(260) # brush angle
    t.forward(25)
    t.setheading(175) # brush angle
    t.forward(30)
    t.setheading(100) # brush angle
    t.forward(13)
    t.end_fill()
# of feet on the screen
def Foots():
    # Feet
    t.begin_fill()
    t.fillcolor("yellow")
    t.pensize(2)
    t.pu() # lift the pen
    t.goto(-70, -200) # Initial position of the pen tip
    t.pd() # write
    t.setheading(225) # brush angle
    left(0.5, 1.2, 0, 12)
    left(35, 0.6, 0, 4)
    left(1, 1.2, 0, 18)
    t.setheading(160) # brush angle
    t.forward(13)
    t.end_fill()
    t.begin_fill()
    t.fillcolor("yellow")
    t.pensize(2)
    t.pu() # lift the pen
    t.goto(70, -200) # Initial position of the pen tip
    t.pd() # write
    t.setheading(315) # brush angle
    right(0.5, 1.2, 0, 12)
    right(35, 0.6, 0, 4)
    right(1, 1.2, 0, 18)
    t.setheading(20) # brush angle
    t.forward(13)
    t.end_fill()
# Picture Body
def Body():
    # Outline
    t.begin_fill()
    t.pu() # lift the pen
    t.goto(112, 0) # initial position of the pen tip
    t.pd() # write
    t.setheading(90) # brush angle
    t.circle(112, 180)
    t.setheading(250) # brush angle
    left(1.6, 1.3, 0, 50)
    left(0.8, 1.5, 0, 25)
    t.setheading(255) # brush angle
    left(0.4, 1.6, 0.2, 27)
    left(2.8, 1, 0, 45)
    right(0.9, 1.4, 0, 31)
    t.setheading(355) # brush angle
    right(0.9, 1.4, 0, 31)
    left(2.8, 1, 0, 45)
    left(0.4, 7.2, -0.2, 27)
    t.setheading(10) # brush angle
    left(0.8, 1.5, 0, 25)
    left(1.6, 1.3, 0, 50)
    t.end_fill()
def Eyes_Open():
    # Left eye
    t.begin_fill()
    t.fillcolor("black")
    t.pu() # lift the pen
    t.goto(-46, 10) # Initial position of the pen tip
    t.pd() # write
    t.setheading(90) # brush angle
    t.circle(5, 360)
    t.end_fill()
    # Right eye
    t.begin_fill()
    t.fillcolor("black")
    t.pu() # lift the pen
    t.goto(46, 10) # Initial position of the pen tip
    t.pd() # write
    t.setheading(-90) # brush angle
    t.circle(5, 360)
    t.end_fill()
# Picture
def Face():
    # Face
    t.begin_fill()
    t.fillcolor("red")
    t.pu() # lift the pen
    t.goto(-63, -10) # Initial position of the pen tip
    t.pd() # write
    t.setheading(90) # brush angle
    t.circle(10, 360)
    t.end_fill()
    t.begin_fill()
    t.fillcolor("red")
    t.pu() # lift the pen
    t.goto(63, -10) # Initial position of the pen tip
    t.pd() # write
    t.setheading(-90) # brush angle
    t.circle(10, 360)
    t.end_fill()
    # Mouth
    t.pensize(2.2)
    t.pu() # lift the pen
    t.goto(0, 0) # initial position of the pen tip
    t.pd() # write
    t.setheading(235) # brush angle
    right(5, 0.8, 0, 30)
    t.pu() # lift the pen
    t.goto(0, 0) # initial position of the pen tip
    t.pd() # write
    t.setheading(305) # brush angle
    left(5, 0.8, 0, 30)
# Painter
def Hands():
    # Left hand
    t.pensize(2)
    t.pu() # lift the pen
    t.goto(-46, -100) # Initial position of the pen tip
    t.pd() # write
    t.setheading(285) # brush angle
    right(0.4, 1.2, 0, 26)
    right(5, 0.35, 0, 26)
    right(0.3, 1.2, 0, 15)
    # Right hand
    t.pu() # lift the pen
    t.goto(46, -100) # Initial position of the pen tip
    t.pd() # write
    t.setheading(255) # brush angle
    left(0.4, 1.2, 0, 26)
    left(5, 0.35, 0, 26)
    left(0.3, 1.2, 0, 15)
def Eyes_Close():
    # Left eye
    t.pu() # lift the pen
    t.goto(-46, 12) # Initial position of the pen tip
    t.pd() # write
    t.setheading(180) # brush angle
    t.forward(10)
    # Right eye
    t.pu() # lift the pen
    t.goto(46, 12) # Initial position of the pen tip
    t.pd() # write
    t.setheading(0) # brush angle
    t.forward(10)

# Love
def Love(): # Draw love function, that is, draw love with turtle
    t.pensize(1)
    t.penup()
    t.color("pink")
    t.goto(-2.5, -100)
    t.pendown()
    t.begin_fill()
    t.fillcolor('pink')
    t.setheading(120)
    t.circle(20, 195)
    t.fd(20 * 2.4)
    t.lt(90)
    t.fd(20 * 2.4)
    t.circle(20, 195)
    t.end_fill()

# Initialization
def Init():
    Ears()
    Tail()
    Foots()
    Body()
    Face()
    Hands()
    Eyes_Open()
    Love()
# Blinking
def Close():
    Ears()
    Tail()
    Foots()
    Body()
    Face()
    Hands()
    Eyes_Close()
def Open():
    Ears()
    Tail()
    Foots()
    Body()
    Face()
    Hands()
    Eyes_Open()
    Love()


def main():
    # Initialization皮卡丘
    Init()
    # Blinking动画
    for i in range(30):
        if i % 2 == 0:
            t.reset()
            t.hideturtle()
            Close()
            t.update()
            time.sleep(0.3)
        else:
            t.reset()
            t.hideturtle()
            Open()
            t.update()
            time.sleep(1)
t.tracer(0)
main()
t.done()
program analysis
This is a program to draw Pikachu using Python’s turtle library. Here is an analysis of the program: 1. Basic set-up First, the program sets the basic settings such as the size of the canvas, the background color and the title of the window. 2. Drawing left-deviation curve functions and right-deviation curve functions The program defines two functions: left() and right() for drawing left-skewed and right-skewed curves. These functions take four arguments: the rotation angle, the advance distance, the increased advance distance, and the number of segments of the curve. These two functions use the functions provided by the turtle library to realize the drawing of curves. 3. Drawing ears, tails, feet and bodies The program defines four functions, Ears(), Tail(), Foots(), and Body(), and uses these functions to draw the basic outline of a Pikachu. These functions use the previously defined left() and right() functions for curve drawing. 4. Drawing eyes and mouths The program defines a function Face() and draws Pikachu’s facial features in it, including eyes and mouth. 5. painter The program defines a function Hands() and uses the left() and right() functions as well as the basic manipulation functions such as forward and rotate to implement the drawing of hands. In this case, the left and right hands are drawn symmetrically. 6. Blinking animation The program defines the Open() and Close() functions to implement the eye opening and closing animation effect. By constantly calling these two functions, the program implements an animation effect of Pikachu blinking its eyes. 7. Love The program defines a function Love() that draws a love heart using the functions provided by the turtle library and the basic operations of advancing, rotating, and drawing fills. 8. Initialization and main functions The program defines an Init() function that is used to call the previously defined functions to complete the drawing of Pikachu. The program also defines a main() function in which the Init() function is used to initialize Pikachu and to implement the blinking animation effect. Overall, the program uses the functions provided by the turtle library and the base operation functions to draw various parts of Pikachu, and implements a cute Pikachu drawing program by calling the functions and controlling the order and parameters of the base operations.

premium version

Python Pikachu

computer programming
from turtle import *
import turtle as t

def infoPrt():
    print('coordinate: ' + str(t.pos()))
    print('angle: ' + str(t.heading()))

t.tracer(0)
t.pensize(3)
t.hideturtle()
t.colormode(255)
t.color("black")
t.setup(700, 650)
t.speed(1)
t.st()
#t.dot()
t.pu()
#t.goto(-150,100)
t.goto(-210,86)
t.pd()
infoPrt()
...... (complete code at the end of the public free access Oh)
program analysis
This is a program to draw Pikachu using the turtle library. Here is an analysis of the program: 1. Basic set-up The program uses the functions provided by the turtle library and the base manipulation functions for plotting. Before starting to draw, the program makes some basic settings. For example, it sets the canvas size, the thickness of the brush and hides the brush. 2. Mapping The program draws the parts of Pikachu, including the head, body, limbs, and face, using the functions provided by the turtle library and the basic manipulation functions. During the drawing process, the program uses the functions provided by the turtle library such as draw circle, draw line, and fill color. 3. Drawing text The program draws a “Pika!” text and places it on top of the canvas using the functions provided by the turtle library and the base manipulation functions. The program uses a Turtle instance to do the drawing. 4. Drawing love The program draws a pink love heart by using the functions provided by the turtle library and the basic manipulation functions. During the drawing process, the program uses the functions provided by the turtle library such as draw circle, draw line, and fill color. 5. Summary In summary, this is a program to draw a Pikachu using the turtle library. The program uses the functions provided by the turtle library and the basic manipulation functions to implement various drawing functions and draw a cute Pikachu. The program also uses Turtle examples to draw text and places a pink heart on top of the canvas. The program demonstrates the power of Python and the turtle library and also shows the user the cute image of Pikachu.

Deluxe Edition

Python Pikachu

computer programming
import turtle

turtle.setup(999, 777)
turtle.title('Pikachu')
pikachu = Pikachu()
pikachu.start()
turtle.penup()
turtle.goto(-444,200)
turtle.bgcolor("blue")
turtle.color("gold")
turtle.write("Pika !", move = True, align="left", font=("Comic Sans MS", 66, "bold"))
turtle.pensize(1)
turtle.penup()
turtle.color("skyblue")
turtle.goto(-2.5, -100)
turtle.pendown()
turtle.begin_fill()
turtle.fillcolor('skyblue')
turtle.setheading(120)
turtle.circle(30, 195)
turtle.fd(30 * 2.4)
turtle.lt(90)
turtle.fd(30 * 2.4)
turtle.circle(30, 195)
turtle.end_fill()
turtle.pendown()
turtle.mainloop()
...... (complete code at the end of the public free access Oh)
program analysis
This is a program to draw Pikachu using the turtle library: 1. Program infrastructure The program makes some basic settings for the canvas before it starts drawing, including setting the canvas size, setting the brush thickness, speed, hiding the turtle drawing track and dragging events. 2. Mapping the components The program draws various parts of Pikachu including left and right eyes, mouth, red cheeks, left and right ears, etc. by defining different methods using the turtle library. During the drawing process, the program uses the functions provided by the turtle library such as draw circle, draw straight line, fill color, and so on. 3. Drawing body parts The program draws the body parts of Pikachu using the turtle library by defining a body method. During the drawing process, the program used the functions provided by the turtle library such as draw arcs, draw curves, draw straight lines, fill colors, etc., to complete the detail drawing of the body. 4. Drawing of hats The method used to draw the hat is cap, which uses the turtle library to draw the details of the hat, including the shape of the hat, the red part, and the brim. During the drawing process, the program uses the functions provided by the turtle library to draw circles, draw lines, and fill colors. 5. Mapping text and patterns The program draws a “Pika!” text and draws a small pattern using the functions provided by the turtle library and the base manipulation functions. After completing the drawing, the program also uses a Turtle instance to draw the text and place it on top of the canvas. 6. Summary In summary, this is a program to draw a Pikachu using the turtle library. The program uses the functions provided by the turtle library and the basic manipulation functions to implement various drawing functions and draw a cute Pikachu. The program also uses Turtle examples to draw text and places a cute little pattern on top of the canvas. The program demonstrates the power of Python and the turtle library and shows the user a cute image of Pikachu.

tail and voice

Thanks for the support ac!

Recommended Today

DML statements in SQL

preamble Previously we have explained DDL statements in SQL statements. Today we will continue with the DML statement of SQL. DML is the Data Manipulation Language.Used to add, delete, and change data operations on the tables in the library.。 1. Add data to the specified field(INSERT) 2. Modify data(UPDATE) 3. Delete data(DELETE) catalogs preamble I. […]