Creating a bat and ball game
A classic bat and ball game can be created using the drawing tools of canvas and by detecting the collisions of the objects. The user will be able to control the green paddle using the left and right cursor keys to aim the ball at the bricks and hit them until they have all been destroyed.
Getting ready
This example requires graphical output, so you must have a screen and keyboard attached to the Raspberry Pi or use X11 Forwarding and X server if connected remotely from another computer.
How to do it…
Create the following script, bouncingball.py
.
- First, import the
tkinter
andtime
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
- Next, create functions for closing the program, moving the paddle right and left, and for...