Shooting Stars

Intro

Have you ever imagine being in a spaceship looking at the stars flying through you? This project might make the scene alive on your computer!

GIFs

stars with no trails

more stars
stars with trails

Visit the Github Repo for this project

Project inspired by:
-Processing Examples At cs.brynmawr.edu
-The Coding Train

Features

  • Stars flying from far gets larger when closer
  • Be a time controller! Move the mouse to control the speed
  • You may select whether seeing the trails

Controls

action control
Speed Up Move The Mouse To Right
Slow Down Move The Mouse To Right

Dependencies

You will need the pygame package for this game.

1
$ pip install pygame

Run

Download the shooting_stars.py file or clone this repo and run it in the terminal.

1
$ python shooting_stars.py

HOW To Create Your Version

init the frame
1
2
3
4
5
6
width = 500
height = 500
pg.init()
scr = pg.display.set_mode((width, height))
pg.display.set_caption("Shooting Stars")
pg.display.set_icon(scr)
class definition of Star
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
## Star class
class Star:
# constructor
def __init__(self):
self.x = random.randint(-width//2, width//2)
self.y = random.randint(-height//2, height//2)
self.z = random.randint(1, width)
self.oz = self.z

## update function
def update(self, speed):
self.z -= speed

## if the star vanished, initialize it again
if self.z < 1 or self.z == 0:
self.__init__()

## show function
def show(self):
sx = int(self.x/self.z * width)
sy = int(self.y/self.z * height)

r = int((1 - self.z/width) * 4)
pg.draw.circle(scr, (255,255,255), (sx + width//2 + r//2, sy + height//2 + r//2), r)

if(showingMeteors):
px = int(self.x/self.oz * width)
py = int(self.y/self.oz * height)
pg.draw.line(scr, (255,255,255), (sx + width//2, sy + height//2), (px + width//2, py + height//2))

## uncomment this line if you want the trail from some distance away
## instead of the star's origin position
#self.oz = self.z + 50
show and update function for a list of Stars
1
2
3
4
5
6
## show stars and update the speeds 
def draw():
speed = (pg.mouse.get_pos()[0] / width) * 10
for each in stars:
each.update(speed)
each.show()
initialize a list of Stars with a given number and run the main loop
1
2
3
4
5
6
7
8
9
10
11
12
13
## initialize the stars, you may change the number of stars 
num_of_stars = 800
stars = [Star() for i in range(num_of_stars)]

## main loop
while True:
scr.fill((0,0,0))
for eve in pg.event.get():
if eve.type == QUIT:
sys.exit()
draw()
pg.display.update()
sleep(0.005)

Conclusion

Enjoy your journey in the universe! Make sure to create a version yourself!

Improvements you can make

  • add different colors or shapes to the stars
  • make the scene three-dimensional
  • add a spaceship model