Sea Battle - Part 2

Scratch GameHard

In this project we make a game where you captain a ship and battle against submarines and planes! This is part two of this project.

1

Complete part 1 of this project

In a separate lesson you should have completed part 1 of this project. Open that project and continue to the next step.

Or if you wish you can use this starter project that has part 1 completed.

 https://scratch.mit.edu/projects/293643968/editor/

2

Add 'score' and 'lives' variables

We need to create 2 more variables to store our score and lives. Create the following variables:

  • 'score' will store our score
  • 'lives' will store how many lives we have left

Once you've created these new variables we can set their values for the start of the game. Add in the two new blocks under the 'when I receive start' block in the battleship sprite:

when I receive [start v]// add them under here set [lives v] to (3)//start off with 3 lives set [score v] to (0)//set score to 0 at the start go to x (0) y (46)
3

Score a point

Now let's add 1 to your score each time you hit a submarine.

In the submarine sprite, find the 'if touching my torpedo' sprite and add the 'change score by 1' block into it.

if < touching (my torpedo v) > then // find this block change [score v] by (1)// add this new block wait (.2) seconds delete this clone end

Once you've added this block in, test that it works by playing the game. Each time you hit a submarine your score should go up by 1.

4

Lose a life

Next add some code so you lose a life each time you get hit by an enemy torpedo.

Add the following new 'if then' group of blocks inside the 'forever' block in the battleship sprite.

when I receive [start v] go to x (0) y (46) point in direction (90) switch costume to (battleship v) show forever if < key (right arrow v) pressed?> then change x by (5) end if < key (left arrow v) pressed?> then change x by (-5) end if < touching [enemy torpedo v] ? > then // add in these new blocks change [lives v] by (-1)//lose a life end end

Test that this works before moving onto the next step.

5

Game over

Finally add the following code finish the game when you've no lives left.

Again add the following new 'if then' group of blocks inside the 'forever' block in the battleship sprite.

when I receive [start v] go to x (0) y (46) point in direction (90) switch costume to (battleship v) show forever if < key (right arrow v) pressed?> then change x by (5) end if < key (left arrow v) pressed?> then change x by (-5) end if < touching [enemy torpedo v] ? > then change [lives v] by (-1) end if < (lives) < (1) > then // add in these new blocks hide switch backdrop to (game over v) broadcast (gameover v) end end

6

Extra Challenges

Now that you've finished the project why not try and make your game better by adding some effects into your game.

Sound Effects
Can you add some sound effects into the game? For example if you wanted to add sound effects for when a torpedo hits you or an enemy, you would put them in the same place in the code that you get a point or lose a life.

play sound [Clang v]

Animations
You could also add in new costumes to the torpedoes, that make them look like they explode when they hit.

switch costume to [Explode v]
End of lesson
Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.

Scratch is developed by the Lifelong Kindergarten Group at the MIT Media Lab. It is available for free at https://scratch.mit.edu