@@ -157,22 +157,15 @@ První krok bude naprogramovat vesmírnou loď, která půjde ovládat klávesni
157
157
def draw ():
158
158
window.clear()
159
159
160
- for x_offset in (- window.width, 0 , window.width):
161
- for y_offset in (- window.height, 0 , window.height):
162
- # Remember the current state
163
- gl.glPushMatrix()
164
- # Move everything drawn from now on by (x_offset, y_offset, 0)
165
- gl.glTranslatef(x_offset, y_offset, 0 )
166
-
167
- # Draw
168
- batch.draw()
169
-
170
- # Restore remembered state (this cancels the glTranslatef)
171
- gl.glPopMatrix()
160
+ for x_offset in (- window.width, window.width, 0 ):
161
+ for y_offset in (- window.height, window.height, 0 ):
162
+ # Set the view matrix to offset draws
163
+ matrix = pyglet.math.Mat4.from_translation(x_offset, y_offset, 0 )
164
+ window.view = matrix
172
165
```
173
166
Pro přehled, dokumentace k použitým funkcím je tady:
174
- [ glPushMatrix, glPopMatrix ] ( https://www.opengl.org/sdk/docs/man2/xhtml/glPushMatrix.xml ) ,
175
- [ glTranslatef ] ( https://www.opengl.org/sdk/docs/man2/xhtml/glTranslate.xml ) .
167
+ [ window.view ] ( https://pyglet.readthedocs.io/en/latest/programming_guide/migration.html?highlight=matrix#window-projection-and-cameras ) ,
168
+ [ Mat4 ] ( https://pyglet.readthedocs.io/en/latest/modules/math.html#pyglet.math.Mat4 ) .
176
169
177
170
Povedlo se? Můžeš létat vesmírem?
178
171
Čas to všechno dát do Gitu!
@@ -230,26 +223,19 @@ Naše asteroidy jsou zatím docela neškodné. Pojďme to změnit.
230
223
Každý objekt bude potřebovat mít poloměr – atribut ` radius ` .
231
224
* Aby bylo vidět co si hra o objektech „myslí”,
232
225
nakresli si nad každým objektem příslušné kolečko.
233
- Nejlepší je to udělat pomocí
234
- [ pyglet.gl] ( http://pyglet.readthedocs.org/en/latest/programming_guide/gl.html )
235
- a trochy matematiky; pro teď si jen opiš funkci
236
- ` draw_circle ` a pro každý objekt ji zavolej.
237
- Až to bude všechno fungovat, můžeš funkci dát pryč.
226
+ Pyglet na to má třídu ` Circle ` v modulu
227
+ [ ` pyglet.shapes ` ] ( https://pyglet.readthedocs.io/en/latest/programming_guide/shapes.html ) :
238
228
239
229
``` python
240
230
def draw_circle (x , y , radius ):
241
- iterations = 20
242
- s = math.sin(2 * math.pi / iterations)
243
- c = math.cos(2 * math.pi / iterations)
244
-
245
- dx, dy = radius, 0
246
-
247
- gl.glBegin(gl.GL_LINE_STRIP )
248
- for i in range (iterations+ 1 ):
249
- gl.glVertex2f(x+ dx, y+ dy)
250
- dx, dy = (dx* c - dy* s), (dy* c + dx* s)
251
- gl.glEnd()
231
+ circle = shapes.Circle(x = x, y = y, radius = radius)
232
+ circle.opacity = 120 # (ne)průhlednost, od 0 (průhledné) do 255 (plné)
233
+ circle.draw()
252
234
```
235
+ Pro zrychlení můžeš kolečka přidat do ` batch ` a vykreslovat společně se
236
+ zbytkem vesmíru.
237
+
238
+ Až to bude všechno fungovat, můžeš kolečka dát pryč.
253
239
* Když asteroid narazí do lodi, loď exploduje a zmizí.
254
240
Explozi necháme na později, teď je důležité odebrání objektu ze hry.
255
241
Dej ho do metody ` SpaceObject.delete ` ,
0 commit comments