Scratch Lesson 5scratch.ie/sites/all/themes/scratch_theme/resources... · Scratch Primary Lesson 7...

Post on 29-May-2020

11 views 0 download

Transcript of Scratch Lesson 5scratch.ie/sites/all/themes/scratch_theme/resources... · Scratch Primary Lesson 7...

Scratch Primary Lesson 7

Good Guy / Bad Guy Game

creativecomputerlab.com

The Game Idea

• This is a two character game. It has a good guy and a bad guy. 

• You (the player) are the good guy.• You start out with 5 lives.• You must escape from the bad guy.• You can move by using the arrow keys.• If the bad guy touches you, you lose one life.

creativecomputerlab.com

Choose the Characters

• Start a new Scratch project and delete the cat.• Create new sprites. Search through the sprite folders and find:– A good guy – that will be you.– A bad guy.

creativecomputerlab.com

Choose the Characters

• You can choose any characters you’d like. For this example we will use these characters:

• Good guy (or girl): 

• Bad guy (or ghoul):

creativecomputerlab.com

Give the Sprites Their Own Names• GoodGirl:

• BadGuy:

• Giving your own name to sprites makes them easier to find when you are making your scripts.

creativecomputerlab.com

Moving the Good Guy• First, we will enable the player (good guy) to move around by using the arrow keys.

creativecomputerlab.com

Moving the Bad Guy

• Second, we will make the bad guy move around the screen automatically:

creativecomputerlab.com

How to Score?

• A score is made when the bad guy touches the good guy. The good guy loses one life.

• We need to make a script that detects when the sprites are touching.

• The script logic is: “If touched by the bad guy, the good guy loses one life”. 

• We will put this script in the good guy’s script area.

creativecomputerlab.com

Sprite Sensing

• The sensing blocks help a sprite sense the world around it.

• We want the good guy sprite to know when it is touching the bad guy sprite.

creativecomputerlab.com

Good Guy Scoring Script• Use the “if” and “touching” blocks:

• What block do we need to use so that the good guy is always checking whether it is touching the bad guy? 

creativecomputerlab.com

Good Guy Scoring Script

• The good guy must keep checking whether the bad guy is touching. This is called “polling”.

• We use the “forever” loop to keep checking constantly: 

creativecomputerlab.com

Test the Game• Click the green flag and test whether the good guy says something when it is touched by the bad guy:

creativecomputerlab.com

Keeping Score

• We need to write a script to keep score.

• The script must:– Start the game with 5 lives– Subtract 1 life when touched by the bad guy– End the game when good guy runs out of lives

• We must keep the current score in a variable

creativecomputerlab.com

Variables and Data• A variable is a place to hold a number or letters so it can be used by the computer.

• The value of the data which the variable is holding can be any value and it can be changed at any time. It is called a variable because its value can vary. 

• In our game, the score is the data. We are going to see how we can create a variable to hold the score…

creativecomputerlab.com

Variables in Scratch• Go to the “Data” palette to create a variable. Notice that there are no variables created yet.

• Click the “Make a variable” button.creativecomputerlab.com

Make the “Lives” Variable

• Give the new variable the name “Lives”– Make it for all sprites:

creativecomputerlab.com

Make the “Lives” Variable

• The variable “Lives” is created with value 0:

• You can:• Set “Lives” to any value.• Change “Lives” by any value.• Hide “Lives”.• Show “Lives”.

creativecomputerlab.com

Adding Scoring to The Game• We want to set “Lives” to 5 when the game starts.

• Every time the bad guy touches the good guy, we lose 1 life:

creativecomputerlab.com

Adding Scoring to The Game• This is how the project should look:

– Click the check box by the “Lives” variable to show it

creativecomputerlab.com

Click the Green Flag and Play The Game

• What happens to the good guy’s lives?

• What new features would you like to put into the game?

creativecomputerlab.com

New Features For Good Guy / Bad Guy Game

• End the game when “Lives” becomes equal to 0.• Find a way to win the game:

– Create a timer and see how long you can last.– Create another “health” sprite that adds one life when the good guy touches it.

• Create a game over screen.– Create a “You Lose  ” screen.– Create a “You Win ☺” Screen.

creativecomputerlab.com