Как нарисовать звезду в python turtle
In this article, we will learn how to make a Star using Turtle Graphics in Python. For that let’s first know what is Turtle Graphics.
Turtle graphics
Turtle is a Python feature like a drawing board, which let us command a turtle to draw all over it! We can use many turtle functions which can move the turtle around. Turtle comes into the turtle library. The turtle module can be used in both object-oriented and procedure-oriented ways.
Hacks and tweaks: Program your turtle to draw a star wherever you click on the screen with the mouse.
In the Starry Night project, a turtle draws and fills the screen with a variety of stars. But what if you wanted the turtle to draw a star wherever you clicked with your mouse on the screen.
- Create a new trinket Python project.
- In your new trinket, copy the imports and the draw_star(size , color , points , x , y) function from your Starry Nighttrinket project.
- Create an instance of the turtle Screen class and store it in a variable called screen. Place this line of code after the import statements.
4. Create a new function and call it draw. Pass it two parameters x and y. This function will be passed as an argument to the screen object’s onclick() method.
5. Copy the randomSize, randomColor and randomPoints variables from your Starry Night trinket project and paste them inside of the newly created function draw( x , y ).
6. In the draw( x , y) function, after pasting the above variable declarations (step 4), call the draw_star( size , color , points , x , y) function and pass it the randomSize, randomColor, randomPoints, x and y variables where x an y will be the coordinates of the point where your mouse is clicked and these variables will be received by the draw(x , y) function from screen object’s onclick() method.
7. After the draw(x , y) function, call the showturtle() function on your turtle.
8. Finally, call the screen.onclick() method and pass it the draw( x , y ) function.
10. Run the program.
11. Now whenever you will click on the trinket canvas with your mouse, the screen.onclick() method will be triggered.
12. The screen.onclick() method will then call the draw( x , y ) function passing it the coordinates of the mouse click in variables x and y respectively.
13. The draw( x , y ) function will run and start drawing the star at the coordinates it received.
14. Your final code should look something like this:
Challenge
There is a bug in this code. Try to find it and fix it. (See solution in the end)
Resources
Solution to Challenge
While your program is running, whenever you click on the trinket canvas, the screen.onclick() method is triggered. If you don’t wait for the turtle to complete drawing the star and click again anywhere else on the canvas, the turtle will start drawing at the new clicked position leaving the star that it was drawing earlier. The previous star will look incomplete and the turtle will seem confused.
To fix this issue, we will need to check if the turtle is already drawing a star. If so, it should complete drawing it and should not respond to other mouse clicks on the screen. Let’s do this!
- After the import statements, create a global variable called working and assign it the value of False.
2. We will use the variable working inside the draw(x, y) function to check if the turtle is currently drawing or not. We need to add the keyword global before the variable working. If you do not do this, the draw(x, y) function will not recognize the variable as it is out of its scope.
3. Before calling the draw_star(size, color, points, x, y) function, we will add an if statement as follows:
4. Check if working is False (means turtle is not busy drawing). If so, assign working the value of True (as turtle is about to start drawing), call the draw_star(size,color,points,x,y) function (the turtle is drawing) and then assign working the value of False (the turtle has finished drawing and now is free to draw another star).
Python Turtle Star – How to Draw
In this Python tutorial, we will learn How to draw star shapes in Python Turtle and we will also cover different examples related to Turtle Star. And, we will cover these topics.
- Python turtle star
- Python turtle star position
- Python turtle star code
- Python turtle starry night
Table of Contents
Python turtle star
In this section, we will learn about how to draw a star shape in a python turtle.
Before moving forward we should have a piece of knowledge about stars. Stars are heavenly bodies that are visible at night and when somebody is seen in the sky it looks like a fixed point of light. The nearest star which is nearest to earth is the sun. Turtle star is that which is drawn on the drawing board with the help of the turtle.
Code:
In the following code, we will import the turtle module from turtle import *, import turtle. The turtle() method is used to make objects.
- tur.pensize(4) is used to give the size to the pen.
- tur.penup() is used to pick up the pen and turtle stop drawing.
- tur.setpos(-90,30) is used to set the position of turtle.
- tur.pendown() is used to start drawing.
- tur.pencolor(color[i]) is used to give the color to pen.
- tur.forward(200) is used to move the turtle in the forward direction.
- tur.right(144) is used to move the turtle in the right direction.
Output:
After running the above code, we will get the following output in which we can see a beautiful colored star is drawn on the screen which attracts the user’s eye.
Python turtle star position
In this section, we will learn about the turtle star position in python turtle.
As we know the star is visible at night and present in the sky looks like a fixed point of light. We also draw the star on the drawing board with the help of the turtle and measure the position of the turtle where the star is drawn at what position.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle, and also import sys this module provides various functions that handle the runtime environment of python from different parts.
- tur.color(clr) is used to give color to the turtle.
- tur.position() is used to give the starting position to the turtle the drawing start.
- print(“Exception:”, point, file=sys.stderr) is used to print the position on the screen.
- tur.reset() is used to again set the position of the turtle.
Output:
After running the above code, we will get the following output in which we can see the stars is drawing on the screen.
When the turtle starts to make a star then the starting position of the star is captured in the command prompt as we can see in the below picture.
Python turtle star code
In this section, we will learn about how to create a star code in python turtle.
As we know stars are heavenly bodies that are visible at night. It is a fixed luminous point. When we see the star at night it twinkles continuously and looks very beautiful which attracts people’s eyes. Here we use a turtle to draw the shape of a star.
Code:
In the following code, we will import the turtle module from turtle import *, import turtle as tur. The turtle() method is used to make objects.
- tur.Screen() is used to create a screen on which we draw the shape of a star.
- turt.forward(100) is used to move the turtle in the forward direction.
- turt.right(144) is used to move the turtle in the right direction.
Output:
After running the above code, we get the following output in which we can see a beautiful star shape is drawn whit the help of a turtle.

