In this project we make a game where you captain a ship and battle against submarines! This is part one of two parts of this project.
We've created a starter project that has some custom backdrops and sprites already setup. Open this starter project by clicking on the following link:
There are 3 backdrops included with this project, a 'title' backdrop to appear at the start of the game, a 'sea' backdrop to appear while you play the game and a 'game over' backdrop to appear when the game is finished.
Add the following code to the backdrop:
when green flag clicked
switch backdrop to (title v)
when I receive [start v]
switch backdrop to (sea v)
when I receive [gameover v]
switch backdrop to (game over v)
Messages are a great way to start a piece of code at a particular time. To create a new message follow these steps:
When the instructions screen is showing, we also want to display a Start button that will start the game when the user clicks on it.
Add the following code to the 'Button2' sprite:
when green flag clicked
show
when this sprite clicked
broadcast [start v]
hide
when I receive [gameover v]
show
Now we will setup the 'battleship' sprite for the start of the game and code it to move when you press the left and right arrows. Add the following code to the 'battleship' sprite:
when green flag clicked
set size to (50) %
hide
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
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!
We're going to program the 'x' key to fire our torpedo when it is pressed. When fired, the torpedo will continually move down until it touches a submarine or the edge of the screen.
Add the following code to the 'my torpedo' sprite:
when [x v] key pressed
create clone of (myself v)
when I start as a clone
switch costume to (normal v)
go to (battleship v) // the torpedo needs to start at the same current location of your battleship
show
repeat until < touching (edge v)>
if < touching (submarine v)> then
switch costume to (hit v) // this is the explosion costume of the torpedo
wait (0.2) secs
delete this clone
end
change y by (-4) // move it down
end
delete this clone
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!