Level 2 Tutorial
Level 2 Tutorial
GraphicsWindow.DrawImage("https://aka.ms/mz1", 0, 0)
Press and see the a maze for the turtle to run through! Let’s teleport the turtle to the
top left of the window, so we can start at the beginning of the maze. You know how we used 0s
to make the image start at the top left corner? We’re going to do something similar with the
turtle, but we’re going to set it down a little bit away from the top left corner. Let’s use 50
instead of 0. We’re going to make sure the turtle is 50 pixels away from the left edge:
Turtle.X = 50
and 50 pixels away from the top:
Turtle.Y = 50
Turtle.TurnRight()
Turtle.Move(100)
Click the button at the top and see what the turtle does.
Oh no, it didn’t go far enough! Can you change how far the turtle moves until it looks like this?
Great! Now the turtle is ready to go down the next path. What does it have to do now? It has to
turn before it can keep going! Add another line of code at the bottom:
Turtle.TurnRight()
And then run it with the button to make sure the turtle is facing the correct direction.
Keep going- make the turtle move through the whole maze until it reaches the red flowers at
the bottom left!
CHALLENGE: Can you make the turtle do a celebration dance at the end?
Want to do something a little different? Here are some things you can try! Where do you think
you would put them in the code?
We’re going to do something any time a keyboard key is pressed down. Any time that happens,
we want to run a subroutine (it’s like a little program within our bigger program) that will
handle what we do with that keyboard key.
Here’s how we do it:
GraphicsWindow.KeyDown = WhenKeyPressed
Sub WhenKeyPressed
EndSub
Any code between Sub and EndSub is our subroutine, and it’ll run any time a keyboard key is
pressed down. It doesn’t do anything yet so let’s start by making the turtle move forward when
we press the “Up” arrow. To do this we want to add some code in the subroutine to check
which key was pressed, and then we want to do what the user wants that key to do. So, for
example, if the key was the “Up” arrow, we want the turtle to move forward.
Sub WhenKeyPressed
If GraphicsWindow.LastKey = "Up" Then
Turtle.Move(25)
EndIf
EndSub
Run this with the button to see if you have it all working! (If your turtle is moving
because of the code you wrote from steps 1-4, you can delete that code now).
Great! Any time the “Up” arrow is pressed, the turtle moves forward a bit! Now we should
allow the user to make the turtle turn using the left and right arrows. Can you figure out how to
do that?
Sub WhenKeyPressed
If GraphicsWindow.LastKey = "Up" Then
Turtle.Move(25)
EndIf
If GraphicsWindow.LastKey = "Right" Then
What do we put here?
EndIf
If GraphicsWindow.LastKey = "Left" Then
What do we put here?
EndIf
EndSub
Once you have that completed, press the button again and finish the maze yourself!
CHALLENGE: Write your name with the turtle!
Lesson 6: Winner, Winner!
Now we can easily guide the turtle to the end, but wouldn’t it be nice if our hard work was
rewarded? Small Basic will allow us to draw a box on the screen and then we can detect if the
turtle is inside it. We can use that to detect when we’ve “won” the maze!
First, let’s draw a rectangle. We want it to be at x=20, y=240, and we want the height to be 40
and width to be 50. How do we do this? Use the auto-complete to figure out what method on
GraphicsWindow to use, and then read the right-side panel to put the parameters (the
numbers inside the parenthesis) in the correct order. After you’re done, the screen should look
like this:
If Turtle.X > 20 And Turtle.X < 20+50 And Turtle.Y > 240 And Turtle.Y < 240+40 Then
GraphicsWindow.ShowMessage("You won!", "Congrats!")
EndIf
Lesson 7: Challenge Time
Now that you beat that maze, why not try something a little harder? Or make your own!
GraphicsWindow.DrawImage(
"https://aka.ms/mz2", 0, 0)
GraphicsWindow.DrawImage(
"https://aka.ms/mz3", 0, 0)
GraphicsWindow.DrawImage( GraphicsWindow.DrawImage(
"https://aka.ms/mz4", 0, 0) "https://aka.ms/mz5", 0, 0)
code, run it and edit it. Simply click the button in the top menu. You’ll get a popup
that looks like this!
The special code in blue is what you can share with your
friends who also use Small Basic. The link in purple is what
you can share with friends and family who don’t write
code; you can email or text it to them!
Lesson 9: Learn from others!
In Small Basic, you can import someone else’s code to work on it and fix a problem, make it
better, or just customize it for fun! It’s also a good way to learn how others write code and
learn from their examples. We’re going to import in a Tetris game and then play with the code.
Click and type in “tetris” (all lowercase). The Tetris code appears!
Let’s run it and see what it looks like! The instructions are on the right. The Left and Right
arrows move it, Up rotates, and Down drops your piece. Do you see the title that says “Small
Basic Tetris”? Let’s change that!