Python turtle starry night
In this section, we will learn about how to create a starry night with the help turtle in python turtle.
Starry night is a beautiful view that is only seen at night and this night view is captured in form of a painting of the eloquent night sky. As the word defines starry night the night is full of lots of stars. It seems like a dream we sit in the shade of stars there is mountains and river near to us and we capture all view in the form of a picture.
Here we draw this starry night painting with the help of a turtle. Turtle is worked as a brush and draw the landscape painting of an eloquent night sky.
Code:
In the following code, we will import the turtle module from turtle import *, import turtle as turt. We will also import random module for the generation of the random numbers.
- The turtle() method is used to make objects.
- turt.Screen() is used for creating the screen in which we can draw the shapes.
- tur.speed(0) is used to give the speed to the speed.
- ws.bgcolor(“black”) is used to give the background color to the screen.
- tur.color(“white”) is used to give the color to the turtle.
- ws.title(“Python Guides”) is used to give the title to the screen.
- random.randint(-640, 640) is used to generate the random integer value for x and y.
- stars() calling the function star to draw a star.
- tur.up() is used to pick up the pen to stop the drawing.
- tur.down() is used to start drawing.
- tur.begin_fill() is used to fill the color in the shape.
- tur.circle(80) is used to draw the circle shape .
- tur.end_fill() is used to stop filling color in the shape.
Output:
After running the above code, we will get the following output in which we can see the starry sky in which lots of stars are generated randomly and the sky looks very pretty.
You may also like to read the following tutorials on python Turtle.
So, in this tutorial, we discussed Python Turtle Star and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.
- Python turtle star
- Python turtle star position
- Python turtle star code
- Python turtle starry night

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.
How to draw a filled star with Python Turtle
I overdid things a little when trying to teach 6th graders how to draw a star using Python Turtle. But I thought that it was fun anyways so I’m sharing it here.
Note that this is also my first dev.to article. I appreciate any suggestion.
Start with a zig zag
A zig-zag can be simple to explain. We just have to move the cursor left then right over and over
Exit fullscreen mode
Here’s what we get
As we have repeating code we want to use a loop to reduce the code
Exit fullscreen mode
Above we can change the value of pointies = 10 to change the number of zig zags
Rotate the zig zag
What happens if we rotate the zig zag by a certain value
Exit fullscreen mode
All we did above was added this statement t.right(ROTATION) . We also said ROTATION = 30
and here’s what we get

But wouldn’t it be nice if the ends were connected? To achieve this we just need to set rotation to ROTATION = 360/pointies . Remember that 360 degrees is a full rotation.
Exit fullscreen mode

Color the star
To color the star we first set the color using t.color(«red»,»red») . We then use the begin_fill and end_fill commands to start and stop filling the area. Here’s it is:
Exit fullscreen mode
In the code above we have also added a statement t.speed(0) to speed things up

We can now draw stars with any number of points of any color. Here are some examples:

Rotate the star
Why stop at drawing a star when we can rotate it! To do this we use a nested for-loop. This means a loop inside a loop