Lab 4: Scratching Structures¶
Scratch provides all of our basic structures so we can solve problems. It also has tools for working out expressions. Let’s do a silly task. We want a figure, like a ball (that is up to you) to move along a path, and we want to make sure we do this with just the structures and a bit of math. We also need a few variables so we can store numbers and use them in our project.
The Goal¶
The goal is actually fairly simple. We are going to drive our object along a square path. There is a twist (literally) to this, though!
The Program¶
There are exact steps you need to follow to build this program.
Warning
This code is going to be a bit long!
Step 1: Select your “sprite”¶
At the bottom of the “stage” (where the object is shown), you can select a new object to move. I used a basketball for my testing.
Step 2: Create some Variables¶
Create Variables with the following names:
Xpos
- the screen “X position” we want the object to go toYpos
- the screen “Y position” we want the object to go tosteps
- this sets how big each step iscount
- number of steps to move along a side
Step 3: Initialize the variables¶
We need to assign some values to our variables. Using the set <variable> to <value> controls, initialize the variables to these values:
Xpos
= -150Ypos
= -100steps
= 5count
= 30
Note
You might need to adjust these values a bit so the ball motion ends up on your screen. These values worked on my Mac. The nice thing about using variables is that no code needs to be adjusted, just the variables.
Step 4: Position the object¶
Now add the when Flag clicked control at the top of your code. This should be placed above the initialization code from step 3.
Below that code, place a go to x:<> Y:<> control. We will not enter numbers
in this control ever! Instead, we will place the variable names (Xpos
and
Ypos
) in those two slots. This will allow us to adjust the variable values
and move the object to whatever position we want.
Test this code and make sure the object moves to the lower left corner of your screen
Step 5: Move Right¶
Follow this one closely. We want our ball to move smoothly to the right from
its initial position. We need to adjust the value stored in Xpos
,
increasing that value as we take a “step”. You need a loop to do this. That
loop will spin count
times.
- Add a repeat <> control and place count` in the <> spot.
Adjusting a Variable¶
We need to change the value stored in Xpos
by the amount stored in step
each time the above loop works We do this with something like this (in Pseudo
Code):
Xpos = -150
LOOP
SET Xpos = Xpos + step
END LOOP
This will take whatever is in Xpos when we encounter that “set” line and add
“step” to it. It will place the result back in the Xpos
variable for use
later. This simple trick is how we will make the ball “move”!
Now add another go to x:<> y:<> contorol this one inside of the loop, below
that “set line. Make sure you again use the Xpos
and Ypos
variables.
The result should be something that looks like the pseudo code above.
REPEAT "count" TIMES
Set Xpos to Xpos + step
GOTO Xpos, Ypos
END REPEAT
Test you code and make sure your ball slides to the right on the screen,
Step 6: Move Up¶
If you get that last step running, this is easy. We will be leaving Xpos
alone, and taking “steps” in the Y direction. We need to add step
to
Ypos
in this loop.
All you need to do is copy the loop you created above, and change it so the ball moves up.
Test your code and make sure the ball returns to the starting point, slides right smoothly, then slides up smoothly.
Step 7: Move back Left¶
This step is the same as step 5, except here we need to decrease Xpos
on each step
Be sure to test your code after this step. Hey only one more to go!
Step 8: Close the Square¶
The last step is a repeat of step 6, this time decreasing the value of
Ypos
. With any luck, your ball should be right back where it started!
Be sure to test this code.
Sorry, you are not done!
Step 9: The Twist¶
This one is a bit of a challenge.
On each step in the X direction (either right, or left), I want the ball to
rotate clockwise 15 degrees for each step if the value of Xpos
is
greater than -50 on the bottom side. Along the top side, I want it to rotate
counter-clockwise 15 degrees for each step, if the value of Xpos
is
less than -100!
Yikes.
To do these two things, you need to add an “IF” block with a turn <>
degrees control inside, in each of the code that moves the ball along those
two sides. . You are going to need some “Logical Expressions” to figure out if
you are supposed to rotate, or not. You will use one of the rotate
controls
to do the “twist”.
On what last “twist” I used a “compound logical expression” I actually checked
if the ball was at Ypos
> -50 and Xpos
< -100! That was not strictly
needed, but it is a chance to use a more complex question.
Final Inventory¶
My final code had this shape:
- Only 10 real numbers were seen anywhere:
- -150, -100, 5, 30, -50, 15, -20, -100, 15
- All five “go to” statements have
Xpos
andYpos
in them- Four “repeat count” loops
- Eight “set” statements (to adjust position variables”
- Two “if statements” to decide to twist or not.
You do not need to match these numbers, but you should be close!
Warning
Please delete any code you are not using from the code screen. That clutter makes reading your code harder!
Extra Credit¶
For an extra five points, make the ball spin around, doing the twist exactly 5 times!
Note
This is so simple, I am tempted to subtract points if you do not do it!
Wrap Up¶
This example is a chance to see how we solve problems with just the basic structures and a bit of math. That is what large chunks of programming is all about. You face some problem, like slide along the side, then add other small chunks of code, building up to the final solution. You test as you go, to make sure what you have now is working in the right direction!
If someone throws a new problem at you in the middle of your project (like the twist), you do not throw things away, you add them in.
Learning how to do this takes practice. I bet you are surprising yourself with how simple this seems.
We are about to leave the itchy world of Scratch and begin using a “Real” language, C++. Before we do that, though, there is the issue of an Exam! Sorry, that will happen next week!