ARGH
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / docs / tilevid5.html
1 <html>
2 <head>
3 <title>tutorial on how to add Sprite collision hit handlers, custom painting</title>
4 <style type='text/css'><!--
5 .sections { 
6         border: 1px solid black;
7         background: #fef790;
8         margin-left: 8px;
9         }
10         
11 .sections th {
12         background: #fde42d; 
13         }
14         
15 .sections .h1 {
16         padding: 2px;
17         padding-left: 0px;
18         }
19 .sections .h2 {
20         padding: 2px;
21         padding-left: 16px;
22         }
23 .sections .h3 {
24         padding: 2px;
25         padding-left: 32px;
26         }
27 .sections .h4 {
28         padding: 2px;
29         padding-left: 48px;
30         }
31         
32 .sections .h5 {
33         padding: 2px;
34         padding-left: 64px;
35         }
36 .sections .h6 {
37         padding: 2px;
38         padding-left: 72px;
39         }
40
41 .wiki h1, .wiki h2, .wiki h3, .wiki h4, .wiki h5, .wiki h6 {
42         border: 0px;
43         padding: 0px;
44         background: none;
45         border-bottom: 1px solid #bbbbbb;
46
47         }
48         
49 .wiki div.h1 {
50     }
51 .wiki div.h2 {
52     }
53     
54 .wiki dt {
55     font-style: italic;
56     }
57     
58 .wiki dd,dd,p.dd {
59     font-style: normal;
60     margin-left: 48px;
61     margin-top: 0px;
62     margin-bottom: 8px;
63     display: block;
64     }
65     
66 .wiki h3 {
67     margin-left: 48px; 
68     }
69 .wiki div.h3 {
70     margin-left: 48px;
71     }
72         
73 .wiki code, .wiki pre {
74     background: #eeeeee;
75     padding: 4px;
76     border: 1px dashed #888888;
77     }
78     
79     
80 .wiki table {
81     border-top: 1px solid #888888;
82     border-left: 1px solid #888888;
83     border-spacing: 0px; border-collapse: collapse;
84     }
85 .wiki td {
86     border-bottom: 1px solid #888888;
87     border-right: 1px solid #888888;
88     padding: 3px;
89     }
90 .wiki th {
91     background: #eeeeee;
92     border-bottom: 1px solid #888888;
93     border-right: 1px solid #888888;
94     padding: 3px;
95     }
96
97 -->
98 </style>
99
100 </head>
101 <body>
102 <div align='center'>
103
104 <table cellpadding=8><tr><td>
105 <a href='http://www.imitationpickles.org/pgu/'><img src='skin/logo.gif' alt="Phil's pyGame Utilities" border=0></a>
106 <td style='font-size:48px'>Documentation
107 </table>
108
109 </div>
110
111 <hr width=500 align=center>
112
113 <table width='100%'><tr><td valign=top width=120 style='padding-right:6px;border-right: 1px solid black;'>
114 <a href='index.html'>Overview</a><br><br>
115 <strong>Scripts</strong><br>
116 <a href="tileedit.html">tileedit</a>
117  | <a href="leveledit.html">leveledit</a>
118  | <a href="tganew.html">tganew</a>
119  | <a href="levelfancy.html">levelfancy</a>
120 <br><br>
121 <strong>Reference</strong><br>
122 <a href="algo.html">algo</a>
123  | <a href="ani.html">ani</a>
124  | <a href="engine.html">engine</a>
125  | <a href="fonts.html">fonts</a>
126  | <a href="high.html">high</a>
127  | <a href="html.html">html</a>
128  | <a href="layout.html">layout</a>
129  | <a href="text.html">text</a>
130  | <a href="timer.html">timer</a>
131  | <a href="vid.html">vid</a>
132 <br><br>
133 <strong>Tutorials</strong><br>
134 <a href="tilevid1.html">1</a>
135  | <a href="tilevid2.html">2</a>
136  | <a href="tilevid3.html">3</a>
137  | <a href="tilevid4.html">4</a>
138  | <a href="tilevid5.html">5</a>
139 <br><br>
140 <strong>GUI&nbsp;Ref.</strong><br>
141 <a href="gui.theme.html">theme</a>
142  | <a href="gui.style.html">style</a>
143  | <a href="gui.widget.html">widget</a>
144  | <a href="gui.surface.html">surface</a>
145  | <a href="gui.const.html">const</a>
146 <br><br>
147 <strong>Containers</strong><br>
148 <a href="gui.container.html">container</a>
149  | <a href="gui.app.html">app</a>
150  | <a href="gui.table.html">table</a>
151  | <a href="gui.document.html">document</a>
152  | <a href="gui.area.html">area</a>
153 <br><br>
154 <strong>Forms</strong><br>
155 <a href="gui.form.html">form</a>
156  | <a href="gui.group.html">group</a>
157 <br><br>
158 <strong>Widgets</strong><br>
159 <a href="gui.basic.html">basic</a>
160  | <a href="gui.button.html">button</a>
161  | <a href="gui.input.html">input</a>
162  | <a href="gui.keysym.html">keysym</a>
163  | <a href="gui.slider.html">slider</a>
164  | <a href="gui.select.html">select</a>
165  | <a href="gui.misc.html">misc</a>
166 <br><br>
167 <strong>Other</strong><br>
168 <a href="gui.menus.html">menus</a>
169  | <a href="gui.dialog.html">dialog</a>
170 <br><br>
171 <strong>Tutorials</strong><br>
172 <a href="gui1.html">1</a>
173  | <a href="gui2.html">2</a>
174  | <a href="gui3.html">3</a>
175  | <a href="gui4.html">4</a>
176  | <a href="gui5.html">5</a>
177  | <a href="gui6.html">6</a>
178  | <a href="gui7.html">7</a>
179  | <a href="gui8.html">8</a>
180  | <a href="gui9.html">9</a>
181  | <a href="gui10.html">10</a>
182 <br><br>
183
184 <td valign=top style='padding-left:6px;'>
185
186 <h1>tutorial on how to add Sprite collision hit handlers, custom painting</h1>
187
188 <div class='wiki'>
189 <a name="">
190 <div class="h1">
191
192 </div>
193 In player_new() I add the shoot handler.
194 <pre>
195   28:    s.shoot = player_shoot
196 </pre>
197 In player_loop(), I check for the spacebar.  The spacebar triggers a shot every 8 frames.
198 <pre>
199   48:    if keys[K_SPACE] and g.frame%8==0:
200   49:        shot_new(g,s,None)
201 </pre>
202 The player_shoot() handler, as well as the shot Sprite functions.  The shot group
203 has its agroup set to 'enemy' so it can hit 'enemy' Sprites.
204 <pre>
205   55:def player_shoot(g,s):
206   56:    shot_new(g,s,None)
207   57:
208   58:def shot_new(g,t,value):
209   59:    s = tilevid.Sprite(g.images['shot'],(t.rect.right,t.rect.centery-2))
210   60:    g.sprites.append(s)
211   61:    s.agroups = g.string2groups('enemy')
212   62:    s.hit = shot_hit
213   63:    s.loop = shot_loop
214   64:
215   65:def shot_loop(g,s):
216   66:    s.rect.x += 8
217   67:    if s.rect.left > g.view.right:
218   68:        g.sprites.remove(s)
219 </pre>
220 In enemy_new(), I've added a lot more detail.
221 - A move function to handle the type of movement the enemy will do.
222 - A record of the origin and entering frame of the enemy (useful for the move functions.)
223 - Set up the groups and agroups and a hit handler for the enemy.
224 <pre>
225   86:    s.move = value['move']
226   87:    s.origin = pygame.Rect(s.rect)
227   88:    s.frame = g.frame
228   89:    s.groups = g.string2groups('enemy')
229   90:    s.agroups = g.string2groups('player')
230   91:    s.hit = enemy_hit
231 </pre>
232 When an enemy is hit, the game quits.
233 <pre>
234   96:def enemy_hit(g,s,a):
235   97:    g.quit = 1
236 </pre>
237 In enemy_loop() we call the move handler.
238 <pre>
239  103:    s.move(g,s)
240 </pre>
241 The enemy movement handlers.
242 <pre>
243  110:def enemy_move_line(g,s):
244  111:    s.rect.x -= 3
245  112:
246  113:def enemy_move_sine(g,s):
247  114:    s.rect.x -= 2
248  115:    s.rect.y = s.origin.y + 65*math.sin((g.frame-s.frame)/10.0)
249  116:
250  117:def enemy_move_circle(g,s):
251  118:    s.origin.x -= 1
252  119:    s.rect.y = s.origin.y + 50*math.sin((g.frame-s.frame)/10.0)
253  120:    s.rect.x = s.origin.x + 50*math.cos((g.frame-s.frame)/10.0)
254 </pre>
255
256 The codes data has been updated to include information about the appropriate
257 movement handlers for enemies.
258 <pre>
259  151:cdata = {
260  152:    1:(player_new,None),
261  153:    2:(enemy_new,{'move':enemy_move_line}),
262  154:    3:(enemy_new,{'move':enemy_move_sine}),
263  155:    4:(enemy_new,{'move':enemy_move_circle}),
264  156:    }
265 </pre>
266 In init(), set the g.view size so that all the handlers will work properly.  (The player_loop one depends on view having the correct size.)
267 <pre>
268  171:    g.view.w,g.view.h = SW,SH
269 </pre>
270 In init() I no longer have tga_load_level load the background layer, as
271 we will generate our own multi-layered starfield.
272 <pre>
273  181:    g.tga_load_level('level.tga')
274 </pre>
275 In run(), adding a pause variable to the game.
276 <pre>
277  195:    g.pause = 0
278 </pre>
279 In run(), initializing the stars.
280 <pre>
281  202:    stars = []
282  203:    NS = 256
283  204:    for n in range(0,NS):
284  205:        stars.append([random.randrange(0,SW),random.randrange(0,SH),random.randrange(2,8)])
285 </pre>
286 In run(), in the event loop, checking for F10 for full screen, RETURN for pause.
287 <pre>
288  214:            if e.type is KEYDOWN and e.key == K_F10:
289  215:                #g.screen = pygame.display.set_mode((SW,SH),FULLSCREEN|HWSURFACE|DOUBLEBUF)
290  216:                pygame.display.toggle_fullscreen()
291  217:
292  218:            if e.type is KEYDOWN and e.key == K_RETURN:
293  219:                g.pause ^= 1
294 </pre>
295 In run(), handles pause, and also renders the star field before the
296 foreground is painted.
297 <pre>
298  225:        if not g.pause:
299  226:            g.view.x += SPEED
300  227:            g.run_codes(cdata,(g.view.right/TW,0,1,17))
301  228:
302  229:            g.loop()
303  230:
304  231:            g.screen.fill((0,0,0))
305  232:            n = 0
306  233:            for n in range(0,NS):
307  234:                x,y,s = stars[n]
308  235:                if ((g.frame*s)%8) < s:
309  236:                    x -= 1
310  237:                if x < 0: x += SW
311  238:                stars[n][0] = x
312  239:                g.screen.set_at((x,y),(255,255,255))
313  240:
314  241:            g.paint(g.screen)
315  242:            img = g.font.render('%05d'%g.player.score,1,(0,0,0))
316  243:            g.screen.blit(img,(0+1,SH-img.get_height()+1))
317  244:            img = g.font.render('%05d'%g.player.score,1,(255,255,255))
318  245:            g.screen.blit(img,(0,SH-img.get_height()))
319  246:            pygame.display.flip()
320  247:
321  248:            g.frame += 1
322 </pre>
323
324 </div>
325
326 </table>
327
328 <hr width=500 align=center>
329 <div align='center'>all content (c) 2006 Phil Hassey - <a href='http://www.imitationpickles.org/pgu/'>Phil's pyGame Utilities</a></div>
330 </body>
331 </html>

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