3 # This is not needed if you have PGU installed
5 sys.path.insert(0, "..")
11 from pgu import gui, timer
14 class DrawingArea(gui.Widget):
15 def __init__(this, width, height):
16 gui.Widget.__init__(this, width=width, height=height)
17 this.imageBuffer = pygame.Surface((width, height))
19 def paint(this, surf):
20 # Paint whatever has been captured in the buffer
21 surf.blit(this.imageBuffer, (0, 0))
23 # Call this function to take a snapshot of whatever has been rendered
24 # onto the display over this widget.
25 def save_background(this):
26 disp = pygame.display.get_surface()
27 this.imageBuffer.blit(disp, this.get_abs_rect())
29 class TestDialog(gui.Dialog):
31 title = gui.Label("Some Dialog Box")
32 label = gui.Label("Close this window to resume.")
33 gui.Dialog.__init__(this, title, label)
35 class MainGui(gui.Desktop):
42 def __init__(this, disp):
43 gui.Desktop.__init__(this)
45 # Setup the 'game' area where the action takes place
46 this.gameArea = DrawingArea(disp.get_width(),
49 this.menuArea = gui.Container(
50 height=disp.get_height()-this.gameAreaHeight)
52 tbl = gui.Table(height=disp.get_height())
63 tbl = gui.Table(vpadding=5, hpadding=2)
71 btn = gui.Button("Modal dialog", height=50)
72 btn.connect(gui.CLICK, dialog_cb)
75 # Add a button for pausing / resuming the game clock
77 if (this.engine.clock.paused):
82 btn = gui.Button("Pause/resume clock", height=50)
83 btn.connect(gui.CLICK, pause_cb)
86 # Add a slider for adjusting the game clock speed
89 timeLabel = gui.Label("Clock speed")
94 slider = gui.HSlider(value=23,min=0,max=100,size=20,height=16,width=120)
97 this.engine.clock.set_speed(slider.value/10.0)
99 slider.connect(gui.CHANGE, update_speed)
106 this.menuArea.add(tbl, 0, 0)
108 def open(this, dlg, pos=None):
109 # Gray out the game area before showing the popup
110 rect = this.gameArea.get_abs_rect()
111 dark = pygame.Surface(rect.size).convert_alpha()
112 dark.fill((0,0,0,150))
113 pygame.display.get_surface().blit(dark, rect)
114 # Save whatever has been rendered to the 'game area' so we can
115 # render it as a static image while the dialog is open.
116 this.gameArea.save_background()
117 # Pause the gameplay while the dialog is visible
118 running = not(this.engine.clock.paused)
120 gui.Desktop.open(this, dlg, pos)
121 while (dlg.is_open()):
122 for ev in pygame.event.get():
124 rects = this.update()
126 pygame.display.update(rects)
131 def get_render_area(this):
132 return this.gameArea.get_abs_rect()
135 class GameEngine(object):
136 def __init__(this, disp):
138 this.square = pygame.Surface((400,400)).convert_alpha()
139 this.square.fill((255,0,0))
140 this.app = MainGui(this.disp)
141 this.app.engine = this
143 # Pause the game clock
147 # Resume the game clock
151 def render(this, dest, rect):
152 # Draw a rotating square
153 angle = this.clock.get_time()*10
154 surf = pygame.transform.rotozoom(this.square, angle, 1)
156 r.center = rect.center
157 dest.fill((0,0,0), rect)
158 this.disp.blit(surf, r)
160 def draw_clock(name, pt, radius, col, angle):
161 pygame.draw.circle(dest, col, pt, radius)
162 pygame.draw.line(dest, (0,0,0), pt,
163 (pt[0]+radius*math.cos(angle),
164 pt[1]+radius*math.sin(angle)), 2)
165 tmp = this.font.render(name, True, (255,255,255))
166 dest.blit(tmp, (pt[0]-radius, pt[1]+radius+5))
168 # Draw the real time clock
169 angle = this.clock.get_real_time()*2*math.pi/10.0
170 draw_clock("Real time", (30,30), 25, (255,200,100), angle)
172 # Now draw the game clock
173 angle = this.clock.get_time()*2*math.pi/10.0
174 draw_clock("Game time", (90,30), 25, (255,100,255), angle)
180 pygame.display.flip()
182 this.font = pygame.font.SysFont("", 16)
184 this.clock = timer.Clock() #pygame.time.Clock()
188 for ev in pygame.event.get():
189 if (ev.type == pygame.QUIT or
190 ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE):
193 # Pass the event off to pgu
196 rect = this.app.get_render_area()
198 this.disp.set_clip(rect)
199 lst = this.render(this.disp, rect)
207 # Give pgu a chance to update the display
208 lst = this.app.update()
211 pygame.display.update(updates)
216 disp = pygame.display.set_mode((800, 600))
217 eng = GameEngine(disp)