Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Raspberry Pi Projects for Kids (Second Edition)

You're reading from   Raspberry Pi Projects for Kids (Second Edition) Leverage the power of programming to use the Raspberry Pi to create awesome games

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher
ISBN-13 9781785281525
Length 146 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Daniel Leonard Bates Daniel Leonard Bates
Author Profile Icon Daniel Leonard Bates
Daniel Leonard Bates
Arrow right icon
View More author details
Toc

Coding the game

Here's a quick recap of how this example game is going to work. The Raspberry Pi will choose a random button and ask the player to press it. Every time the player presses the correct button, they get a point, and every time they press a wrong button, they lose a point. Once the correct button has been pressed, the Raspberry Pi selects a new button as the target. The aim is to score as many points as possible in 30 seconds.

In Python 2, navigate to File | New Window. This will bring up a new empty Code Editor window, which is where all our code will go.

Random behavior

The first job, then, is to write some code that will choose a random button for the player to press. Take a look at the following code snippet:

import random
options = [22, 23, 24, 25]

def nexttarget():
    target = random.choice(options)
    print target
    return target

In the first line of the code, we import the random module, which we saw in the previous chapter, Chapter 4, Creating Random Insults.

In...

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 €18.99/month. Cancel anytime