lesson 3: shark attack! - …madisonscratch.azurewebsites.net/handouts/lesson 3 - shark...

22
Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates Sensing Variables Goal: Create a game where a shark eats fish to score points!

Upload: vuongtruc

Post on 01-May-2018

220 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

Lesson 3: Shark Attack!

In this lesson you will learn about: Movement (x, y) coordinates Sensing Variables

Goal: Create a game where a shark eats fish to score points!

Page 2: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

1: Set up

Delete any sprites already on the stage.

Right-click on the sprite(s) and select delete.

Edit the current background. Paint the background plain blue.

Click Edit to open the Paint Editor.

Use the Fill Tool to color your background quickly!

Page 3: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

Make a new Shark sprite by choosing one from a file.

Select shark1-a from the Animals folder.

Create a new Fish sprite by choosing one from a file.

Select any fish you like!

Edit your Fish so that it faces left and is smaller than the Shark sprite.

Click Edit to open the Paint Editor.

This button flips. This button shrinks.

Your Stage should look similar to this:

Page 4: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

2: Moving the Shark

In this game, you will move the Shark sprite with the arrow keys. Select the Shark sprite to build a script for each key.

Start with the Control set:

Click and drag the “when [space] key pressed” block to the Scripts Area. Change [space] to [right arrow key].

Switch to the Motion set: Snap the “point in direction [90]” and “move [] steps” blocks to your control block.

You can change the number of steps if you like. Smaller numbers will make the Shark move slower; larger numbers will make it move faster!

Duplicate your script enough times so that each key has a motion script.

Right-click on the top block on your script to duplicate the script.

Page 5: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

Make sure you change the “when [] key pressed” and “point in direction” blocks so that you Shark can move left, down, and up.

Click the drop-down menu on the blocks to get more options!

Set your Shark to only face left-right at the top of the Scripts Area. (This will prevent the Shark from spinning in circles!)

Finally, test (try) your scripts to make sure they work! Your finished scripts will look like this:

Your “move [] steps” numbers might be different!

Page 6: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

3: Moving the Fish To move the Fish, you will need to understand a little about (x, y) coordinates. Positions on the Stage are represented by numbers. The number of the horizontal (left and right) location is called the x position. The number of the vertical (up and down) location is called the y position. The two numbers together are called (x, y) coordinates. Each pair of (x, y) coordinates represents a unique spot on the Stage.

Some important locations to remember:

(x: 0, y: 0) is the middle of the stage

(x: -240) is the far left edge

(x: 240) is the far right edge

(y: 180) is the top edge

(y: -180) is the bottom edge

All numbers in between these extremes will keep your sprite on the screen!

Page 7: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

For this game, your Fish will move right to left during the whole game. We can break this down into three steps:

1. Start the Fish on the right edge 2. Make the Fish glide from right to left 3. Make the Fish move the whole

1. Start the Fish on the right edge.

Start with the Motion set:

You want the Fish to start at the right edge every time. Click and drag the “go to x: [] y:[]” block to the Scripts Area.

In order for the Fish to go to a starting location on the right edge every time, you need to fill in the correct (x, y) coordinates in the block’s boxes.

Which x position would you use to represent the far right edge of the Stage?

Make your game interesting by starting the Fish at a random y position each time it moves across the screen! You can do this by using the “pick random” block from Operators.

Fill in the “pick random” blocks blanks with the lowest (bottom) and highest (top) y positions as the range. Drag and drop the “pick random” block into the “y: “ box in the “go to” block. Your finished “go to” block should look like this:

Page 8: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

2. Make the Fish glide to the left edge.

Select the Motion set:

Click and drag the “glide [] secs to x: [] y: []” block to the Scripts Area.

The “glide” block has spaces for three numbers:

The number of seconds it takes to move to the new position

The x position to move to

The y position to move to Which x position would move your Fish to the left of the screen?

The game will be much more interesting if the fish glides to a random y position! How would you modify (change) the glide block to make this happen?

You can also make your fish move at a random speed across the screen. Do this by adding a “pick random” block to the “secs” box in the “glide block.”

Don’t make it so fast that you can’t catch it!

Larger numbers means the glide will take more time (move slower); smaller numbers means it will take less time (move faster). Snap the “glide” block to the bottom of the “go to” block to make this script:

