How to do it...

Create the following script, bouncingball.py:

  1. First, import the tkinter and time modules, and define constants for the game graphics:
#!/usr/bin/python3 
# bouncingball.py 
import tkinter as TK 
import time 
 
VERT,HOREZ=0,1 
xTOP,yTOP = 0,1 
xBTM,yBTM = 2,3 
MAX_WIDTH,MAX_HEIGHT = 640,480 
xSTART,ySTART = 100,200 
BALL_SIZE=20 
RUNNING=True 
  1. Next, create functions for closing the program, moving the paddle right and left, and for calculating the direction of the ball:
def close(): global RUNNING RUNNING=False root.destroy() def move_right(event): if canv.coords(paddle)[xBTM]<(MAX_WIDTH-7): canv.move(paddle, 7, 0) def move_left(event): if canv.coords(paddle)[xTOP]>7: canv.move(paddle, -7, 0) def determineDir(ball,obj): global delta_x,delta_y ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.