.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_gallery_event_handling_pong_sgskip.py:
====
Pong
====
A small game demo using Matplotlib.
.. only:: builder_html
This example requires :download:`pipong.py `
.. code-block:: default
import time
import matplotlib.pyplot as plt
import pipong
fig, ax = plt.subplots()
canvas = ax.figure.canvas
animation = pipong.Game(ax)
# disable the default key bindings
if fig.canvas.manager.key_press_handler_id is not None:
canvas.mpl_disconnect(fig.canvas.manager.key_press_handler_id)
# reset the blitting background on redraw
def handle_redraw(event):
animation.background = None
# bootstrap after the first draw
def start_anim(event):
canvas.mpl_disconnect(start_anim.cid)
def local_draw():
if animation.ax.get_renderer_cache():
animation.draw(None)
start_anim.timer.add_callback(local_draw)
start_anim.timer.start()
canvas.mpl_connect('draw_event', handle_redraw)
start_anim.cid = canvas.mpl_connect('draw_event', start_anim)
start_anim.timer = animation.canvas.new_timer()
start_anim.timer.interval = 1
tstart = time.time()
plt.show()
print('FPS: %f' % (animation.cnt/(time.time() - tstart)))
.. _sphx_glr_download_gallery_event_handling_pong_sgskip.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: pong_sgskip.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: pong_sgskip.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
Keywords: matplotlib code example, codex, python plot, pyplot
`Gallery generated by Sphinx-Gallery
`_