~ubuntu-branches/ubuntu/gutsy/pygame/gutsy

« back to all changes in this revision

Viewing changes to examples/stars.py

  • Committer: Bazaar Package Importer
  • Author(s): Ed Boraas
  • Date: 2002-02-20 06:39:24 UTC
  • Revision ID: james.westby@ubuntu.com-20020220063924-amlzj7tqkeods4eq
Tags: upstream-1.4
ImportĀ upstreamĀ versionĀ 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
                surface.set_at(pos, color)
 
46
 
 
47
 
 
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()
 
55
                else:
 
56
                        vel[0] = vel[0] * 1.05
 
57
                        vel[1] = vel[1] * 1.05
 
58
        
 
59
 
 
60
def main():
 
61
        "This is the starfield code"
 
62
        #create our starfield
 
63
        random.seed()
 
64
        stars = initialize_stars()
 
65
 
 
66
        #initialize and prepare screen
 
67
        pygame.init()
 
68
        screen = pygame.display.set_mode(WINSIZE)
 
69
        pygame.display.set_caption('pygame Stars Example')
 
70
        white = 255, 240, 200
 
71
        black = 20, 20, 40
 
72
        screen.fill(black)
 
73
 
 
74
        #main game loop
 
75
        done = 0
 
76
        while not done:
 
77
                draw_stars(screen, stars, black)
 
78
                move_stars(stars)
 
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):
 
83
                                done = 1
 
84
                                break
 
85
                        elif e.type == MOUSEBUTTONDOWN and e.button == 1:
 
86
                                WINCENTER[:] = list(e.pos)
 
87
 
 
88
 
 
89
 
 
90
# if python says run, then we should run
 
91
if __name__ == '__main__':
 
92
        main()
 
93
 
 
94