-
Notifications
You must be signed in to change notification settings - Fork 0
/
starfield.py
78 lines (63 loc) · 2.15 KB
/
starfield.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Parallax Starfield Simulation
by Leonel Machava <leonelmachava@gmail.com>
Website: http://codentronix.com
Facebook: http://facebook.com/codentronix
Twitter: http://twitter.com/codentronix
"""
import pygame
from random import randrange, choice
MAX_STARS = 350
STAR_SPEED = 2
def init_stars(screen):
""" Create the starfield """
global stars
stars = []
for i in range(MAX_STARS):
# A star is represented as a list with this format: [X,Y,speed]
star = [randrange(0, screen.get_width() - 1),
randrange(0, screen.get_height() - 1),
choice([1, 2, 3])]
stars.append(star)
def move_and_draw_stars(screen):
""" Move and draw the stars in the given screen """
global stars
for star in stars:
star[1] += star[2]
# If the star hit the bottom border then we reposition
# it in the top of the screen with a random X coordinate.
if star[1] >= screen.get_height():
star[1] = 0
star[0] = randrange(0, screen.get_width()-1)
star[2] = choice([1, 2, 3])
# Adjust the star color according to the speed.
# The slower the star, the darker should be its color.
color = None # initialize color to be pep8 compliant
if star[2] == 1:
color = (100, 100, 100)
elif star[2] == 2:
color = (190, 190, 190)
elif star[2] == 3:
color = (255, 255, 255)
# Draw the star as a rectangle.
# The star size is proportional to its speed.
screen.fill(color, (star[0], star[1], star[2], star[2]))
def main():
# Pygame stuff
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Parallax Starfield Simulation")
clock = pygame.time.Clock()
init_stars(screen)
while True:
# Lock the frame rate at 50 FPS
clock.tick(50)
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
screen.fill((0, 0, 0))
move_and_draw_stars(screen)
pygame.display.flip()
if __name__ == "__main__":
main()