176edfb1448718f1319e26ae37b55d81f71f43e6
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / examples / gui10.py
1 """<title>Integration with a Game</title>
2 For games, it is usually preferrable to not have your game within
3 a GUI framework.  This GUI framework can be placed within your game.
4 """
5
6 import time
7 import random
8 import pygame
9 from pygame.locals import *
10
11 # the following line is not needed if pgu is installed
12 import sys; sys.path.insert(0, "..")
13
14 from pgu import gui
15 from gui7 import ColorDialog
16
17 # The maximum frame-rate
18 FPS = 30
19 WIDTH,HEIGHT = 640,480
20
21 ##You can initialize the screen yourself.
22 ##::
23 screen = pygame.display.set_mode((640,480),SWSURFACE)
24 ##
25
26 class StarControl(gui.Table):
27     def __init__(self,**params):
28         gui.Table.__init__(self,**params)
29
30         def fullscreen_changed(btn):
31             #pygame.display.toggle_fullscreen()
32             print("TOGGLE FULLSCREEN")
33
34         def stars_changed(slider):
35             n = slider.value - len(stars)
36             if n < 0:
37                 for i in range(n,0): 
38                     stars.pop()
39             else:
40                 for i in range(0,n):
41                     stars.append([random.randrange(-WIDTH*span,WIDTH*span),
42                                   random.randrange(-HEIGHT*span,HEIGHT*span),
43                                   random.randrange(1,dist)])
44
45         fg = (255,255,255)
46
47         self.tr()
48         self.td(gui.Label("Phil's Pygame GUI",color=fg),colspan=2)
49         
50         self.tr()
51         self.td(gui.Label("Speed: ",color=fg),align=1)
52         e = gui.HSlider(100,-500,500,size=20,width=100,height=16,name='speed')
53         self.td(e)
54         
55         self.tr()
56         self.td(gui.Label("Size: ",color=fg),align=1)
57         e = gui.HSlider(2,1,5,size=20,width=100,height=16,name='size')
58         self.td(e)
59         
60         self.tr()
61         self.td(gui.Label("Quantity: ",color=fg),align=1)
62         e = gui.HSlider(100,1,1000,size=20,width=100,height=16,name='quantity')
63         e.connect(gui.CHANGE, stars_changed, e)
64         self.td(e)
65         
66         self.tr()
67         self.td(gui.Label("Color: ",color=fg),align=1)
68         
69         
70         default = "#ffffff"
71         color = gui.Color(default,width=64,height=10,name='color')
72         color_d = ColorDialog(default)
73
74         color.connect(gui.CLICK,color_d.open,None)
75         self.td(color)
76         def update_col():
77             color.value = color_d.value
78         color_d.connect(gui.CHANGE,update_col)
79         
80         btn = gui.Switch(value=False,name='fullscreen')
81         btn.connect(gui.CHANGE, fullscreen_changed, btn)
82
83         self.tr()
84         self.td(gui.Label("Full Screen: ",color=fg),align=1)
85         self.td(btn)
86         
87         self.tr()
88         self.td(gui.Label("Warp Speed: ",color=fg),align=1)
89         self.td(gui.Switch(value=False,name='warp'))
90         
91
92 ##Using App instead of Desktop removes the GUI background.  Note the call to app.init()
93 ##::
94
95 form = gui.Form()
96
97 app = gui.App()
98 starCtrl = StarControl()
99
100 c = gui.Container(align=-1,valign=-1)
101 c.add(starCtrl,0,0)
102
103 app.init(c)
104 ##
105
106 dist = 8192
107 span = 10
108 stars = []
109 def reset():
110     global stars
111     stars = []
112     for i in range(0,form['quantity'].value):
113         stars.append([random.randrange(-WIDTH*span,WIDTH*span),
114                       random.randrange(-HEIGHT*span,HEIGHT*span),
115                       random.randrange(1,dist)])
116         
117
118 def render(dt):
119     speed = form['speed'].value*10
120     size = form['size'].value
121     color = form['color'].value
122     warp = form['warp'].value
123
124     colors = []
125     for i in range(256,0,-1):
126         colors.append((color[0]*i/256,color[1]*i/256,color[2]*i/256))
127         
128     n = 0
129     for x,y,z in stars:
130         if warp:
131             z1 = max(1,z + speed*2)
132             x1 = x*256/z1
133             y1 = y*256/z1
134             xx1,yy1 = x1+WIDTH/2,y1+HEIGHT/2
135     
136         x = x*256/z
137         y = y*256/z
138         xx,yy = x+WIDTH/2,y+HEIGHT/2
139         c = min(255,z * 255 / dist)
140         col = colors[int(c)]
141
142         if warp:
143             pygame.draw.line(screen,col,
144                              (int(xx1),int(yy1)),
145                              (int(xx),int(yy)),size)
146         
147         pygame.draw.circle(screen,col,(int(xx),int(yy)),size)
148         
149         ch = 0
150         z -= speed*dt
151         if z <= 0: 
152             ch = 1
153             z += dist
154         if z > dist: 
155             ch = 1
156             z -= dist
157         if ch:
158             stars[n][0] = random.randrange(-WIDTH*span,WIDTH*span)
159             stars[n][1] = random.randrange(-HEIGHT*span,HEIGHT*span)
160         stars[n][2] = z
161         
162         n += 1
163         
164
165 ##You can include your own run loop.
166 ##::
167 reset()
168 clock = pygame.time.Clock()
169 done = False
170 while not done:
171     for e in pygame.event.get():
172         if e.type is QUIT: 
173             done = True
174         elif e.type is KEYDOWN and e.key == K_ESCAPE: 
175             done = True
176         else:
177             app.event(e)
178
179     # Clear the screen and render the stars
180     dt = clock.tick(FPS)/1000.0
181     screen.fill((0,0,0))
182     render(dt)
183     app.paint()
184     pygame.display.flip()
185
186

UCC git Repository :: git.ucc.asn.au