This is a great example, and just needs sound effects (and for the CPU
player not to cheat!). If you relicense the code under a BSD license
I can include it in the pyglet examples/ distribution.
Cheers
Alex.
hi Tristam,
this is a great example. i better understand states now. i found one
typo in the last line of main update function, though i don't think
that 'else' condition is ever met.
# update callback
def update(dt):
# update the topmost state, if we have any
if len(states):
states[-1].update(dt)
# otherwise quit
else:
app.exit()
the last line should be
pyglet.app.exit()
Also, I noticed there's a function to handle window resizing, but the window is not resizable. Adding the window attribute "resizable=True" makes it work as expected.
This change to the on_resize function will enforce a 4:3 viewport in the Pong example, thus allowing user resizing of the window without distortion:
def on_resize(width, height):
# prevent division by zero
if width == 0: width = 1
if height == 0: height = 1
# enforce 4:3 aspect ratio by reducing the excess dimension
if 1.0 * width / height > 4.0 / 3:
width = int(height * 4.0 / 3)
elif 1.0 * width / height < 4.0 / 3:
height = int(width * 3.0 / 4)
# center viewport in window
glViewport((window.width - width) // 2, (window.height - height) // 2, width, height)
Nice work! I was thinking of adapting the drawing code to accomplish the same effect, but that is far simpler. Mind if I include that change in the updated version next week?