Page 9: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

3. Make the Fish move for the whole game You want your Fish to move this way the entire game! To do that, you will need to put your script into a loop. Remember, a loop is a Control block that causes the statements inside it to repeat, or happen over and over, until the loop ends.

Select the Control set to find the loops: Which loop would you select to make this script run all the time?

Drag and snap your motion script inside the loop. You also want your loop to execute as soon as the game starts without having to activate it manually! Many programs in Scratch start when you click the green flag in the upper right corner of the Stage.

Which Control block would you use to start you script when the green flag is clicked? Test (try) your script to make sure it works! Your finished motion script will look similar to this:

Page 10: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

4: Make the Shark eat the Fish The way players will score points in this game is by making the Shark eat Fish! We can break this down into three steps:

1. Have the Shark “bite” when the space key is pressed 2. Get the Fish to “know” when the Shark eats it 3. Make the Fish disappear after being eaten

1. Have the Shark “bite” when the space key is pressed To make the game more challenging (and look cool), the Shark will have to “bite” the fish in order to eat them. To do this, you will animate the shark by changing its costume. Import a new costume, shark1-b, for your Shark sprite.

Click the Import button under the Costumes tab. Find shark1-b in the Animals folder.

Make the Shark “bite” by switching costumes when the spacebar is pressed. Which Control block would you use to make the costume change on a key press?

Drag the “when [space] key pressed” block to the Scripts Area.

Page 11: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

Switch to the Looks set: Snap the “switch to costume [shark1-b] to the bottom of your new Control block.

You can use “next costume,” but the “switch” block gives you greater control.

To animate the Shark, you will want to switch back to its first costume (shark1-a). Snap another “switch to costume” block to the first one, and select “shark1-a” from its drop-down menu.

Press the spacebar. What happens? The Shark switches costumes so fast, you barely see it! You need to wait a short amount of time before switching costumes.

Select the Control set:

Drag the “wait [1] secs” block and snap it between the two “switch to costume” blocks (underneath the “switch to costume [shark1-b]” block).

Change 1 to 0.25 seconds in the “wait” block to make the Shark bite fast!

Test (try) your script to make sure it works! Your new script will look like this:

Page 12: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

2. Get the Fish to “know” when the Shark “eats” it How will the Fish “know” when the Shark “eats” it? You need to give it conditions! A condition is something that can be true or false. For a Fish to be “eaten” in your game, the following conditions need to be true:

The Shark must be biting

The Fish must be touching the Shark

The Fish must be touching the Shark’s teeth All of these conditions can be checked for by using sensing.

Switch to the Sensing set: Which condition looks to see if the Shark is biting?

The Shark only “bites” when the spacebar is pressed!

Which condition will check if the Fish is touching the Shark?

The “touching []” block can check if the sprite is touching other sprites. Use the drop-down menu to pick the Shark sprite!

You can rename your sprite(s) to make it easier to tell them apart!

Page 13: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

Which condition could you use to see if the sprite is touching the Shark’s teeth?

The Shark’s teeth are a distinct color (white)! You can use this information to “know” when the Fish is touching the teeth. First, you need to change the color that appears in the “touching color” block. Click on the box that shows the color.

Your mouse pointer will turn into an eyedropper.

Put the tip of the eyedropper on the color you want the Fish to be touching. In this case, it will be the white of the Shark’s teeth.

If you do it right, the box in the “touching color” block should be white.

If it doesn’t work the first time, keep trying!

You should now have all three conditions inside your Scripts Area:

Page 14: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

For the Fish to be eaten, all three of these conditions need to be true. You will need to combine them using an “and” block.

Select the Operators set: Drag an “and” block into the Scripts Area.

Using this block means that both of the conditions it holds must be true.

Notice it has space for two conditions. We have three! You can use one of the spaces in the “and” block to insert another “and” block.

Now, your “and” blocks can hold three conditions! Drag and drop all three of your conditions into the block. You now have a conditional statement! A conditional statement is a number of conditions linked together (like a sentence) that can be true or false.

It doesn’t matter what order your conditions are in!

You want to check for these conditions the whole time that the game is running. That way, the Fish always has a chance of being eaten! Which kind of Control block would you use to repeat an action during the whole game? You will need a loop!

