700 likes | 711 Views
Scratch for Interactivity. Dr. Ben Schafer Department of Computer Science University of Northern Iowa. Activity #1 – adding simple interactivity. Yesterday at one point I suggested that you create a sequence of actions so that the sprite draws A square (saved as “SquareTracer” ??)
E N D
Scratch for Interactivity Dr. Ben Schafer Department of Computer Science University of Northern Iowa CS4HS Summer 2010
Activity #1 – adding simple interactivity • Yesterday at one point I suggested that you create a sequence of actions so that the sprite draws • A square (saved as “SquareTracer” ??) • An octagon • An equilateral triangle
Activity #1 – adding simple interactivity • Suppose that instead I asked you to write a program that had the sprite draw an n-sided regular polygon where n was provided by the human user of our program.
Activity #1 – adding simple interactivity Learn about introduce variables introduce mathematical operations introduce slider based interaction
Some challenges • How do we get the input from the user? • How do we tell the computer to repeat based on this answer? • How do we change out side length and angles based on this answer?
One solution • While we technically have several problems, all are solved through the introduction of a variable.
Thought questions • What is a variable? • Where do we use one in general? • Where do we use one(s) in this problem?
Variables • Select the variables menu from the script block area. • Notice that by default none are provided and that there are no script blocks available. • Press the Make a variable button.
Variables • We need to give our variable a good name. In this situation we want a variable to store the number of sides on our polygon. Therefore…
Variables • After creating a variable several script blocks appear below a list of our current variables (at this point, only one).
Variables • Insert the “set … to … “ block at the beginning of the cat’s script and assign it’s initial value to 4
Variables • Replace the numerical 4 in the repeat block with the sides block from the variables menu (notice it has rounded edges meaning it can be placed there)
Variables • Run this program. • What do you observe? • Change the “4” to a “6” • What do you observe?
Variables • Despite the fact that we have a six in as the sides we still get a square. • Although, notice the cat is in a different place. • What’s going on?
Variables • We need to adjust our angle when we change the number of sides. • Should that be another variable? • It could be, but there’s a better (easier) way.
Operators • How is the amount of each turn (angle) related to the number of sides? • If there is a mathematical relationship (and there is) we can use operator blocks to have the computer make the calculation.
Operators • Notice that the Operators menu contains several mathematical (rounded edge) and boolean/logical (angle edge) blocks
Modifying our code • We can improve our solution by replacing the “hard coded” numbers of 100 and 90 with mathematical calculations based off of the original number of sides. • Try a variety of inputs. • Does it seem to work?
Other ways to provide input? • Ok, a human user SORT of has a way to change the sides, but it is pretty lame. • How can we provide input during execution (“run-time”)?
Variable Sliders • Right click on the display for “sides” • Select the slider option. • Test out the resulting program.
Slider values are too extreme • Right click again on the sides display. • Notice that the options have changed now that it is a slider. • Select “set slider min and max” • Set min=3 and max=20
Activity #2 – A game We will make a game where the player controls a baby using the arrow keys to catch a falling ball. If the baby misses the ball the game is over.
Activity #2 - Goals Learn about introduce event handling introduce conditionals more with loops more with variables more with parallel execution (yesterday) more with message passing (yesterday)
Delete the Cat Click on the scissors and your cursor turns to scissors and then click on the cat to delete it Or right click on the cat and pick delete
Add the Baby Click on the button with the picture of folder with a star in it if you hover over it, it says "Choose new sprite from file"
Scroll to the Baby Click on the baby and then OK
Resize Your Sprite! You can make your sprite larger or smaller by using the “grow sprite” or “shrink sprite” icons. You click on one of these icons, then click on your sprite until it is the size you’d like. shrink grow
Move the Sprite Select (click on the arrow and then on the sprite) and click and drag the sprite to the bottom of the window
Add a Background Click on the Stage In the bottom right area Click on the Backgrounds tab In the center Click on the import button Pick a background Like bedroom1 in indoors
Event Handling We want to control the baby using the arrow keys When we click the left arrow the baby should move left When we click the right arrow the baby should move right This is a form of event handling Responding to user actions like mouse clicks and key presses
The Scratch Stage The Scratch stage is 480 pixels wide and 360 pixels high. 180 Moving left decreases the x value Moving right increases the x value -240 240 0,0 at the center of the stage -180
Respond to Arrow Keys Click on Control and then drag out "when space key pressed"
Respond to Right Arrow Click on down arrow next to space and select right arrow
Respond to Right Arrow Click on Motion and drag out "move 10 steps"
Change the move amount Click on the 10 it will highlight in blue Type 5 and press enter
Respond to Arrow Keys Drag out "when space key pressed" Change "space" to "left arrow" Drag out "move 10 steps" Change it to -5 (to move left) Click on the stage and try out the left and right arrow keys Does the sprite leave the window?
Paint a Ball Click on the paint brush and star It will say "Paint new sprite" if you hover over it
Draw the Ball Click the circle tool and then use the eyedropper to pick a color and then click in the drawing area and drag to create the ball Circle tool and eyedropper Drawing area Click ok when done
Size the ball as desired and move it to the top Click and drag the ball to the top of the window
Make the Ball Fall When the green flag is clicked we want the ball to always start at the top and fall down Click on Control Drag out "When green flag clicked"
Start the Ball Click on Motion Drag out “go to x # y # “ this will always start the ball at its current position (Scratch doesn’t automatically put it back for you).
Sequential Execution One block is executed after the other In order from top to bottom When the green flag is clicked the ball will go to the specified x and y location
Loops We want the ball to continue to move down unless the baby catches it How do we make this happen? We could use lots of blocks one after the other But, that would be slow and repetitive We need a way to repeat a block or set of blocks This is called a loop or iteration
Make the ball fall Click on Control drag out "forever" Click on Motion drag out “change y by 10” Change it to -1 Try it out!
Catch the ball! If the ball touches the baby then it is caught Let's track how many balls we have caught with a score We will increment the score each time we catch a ball
Variables If we are going to keep track of the score We want something to hold the current score And we want to be able to change the score We want the value to change or vary This is called a variable
Track the score When we start the game set the score to 0 Click on Variables Click on Make a Variable Name it score
Set score to 0 In the ball script Drag the “forever” down Drag out "set score to 0“ Drag the “forever” back up Notice the score showing on the window