Pong
一个使用Matplotlib的小游戏演示。
此示例需要pipong.py
import timeimport matplotlib.pyplot as pltimport pipongfig, ax = plt.subplots()canvas = ax.figure.canvasanimation = pipong.Game(ax)# disable the default key bindingsif 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 redrawdef handle_redraw(event):animation.background = None# bootstrap after the first drawdef start_anim(event):canvas.mpl_disconnect(start_anim.cid)def local_draw():if animation.ax._cachedRenderer: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 = 1tstart = time.time()plt.show()print('FPS: %f' % (animation.cnt/(time.time() - tstart)))