Page 15: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

Scratch has a special kind of loop that has a spot for a conditional statement. It is the “forever if” loop.

The “forever if” loop will check for its conditions each time it executes (runs). If its conditions are true, it will execute (run) the scripts inside of it. Place your conditional statement into the “forever if” loop.

This loop needs to execute (run) the entire time the game is running. Which Control block did you use earlier to make a loop run for the whole game? Remember that your game will start running when the green flag is clicked!

Snap this block to the top of your conditional statement loop. Your unfinished script should look like this:

Page 16: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

3. Make the Fish “disappear” after being eaten You don't want the Fish to stick around after it has been “eaten!” Something should happen to it to let the person playing the game know that their Shark has successfully “eaten” a Fish. The easiest thing is to hide the Fish so it disappears from the Stage once “eaten.”

Select the Looks set: Drag the hide block to the Scripts Area.

Where would you snap this block to make the Fish hide only after it has been “eaten?”

It’s not much fun if the game ends after the Shark eats just once. In this game, you want the Shark to be able to eat the Fish again! To make this happen, you will need to show the Fish again. Use the Show block, also from the Looks set.

You want to show the Fish at the correct time! Show it after it gets to its starting point at the right edge, but before it begins gliding across the Stage.

Page 17: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

Your Fish scripts should look like this:

And your Shark scripts should look like this:

Test (try) the scripts to make sure they work! The Shark should move up/down/left/right when the arrow keys are pressed

The Shark should “bite” when the spacebar is pressed

The Fish should move across the screen from right to left

The Fish should disappear only when the Shark bites it

The Fish should reappear again at the right edge of the screen

Page 18: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

5: Keeping Score Most games have a score! For this game, the score is going to represent how many Fish the Shark eats. To keep track of the score, you are going to use a variable.

Select the Variables set: Notice there are no blocks here yet. There are only two options. Select Make a Variable.

A window will pop up.

In this window, you can give your new variable a name and decide which sprites will be able to access (use) it.

Page 19: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

Name your new variable “score” and make it “For all sprites” (which is already selected for you).

Click “ok” to make the variable! The Variables set now has several blocks you can use. Your variable is also visible on the Stage.

New script blocks. Stage view.

A variable is a container. The container is given a name. It can store objects and values.

By storing objects in a variable, you can easily change and reset them.

Page 20: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

In this case, your variable stores the value of a number. You can change and reset this number without knowing what number it is. In this game, you want to change the number each time the Shark eats a Fish. Drag and drop a “change [score] by” block into the Scripts Area for your Fish.

This block will add the number in the box to the number stored by “score.” Where would you put this block to add one to “score” each time the Shark eats the Fish?

Putting it in the “forever if” loop makes “score” change by +1 (increase by one) each time a Fish is eaten by the Shark. Test (try) your script to make sure it works! The “score” on the Stage should go up by one every time the Shark eats a Fish.

Your “forever if” loop should look like this:

Page 21: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

6: Duplicating Fish sprites You can now duplicate (copy) this Fish sprite! Duplicate it 2 - 3 times to give the Shark a few more fish to choose from!

Right-click on the sprite in the Sprite List and select “duplicate.”

Your Stage should look similar to this:

Play your game to make sure it works. Have fun! Go to the next page for ideas on how to make your game more challenging to play!

Page 22: Lesson 3: Shark Attack! - …madisonscratch.azurewebsites.net/handouts/Lesson 3 - Shark Attack... · Lesson 3: Shark Attack! In this lesson you will learn about: Movement (x, y) coordinates

7: Make the game challenging! Here are some ways you can modify your game to make it more interesting and challenging for players:

Reset the “score” at the beginning of each game

Make the Fish sprites wait a random number of seconds before reappearing

Create different Fish sprites that are worth more points than others

Make the Shark get bigger as it eats more Fish

Create a “bad” Fish sprite that takes away points when eaten by the Shark

Use a timer to give your game a time limit o Stop the game when it reaches a certain time o Reset the timer each time the game restarts

Make less Fish appear as the “score” gets higher

Control the Shark with the mouse instead of the keyboard o Have the Shark follow the mouse pointer o Make the Shark “bite” when the mouse is clicked

Create a background that only appears when the player wins