Date post: | 06-Jan-2018 |
Category: |
Documents |
Upload: | winfred-fields |
View: | 213 times |
Download: | 0 times |
Variables and Random Variables and Random NumbersNumbers
Computer App Session 4Computer App Session 4
Variables and Random NumbersVariables and Random Numbers
Learning Objectives:Learning Objectives:
•I know what a variable is.I know what a variable is.•I can create and use a variable I can create and use a variable and a random number in Scratch.and a random number in Scratch.•I will create a simple game where I will create a simple game where a sprite tries to touch another a sprite tries to touch another sprite.sprite.
Follow these instructions:Follow these instructions:• Push in the keyboard,Push in the keyboard,• Hands OFF the computer,Hands OFF the computer,• Eyes up front,Eyes up front,• Voices off,Voices off,• Ears listeningEars listening
Now, you may go to the Now, you may go to the computers computers
• Click on your username at the Click on your username at the right corner and choose My right corner and choose My Stuff.Stuff.
• Open your project from last time.Open your project from last time.
• Click on the DataData category.• Click on Make a VariableMake a Variable
First, you will add a variableFirst, you will add a variable
• Name the variable “Score”.• Leave it as is For all sprites For all sprites (you can have
variables for just one sprite, but then they can’t be accessed by other sprites. For almost all variables, you will choose For all spritesFor all sprites.)
• Leave Cloud Variable Cloud Variable unchecked.• Click OKOK
• Note that the score has appeared on the screen.
• Let’s add a target sprite. From the New sprite New sprite area, choose library.
• Choose an object to be your sprite. Click OK.OK.• Shrink the new sprite so it is about the same
size as the first sprite.
Now you will code your Now you will code your target spritetarget sprite
• From the EventsEvents category, drag the “when green flag clicked” into the code space.
• Return to the DataData category and drag “set score to zero” to be under “when green flag clicked”.
• This means that every time
the game starts, the score is reset to zero.
• Now you’ll add code so that every time your target sprite touches the first sprite, it will add 10 points. We need to check constantly to see if they are touching, so under this add a “forever” block from the ControlControl category.
• Add an “if/then” block inside the “forever” block.
• From the SensingSensing category, drag in the first “touching” diamond so that it fits after the “if”.
• In the drop down of the “touching” diamond, select the name of the first sprite. I have Bat1 so my code looks like:
• You need to two things when the two sprites are touching. The first is to increase the score.
• From the DataData category, drag in “change score by 1”. Change the 1 to a 10.
• The second thing is to move the sprite to a random part of the screen so it can be caught again.
• From the MotionMotion category, drag “go to x:__y:__” under the “change score by 10”
But we need random numbers!But we need random numbers!• From the Operators Operators category, drag “pick
random from 1 to 10” into the x space. Change the values to -240 and 240.
• Now do the same for the y value. Change these values to -180 and 180
Try it out!Try it out!• Click the green flag. • Use the arrow keys to move the first sprite
moves around and catches the second sprite.• Make sure that each time you catch it, it moves
to a new spot and the score goes up by 10.• If you finish early, make a two player game
where two sprites race to catch the target sprite first. Think about how you will control their movement.