Tutorial 5: Arrays

Wow, we're really powering through! In this tutorial, we will cover arrays. What they are, how to use them and why we should.

Arrays are incredible and powerful tools for programming. In fact, almost every video game in the world uses many arrays to keep track of everything it needs.

An array is a table of variables. We can give each position in the table a value and use it later in the program.

Arrays are used for a huge variety of things, from inventories to maps to putting stars in your space background, but in this first tutorial we'll be creating a simple fortune teller game.

A fortune teller needs to have a selection of answers stored so we can randomly choose one to give the player.

Before we write our selection of answers, we must create our array. To do this, we must use the word array. Who would have thought?!

Type the following line into the FUZE4 Nintendo Switch code editor:

  1. array answers[4]

This line sets up an empty array. Think of this like an empty chest of drawers. Notice the square brackets in this line. When we create or access an array, we always use square brackets. Because we put a 4 in the square brackets, we have 4 places to store things.

We have called our array "answers". This can of course be anything you like, but as always it's good to name our variables and arrays as things which make sense.

Next up, we're going to store some information in the elements to our array. In each of these elements, we can store something. We will store a variety of statements.

  1. array answers[4]
  2. answers[0] = "It is certain!"
  3. answers[1] = "It does not look good..."
  4. answers[2] = "You might be in luck!"
  5. answers[3] = "Definitely not."

Edit your code so that it looks like the program above. Feel free to copy and paste the code if you don't feel like typing!

The program above now stores the four different text answers we have into the four elements of our array. These are labeled as element 0, 1, 2 and 3.

The image below might help you picture this in your mind:

As you can see, each position in the table is labeled with a number (0 - 3). Because of this, we can easily access any element and use the information.

There is another way to lay out our array too, and the results are exactly the same. See below:

 1. answers = [
 2.     "It is certain!",
 3.     "It does not look good...",
 4.     "You might be in luck!",
 5.     "Definitely not."
 6. ]

There is no difference whatsoever in the result. It's all about which ever you find easiest to understand!

Below we've made a few additions to the program. Add the lines 6 to 19 and run it.

  1. array answers[4]
  2. answers[0] = "It is certain!"
  3. answers[1] = "It does not look good..."
  4. answers[2] = "You might be in luck!"
  5. answers[3] = "Definitely not."
  6. print( "Welcome to the fortune teller, ask me a question! \n" )
  7. update()
  8. question = input( "Type your question here." )
  9. sleep( 1 )
 10. print( "Are you ready to know your fortune? \n" )
 11. update()
 12. sleep( 1 ) 
 13. print( "My answer to your question is... \n" )
 14. update()
 15. sleep( 1 )
 16. num = random( 4 )
 17. print( answers[num] ) 
 18. update()
 19. sleep( 2 )

Now we can play! With a couple of sleep commands, we can really ramp up the tension before we get our answer!

Accessing the array

In order to print one of our answers on the screen, we must access the array. Line 17 is where we do this.

Because we want to print a random answer, we use the random() function to choose a random number. We can then use this as an index into our array.

We store the random number in a variable called num and use that variable to access the array on line 17:

 17. print( answers[num] ) 

Challenge

Could you add some more answers to the array? You will need to create additional lines of code to do this. Try to add 3 more answers.

Hint: Don't forget to look at this line:

 16. num = random( 4 )

Re-cap

An array is a table of variables used to store information.

Every position in the table has a number which we can use to access the information stored there.

Arrays are used everywhere! A computer controls a screen using a gigantic array, where each element of the array is a single pixel.

Stay tuned for the next arrays project in which we will throw some shapes around the screen!

Functions and Keywords used in this tutorial

array, input(), print(), sleep(), update()