Scratch Lesson 2 - Target Game:

The Scratch program was developed by MIT to teach young students programming concepts and develop skill in multimedia communication. Using a visual system of "Tiles" that contain commands users can connect together to create programs.  These programs direct the characters and objects in the game. 

Unique Scratch Fact: The Scratch program runs in a very different way than traditional programs such as Word or Excel.   When you run "Scratch" you are running a program called "Squeak" with the "Scratch.image."  (I am not making this up!)  The result is that the visual environment looks somewhat different than Windows XP. When you run Scratch and Squeak on different operating systems (Windows, OS X, Linux) the look and feel of Scratch remains the same. 

Scratch Layout



Pre-Planning:  List your Objects (Sprites) and their Methods (Scripts) and Properties:

Before doing programming, I usually make a list of what Sprites I want in the program and what they will do.  For the Target Game, we have the following Sprites:

Sprite (Object)
Scripts (Methods)
Properties
Cannon
-Turns right and left on user input from arrow keys
-Records current direction as a variable
-One costume
Ball
-Stays hidden until Space Key pressed
-Starts traveling from Cannon
-Moves 300 steps on Space Bar user input
-Leaves a Trail as it moves
-Bounces off the edge of the boundry
-Takes direction from the Cannon
-One costume
Targets
-Move randomly about the playing area
-Change costumes to "flap wings"
-Hide and make sound when touching the Ball
-Show after 3 seconds of hiding.
-Two Costumes
-One Sound


Step 1: Drawing Your Sprite (The Cannon) and making it point.


A Sprite is an character or object in your game.  Sprites can move and be active or be props that stay still.  We will draw a Sprite that will act as the "cannon."

1. Open Scratch
a. Go to the folder where you copied "Scratch."
b. Double click on the Scratch icon.

2. Delete the Cat.

3. Click the "Paint New Sprite" icon.

4. Draw the cannon.  To simplify things later, draw the cannon pointing to the right.



5.   Name the Sprite (notice my misspelling of the word "cannon" - I am a musician . . .)
(Always name your Sprites - this is like doing comments in text based programming - it will help you and people who read your programs figure out the flow of the program.)

6.  We want the "Cannon" to turn left and right as the user presses the arrow keys. 
a.  Drag a yellow "When Space key Pressed" into the Scripts pane.  
b.  Add a "turn 15 degrees" clockwise to this Script. 
c.  Change "Space" to "right arrow"
d.  Do the same for the left arrow.
 



Step 2:  Creating the "Ball" and having it Move, Stamp, and Aim.

1.  Click the "Select Sprite From File" icon and choose a Sprite.  I choose a ball from "Things." 



2.  Name your Sprite.  (I called it "Ball")

3.  We want the ball to move 300 steps.  We will use the "Repeat" and "Move 10 steps" command.  Drag a "Repeat" into the Balls Scripts pane.  Put a "move 10 steps" inside the repeat.  (I teach this as the basic "looping" or "iteration" concept.)  (Also makes a great lesson in Distance = Rate x Time)



4.  If we double click on the Script, the ball will move.  Now we want the ball to "stamp"  (leave a trail) and bounce off the edge:
  a.  Drag "stamp" into the "repeat 30" stack.
  b.  Drag "if on edge, bounce" into the "repeat 30" stack.



