Python Halloween Gift

Halloween gift

Article directory

  • Series of articles
  • Preface
  • Quick Start for Little Turtles
  • halloween bat
  • halloween pumpkin head
  • halloween gifts
  • end

Series of articles

Serial number Article directory Direct link
1 Romantic 520 Confession Code https://want595.blog.csdn.net/article/details/130666881
2 Full screen confession code https://want595.blog.csdn.net/article/details/ 129794518
3 Bumping love https://want595.blog.csdn.net/article/details/129503123
4 Floating love https://want595.blog.csdn.net/article/details/128808630
5 Love Light Wave https://want595.blog. csdn.net/article/details/132311588
6 Meteor Shower https://want595.blog.csdn.net/article/details/129395465
7 Gypsophila https://want595.blog.csdn.net/article/details/129572082
8 Fireworks Show https ://want595.blog.csdn.net/article/details/128746664
9 Christmas tree https://want595.blog.csdn.net/article/details/128213770
10 Snowflake Code https://want595.blog.csdn.net/article/ details/129038108
11 Simulated starry sky https://want595.blog.csdn.net/article/details/129948882
12 Birthday cake https://want595.blog.csdn.net/article/details/129694998
13 Sakura tree https://want595.blog .csdn.net/article/details/130350743
14 Colorful Balloons https://want595.blog.csdn.net/article/details/130950744
15 Colorful flowers https://want595.blog.csdn.net/article/details/130897838
16 Spoof code https://want595.blog.csdn.net/article/details/131274862
17 Code Rain https://want595.blog.csdn.net/article/details/132574687
18 Mid-Autumn Starry Sky https://want595.blog.csdn.net/ article/details/132910075
19 National Celebration https://want595.blog.csdn.net/article/details/133427031
20 Pikachu https://want595.blog.csdn.net/article/details/133783136
21 Rose https://want595. blog.csdn.net/article/details/133851128
22 Detective Conan https://want595.blog.csdn.net/article/details/133903847
23 Halloween Bat https://want595.blog.csdn.net/article/details/133935474
24 Pumpkin Head https://want595.blog.csdn.net/article/details/133973340
25 Halloween gift https://want595.blog.csdn.net/article/details/134011397

Foreword

Halloween is coming, come and get the gifts prepared by the blogger!

Quick Start for Little Turtle

Python’s Turtle Graphics is a simple and interesting drawing library suitable for beginners to learn programming. It allows you to use Python language to draw graphics and create a variety of interesting graphics through simple commands. In this article, I will provide you with an introductory introduction to turtle drawing in Python.

Baby turtle drawing can be traced back to the Logo language, which was developed by Seymour Papert in the 1960s. In Python, we can use the turtle library to achieve similar functions.

First, we need to import the turtle library in Python. This can be achieved using the following code:

import turtle

Now, we can create a turtle instance and name it my_turtle:

my_turtle = turtle.Turtle()

A turtle instance is a graphics window in which you draw graphics. It has a default orientation, towards the right side of the screen.

Let’s try some basic drawing operations. For example, to draw a square, the code is as follows:

my_turtle.forward(100)
my_turtle.right(90)
my_turtle.forward(100)
my_turtle.right(90)
my_turtle.forward(100)
my_turtle.right(90)
my_turtle.forward(100)

In this example, we use the forward() method to draw a straight line forward, and the right() method to turn a certain angle to the right. By repeating these two operations, we can draw a square.

Now, let’s try drawing a more complex shape, such as a five-pointed star. code show as below:

for i in range(5):
    my_turtle.forward(100)
    my_turtle.right(144)

In this example, we use a for loop to repeatedly perform a series of operations. The parameters of the forward() method and right() method can be adjusted according to your needs to achieve the desired graphics effect.

In addition to basic straight line and angle operations, the turtle library also provides other functions, such as changing the color, thickness, movement, filling and other operations of the brush. You can explore these features as needed.

