Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables and Random Numbers Computer App Session 4.

Similar presentations


Presentation on theme: "Variables and Random Numbers Computer App Session 4."— Presentation transcript:

1 Variables and Random Numbers Computer App Session 4

2 Variables and Random Numbers Learning Objectives: I know what a variable is. I know what a variable is. I can create and use a variable and a random number in Scratch. I can create and use a variable and a random number in Scratch. I will create a simple game where a sprite tries to touch another sprite. I will create a simple game where a sprite tries to touch another sprite.

3 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 listening Ears listening

4 Now, you may go to the computers Click on your username at the right corner and choose My Stuff. Click on your username at the right corner and choose My Stuff. Open your project from last time. Open your project from last time.

5 Data Click on the Data category. Make a Variable Click on Make a Variable First, you will add a variable

6 Name the variable “Score”. For all sprites For all sprites Leave it as is 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 sprites.) Cloud Variable Leave Cloud Variable unchecked. OK Click OK

7 Note that the score has appeared on the screen. New sprite Let’s add a target sprite. From the New sprite area, choose library. OK. Choose an object to be your sprite. Click OK. Shrink the new sprite so it is about the same size as the first sprite.

8 Now you will code your target sprite Events From the Events category, drag the “when green flag clicked” into the code space. Data Return to the Data 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.

9 Control 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 Control category. Add an “if/then” block inside the “forever” block.

10 Sensing From the Sensing 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:

11 You need to two things when the two sprites are touching. The first is to increase the score. Data From the Data 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. Motion From the Motion category, drag “go to x:__y:__” under the “change score by 10”

12 But we need random numbers! Operators From the 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

13 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.


Download ppt "Variables and Random Numbers Computer App Session 4."

Similar presentations


Ads by Google