3 <title>tutorial on how to load, display, and use Sprites</title>
4 <style type='text/css'><!--
6 border: 1px solid black;
41 .wiki h1, .wiki h2, .wiki h3, .wiki h4, .wiki h5, .wiki h6 {
45 border-bottom: 1px solid #bbbbbb;
73 .wiki code, .wiki pre {
76 border: 1px dashed #888888;
81 border-top: 1px solid #888888;
82 border-left: 1px solid #888888;
83 border-spacing: 0px; border-collapse: collapse;
86 border-bottom: 1px solid #888888;
87 border-right: 1px solid #888888;
92 border-bottom: 1px solid #888888;
93 border-right: 1px solid #888888;
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
111 <hr width=500 align=center>
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>
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>
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>
140 <strong>GUI 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>
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>
154 <strong>Forms</strong><br>
155 <a href="gui.form.html">form</a>
156 | <a href="gui.group.html">group</a>
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>
167 <strong>Other</strong><br>
168 <a href="gui.menus.html">menus</a>
169 | <a href="gui.dialog.html">dialog</a>
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>
184 <td valign=top style='padding-left:6px;'>
186 <h1>tutorial on how to load, display, and use Sprites</h1>
193 Here are the various functions I used for the player and enemy logic.
194 - After creating a Sprite, I set the clayer to 0 so that more than one player / enemy is created.
195 - the Sprite must be added to the sprites list
196 - instead of using class methods, I prefer to use set functions for
197 the various methods -- loop and hit.
198 - Enemies are removed when they go off screen.
200 23:def player_new(g,t,value):
201 24: g.clayer[t.ty][t.tx] = 0
202 25: s = tilevid.Sprite(g.images['player'],t.rect)
203 26: g.sprites.append(s)
204 27: s.loop = player_loop
206 29:def player_loop(g,s):
207 30: s.rect.x += SPEED
209 32: keys = pygame.key.get_pressed()
211 34: if keys[K_UP]: dy-=1
212 35: if keys[K_DOWN]: dy+=1
213 36: if keys[K_LEFT]: dx-=1
214 37: if keys[K_RIGHT]: dx+=1
217 40: s.rect.clamp_ip(g.view)
219 42:def enemy_new(g,t,value):
220 43: g.clayer[t.ty][t.tx] = 0
221 44: s = tilevid.Sprite(g.images['enemy'],t.rect)
222 45: g.sprites.append(s)
223 46: s.loop = enemy_loop
225 48:def enemy_loop(g,s):
226 49: if s.rect.right < g.view.left:
227 50: g.sprites.remove(s)
229 Here I initialize the image data. The columns are (name,file_name,shape)
232 55: ('player','player.tga',(4,4,24,24)),
233 56: ('enemy','enemy.tga',(4,4,24,24)),
234 57: ('shot','shot.tga',(1,2,6,4)),
237 Here I initialize the code data. The columns are (function, config).
240 63: 1:(player_new,None),
241 64: 2:(enemy_new,None),
242 65: 3:(enemy_new,None),
243 66: 4:(enemy_new,None),
246 In init(), loading in the sprite images.
248 81: g.load_images(idata)
250 In init(), running the codes for the initial screen.
252 86: g.run_codes(cdata,(0,0,25,17))
254 In run(), each frame I make sure to run the codes that are on the far
257 109: g.run_codes(cdata,(g.view.right/TW,0,1,17))
264 <hr width=500 align=center>
265 <div align='center'>all content (c) 2006 Phil Hassey - <a href='http://www.imitationpickles.org/pgu/'>Phil's pyGame Utilities</a></div>