Advertisement

Starry Night ( Python Project )

 Starry Night

Fill your screen with beautiful stars! This project uses Python’s turtle module to draw star shapes. Random numbers scatter the stars over the screen and vary their color, size, and shape.


What happens

First a nighttime sky is drawn, then a single star appears in the sky. As the program continues, the sky begins to fill with more and more stars in a wide range of different styles. The longer you leave the program running, the more fantastic and colorful the sky becomes.

How it works

The code for this project draws star shapes at random locations in a Turtle Graphics window. You’ll write Python code to create a function that can draw a single star. Then you’ll make a loop that repeats it over and over, drawing lots of different stars all over the screen.

Starry Night flowchart

The flowchart is quite simple, with no questions to be asked or decisions to be made. Once the turtle has drawn the first star, the program loops back and repeats the star-drawing steps nonstop until you quit.



1     Create a new file

Open IDLE. Go to the File menu, then select New File. Save the file as “starry_night.py”.

2     Import turtle

Type this line into the editor window that appears. It loads the turtle module, ready for you to start drawing your star.

3     Write some instructions

Now add this code beneath the command to import turtle. It creates variables that set the size and shape of the star. It also tells the turtle how to move over the window to draw the star.

4     Draw a test star

From the IDLE menu, select Run and then Run Module to test the project. The Turtle Graphics window will appear (it might be behind another window) and you’ll see the turtle arrow begin to draw your star.

5     Add an angle calculator

It would be good to be able to draw stars with different numbers of points. Make this change to the code. It will calculate the angle of the turns that the turtle needs to make to draw a star with however many points you choose.

6     Color it!

You’ve drawn a nice, neat star, but it looks rather dull at the moment. Let’s add some color to make it more attractive. Change the code as shown on the right to paint your star yellow.

7     Run the project

The turtle should draw a yellow star. See if you can change thestar’s color by editing the code.

8     Draw different stars

Try changing the number after the equals sign in the variable points and you’ll see that you can draw different stars. Note that the code only works for stars with odd numbers of points. Even numbers will mess things up.

9     Create the star function

Edit the code as shown here. It replaces nearly all of your existing code with a new version. The large block wraps up all the star-drawing instructions and keeps them neatly together as a function. You can now use this function to draw a star in your main code with a single line of Python, draw_star().

10    Run the project

The turtle should draw a single yellow star on a blue background.



11    Add random numbers

Now mix things up by adding some random numbers to your code. Type this line under the line that imports turtle. It brings in the randint() and random() functions from Python’s random module.

12    Create a loop

Make this change to the #Main code section. It adds a while loop that continually randomizes the parameters used to set the stars’ size, shape, color, and position.

13    Run the project again

The window should slowly fill up as the turtle draws star after star in a range of colors, shapes, and sizes.



CODE

import turtle as t
from random import randint, random

def draw_star(points, size, col, x, y):
    t.penup()
    t.goto(x, y)
    t.pendown
    angle = 180 – (180 / points)
    t.color(col)
    t.begin_fill()
    for i in range(points):
t.forward(size)
t.right(angle)
    t.end_fill()

# Main code
t.Screen().bgcolor('dark blue')

while True:
    ranPts = randint(2, 5) * 2 + 1
    ranSize = randint(10, 50)
    ranCol = (random(), random(), random())
    ranX = randint(–350, 300)
    ranY = randint(–250, 250)

    draw_star(ranPts, ranSize, ranCol, ranX, ranY)



Post a Comment

0 Comments