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

A real tune


So, we've had fun creating random tunes; now let's try making something a little more structured. Open a fresh workspace by clicking on one of the Workspace tabs underneath the Code Editor. This is where we'll create our new tune. You can switch back to your previous program any time by clicking on its tab.

In this section, we're going to build on the same piece of code, getting the sound we produce closer and closer to a tune you might know. How far will you get before you recognize the tune? A complete code listing is at the end of this chapter just in case you get stuck.

Here's the very first version of the code— you might recognize it already!

  play :G
  sleep 1
  play :G
  sleep 1
  play :G
  sleep 1
  play :Eb
  sleep 1
  play :Bb
  sleep 1
  play :G
  sleep 1
  play :Eb
  sleep 1
  play :Bb
  sleep 1
  play :G
  sleep 1

This code is simple but long. It's very easy to miss a line, and it's boring to write sleep 1 so many times. Let's tidy it up a little by writing a function...

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