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
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.
from turtle import *
import turtle as tur
ws = tur.Turtle()
color= ['yellow','orange','green','cyan','blue']
tur.pensize(4)
tur.penup()
tur.setpos(-90,30)
tur.pendown()
for i in range(5):
tur.pencolor(color[i])
tur.forward(200)
tur.right(144)
tur.penup()
tur.setpos(80,-140)
tur.pendown()
tur.pencolor("Black")
tur.done()
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.
Also, check: Python Turtle Get Position
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.
from turtle import *
import turtle as tur
import sys
from time import sleep
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
def normal_star(siz, clr, point):
if point <= 4:
raise ValueError('Not enough point')
tur.color(clr)
for coprime in range(point // 2, 1, -1):
if gcd(point, coprime) == 1:
print("({},{})".format(point, coprime), file=sys.stderr)
start = tur.position()
for _ in range(point):
tur.forward(siz)
tur.left(360.0 / point * coprime)
tur.setposition(start)
return
abnormal_star(siz, clr, point)
def abnormal_star(siz, clr, point):
print("Exception:", point, file=sys.stderr)
for point in range(7, 20):
tur.reset()
normal_star(250, 'blue', point)
sleep(6)
tur.exitonclick()
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.
Read: Python Turtle Draw Line
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.
from turtle import *
import turtle as tur
ws = tur.Screen()
turt = tur.Turtle()
for i in range(5):
turt.forward(100)
turt.right(144)
tur.done()
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.
Read: Python Turtle Nested Loop
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.
from turtle import *
import turtle as turt
import random
tur = turt.Turtle()
ws = turt.Screen()
tur.speed(0)
ws.bgcolor("black")
tur.color("white")
ws.title("Python Guides")
def stars():
for x in range(5):
tur.fd(10)
tur.right(144)
for x in range(100):
a = random.randint(-640, 640)
b = random.randint(-330, 330)
stars()
tur.up()
tur.goto(a, b)
tur.down()
tur.up()
tur.goto(0, 170)
tur.down()
tur.color("white")
tur.begin_fill()
tur.circle(80)
tur.end_fill()
tur.hideturtle()
ws.exitonclick()
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.
- Python turtle draw letters
- Python Turtle Mouse
- Python Turtle Pen
- Python Turtle Random
- Python Turtle Oval
- Python turtle input
- Python Turtle Grid
- Python Turtle Polygon
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
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.