FlappyBird6

Download and Open Scenario

  1. Click on the following link to download the Scenario: FlappyBird6

  2. Select Open.

  3. Copy the folder FlappyBird6 and paste it in your MyGreenfoot folder.

  4. Launch Greenfoot and select Open from the Scenario menu.

  5. Navigate to your MyGreenfoot folder, select the FlappyBird6 Scenario, and click Open.


In this lesson a scrolling background is added to the game.

Scrolling Background

The following image is assigned as the background for FlappyWorld.

Since the size of the image (139 x 400) is smaller than the size of the world (600 x 400), the World class automatically duplicates the image horizontally and vertically as much as needed to completely fill the world.

The original image was specifically designed so that when it is duplicated it fits together like the pieces of a puzzle.

There is, however, a problem with the image design as it relates to the dimensions of the world. If you look closely at the background image created by Greenfoot the left and right edges do not match up. This image will not work as a scrolling background because the left edge needs to fit seemlessly with the right edge to create a smooth transition as the background is scrolled.

To fix this problem I either had to redesign the original image so that its width would divide evenly into 600 or I just needed to increase the size of the world so that the original image divided evenly into it. The second option was easier so I changed the dimensions of the world to 695 x 400. Recall that the original image had dimensions (139 x 400). The reason I choose 695 is because 139 x 5 = 695. Using a width of 695 allowed Greenfoot to use exactly 5 original images to create a background image that matched up on both ends.

FlappyWorld class

To create the scrolling background effect a method named scrollBackground was added to the FlappyWorld class and called in its act method. A constant named SCROLL_SPEED was also added and assigned a value of -3 so that the background image scrolls from right to left.

For an explanation of how this code works refer to unit 3.

Program Modifications

Currently, pipes are created off-screen and then instructed to move left to right across the world. But after a pipe has slid across the world and is no longer visible, where do they go? The answer is: they keep going. The longer you play, the more off-screen pipes there are taking up valuable memory space and processing power. Modify the Pipe class so that pipes are removed when they are no longer visible on the screen.

To complete this task do the following:

  1. Create a method in the Pipe class named deleteMe.

  2. Implement the deleteMe method using the following pseudocode:

    if pipe at left edge of world then
       getWorld().removeObject(this)
    end if

    Hints

    Note: if a pipe is traveling across the world from the right edge of the world to the left edge of the world what is the pipe's x-coordinate when it reaches the left edge of the world? This value should control when your if statement becomes true.

    Note: this is a keyword in Java that means "this" object. In this case it means "this" pipe.

  3. Make a call to the deleteMe method in the Pipe class's act method.

  4. How do you know if your code is actually removing pipes?

    To find out add the following code to FlappyWorld act method:

    System.out.println(getObjects(Pipe.class).size());

    This code displays how many pipes are currently in the world. If it continually displays the number 4 your code is working properly.