Finally, when you are finished plotting, remember to use the following code to keep the graphics window open so you can view the results of your plot:

turtle.done()

This is a simple introduction to turtle drawing in Python. By mastering these basic concepts and operations, you can start creating all kinds of interesting graphics and artwork. I hope this introduction is helpful to you, and I wish you success in learning Python turtle drawing!

Halloween Bat

Let’s start with a little bat!

bat

Programming

import turtle as t
k=0.5

t.title("bat")
t.setup(0.5, 0.5)
t.begin_fill()
t.fillcolor('black')
t.forward(k*10)
t.setheading(75)
t.forward(k*35)
t.setheading(-75)
t.forward(k*55)
t.setheading(0)
t.circle(k*40,90)
t.right(90)
t.forward(k*100)
t.left(180)
t.circle(k*100,90)
t.setheading(180)
t.circle(k*70,90)
t.left(180)
t.circle(k*70,90)
t.right(90)
t.circle(k*100,90)
t.right(180)
t.forward(k*100)
t.right(90)
t.circle(k*40,90)
t.setheading(75)
t.forward(k*55)
t.setheading(-75)
t.forward(k*35)
t.setheading(0)
t.forward(k*10)
t.end_fill()
t.mainloop()

Program Analysis

This code uses the turtle library to draw a black bat on the canvas. The following is a detailed analysis of the code:

  1. Import the turtle library and give it the alias t.
import turtle as t
  1. Set the window title and window size. Here the window size is half the default canvas size.
t.title("bat")
t.setup(0.5, 0.5)
  1. Start filling with black color.
t.begin_fill()
t.fillcolor('black')
  1. First walk forward for a certain distance, then turn to 75 degrees and draw a line segment to the upper right with a length of 35k, then turn to -75 degrees and draw a line segment to the lower right with a length of 55k. Finally, turn to 0 degrees, take the current position as the center of the circle, and draw a 1/4 arc with a radius of 40k. These operations outline the outline of the bat’s head and body.
t.forward(k*10)
t.setheading(75)
t.forward(k*35)
t.setheading(-75)
t.forward(k*55)
t.setheading(0)
t.circle(k*40,90)
  1. Go 100k to the right, then turn 180 degrees, take the current position as the center of the circle, and draw a 1/4 arc with a radius of 100k, thus drawing the bat’s wings.
t.right(90)
t.forward(k*100)
t.left(180)
t.circle(k*100,90)
  1. Next, first turn 180 degrees, draw a 1/4 arc with a radius of 70k with the current position as the center, and then turn 180 degrees, draw a 1/4 circle with a radius of 70k with the current position as the center. Arc, so that the lower half of the bat’s body and the outline of the left wing are drawn.
t.setheading(180)
t.circle(k*70,90)
t.left(180)
t.circle(k*70,90)
  1. Then, turn 90 degrees to the right, take the current position as the center of the circle, and draw a 1/4 arc with a radius of 100k. This will draw the outline of the upper part of the bat’s body and the right wing.
t.right(90)
t.circle(k*100,90)
  1. Turn right 180 degrees, walk forward 100k, then turn right 90 degrees, take the current position as the center of the circle, and draw a 1/4 arc with a radius of 40k. This will draw the tail of the bat and the back of the right wing. Outline.
t.right(180)
t.forward(k*100)
t.right(90)
t.circle(k*40,90)
  1. Finally, starting from the current position, draw a line segment to the upper left and lower left with lengths of 35k and 55k respectively, then turn to 0 degrees and walk forward 10k, thus drawing the front of the left wing and body of the bat. Outline.
t.setheading(75)
t.forward(k*55)
t.setheading(-75)
t.forward(k*35)
t.setheading(0)
t.forward(k*10)
  1. End filling and enter the message loop to wait for events to occur.
t.end_fill()
t.mainloop()

Halloween Pumpkin Head

How can Halloween be complete without pumpkin heads~

Pumpkin Head

Programming

import turtle

