Tutorial 1: Loops
Every programmer must start somewhere. In this tutorial, we'll be writing a version of perhaps the most famous program in existence: Hello World.
Before we jump right in, we need a couple of things. We'll need a print()
function to instruct FUZE4 Nintendo Switch what to print. We'll also need an update()
function to update the screen with the text we want and finally, we'll need a sleep()
function at the end so our program stays on screen!
Type (or copy and paste) the code below into the FUZE code editor. Run the program with +, or the F5 key if you're using a USB keyboard.
1. print( "Hello Nintendo" )
2. update()
3. sleep( 2 )
We should see our chosen text appear magnificently on screen for 2 seconds. Now let's make FUZE do this over and over again, using one of the most important concepts in programming: a loop. For this, we can remove the sleep()
function because our program will run continuously. We will still need the update()
though, because we want our text to appear on the screen!
1. loop
2. print( "Hello Nintendo" )
3. update()
4. repeat
Think of a loop like a sandwich. We have a top piece of bread (loop
), some filling in the middle and a bottom piece of bread (repeat
)! Without one of the pieces of bread, our sandwich is no longer a sandwich!
What I'm saying is...
Without loop
, repeat
will do nothing and vice-versa!
There is an important point when it comes to loops. Take a look below:
1. loop
2. print( "Hello Nintendo" )
3. update()
4. repeat
5. print( "What about me?" )
See the new print()
function on line 5? This line will never appear on screen. Why?
When FUZE gets to the repeat
keyword on line 4, it returns to the last loop
keyword. So, our program will read:
Line 1, line 2, line 3, Line 4... Line 1, line 2, line 3, Line 4... Line 1, line 2, line 3, Line 4...
FOREVER! Well... Not quite. It will run until we tell it stop. Press + to stop the program and return to the editor (use the F5 key if using a USB keyboard).
Line 5 is not in the loop
. To make this line happen too, our program must look like this:
1. loop
2. print( "Hello Nintendo" )
3. print( "What about me?" )
4. update()
5. repeat
Now the new print()
line is included inside the loop and is very happy indeed.
Okay. Let's get some colour going in our program. We use the ink()
function to do this.
1. ink( white )
2. loop
3. print( "Hello Nintendo" )
4. print( "What about me?" )
5. update()
6. repeat
We've used the name for the colours we want, but we can use numbers instead. Every colour is stored in a big database, each one with a number. We have 64 colours in total to choose from.
This means we can use a random selection for our colours. We'll need the random()
function to achieve this!
1. ink( random( 64 ) )
2. loop
3. print( "Hello Nintendo" )
4. print( "What about me?" )
5. update()
6. repeat
The brackets might look a little tricky here. Remember, the whole of random(64)
must go in the brackets for the ink()
line. It might look wrong at first, but we must have the same number of open and close brackets.
To change the colours, you will have to stop and start the program again. A little boring... Let's change that!
1. loop
2. ink( random( 64 ) )
3. print( "Hello Nintendo" )
4. print( "What about me?" )
5. update()
6. repeat
Now we've brought the colour line into the loop
! Can you predict what will happen?
This way, FUZE will set the random colour on every repetition of the loop
!
Let's say we wanted one colour for "Hello Nintendo" and another colour for "What about me?".
1. loop
2. ink( fuzepink )
3. print( "Hello Nintendo" )
4. ink( fuzeblue )
5. print( "What about me?" )
6. update()
7. repeat
In the example above, line 2 sets a colour for the print()
function on line 3. Then, line 4 sets a different colour for the print()
function on line 5. If we want something to affect a specific line, we must put the instructions before the chosen line.
Finally, let's change the size of our text with the textSize()
function:
1. textSize( 100 )
2. loop
3. ink( fuzepink )
4. print( "Hello Nintendo" )
5. ink( fuzeblue )
6. print( "What about me?" )
7. update()
8. repeat
textSize()
allows us to set the size of our text in pixels. With textSize(100)
the maximum height for our letters will be 100 pixels tall.
Congratulations! You've written the best program ever. It's all downhill from here.
Functions and Keywords used in this Tutorial