annotate gamelib/main.py @ 4:e8ddbc95cbf3

Silly (and broken) first stab at menu thing.
author Jeremy Thurgood <firxen@gmail.com>
date Sun, 30 Aug 2009 12:48:02 +0000
parents e057e9483488
children 67b79658b047
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
1 '''Game main module.
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
2
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
3 Contains the entry point used by the run_game.py script.
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
4
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
5 Feel free to put all your game code here, or in other modules in this "gamelib"
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
6 package.
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
7 '''
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
8
4
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
9 SCREEN = (800, 600)
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
10
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
11 import time
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
12 import random
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
13
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
14 import pygame
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
15 from pgu import gui
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
16 from pygame.locals import *
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
17
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
18 from mainmenu import MainMenu
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
19
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
20 W,H = 640,480
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
21 W2,H2 = 320,240
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
22
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
23 ##Using App instead of Desktop removes the GUI background. Note the call to app.init()
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
24 ##::
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
25
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
26 form = gui.Form()
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
27
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
28 app = gui.App()
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
29 main_menu = MainMenu()
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
30
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
31 c = gui.Container(align=-1,valign=-1)
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
32 c.add(main_menu,0,0)
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
33
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
34 app.init(c)
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
35 ##
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
36
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
37 dist = 8192
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
38 span = 10
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
39 stars = []
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
40 def reset():
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
41 global stars
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
42 stars = []
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
43 for i in range(0,form['quantity'].value):
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
44 stars.append([random.randrange(-W*span,W*span),
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
45 random.randrange(-H*span,H*span),
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
46 random.randrange(1,dist)])
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
47
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
48
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
49 def render(dt):
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
50 speed = form['speed'].value*10
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
51 size = form['size'].value
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
52 color = form['color'].value
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
53 warp = form['warp'].value
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
54
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
55 colors = []
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
56 for i in range(256,0,-1):
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
57 colors.append((color[0]*i/256,color[1]*i/256,color[2]*i/256))
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
58
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
59 n = 0
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
60 for x,y,z in stars:
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
61 if warp:
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
62 z1 = max(1,z + speed*2)
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
63 x1 = x*256/z1
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
64 y1 = y*256/z1
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
65 xx1,yy1 = x1+W2,y1+H2
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
66
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
67 x = x*256/z
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
68 y = y*256/z
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
69 xx,yy = x+W2,y+H2
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
70 c = min(255,z * 255 / dist)
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
71 col = colors[int(c)]
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
72
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
73 if warp:
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
74 pygame.draw.line(screen,col,
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
75 (int(xx1),int(yy1)),
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
76 (int(xx),int(yy)),size)
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
77
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
78 pygame.draw.circle(screen,col,(int(xx),int(yy)),size)
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
79
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
80 ch = 0
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
81 z -= speed*dt
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
82 if z <= 0:
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
83 ch = 1
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
84 z += dist
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
85 if z > dist:
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
86 ch = 1
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
87 z -= dist
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
88 if ch:
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
89 stars[n][0] = random.randrange(-W*span,W*span)
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
90 stars[n][1] = random.randrange(-H*span,H*span)
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
91 stars[n][2] = z
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
92
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
93 n += 1
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
94
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
95 screen = pygame.display.set_mode(SCREEN,SWSURFACE)
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
96
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
97 ##You can include your own run loop.
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
98 ##::
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
99
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
100 def gameloop(screen):
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
101 reset()
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
102 clock = pygame.time.Clock()
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
103 done = False
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
104 while not done:
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
105 for e in pygame.event.get():
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
106 if e.type is QUIT:
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
107 done = True
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
108 elif e.type is KEYDOWN and e.key == K_ESCAPE:
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
109 done = True
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
110 else:
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
111 app.event(e)
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
112
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
113 # Clear the screen and render the stars
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
114 dt = clock.tick()/1000.0
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
115 screen.fill((0,0,0))
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
116 render(dt)
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
117 app.paint(screen)
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
118 pygame.display.flip()
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
119 pygame.time.wait(10)
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
120
2
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
121
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
122 def main():
4
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents: 2
diff changeset
123 gameloop(screen)