def init():
    turtle.setup(1.0,1.0)
    turtle.speed(10)
    turtle.hideturtle()
    turtle.bgcolor('orange')

def outline():
    turtle.color('#CF5E1A', '#CF5E1A')
    turtle.penup()
    turtle.goto(250, 30)
    turtle.pendown()
    turtle.seth(90)
    turtle.begin_fill()
    for j in range(25):
        turtle.fd(j)
        turtle.left(3.6)
    for j in range(25, 0, -1):
        turtle.fd(j)
        turtle.left(3.6)
    turtle.seth(-90)
    turtle.circle(254, 180)
    turtle.end_fill()

def eyes(args):
    for items in args:
        position, angle, direction = items
        turtle.pensize(6)
        turtle.penup()
        turtle.goto(position, 0)
        turtle.pendown()
        turtle.color('#4C180D', '#4C180D')
        turtle.begin_fill()
        turtle.seth(angle)
        for j in range(55):
            turtle.fd(3)
            if direction:
                turtle.left(3) # Turn left 3 degrees
            else:
                turtle.right(3) # Turn right 3 degrees
        turtle.goto(position, 0)
        turtle.end_fill()
def nose():
    # nose
    turtle.penup()
    turtle.goto(0, 0)
    turtle.seth(180)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(50, steps=3)
    turtle.end_fill()


def mouth():
    # mouth
    turtle.color('#F9D503', '#F9D503')
    turtle.pensize(6)
    turtle.penup()
    turtle.penup()
    turtle.goto(-150, -100)
    turtle.pendown()
    turtle.begin_fill()
    turtle.seth(-30)
    turtle.fd(100)
    turtle.left(90)
    turtle.fd(30)
    turtle.right(90)
    turtle.fd(60)
    turtle.left(60)
    turtle.fd(60)
    turtle.right(90)
    turtle.fd(30)
    turtle.left(90)
    turtle.fd(100)
    turtle.end_fill()
    turtle.done()

def tail():
    turtle.penup()
    turtle.goto(0, 180)
    turtle.pendown()
    turtle.color('#2E3C01')
    turtle.seth(100)
    turtle.pensize(25)
    turtle.circle(60, 100)

turtle.tracer(0)
init()
outline()
tail()
eyes_items = [(-60, 230, 0), (60, -50, 1)]
eyes(eyes_items)
nose()
mouth()
turtle.done()

Program Analysis

This code uses the turtle library to draw a pumpkin head on the canvas. The following is a detailed analysis of the code:

  1. Import the turtle library.
import turtle
  1. Define an init function to initialize the canvas. The window size is set to 1.0 x 1.0, the background color is orange, and the brush speed is 10.
def init():
    turtle.setup(1.0,1.0)
    turtle.speed(10)
    turtle.hideturtle()
    turtle.bgcolor('orange')
  1. Define an outline function to draw the outline of the pumpkin head. First move the brush to the position of (250, 30), and then use this point as the starting point to alternately draw line segments and arcs to the left and right sides to form the outline of the pumpkin head. Finally, use the arc as the bottom and fill it with orange to complete the outline of the pumpkin head.
def outline():
    turtle.color('#CF5E1A', '#CF5E1A')
    turtle.penup()
    turtle.goto(250, 30)
    turtle.pendown()
    turtle.seth(90)
    turtle.begin_fill()
    for j in range(25):
        turtle.fd(j)
        turtle.left(3.6)
    for j in range(25, 0, -1):
        turtle.fd(j)
        turtle.left(3.6)
    turtle.seth(-90)
    turtle.circle(254, 180)
    turtle.end_fill()
  1. Define an eyes function to draw the eyes of the pumpkin head. The function accepts a list parameter args, where each element represents the position, angle and direction of an eye. In the function, according to the parameter information, move the brush to the eye position, then draw the outline of the eye at a certain angle and direction, and finally fill it with black to complete the eye drawing.
