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.
9 from pygame.locals import *
11 # the following line is not needed if pgu is installed
12 import sys; sys.path.insert(0, "..")
15 from gui7 import ColorDialog
17 # The maximum frame-rate
19 WIDTH,HEIGHT = 640,480
21 ##You can initialize the screen yourself.
23 screen = pygame.display.set_mode((640,480),SWSURFACE)
26 class StarControl(gui.Table):
27 def __init__(self,**params):
28 gui.Table.__init__(self,**params)
30 def fullscreen_changed(btn):
31 #pygame.display.toggle_fullscreen()
32 print("TOGGLE FULLSCREEN")
34 def stars_changed(slider):
35 n = slider.value - len(stars)
41 stars.append([random.randrange(-WIDTH*span,WIDTH*span),
42 random.randrange(-HEIGHT*span,HEIGHT*span),
43 random.randrange(1,dist)])
48 self.td(gui.Label("Phil's Pygame GUI",color=fg),colspan=2)
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')
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')
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)
67 self.td(gui.Label("Color: ",color=fg),align=1)
71 color = gui.Color(default,width=64,height=10,name='color')
72 color_d = ColorDialog(default)
74 color.connect(gui.CLICK,color_d.open,None)
77 color.value = color_d.value
78 color_d.connect(gui.CHANGE,update_col)
80 btn = gui.Switch(value=False,name='fullscreen')
81 btn.connect(gui.CHANGE, fullscreen_changed, btn)
84 self.td(gui.Label("Full Screen: ",color=fg),align=1)
88 self.td(gui.Label("Warp Speed: ",color=fg),align=1)
89 self.td(gui.Switch(value=False,name='warp'))
92 ##Using App instead of Desktop removes the GUI background. Note the call to app.init()
98 starCtrl = StarControl()
100 c = gui.Container(align=-1,valign=-1)
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)])
119 speed = form['speed'].value*10
120 size = form['size'].value
121 color = form['color'].value
122 warp = form['warp'].value
125 for i in range(256,0,-1):
126 colors.append((color[0]*i/256,color[1]*i/256,color[2]*i/256))
131 z1 = max(1,z + speed*2)
134 xx1,yy1 = x1+WIDTH/2,y1+HEIGHT/2
138 xx,yy = x+WIDTH/2,y+HEIGHT/2
139 c = min(255,z * 255 / dist)
143 pygame.draw.line(screen,col,
145 (int(xx),int(yy)),size)
147 pygame.draw.circle(screen,col,(int(xx),int(yy)),size)
158 stars[n][0] = random.randrange(-WIDTH*span,WIDTH*span)
159 stars[n][1] = random.randrange(-HEIGHT*span,HEIGHT*span)
165 ##You can include your own run loop.
168 clock = pygame.time.Clock()
171 for e in pygame.event.get():
174 elif e.type is KEYDOWN and e.key == K_ESCAPE:
179 # Clear the screen and render the stars
180 dt = clock.tick(FPS)/1000.0
184 pygame.display.flip()