In the first sentence of the body, add “I am participating in the Mid-Autumn Festival creative submission contest. For details, please see:Mid-Autumn Festival Creative Submission Contest”
preface
Since ancient times, the 15th day of the eighth month of the Lunar calendar has been the best time to enjoy the beauty of the moon every year. When we go to school, we have a glimpse of the beauty of the moon through the poems of poets:
- “The moon out of the Tianshan Mountains, vast sea of clouds”, Li Bai for us to describe the moon just out of the magnificent scene ๐
- “Poor September third night, dew like pearl moon like bow”, Bai Juyi described for us as the bow of the moon ๐
- “Dew from tonight white, the month is hometown bright”, Wang Wei divulge alone in a foreign land wandering children place the yearning of hometown on the moon ๐
Not only the poem on the moon pinned a variety of emotions, familiar folk legends such as Chang ‘e run to the moon, Yutu ramming medicine, diao cicada worship the moon, to modern science and technology on the exploration of the moon has never stopped ๐
Now, it is the Mid-Autumn Festival again. Before we celebrate the holiday, let’s draw a picture of the full moon in Python Turtle
1. The Turtle overview
Turtle, also known as Turtle in Chinese, is a Python drawing library designed to guide beginners to programming.
Python vividly uses the baby turtle to move in the plane system from the origin (0,0) of the X-axis and Y-axis, and its crawling path is the graph we draw.
Turtle syntax format:
* # Create a Turtle object t = turtle ()Copy the code
2. Turtle components
-
Canvas ๐ : Canvas is the area provided by Turtle for drawing
-
Use the turtle. The setup ()
Screen = screen (screen). The setup (800800).Copy the code
-
Using turtle. Screensize (width, height, bg)
Screen = screen (screen). Screensize (800800, bg = "black")Copy the code
-
-
Brush ๐๏ธ : the default is a small turtle crawling from the origin (0,0)
-
Brush action
methods role turtle.pendown() Brush sets turtle.penup() Picked up a brush -
Brush attributes
methods role turtle.pensize() Sets the brush width turtle.pencolor() Returns the current pen color with no arguments passed in turtle.speed(speed) Set brush movement speed, brush drawing speed range [0,10]
-
-
Drawing ๐ :
- Brush movement
- Brush control
- The global control
2. Turtle basic methods
๐ Brush movement method
methods | instructions |
---|---|
turtle.forward(distance) | Moves the distance pixel length in the current brush direction |
turtle.backward(distance) | Moves the distance pixel length in the opposite direction of the current brush |
turtle.right(degree) | Move clockwise by degree degree |
turtle.left(degree) | Move counterclockwise by degree degrees |
turtle.goto(x,y) | Move the brush to x,y |
turtle.circle() | Draw a circle with a radius of positive (negative), meaning the center of the circle is drawn to the left (right) of the brush |
setx( ) | Moves the current X-axis to the specified position |
sety( ) | Moves the current y axis to the specified position |
setheading(angle) | Set the current orientation to Angle |
home() | Sets the current brush position to origin, facing east. |
dot(r) | Draws a dot with a specified diameter and color |
๐ brush control method
methods | instructions |
---|---|
turtle.fillcolor(colorstring) | Draw the fill color of the graph |
turtle.color(color1, color2) | Set pencolor=color1 and fillcolor=color2 |
turtle.filling() | Returns whether the current state is filled |
turtle.begin_fill() | Ready to fill the graph |
turtle.end_fill() | The filling is complete |
turtle.hideturtle() | Hide the turtle shape of the brush |
turtle.showturtle() | Shows the turtle shape of the brush |
๐ Global control command
methods | instructions |
---|---|
turtle.clear() | Clear the Turtle window, but the position and status of Turtle will not change |
turtle.reset() | Clear the window and reset turtle to the start state |
turtle.undo() | Undo the previous turtle action |
turtle.isvisible() | Returns whether the current Turtle is visible |
stamp() | Copy current graph |
turtle.write(s [,font=(“font-name”,font_size,”font_type”)]) | Write text, s is the text content, font is the font parameter, respectively is the font name |
1. Sky map of full moon
The simple version of the full moon sky map in Turtle is as follows:
๐ฃ Important Note:
-
Start by creating a canvas
-
The canvas is 1200 by 900
-
The background color of the canvas is black
-
Change the canvas color mode to RGB format
Screen = screen () width,height = 1200,900 screen.setup(width,height) screen.bgcolor("black") screen.delay() screen.colormode(255)Copy the code
-
-
Draw a bright moon
- Start by defining a Turtle object
- Both the brush color and the fill color are white
- Finally use turtle.circle(120) to draw a circle of size 120
def moon(): m = Turtle(visible=False, Shape ="circle") m.pensize(10) m.pencolor("white") m.Penup () m.Stringcolor ("white") m.goto(width/3,120) m.Pendown () m.begin_fill() m.circle(120) m.end_fill()Copy the code
-
Draw a little star
- Creating a Turtle object
- Define the brush and fill colors as random colors
- Finally, use the drawing steps: penup, pendown, goto
def stars(x,y,left,edge): t = Turtle(visible=False, shape="turtle") t.pensize(2) t.pencolor(randomcolor()) t.fillcolor(randomcolor()) t.speed(0) t.penup() t.goto(x,y) t.pendown() t.left(left) for _ in range(5): t.forward(edge) t.right(144) t.end_fill() t.left(-left) Copy the code
-
We use the for loop to create small stars with random positions
for _ in range(100): x = random.randint(-450,450) y = random.randint(0,400) edge = random.randint(3,8) left = random.randint(0,180) stars(x,y,left,edge) Copy the code
-
The final complete code is as follows:
From Turtle import * import random screen = screen () width,height = 1200,900 screen.setup(width,height) screen.bgcolor("black") screen.delay() screen.colormode(255) def stars(x,y,left,edge): t = Turtle(visible=False, shape="turtle") t.pensize(2) t.pencolor(randomcolor()) t.fillcolor(randomcolor()) t.speed(0) t.penup() t.goto(x,y) t.pendown() t.left(left) for _ in range(5): t.forward(edge) t.right(144) t.end_fill() t.left(-left) def moon(): m = Turtle(visible=False, Shape ="circle") m.pensize(10) m.pencolor("white") m.Penup () m.Stringcolor ("white") m.goto(width/3,120) m.Pendown () m.begin_fill() m.circle(120) m.end_fill() def randomcolor(): R = random.randint(0, 255) G = random.randint(0, 255) B = random.randint(0, 255) moon() for _ in range(100): X = random.randint(-450,450) y = random.randint(0,400) edge = random.randint(3,8) left = random.randint(0,180) stars(x,y,left,edge)Copy the code
conclusion
In this episode, we will learn how to use turtle image library components and related methods to draw a simple version of the full moon and sky map to welcome the Mid-Autumn Festival
The above is the content of this issue, welcome big guys to point up comments and corrections, see you next time ~แฆ(ยด แด ยท ‘) than heart ๐น๐น แด