Skip to content

Commit 81afdc4

Browse files
committed
projects/asteroids: Port to pyglet 2.0, avoid direct OpenGL calls
1 parent 70cea50 commit 81afdc4

File tree

1 file changed

+16
-30
lines changed

1 file changed

+16
-30
lines changed

lessons/projects/asteroids/index.md

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -157,22 +157,15 @@ První krok bude naprogramovat vesmírnou loď, která půjde ovládat klávesni
157157
def draw():
158158
window.clear()
159159

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
172165
```
173166
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).
176169

177170
Povedlo se? Můžeš létat vesmírem?
178171
Č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.
230223
Každý objekt bude potřebovat mít poloměr – atribut `radius`.
231224
* Aby bylo vidět co si hra o objektech „myslí”,
232225
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):
238228

239229
```python
240230
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()
252234
```
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č.
253239
* Když asteroid narazí do lodi, loď exploduje a zmizí.
254240
Explozi necháme na později, teď je důležité odebrání objektu ze hry.
255241
Dej ho do metody `SpaceObject.delete`,

0 commit comments

Comments
 (0)