Computer Science 1000 Algorithms II Multiple Sprites Scratch allows us to have multiple sprites on the screen simultaneously three ways: draw your own choose from file (most common) let Scratch choose Draw your own Choose from file Let Scratch choose Multiple Sprites note that each sprite has its own code to see the code for a particular sprite, click on its image in the bottom right pane of the screen Multiple Sprites note that double-clicking on one sprite’s code does not affect the other Multiple Sprites what if we do want both sprite’s code to execute simultaneously? Solution: use when the green flag is clicked, it will cause both code blocks to start executing simultaneously Event-Driven Programming the previous is an example of event-driven programming code block(s) occur in response to a user event computer today are almost exclusively event-driven in this case, the user clicking the green button caused both sprites’ code to be executed mouse clicks keyboard events other examples of events in Scratch exist we will consider another shortly Loops it is very common in any computer program to repeat operations Loops rather than duplicate code, Scratch allows us to specify code blocks that should be repeated from our previous example, it would be nice to ask Scratch to repeat these code blocks 4 times, rather than duplicating the blocks multiple times Repeat Loop (Controls) the repeat loop repeats the code block that is placed inside of it the number of times it is repeated depends on the value that is in its input The code in this loop will be repeated 10 times. The code to be repeated is placed in here. Example: Convert our previous code for moving the sprite in a square, using a loop to avoid duplicating code Example: Write a program that asks the user for a number, and moves the sprite by that distance. This process should be repeated three times. Use a loop to avoid duplicated code. Loops suppose you do not know in advance how many times you wish to loop Solution: use a forever block the forever block will continue to loop indefinitely, until the user clicks the stop button in the top right corner e.g. convert our previous example, so that the cat walks continuously around the square until stop is pressed Event-Driven Programming the previous is also an example of code responding to an event the wait block completes execution in response to one second passing we can also wait for other events to occur mouse clicks keyboard events etc Event-Driven Programming instead of using a wait 1 sec, block, we can use a wait until block this block takes an event as input, such as one from our previous slide example: convert our previous program, so that the cat only moves/turns in response to the mouse button being pressed Event-Driven Programming in your assignment, you are asked to perform an action with a sprite in response to another sprite touches it Hint: use the structure from the previous example, but instead of a mouse down block, use the touching block