Star field
So we have many things on the screen. Let's move them and try to create something useful. We can draw small circles moving down the screen and simulate a star field.
A bit of theory: conditionals
You may have seen this before, but it's good to refresh our memories. Type down the following program:
number = 4
if number > 10:
print("The number is greater than 10")
elif number == 10:
print("The number is 10")
else:
print("The number is less than 10")
- What does the program do?
- How can you change the first line so that a different message is printed?
Flying though space
We can now use all we have learned so far to create a star field animation. As usual, start a new program from scratch that does the following:
- Import the
random
library.
- Import Pygame.
- Define a class
Star
.
- Create an empty list where we'll put our stars.
- Write a loop that will run 100 times. Each time it will do the following:
- Create a
Star
object and put it in a variable.
- Give the star a random
x
coordinate, between 0 and 800.
- Give the star a random
y
coordinate, between 0 and 600.
- Give the star a random
size
, between 1 and 3.
- Give the star a random
speed
, between 1 and 3.
- Give the star a random
brightness
, between 0 and 255.
- Add the object to the list of stars.
- Initialize Pygame
- Create a window of size 800x600
- Write a loop that will run 500 times. On each iteration, do the following:
- Paint the screen completely black
- For each star in the list, do the following:
- Draw a circle on the screen so that:
- The radius is the size of the star
- The colour is the brightness of the star, three times as a list. Eg: if the brightness is
150
, the colour will be [150, 150, 150]
.
- The position uses the
x
and y
values for the star.
- Change the
y
coordinate of the star so that:
- If it is less than 600, it increases by 1
- Otherwise, it is set to 0
- Update the screen