5.  Now we want the Ball to "hide" after the script is done:  ("Hide" makes a Sprite invisable.  "Clear erases "stamps" and "pens.")
  a.  Drag a "hide" onto the bottom of the "repeat."
  b.  Drag a "clear" onto the bottom of "hide." 



6.  We want the Ball to start this script when the space key is pressed:
  a. Drag a "when space key pressed" into the scripts pane.
  b. Add a "go to Canon"
  c. Add a "show"
  d. Add a "point in direction 90"
  e. Make all these one stack




7.  Press the space bar.  Your Ball should "shoot" from the Cannon.  However, it does not aim.  In order to aim we have to tell the Ball what direction the Cannon is pointing in.  We are going to use a Variable for this function. 
(Note:  In later versions of Scratch, this function is done automatically.  However, I like to teach it this way as an introduction to using variables.)

8.  Click on the Variables button and make a variable called "Direction."



9.  On the Cannon (or "Canon"), make the "Direction" variable = to the Cannon's direction:
  a.  Drag a "When Green Flag Clicked" and "forever" into the scripts pane. 
  b.  Drag the "set Direction to 0" inside the forever
  c.  Click on "Motion."  Drag the blue "direction" and put it in the "set Direction to" script.




10.   Now use the arrow keys to move the Cannon.  The Direction variable should change as the Cannon turns.

11.  Pass the "Direction" variable value to the "Ball."
  a.  Click on the Ball Sprite
  b.  Drag the Orange "Direction" oval from the Variable pane into the "point in direction" script of the Ball.




12.  Click the Green Flag.  (This starts the program)  Aim with the arrows and press the space bar.  This should "shoot" the Ball from the Cannon.

Step 3:  Creating the Targets.  Automatic Movement and Conditional Statements.

We have created the Cannon and the Ball.  Now it is time to make the targets.  We will create one Target that moves, and hides when hit by the ball.  Then we will copy the Target several times.

1.  Click the "Select Sprite from File" icon and select a Sprite that has multiple costumes.  (We will use this later to simulate movement.)





2.  Click the "Costumes" tab on the new Sprite.  Use the "Import" key to select the second costume.




3.  We want the Sprite to Glide to random locations on the top area of the World.  The World is a 2 dimensional array with x axis values ranging from -240 to 240 and y axis values ranging from -180 to 180.  The Target will need to choose points ranging from:

x:  -240 to 240
y:    20 to 180

To do this we will use the "Glide" command and the "pick random" command.




4.  Name the Sprite (I chose "Butterfly")
  a.  Drag a "When Green Flag Clicked" into the scripts pane.
  b.  Drag and connect a "Forever" to the "When Green Flag Clicked"
  c.  Drag a "glide 1 secs to x: y: "



5.  Now set the random numbers:
  a.  Click on the Green "Numbers" tab and drag two "pick random" green ovals to the scripts pane.
  b.  Put "-240" in one "pick random" value and "240" in the other.
  c.  In the second "pick  random," put the values 20 and 180



6.  Drag the "pick random -240 to 240" to the x value of the "glide" block.  Drag the "pick random 20 to 180" to the y value of the "glide" block.

7.  Click the Green Flag.  Your Target should now move about the screen.

8.  To simulate wings flapping.  Use the following Scripts:



Final steps:  Make the Target Hide, Show, Play a Sound, and Change Score.

10. Drag a "When Green Flag Clicked" into the Target's Scripts pane.

11. Drag a "Forever If" into the Scripts pane.  (Conditional Statement.)

12. Click the "Sensing" Icon.  Drag a "Touching" hexagon into the "Forever If" Script.

13. Change the "Touching" to "Ball."

14. Put the following inside the "Forever If."
  a. Hide
  b. Wait 3 Seconds
  c. Show

15. Click on "Variables"

16. Make a variable called "Score"

17. Put "Set Score to 0" under the "When Green Flag Clicked."

18. Put a "Change Score by 1" inside the "Forever If."

19. Click on the "Sounds" tab

20. Import a new sound




21.  Drag the "Play sound" block into the "Forever If."

 


22. Complete Target Script should look like this:



23.   Click the "Stamp" tool and copy the Target several times.




24.   Game should look something like this.



25.  To change the "Stage."   (Like importing costumes)
  a. Click the "Stage" icon
  b. Click the "Backgrounds" tab
  c. Click "Import."
  d. Select the desired background.



26.  Click the Green Flag and try your game!

CSTA Standards:
CSTA Level I Standard 4: (3rd to 5th) Use general purpose productivity tools and peripherals to support personal productivity, remediate skill deficits, and facilitate learning throughout the curriculum.
CSTA Level I Standard 6: (6th to 8th): Design, develop, publish, and present products using technology resources that demonstrate and communicate curriculum concepts to audiences inside and outside the classroom.
CSTA Level I Standard 12: (6th to 8th): Understand the fundamental ideas of logic and its usefullness for solving real-world problems.
CSTA Level II Standard 2: The basic steps in algorithmic problem-solving (problem statement and exploration, examination of sample instances, design, program coding, testing, and verification.)


GPS Standards:
M5P3: Students will communicate mathematically
M5P5: Students will represent mathematics in multiple ways.
M5A1 Students will represent and interpret the relationships between quantities algebraically.
M5P1 Students will solve problems (using appropriate technology).
M4G3 Students will use the coordinate system.