Preface
In the world of programming, graphical programming is a very interesting and intuitive way to help beginners quickly understand the basic concepts of programming. Python's turtle module is such a tool that draws various graphics by moving a virtual "turtle" across the screen. From simple lines to complex geometric patterns, turtle can handle it easily. Today, we will use the turtle module to explore how to draw amazing geometric patterns using simple code.
1. Installation and Import
First, make sure your Python environment is installedturtle
Module. In most cases,turtle
is part of the Python standard library, so you may not need additional installation. If you encounter problems, you can try installing with the following command:
pip install PythonTurtle
Next, import in your Python script or Jupyter Notebookturtle
:
import turtle
2. Basic operations
2.1 Creating a canvas
Before we start drawing, we need to create a canvas. This can be called()
To achieve:
screen = ()
2.2 Creating a Turtle
Next, create a turtle object, which will be our main tool for drawing the graphics:
t = ()
2.3 Movement and steering
-
go ahead:
(100)
Move the turtle forward by 100 units. -
Back:
(100)
Move the turtle backward by 100 units. -
Turn left:
(90)
Turn the turtle to the left by 90 degrees. -
Turn right:
(90)
Turn the turtle to the right by 90 degrees.
2.4 Set color and speed
-
Set color:
('red')
Set the turtle's handwriting color to red. -
Set the fill color:
('blue')
Set the fill color. -
Set the speed:
(10)
Set the speed of the turtle moving (1-10, 1 slowest, 10 fastest).
2.5 Start and end padding
-
Start filling:
t.begin_fill()
Called before drawing the shape to prepare the fill. -
End fill:
t.end_fill()
Called after drawing the shape to complete the fill.
3. Draw basic graphics
3.1 Drawing a square
for _ in range(4): (100) (90)
3.2 Drawing equilateral triangles
for _ in range(3): (100) (120)
3.3 Drawing a circle
(50) # Circles with radius of 50
4. Draw complex patterns
4.1 Multiple spirals
def draw_spiral(t, n, r=5): """Draw spiral lines""" for i in range(n): (r + i, 45) draw_spiral(t, 50, 5)
4.2 Petal pattern
def draw_petal(t, r): """Draw petals""" (r, 60) (120) (r, 60) (120) def draw_flower(t, r, petals): """Draw flowers""" for _ in range(petals): draw_petal(t, r) (360 / petals) (10) ('pink') ('pink') t.begin_fill() draw_flower(t, 100, 8) t.end_fill()
4.3 Star Pattern
def draw_star(t, size): """Draw the stars""" for _ in range(5): (size) (144) (10) ('yellow') ('yellow') t.begin_fill() draw_star(t, 200) t.end_fill()
5. Save the image
If you want to save the drawn pattern as an image file, you can use the following code:
ts = () ().postscript(file="")
This will generate a name calledThe file you can use image editing software to convert it to other formats such as PNG or JPEG.
6. End drawing
After all drawings are finished, remember to close the window:
()
Alternatively, if you are using a Jupyter Notebook, you can use () to end the drawing and display the results.
7. Summary
With the above examples we can see that even the simplest code can create amazing visual effects. The turtle module is not only suitable for beginners to learn the basics of programming, but also a powerful tool for artists and designers to create digital art. I hope this article will inspire your creativity and let you discover more fun in the world of programming!
The above is the detailed content of the simple code for using Python Turtle to draw cool geometric patterns. For more information about Python Turtle to draw geometric patterns, please pay attention to my other related articles!