Sea Battle - Part 1

Scratch GameHard

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

1

Create a new Scratch 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:

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

2

Program the backdrops

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)


3

Program the Start button

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


4

Move your battleship

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



5

Fire your torpedo

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



6

Create some variables

We'll need to create some variables to store a few things. Create the following variables:

  • 'sub start x' this is the x value of where the submarine will start (either on the left or the right of the screen)
  • 'et x' this is the x value of where the enemy torpedo will start from when it's fired.
  • 'et y' this is the y value of where the enemy torpedo will start from when it's fired.
7

Program the submarines

We're now going to program submarines to randomly appear either on the left or the right and move across the screen. The 'submarine' sprite has two different submarine costumes, a large sub and a small sub and we'll switch between these.

Add the following code to the 'submarine' sprite:

when I receive [start v] set rotation style [left-right v] // this stops them appearing up-side-down hide forever wait (pick random (1) to (5)) seconds next costume create clone of [myself v] end when I start as a clone if <(pick random (1) to (2)) = (1) > then set [sub start x v] to (-180) // start on the left point in direction (90) // point towards the right else set [sub start x v] to (180) // start on the right point in direction (-90) // point towards the left end go to x (sub start x) y (pick random (-170) to (10)) // the range of the y value is the depth of the sea show repeat until < touching (edge v)> if < touching (my torpedo v)> then // if you hit it with a torpedo then delete it wait (0.2) secs delete this clone end move(2) steps end delete this clone

That was a lot of code you added! Test your code to make sure that:

  1. You can move your battleship
  2. You can fire torpedoes
  3. The enemy submarines appear
  4. They disappear when you hit them with a torpedo

If you any of them are not working correctly, try and find the piece of code that is incorrect.




8

Fire the enemy torpedoes

Finally we need to get the enemy subs to fire their torpedoes! To do this we need to save the position of the submarine (in the 'et x' and 'et y' variables) and then send a 'fire' message. We'll use this message in the next step.

For now add the following code to the 'submarine' sprite, underneath the move 2 steps block:

when I start as a clone if <(pick random (1) to (2)) = (1) > then set [sub start x v] to (-180) point in direction (90) else set [sub start x v] to (180) point in direction (-90) end go to x (sub start x) y (pick random (-170) to (10)) show repeat until < touching (edge v)> if < touching (my torpedo v)> then wait (0.2) secs delete this clone end move(2) steps // add code underneath here if <(pick random (1) to (50)) = (1)> then // we don't want the subs continually firing torpedoes so we use this random block to space them out set [et x v] to (x position) // remember the x position of the sub set [et y v] to (y position) // remember the y position of the sub broadcast (fire v) end end delete this clone

9

Program the enemy torpedo

Now let's program the 'enemy torpedo' to fire when it receives the 'fire' message.

Add the following code to the 'enemy torpedo' sprite:

when I receive [fire v] create clone of (myself v) when I start as a clone point in direction (90) switch costume to (normal v) go to x (et x) y (et y) show repeat until < touching (edge v)> if < touching (battleship v)> then switch costume to (hit v) // the explosion costume of the enemy torpedo wait (0.2) secs delete this clone end change y by (4) end delete this clone



10

Well done!

You've completed part 1 of this project! Play the game and see how long you can last before you get hit!

In part 2 of this project we will:

  1. Add a score and lives
  2. Add some sound effects
  3. Add some animation effects
  4. Display the game over screen when you run out of lives
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