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