Sunday, November 8, 2015

Using Scratch To Teach Programming

Amongst MIT Media Lab's awesome creations, is their Scratch project. With Scratch, you can easily  program your own stories, games, and animation. This tool helps young people learn to think creatively, reason systematically, and work collaboratively. It is being used in schools to teach computer programming to kids.
I messed around with Scratch and created a simple maze game. The drag and drop based coding functionality makes it really easy to use. I absolutely loved it.
Scratch lets you create sprites that you use in your games as characters/backgrounds etc, or for your animations and stories. For my maze game, I created two sprites- 1) a smiley face, which I used as the character that will move through the maze 2) a maze, a path through which I will move the smiley face character. The sprites can be shrunk and enlarged as required. So, I shrunk the smiley character to make it fit the maze path.

Scratch has eight categories of ready to use programming features/statements. The categories are color coded to differentiate each from the other.
  1. Motion - To move the sprites 
  2. Variables - To create variables in your code
  3. Control - Implementation of if/else, while, for, or what to do if an onclick/key press event occurs 
  4. Sensing - For checking for events such as one color touching another, button/key press etc
  5. Operators - For checking for logical operators, or performing other mathematical operations such as addition, modulo etc
  6. Looks - For changing the physical appearance of sprites, or displaying alerts etc.
  7. Sounds - For playing sounds or changing the volume
  8. Pen - For changing the pen settings


To start programming, you click the sprite on which you want to add the functionality, and start dragging the relevant programming statements into the script area.
The very first step in the maze game is to specify when to start the game. In order to start the game whenever the green flag is clicked, I drag the following statement to the script region.

Next, whenever the game is started, the smiley character should move to a specific location. Therefore, I set the x and y coordinates of the smiley sprite to 75 and 85 respectively to always have it on a specific location in front of the maze.

Next, I implement the logic of moving the smiley character in left, right, up, and down directions. To do this, I drag an if block (control feature) for each of the four directions and move the smiley character by 15 pixels (using motion feature) respectively in a particular direction if the corresponding key is pressed (using sensing feature).

At the same time, I want to ensure that the character does not move through the lines in the maze. Otherwise, the game would be of no use. To do this, I will use a statement from the looks category and check for the instances when the smiley character touches the black color of the maze. If it does, the character should not move.

Lastly, the user can keep playing until they hit the space bar. Therefore, the if blocks are placed inside the repeat until space bar is pressed block.