def eyes(args):
    for items in args:
        position, angle, direction = items
        turtle.pensize(6)
        turtle.penup()
        turtle.goto(position, 0)
        turtle.pendown()
        turtle.color('#4C180D', '#4C180D')
        turtle.begin_fill()
        turtle.seth(angle)
        for j in range(55):
            turtle.fd(3)
            if direction:
                turtle.left(3) # Turn left 3 degrees
            else:
                turtle.right(3) # Turn right 3 degrees
        turtle.goto(position, 0)
        turtle.end_fill()
  1. Define a nose function to draw the nose of the pumpkin head. First move the brush to the center of the canvas, then draw an equilateral triangle with a radius of 50 with the center point as the center of the circle, and finally fill it with black to complete the nose drawing.
def nose():
    turtle.penup()
    turtle.goto(0, 0)
    turtle.seth(180)
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(50, steps=3)
    turtle.end_fill()
  1. Define a mouth function to draw the mouth of the pumpkin head. First move the brush to the position of (-150, -100), then draw the outline of the mouth at a certain angle and direction, and finally fill it with yellow to complete the drawing of the mouth.
def mouth():
    turtle.color('#F9D503', '#F9D503')
    turtle.pensize(6)
    turtle.penup()
    turtle.penup()
    turtle.goto(-150, -100)
    turtle.pendown()
    turtle.begin_fill()
    turtle.seth(-30)
    turtle.fd(100)
    turtle.left(90)
    turtle.fd(30)
    turtle.right(90)
    turtle.fd(60)
    turtle.left(60)
    turtle.fd(60)
    turtle.right(90)
    turtle.fd(30)
    turtle.left(90)
    turtle.fd(100)
    turtle.end_fill()
    turtle.done()
  1. Define a tail function to draw the tail of the pumpkin head. First move the brush to the position of (0, 180), then use this point as the starting point, draw the outline of the tail according to a certain radius and angle, and finally complete the drawing of the tail.
def tail():
    turtle.penup()
    turtle.goto(0, 180)
    turtle.pendown()
    turtle.color('#2E3C01')
    turtle.seth(100)
    turtle.pensize(25)
    turtle.circle(60, 100)
  1. Finally, call the turtle’s tracer function to turn off the animation effect, and then call the above functions in sequence to complete the drawing of the pumpkin head.
turtle.tracer(0)
init()
outline()
tail()
eyes_items = [(-60, 230, 0), (60, -50, 1)]
eyes(eyes_items)
nose()
mouth()
turtle.done()

Halloween gifts

Let’s combine bats and pumpkin heads!

Halloween gift
Programming

import turtle as tu
import random as ra
import math

tu.setup(1.0, 1.0)
tu.screensize(1.0, 1.0)
tu.title("Halloween")
tu.bgcolor('darkorange')
t = tu.Pen()
t.ht()
colors = ['black']
class Bat(): #Each bat (bat class)
    def __init__(self):
...(For the complete code, follow the public account at the end of the article to get it for free)

Program Analysis

This is a piece of python code that uses the turtle module to create a Halloween scene, including pumpkins and bats. Among them, the shape and color of the pumpkin are very realistic, and the bat achieves the dynamic effect of flying.

A Bat class is defined in the code to create a bat object, including the size, position, color, movement speed and other attributes of the bat. The move() method realizes the effect of random movement of the bat. When the bat flies out of the canvas, a bat object is regenerated. The pumpkin() method uses turtle to draw the shape and characteristics of the pumpkin, including the roundness of the pumpkin and the twist of the mouth.

Finally, 100 bat objects were instantiated in the main program, and a while loop was used to keep them moving in the canvas while updating the picture to show dynamic effects.

The entire code uses some basic syntax of python and functions of the turtle module, such as setup(), screensize(), Pen(), begin_fill(), end_fill(), etc. Through this small example, we can understand the drawing function of turtle and the basic use of python classes.

Epilogue

Thank you for your support, friends, and I wish you all a happy Halloween!