In this codelab, you'll make a game where the player guides a fish to swim between pairs of pipes.

What you'll learn

Open the starter project and click Remix.

First, we are going to set the fish's starting conditions. We will be using two variables: Score and Speed.

Steps

  1. Create new Score and Speed variables.
  2. Set the speed at the beginning of the game to 0.
  3. Set the score at the beginning of the game (What should the score at the beggining of the game be set to?).
  4. Start the fish at the center of the stage.

The fish will naturally fall towards the bottom of the screen (due to gravity!) The fish will fall until he reaches the floor.

Steps

  1. First, have the fish move down the screen by the amount stored in Speed .

  2. Change Speed by -0.3 so that the fish accelerates (falls faster as time goes on).

  3. The above two actions should happen until the fish touches either a pipe or the floor. Hint: What color are both the pipe and the floor?

  4. When the fish hits a pipe or the floor, play a sound to show that the game has ended. You can choose which sound to play!

Now, we will program the fish to jump whenever the player presses the space bar.

Steps

  1. The sprite should only jump when the space bar is pressed. Hint: Which Hat block/Event block should we use?
  2. Make the fish's speed go up by 6.

Now, we'll move on to the pipes. Even though it seems like the fish is moving to the right, the pipes will actually start on the right of the screen, then move to the left of the screen.

Steps

  1. Set the size of the pipes to 200%. This makes the pipes (and the opening between the pipes) bigger, so the game is a little easier to play!

  2. Start the pipe at a random position near the right of the screen. You can do this by setting the x position to 240 and the y position to a random number between -20 and 60. Hint: What special operator do you need for choosing random numbers? Fill in the blank in the picture below.

  3. Make the pipes move 2 steps towards the left of the stage.

  4. If the pipes get to an x position of -230, that means the fish has passed it! When this happens, the pipe should go back to a random position on the right of the screen , and the score should increase by 1. Hint: Use a conditional statement!
  5. Steps 3-4 should only happen until the pipe is touching the fish - if the pipe and fish are touching, the player has lost the game! Hint: How can you use the "repeat until" block to do this?

Once you have a working Flappy Fish, choose one or more additional features to add!
  1. Create a "game over" backdrop that appears when the fish runs into a pipe or the ground and displays the player's score
  2. Track your best runs by creating a variable for "high score"!
  3. Make your own background! You can do this by changing the backdrop. If you want to make the fish smaller, you can do that with a block in the Looks category
  4. Change the sprites for the player and the pipes
When you're finished, be sure to save and share your project, and then add it to the studio:
  1. Giga studio .
  2. Nano studio .