Звезда в питоне код

Turtle Stars

Drawing with turtle library of Python can be very educational and fun.

In this tutorial we will focus on how to draw stars with turtle in Python.

We’re going to show you how to draw monocolor stars such as yellow stars as well as how to draw multicolor stars.

Then we will attempt to scatter a number of stars on a dark blue night sky using turtle drawing and Python. Let’s see.

Star Geometry

This makes everything so much easier as we love repetition in coding. Only 5 iterations with same turn angles will do.

Example 1: One Simple Star

After opening an image you can use the .resize() method on it to create a new image with a new size.

Please Note: Adding turtle.exitonclick() can help terminate turtle window once you’re done with your turtle drawing or observations. If you don’t add this little code piece, turtle tends to freeze which requires a kernel restart which can be annoying. So we will add it to the end of each code and you’re also advised to do so.

import turtle for i in range(5): turtle.forward(150) turtle.right(144) turtle.exitonclick() 

Here is turtle in action. We figured out a single star. Yay!

It’s downhill from this point. Now we can add some spice. Maybe some colors. And definitely some more stars so they don’t feel lonely 😉

Example 2: Yellow Star

We can simply use .color method to give turtle a color: turtle.color(«yellow»)

import turtle turtle.color("yellow") for i in range(5): turtle.forward(150) turtle.right(144) turtle.exitonclick() 

Great now our star has some color even. We can also transform the background to a color like dark blue which will make the star’s yellow pop better.

Example 3: Dark Blue Night Sky

We can use .bgcolor method to change the background color of a turtle drawing window: turtle.bgcolor(«darkblue»)

import turtle turtle.color("yellow") turtle.bgcolor("darkblue") for i in range(5): turtle.forward(150) turtle.right(144) turtle.exitonclick() 

Perfect. On your computer it might look even better actually. On the website it’s being scaled up which makes the resolution appear worse than it is.

Now, let’s find a cure for loneliness.

Example 4: Multiple Stars

turtle’s setpos() or goto() methods can be used to make the cursor travel between different coordinates and this way we can draw a different star at each coordinate. We will have to adjust a few things:

  • Let’s turn down the star size quite a bit. This can be easily adjusted by passing a smaller value to .forward() command which draws each side of the star.
  • We can implement a random.randint() structure which tells a random x,y coordinate on our pseudosky.
  • Also, let’s speed up the turtle so we don’t have to wait too long for each star to be drawn. This can be achieved with turtle.speed(0)
    • 0 : fastest setting while
    • 10 : slowest setting

    Let’s try. Here is the Python code.

    import turtle turtle.color("yellow") turtle.bgcolor("darkblue") turtle.speed(0) turtle.hideturtle() def starmaker(step,angle): for iter in range(5): turtle.forward(step) turtle.right(angle) for i in range(10): turtle.penup() turtle.setpos(random.randint(-350, 350), random.randint(100, 300)) turtle.pendown() starmaker(10,144) turtle.exitonclick() 

    Wow! Now we’re talking. Our turtle drawing is starting to look legit. It might even compete with Van Gogh’s Starry Night, what do you think? Alright, I think that’s an exaggeration too too. But it’s still pretty good for turtle digital art category.

    Where can we go from here? Well, rather than a solid yellow, stars kind of flicker . Maybe we can draw stars with multiple colors just for fun?

    Example 5: Multi-color Multiple Stars

    So, how can we make each side of the star a different random color? Pretty easy actually.

    By placing turtle.color() attribute inside the turtle making function’s for loop, we can make sure turle’s color is redefined at each iteration.

    We can also create a list of predefined colors. Then using random library we can pick a random color by randomly accessing the indexes of this color list. Remember that list index starts at 0 in Python so random.randint() should go to list size-1 at maximum.

    import turtle turtle.bgcolor("darkblue") turtle.speed(0) turtle.hideturtle() color_lst=["red", "orange", "cyan", "yellow", "gray", "lightgreen"] def starmaker(step,angle): for iter in range(5): turtle.color(color_lst[random.randint(0,5)]) turtle.forward(step) turtle.right(angle) for i in range(15): turtle.penup() turtle.setpos(random.randint(-350, 350), random.randint(100, 300)) turtle.pendown() starmaker(10,144) turtle.exitonclick() 

    Here we go we started with a single black & white star and discovered all the way up to multiple little stars with multiple colors on a dark blue night sky.

    Resources

    You can check out other turtle tutorials we have for more examples and turtle knowledge.

    Summary

    In this Python Tutorial, we have learned how to draw stars with turtle.

    We have also advanced the simple idea of drawing a star and played with it a litte. We ended up drawing multiple good looking stars on a night sky.

    This tutorial can be a great practice to demonstrate basic Python concepts. Feel free to freely use and share at your school or work, attributions or source credit are very welcome.

    Thank you for your support!

    Источник

    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

    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.

    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()

    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.

    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 ,<>)".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()

    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 position

    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.

    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()

    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 star code

    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.

    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()

    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

    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.

    Источник

    Читайте также:  Арифметикалы орта табу python
Оцените статью