Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Raspberry Pi cookbook for Python programmers

You're reading from  Raspberry Pi cookbook for Python programmers

Product type Book
Published in Apr 2014
Publisher
ISBN-13 9781849696623
Pages 402 pages
Edition 1st Edition
Languages

Table of Contents (18) Chapters

Raspberry Pi Cookbook for Python Programmers
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Getting Started with a Raspberry Pi Computer Starting with Python Strings, Files, and Menus Using Python for Automation and Productivity Creating Games and Graphics Creating 3D Graphics Using Python to Drive Hardware Sense and Display Real-world Data Creating Projects with the Raspberry Pi Camera Module Building Robots Interfacing with Technology Hardware and Software List Index

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.

Control the bat to aim the ball at the bricks

Getting ready

This example requires a screen and keyboard attached to the Raspberry Pi or X11 Forwarding and X server.

How to do it…

Create the following script, bouncingball.py:

#!/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

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...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}