Here is a direct link to Snap!
To play with Snap! you can just click on the play button on some of the images, like the one above. If you have any problems with it loading, visit Snap! directly in a new browser tab (On that page, click "Run Snap! now"). Another possible solution, if your browser is not Chrome, is to give Chrome a try.
Let's imagine we want simulate a simple racing track ring like the one above, with one traffic light and some number of identical cars driving around it. The cars can speed up or slow down, or fully stop. But these cars never pass each other. The order of cars on the track will always stay the same, for simplicity. We like to experiment with the effects of one car moving too fast, or too slow, and so on.
How do we model and program such a simulation?
Guess what! You already know enough about programming in Snap! to get going with this! Let us walk through the thinking process...
First, think of the kinds of sprites (also known as objects in programming) you would need.
Right, we'll make a sprite and clone it for all the cars that will be on the track. Then, we can make a
to do the traffic control.
But the cars need a way to know their own position and speed, as well as those of the surrounding cars (for simplicity, say only the car in front). Which sprite can give us that knowledge?
As part of the Stage script, as we are creating new clones of the
Next, let's think of the traffic light. What does it do? It should be turning red, green, and yellow, in a loop, with some determined time interval between each signal. How can we represent which light the traffic light is currently showing?
Yes, we already know what we use in programming to store information: variables!
So for the traffic light we can make a signal variable that can have a value of green, yellow, or red. In Snap! you could assign three different costumes for your traffic light, one for each signal.
But sometimes we may need to store more than one data related to an object, and so we will create multiple variables for it. The set of current values for all of the variables for each object, or sprite, is called its state.
So we need to think of what variables we are going to need to represent the state of the
The Stage script will probably have big responsibilities! It needs to know for each car, which car is the one in front of it, so it can report to it their positions and speeds. It also needs to compute the distance between cars, based on their current position on the X and Y coordinates.
It is not much use for objects to have state, if they cannot communicate with one another about their state, that is, their current value for each of their variables. As we have already seen in Snap!, we can use message broadcasts to send and receive information.
So now think of the kinds of messages our objects need to be sending around and receiving.
For example, the traffic light can broadcast its state changes to the world: green, yellow, etc.
So far so good, but what makes our whole simulation to tick? That is, we have't yet simulated the time passing! Everything is frozen at some moment the way we have things. So we can have the to keep looping over some program!
In the next class, we will implement this simulation together.
Before moving on to implementing the traffic light simulation, which is a big project, let us do a simpler task. We want to make a simple ATM, where you can do one of the following:
First draw something that looks like an ATM. You can add three button sprites which we can click to perform each of the 3 actions above.
Now let us implement it... But how?
You guessed it! We'll need to make a variable to store what is the current balance of the account. Use the tile to let the user type in an amount value. Once they enter the amount, you can access the value they entered using the pre-existing variable
.
Question: what could happen with withdraw if we are not careful? That's right! Someone might get naughty and try to get more money from the ATM than the balance amount! Make sure you have the code in a way so that ATM doesn't give any money if more than the balance is requested.
The ATM is no good if anybody can come along and withdraw money from your account! Implement a simple way to insert your card into the ATM and enter the PIN, before you can use the balance, deposit, or withdraw buttons.
Hint: Use a variable called logged-in which starts as false, but is set to true once you drag a
Next: Class 6: Implementing the Traffic Light Simulation
Previous: Class 4: Revising the List Example: Under the Hood
Back to: Table of Contents