15.01.2016 Views

YOUR OFFICIAL RASPBERRY PI MAGAZINE

6014BX9nm

6014BX9nm

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

THE STORY TRAIN<br />

Tutorial<br />

StoryTrain.py<br />

import pygame, time, os<br />

import wiringpi2 as io<br />

from tkFileDialog import askopenfilename<br />

pygame.init()<br />

# initialise graphics<br />

interface<br />

pygame.mixer.quit()<br />

pygame.mixer.init(frequency=22050, size=-16, channels=2,<br />

buffer=512)<br />

os.environ['SDL_VIDEO_WINDOW_POS'] = 'center'<br />

pygame.display.set_caption("The Story Train")<br />

pygame.event.set_allowed(None)<br />

pygame.event.set_allowed([pygame.KEYDOWN,pygame.QUIT])<br />

screen = pygame.display.set_mode([600,400],0,32)<br />

try :<br />

io.wiringPiSetupGpio()<br />

except :<br />

print"start IDLE with 'gksudo idle' from command line"<br />

os._exit(1)<br />

pinList= [2,3] # G<strong>PI</strong>O track sensing switches<br />

def main():<br />

global pageList, pagesInStory<br />

initG<strong>PI</strong>O()<br />

print"The Story Train"<br />

while True:<br />

notFinished = True<br />

page = 1<br />

getStory() # get the story to show<br />

showPage(0) # beginning picture<br />

while notFinished:<br />

holdUntilSwitch(1)<br />

# trigger halfway round display<br />

showPage(-1)<br />

holdUntilSwitch(0)<br />

# display page sound and picture<br />

showPage(page)<br />

page += 1<br />

if page >= pagesInStory :<br />

notFinished = False<br />

showPage(pagesInStory) # the end<br />

time.sleep(2.0)<br />

def initG<strong>PI</strong>O():<br />

for pin in range (0,len(pinList) ):<br />

io.pinMode(pinList[pin],0)<br />

io.pullUpDnControl(pinList[pin],2) # input enable<br />

pull-up<br />

def holdUntilSwitch(pin):<br />

global advance<br />

advance = False<br />

while (io.digitalRead(pinList[pin]) == 1) and (advance<br />

== False):<br />

checkForEvent()<br />

time.sleep(0.2) # let other apps have a look in<br />

return<br />

def showPage(page):<br />

pygame.draw.rect(screen, (0,0,0),<br />

(0,0,600,400),0)<br />

if page ==-1 :<br />

screen.blit(nextPicture,[0,0])<br />

nextSound.play()<br />

else :<br />

screen.<br />

blit(pagePictures[page],[0,0])<br />

pageSounds[page].play()<br />

pygame.display.update()<br />

time.sleep(0.4)<br />

def getStory(): # get the list of pages to show<br />

global pagePictures, pageSounds, pagesInStory,<br />

nextSound, nextPicture<br />

pathP = "nothing"<br />

pathS = "nothing"<br />

while not(os.path.exists(pathP) and (os.path.<br />

exists(pathS)) ):<br />

checkForEvent()<br />

filename = askopenfilename()<br />

path = os.path.dirname(filename)<br />

pathP = path + "/pages/"<br />

pathS = path + "/sounds/"<br />

pageList = [os.path.join(pathP,fn) for fn in next(os.<br />

walk(pathP))[2]]<br />

list.sort(pageList) # put in alphabetical order<br />

pagePictures = [ pygame.image.load(pageList[frame]).<br />

convert_alpha()<br />

for frame in range(1,len(pageList))]<br />

soundList = [os.path.join(pathS,fn) for fn in next(os.<br />

walk(pathS))[2]]<br />

list.sort(soundList) # put in alphabetical order<br />

pageSounds = [ pygame.mixer.Sound(soundList[frame])<br />

for frame in range(1,len(soundList))]<br />

pagesInStory = len(pagePictures)-1<br />

nextSound = pygame.mixer.Sound(path+"/nextComing.wav")<br />

nextPicture = pygame.image.load(path+"/nextComing.<br />

jpg").convert_alpha()<br />

def terminate(): # close down the program<br />

print ("Closing down please wait")<br />

pygame.mixer.quit()<br />

pygame.quit() # close pygame<br />

os._exit(1)<br />

def checkForEvent(): # see if we need to quit<br />

global advance<br />

event = pygame.event.poll()<br />

if event.type == pygame.QUIT :<br />

terminate()<br />

if event.type == pygame.KEYDOWN :<br />

if event.key == pygame.K_ESCAPE :<br />

terminate()<br />

if event.key == pygame.K_RIGHT :<br />

advance = True<br />

# Main program logic:<br />

if __name__ == '__main__':<br />

main()<br />

Language<br />

>PYTHON 2.7<br />

DOWNLOAD:<br />

magpi.cc/1NqJjmV<br />

PROJECT<br />

VIDEOS<br />

Check out Mike’s<br />

Bakery videos at:<br />

magpi.cc/1NqJnTz<br />

raspberrypi.org/magpi January 2016<br />

57

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!