1. #!/usr/bin/env python
  2.  
  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
  6. event management"""
  7.  
  8.  
  9. import random, math, pygame
  10. from pygame.locals import *
  11.  
  12. #constants
  13. WINSIZE = [640, 480]
  14. WINCENTER = [320, 240]
  15. NUMSTARS = 150
  16.  
  17.  
  18. def init_star():
  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[:]
  24.  
  25.  
  26. def initialize_stars():
  27.     "creates a new starfield"
  28.     stars = []
  29.     for x in range(NUMSTARS):
  30.         star = init_star()
  31.         vel, pos = star
  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)
  37.         stars.append(star)
  38.     move_stars(stars)
  39.     return stars
  40.  
  41.  
  42. def draw_stars(surface, stars, color):
  43.     "used to draw (and clear) the stars"
  44.     for vel, pos in stars:
  45.         pos = (int(pos[0]), int(pos[1]))
  46.         surface.set_at(pos, color)
  47.  
  48.  
  49. def move_stars(stars):
  50.     "animate the star values"
  51.     for vel, pos in stars:
  52.         pos[0] = pos[0] + vel[0]
  53.         pos[1] = pos[1] + vel[1]
  54.         if not 0 <= pos[0] <= WINSIZE[0] or not 0 <= pos[1] <= WINSIZE[1]:
  55.             vel[:], pos[:] = init_star()
  56.         else:
  57.             vel[0] = vel[0] * 1.05
  58.             vel[1] = vel[1] * 1.05
  59.  
  60.  
  61. def main():
  62.     "This is the starfield code"
  63.     #create our starfield
  64.     random.seed()
  65.     stars = initialize_stars()
  66.     clock = pygame.time.Clock()
  67.     #initialize and prepare screen
  68.     pygame.init()
  69.     screen = pygame.display.set_mode(WINSIZE)
  70.     pygame.display.set_caption('pygame Stars Example')
  71.     white = 255, 240, 200
  72.     black = 20, 20, 40
  73.     screen.fill(black)
  74.  
  75.     #main game loop
  76.     done = 0
  77.     while not done:
  78.         draw_stars(screen, stars, black)
  79.         move_stars(stars)
  80.         draw_stars(screen, stars, white)
  81.         pygame.display.update()
  82.         for e in pygame.event.get():
  83.             if e.type == QUIT or (e.type == KEYUP and e.key == K_ESCAPE):
  84.                 done = 1
  85.                 break
  86.             elif e.type == MOUSEBUTTONDOWN and e.button == 1:
  87.                 WINCENTER[:] = list(e.pos)
  88.         clock.tick(50)
  89.  
  90.  
  91. # if python says run, then we should run
  92. if __name__ == '__main__':
  93.     main()
  94.  
  95.  
  96.  
[raw code]