Pong

一个使用Matplotlib的小游戏演示。

此示例需要pipong.py

  1. import time
  2. import matplotlib.pyplot as plt
  3. import pipong
  4. fig, ax = plt.subplots()
  5. canvas = ax.figure.canvas
  6. animation = pipong.Game(ax)
  7. # disable the default key bindings
  8. if fig.canvas.manager.key_press_handler_id is not None:
  9. canvas.mpl_disconnect(fig.canvas.manager.key_press_handler_id)
  10. # reset the blitting background on redraw
  11. def handle_redraw(event):
  12. animation.background = None
  13. # bootstrap after the first draw
  14. def start_anim(event):
  15. canvas.mpl_disconnect(start_anim.cid)
  16. def local_draw():
  17. if animation.ax._cachedRenderer:
  18. animation.draw(None)
  19. start_anim.timer.add_callback(local_draw)
  20. start_anim.timer.start()
  21. canvas.mpl_connect('draw_event', handle_redraw)
  22. start_anim.cid = canvas.mpl_connect('draw_event', start_anim)
  23. start_anim.timer = animation.canvas.new_timer()
  24. start_anim.timer.interval = 1
  25. tstart = time.time()
  26. plt.show()
  27. print('FPS: %f' % (animation.cnt/(time.time() - tstart)))

下载这个示例