3
"""A simple starfield example. Note you can move the 'center' of
4
the starfield by leftclicking in the window. This example show
5
the basics of creating a window, simple pixel plotting, and input
9
import random, math, pygame
10
from pygame.locals import *
14
WINCENTER = [320, 240]
19
"creates new star values"
20
dir = random.randrange(100000)
21
velmult = random.random()*.6+.4
22
vel = [math.sin(dir) * velmult, math.cos(dir) * velmult]
23
return vel, WINCENTER[:]
26
def initialize_stars():
27
"creates a new starfield"
29
for x in range(NUMSTARS):
32
steps = random.randint(0, WINCENTER[0])
33
pos[0] = pos[0] + (vel[0] * steps)
34
pos[1] = pos[1] + (vel[1] * steps)
35
vel[0] = vel[0] * (steps * .09)
36
vel[1] = vel[1] * (steps * .09)
42
def draw_stars(surface, stars, color):
43
"used to draw (and clear) the stars"
44
for vel, pos in stars:
45
surface.set_at(pos, color)
48
def move_stars(stars):
49
"animate the star values"
50
for vel, pos in stars:
51
pos[0] = pos[0] + vel[0]
52
pos[1] = pos[1] + vel[1]
53
if not 0 <= pos[0] <= WINSIZE[0] or not 0 <= pos[1] <= WINSIZE[1]:
54
vel[:], pos[:] = init_star()
56
vel[0] = vel[0] * 1.05
57
vel[1] = vel[1] * 1.05
61
"This is the starfield code"
64
stars = initialize_stars()
66
#initialize and prepare screen
68
screen = pygame.display.set_mode(WINSIZE)
69
pygame.display.set_caption('pygame Stars Example')
77
draw_stars(screen, stars, black)
79
draw_stars(screen, stars, white)
80
pygame.display.update()
81
for e in pygame.event.get():
82
if e.type == QUIT or (e.type == KEYUP and e.key == K_ESCAPE):
85
elif e.type == MOUSEBUTTONDOWN and e.button == 1:
86
WINCENTER[:] = list(e.pos)
90
# if python says run, then we should run
91
if __name__ == '__main__':