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.
In a separate lesson you should have completed part 1 of this project. Open your project in Scratch and continue to the next step in this lesson.
Or if you wish you can use this starter project that has part 1 completed.
We need to create 2 more variables to store our score and lives. Create the following variables:
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)
In the Variables palette, create a new variable by clicking the 'Make a Variable' button.
Once you click this button a box will appear asking what you want to call your variable. Give it a name that reminds you what you will be using it for. For example, if you wanted to keep track of your score in a game, you would create a variable called 'score'.
Now let's add 1 to your score each time you hit a submarine.
In the submarine sprite, find the 'if touching my torpedo' code block 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.
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.
If you're using a tablet or iPad without a physical keyboard, you won't be able to use keyboard keys like the arrow keys in your project. Instead, we'll add simple on-screen buttons (using sprites) that you can tap to do the same things. This keeps your project working great! Just follow these steps wherever the lesson talks about pressing a key.
First, add a new sprite for your button:
Now, program your button sprite to make things happen when you tap it. Here's how it works for different situations:
Example 1: If the lesson uses a key to trigger an action on a specific sprite (like making something move, turn, or jump)
Instead of code like this on your target sprite:
when [left arrow v] key pressed
change x by (-10) // or any action
Or this:
if < key [left arrow v] pressed? > then
change x by (-10) // or any action
end
Add this code to your new button sprite:
when this sprite clicked
broadcast [do action v]
Then, on your target sprite, add this to receive the message:
when I receive [do action v]
change x by (-10) // or any action
Tap the button on the screen, and the action will happen, just like pressing the key! Use a unique broadcast name for each different action or key.
Example 2: If the lesson uses a key to change a variable (like adding to a score or setting a value)
Instead of code like this:
when [space v] key pressed
change [score v] by (1)
Or this:
if < key [space v] pressed? > then
change [score v] by (1)
end
Add a new button sprite. Then, put this code on the button sprite:
when this sprite clicked
change [score v] by (1)
(If the variable is "for this sprite only," make sure it's set to "for all sprites" so the button can change it.)
Tap the button, and the variable changes, no message needed since it's something shared!
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
If you're using a tablet or iPad without a physical keyboard, you won't be able to use keyboard keys like the arrow keys in your project. Instead, we'll add simple on-screen buttons (using sprites) that you can tap to do the same things. This keeps your project working great! Just follow these steps wherever the lesson talks about pressing a key.
First, add a new sprite for your button:
Now, program your button sprite to make things happen when you tap it. Here's how it works for different situations:
Example 1: If the lesson uses a key to trigger an action on a specific sprite (like making something move, turn, or jump)
Instead of code like this on your target sprite:
when [left arrow v] key pressed
change x by (-10) // or any action
Or this:
if < key [left arrow v] pressed? > then
change x by (-10) // or any action
end
Add this code to your new button sprite:
when this sprite clicked
broadcast [do action v]
Then, on your target sprite, add this to receive the message:
when I receive [do action v]
change x by (-10) // or any action
Tap the button on the screen, and the action will happen, just like pressing the key! Use a unique broadcast name for each different action or key.
Example 2: If the lesson uses a key to change a variable (like adding to a score or setting a value)
Instead of code like this:
when [space v] key pressed
change [score v] by (1)
Or this:
if < key [space v] pressed? > then
change [score v] by (1)
end
Add a new button sprite. Then, put this code on the button sprite:
when this sprite clicked
change [score v] by (1)
(If the variable is "for this sprite only," make sure it's set to "for all sprites" so the button can change it.)
Tap the button, and the variable changes, no message needed since it's something shared!