Programming Concepts and Skills Supported in Scratch Lifelong Kindergarten Group MIT Media Lab http://scratch.mit.edu Problem-Solving and Project-Design Skills logical reasoning breaking complex problems into simpler parts debugging problems developing ideas from initial conception to completed project sustained focus and perseverence Fundamental Ideas about Computers and Programming computer programs tell the computer precisely what to do, step-by-step writing computer programs doesn’t require special expertise, just clear and careful thinking Specific Programming Concepts Concept Explanation sequence To create a program in Scratch, you need to think systematically about the order of steps. iteration (looping) forever and repeat can be used for iteration (repeating a series of instructions) conditional statements if and if-else check for a condition. Example variables The Variables category allows you to create a new variable and use it in a program. Scratch supports both global and objectspecific variables. threads (parallel execution) Launching two stacks at the same time creates two independent threads that execute in parallel. synchronization broadcast can coordinate the actions of multiple sprites. For example, Sprite1 sends the message winner when condition is met: This script in Sprite2 is triggered when the message is received: real-time interaction mouse_x, mouse_y, and loudness can be used as dynamic input for real-time interaction boolean logic and, or, not are examples of boolean logic. random numbers The pick random block selects random integers within a given range. event handling user interface design when key pressed and when sprite clicked are examples of event handling – responding to events triggered by the user or another part of the program You can design interactive user interfaces in Scratch – for example, using clickable sprites to create buttons. Programming concepts not currently introduced in Scratch: data structures (arrays, etc.) procedures and functions recursion inheritance defining classes of objects exception handling parameter passing and return values text input file input/output Messages Sprites react to key presses or to touching other sprites or colours. A forever loop can also contrain logic to test for certain positions on the stage and so forth. A practical means of communicating between all the object in the applicatiuon (including the stage) is for a sprite to broadcast a message. One technique is to broadcast a message when a sprite reaches the edge of the stage to cause the background picture to change to the next room. This can be achieved by placing a single pixel of a unique colour onto the background at the point where the entrance to the adjoining room occurs. A forever loop allows a test for the sprite touching this unique colour which then broadcasts a message received by an event conrtol on the background which changes the background. The sprite logic is as follows and presented in the messages application: Clicking on the stage icon in the design environment allows program blocks to bne defined for the background. The broadcast messages can be used to move from room to room in the Scratch application: Broadcasting can be used to communicate between sprites and allow sprites to be used as buttons for example by broadcasting a message to all sprites using the sprite clicked control block for the button sprite. Another technique allows the backgound application to monitor a variable against a second variable contining the previous value of the variable to broadcast a message when the slider is used to change the value. The following code is added to the forever loop of the stage: Any sprite that needs to respond to a change in the value of the slider can wait for the relevant message to be broadcast: Iteration Iteration is the second basic concept in compter programming and is the repetition of a sequence of commands (known as a loop). The control blocks in Scratch allow for interation, in particular the forever and repeat blocks. A square, for example, has four sides and can be drawn by repeating the sequence MOVE 60 TURN 90 four times. Double click on the outermost control block to start the sequence and remember to have the pen down if you want the sprite to draw a trail. Experiment with different angles and sequences of commands to produce interesting shapes. Flowers are produced by drawing a simple shape and then turning a little before repeating the shape to form a flower. The following example is a square flower but you can experiment with triangles and hexagons and other shapes. The above example has a repeat iteration nested within another iteration. A forever loop combined with a random number can produce continuous random behaviour easily. Note the prescence of a wait command within the forever block. This is essential programming practise for applications that have more that one sprite so that the forever loop does not hog all the processing power and prevent the commands for other objects from running in a timely fashion. Young programmers should master the creation of triangles, squares, and hexagons using iteration and then move on to creating flowers. Continuos motion can be implemented on the onekey application with the following loop: This loop can be attached to the green flag control block so that the sprite begins moving as the application is started. The onekey commands will still work with this sprite and the updated onekey application is available here. Ask the programmers to update their own onekey application to have a continuosly moving sprite. You can add a second sprite to the application. In this case we want the same command sequences and key press controls to work on the second sprite so the most effective way to duplicate the sprite is to export it and import it back into the application. Change the sprite costume and the initial starting position and you have created the iteration application.