Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Raspberry Pi Projects for Kids

You're reading from  Raspberry Pi Projects for Kids

Product type Book
Published in Mar 2014
Publisher
ISBN-13 9781783982226
Pages 96 pages
Edition 1st Edition
Languages
Author (1):
Daniel Leonard Bates Daniel Leonard Bates
Profile icon Daniel Leonard Bates

The keyboard version


If you do not have access to the components necessary to create your own controller, here is a slightly modified program that uses the keyboard instead. You might notice that its structure is exactly the same as the previous program. Separating tasks out into different functions allows us to make changes like these quickly and easily.

import pygame, pygame.event, pygame.key
from pygame.locals import *
import random
import time

options = {K_UP:"up", K_DOWN:"down", K_LEFT:"left", K_RIGHT:"right"}

def prepare():
    pygame.init()
    screen = pygame.display.set_mode((250, 1))
    pygame.display.set_caption("Test your speed!")

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

def keypressed():
    pygame.event.pump()
    keyspressed = pygame.key.get_pressed()
    for key in options.keys():
        if keyspressed[key]:
            return key
    else:
        return None

def play(duration):
    prepare()

  ...
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}