How it works...

We create a Tkinter application that is 640 x 480 pixels and bind the <Right> and <Left> cursor keys to the move_right() and move_left() functions. We use root.protocol('WM_DELETE_WINDOW', close) to detect when the window is closed, so that we can cleanly exit the program (via close(), which sets RUNNING to False).

We then add a Canvas widget that will hold all our objects to the application. We create the following objects: top, left, right, and bottom. These make up our bounding sides for our game area. The canvas coordinates are 0,0 in the top-left corner and 640,480 in the bottom-right corner, so the start and end coordinates can be determined for each side (using canv.create_line(xStart, yStart, xEnd, yEnd)):

The coordinates ...

Get Raspberry Pi 3 Cookbook for Python Programmers - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.