Portfolio: Flappy Bird Game (Lab 10)

This is Lab 10 in Developing Web Pages (BUS1880) and Web Fundamentals (CSCI1450).

This lab builds on the Portfolio website from Lab 9. Make sure that lab is complete before starting this one.

You’re about to build and customize your very own playable Flappy Bird game. We’ve spent weeks focused on HTML and CSS, but here you’ll see that modifying JavaScript is well within reach, and you’ll walk away with a game you can actually play and show off.

This lab’s Flappy Bird game is adapted from an older W3Schools tutorial. The newer version on their site dropped the gravity animation that makes the bird float and drop naturally, so this older version plays better, but if you’re curious about the current one, it’s worth a look.

Jump to:

Before we start work, let’s play the game!

Click the Accelerate button to fly the black square. Game over? Click here to restart.

The game you played above is the starting point of the game. As you can see, it is modeled after the famous Flappy Bird game. In the lab you will decorate the game with CSS and images. See below for the before and after screenshots.

Before and after screenshots of the Flappy Bird video game showing the plain starting version and a decorated version with a background image, custom colors, and styled buttons.
The Flappy Bird game before and after adding a background image and making design modifications.

On the AFTER example, there are several visible changes to the game.

  • Before the game there are some instructions on how to play.
  • The game canvas has a background-image, border, and border-radius.
  • The colors of the game piece and barriers have been changed.
  • The Accelerate button text has been changed to Flap Wings, and Play Again button has been added. Moreover, the buttons now are designed with CSS.

A. Integrate the Code into Your Website

This is the most difficult part of the lab. Once you have the code integrated into your portfolio, the rest will be easier. You can either try to do this on your own using the comments in the game.html file, or use the step-by-step instructions below.

Step 1. Download and Test the Starting Point

Download the starting point zip file, extract it, and move the game.html file into your portfolio folder.

📎 Download Game Code

Open game.html in a browser to make sure the game works.

Take a look inside the file: the document structure (<!DOCTYPE html>, <html>, <head>, and <body onload="startGame()">) is already set up for you, with comments marking exactly where each piece from your own portfolio goes.

Step 2. Merge Your <head> Content

The <head> of the index.html in your portfolio already has some important contents that you need for game.html. None of that exists yet in game.html. But game.html already has one piece you need to keep: this embedded <style> block that styles the game canvas. You will be able to add to and modify the CSS in the head to customize your game.

Copy everything from inside the <head> of index.html and paste it into the <head> of game.html, above that existing <style> block. Don’t delete the <style> block while you’re pasting the rest in — it needs to stay.

Step 3. Copy Over Your <header> and <nav>

Start by finding this code in game.html. This is what makes the game start when the page loads — without it, nothing will happen.

<body onload="startGame()">

Right after the <body onload=”startGame()”>, add your site’s <header> and <nav> from your index.html, so your game lives on a page that matches the rest of your site.

Step 4. Wrap the Game in a <main> Section

The game content — the <div id="game">, the button, and the <script> — is already sitting between the two comments marking where <main> should open and close. Add the opening <main> tag right after your nav, and the closing </main> tag where the comment marks it, right before the closing </body>.

Step 5. Add Your Footer

Copy the <footer> from index.html and paste it at the end of the page, right after the closing </main> tag.

Step 6. Check the Game

At this stage, your web page should look like this, but with your header, nav and footer:

Before you move on, check the following:

  • Make sure your header, navigation and footer are in place
  • Make sure the game is working

If your game isn’t working, check this first:

  • Make sure your <body> tag still has onload="startGame()" on it — it’s easy to accidentally overwrite when you’re adding your header and nav right after it.

If you still can’t find your error, your best strategy may be to delete the game.html file completely and start over.

B. Make Five Design Modifications

After confirming that your game still works, make at least five design modifications. Choose any five of the following modifications to make the game your own. (Many students make a lot more than 5 changes.) You may want to think of a theme so your choices feel cohesive. If you need inspiration, jump to the gallery at the end of this page.

Check that the game still works after each change — don’t wait until you’ve finished all five to test, or you could lose track of which change broke it, or lose all your work. If something stops working, simply press Ctrl+Z (Windows) or Cmd+Z (Mac) to undo that last change.

Design Canvas Appearance

Quick context: what is a canvas? A <canvas> is a blank drawing surface in HTML. Unlike the HTML elements you have used so far, which display their content directly, a canvas stays empty until JavaScript draws on it. You will not find a <canvas> tag written anywhere in the HTML, though: the game’s JavaScript creates the canvas and inserts it into the page for you. Look for this line in the code: this.interval = setInterval(updateGameArea, 20); — that is the game loop. It reruns the updateGameArea function every 20 milliseconds, about 50 times a second, redrawing the bird and obstacles in a slightly new position each time. That is what creates the animation.

You already know the CSS for most of these from earlier lessons — try any combination. You’ll find a canvas selector already started in the <style> tag in the <head>; just add your properties inside it:

  • background-image
  • background-color
  • border
  • border-radius
  • box-shadow

The best way to make the game your own is to add a background image to it.For the background image, find one on Pixabay.com, save it in your images folder, then add this to the existing canvas selector CSS in the game <head>:

background-image: url(images/my-background.jpg);
background-size: cover;

Design Game Piece

The component() function creates every object in the game, like the game piece and the barriers. You will see this code in the lab:

myGamePiece = new component(30, 30, "black", 10, 120);

Here is a detailed explanation of the code:

  • myGamePiece: the variable name for the flying square (or bird)
  • new component: this creates the game piece
  • 30, 30, "black", 10, 120: these set the initial properties of the game piece
    • 30: the width of the game piece (in pixels)
    • 30: the height of the game piece (in pixels)
    • “black”: the color or image source for the component
    • 10: the initial x-coordinate (horizontal position) on the canvas
    • 120: the initial y-coordinate (vertical position) on the canvas

This code creates a 30×30 pixel black square, positioned at coordinates (10, 120) on the game’s canvas. You can change these properties to match your design.

Heads up: this parameter breakdown is for the default game piece type. The score uses the same component() function with different parameters: myScore = new component("30px", "Consolas", "black", 420, 40, "text"); Because the last parameter is "text", the first two values become the font size and font family instead of width and height.

To change the game piece color, replace "black" with any color name or hex value.

Design Obstacles

To change the obstacle color, find both obstacle lines and replace "#333333":

myObstacles.push(new component(20, height, "#333333", x, 0));
myObstacles.push(new component(20, x - height - gap, "#333333", x, height + gap));

Button Text and Style

Rename the button to fit your game theme. The default button text, ACCELERATE, is most likely not a good fit for your game. I used FLAP WINGS for the game I made. Something like JUMP, FLY, or BOOST might work better, depending on your theme.

You can also add a Play Again button that reloads the page. Here is the code for it:

<a href=""><button class="myButton">PLAY AGAIN</button></a>

Why an empty href? An empty href="" points back to the current page, so clicking the link reloads it, which resets the game. Do not use href="#" instead: a # just jumps to the top of the page without reloading it, so the game would not actually restart.

Style both buttons with CSS using the .myButton class. Try adding a background-color to match your design, and some padding to give the button more space.

C. Add Title and Game Details

Complete all of the following:

  • Add a page title in an <h1> heading. You can simply name it JavaScript Game, or something more creative that fits your theme.
  • Add a comment recognizing W3 Schools for the game and explaining how you modified it. See the text below for ideas.
  • Add some game instructions so the website visitor knows how to play the game.

Here is some code and text that recognizes W3 schools and your work on the game. Be sure to change the text to fit your game changes and button name.

<p>I made this game using the <a href="https://www.w3schools.com/graphics/game_intro.asp">W3Schools JavaScript tutorial</a> as a starting point. I enhanced the game with [describe your changes here].</p>

<p>Use the [your button's name] button to stay in the air. How long can you fly?</p>

Game Alignment

Center the canvas on the page by adding this to your CSS:

canvas {
    display: block;
    margin: 0 auto;
}

Center the buttons too, using the .game-controls class:

.game-controls {
    text-align: center;
}

Check Your Work

When you have completed Section C, your game page should include a title, game instructions, recognition for W3 Schools, and a centered canvas. Compare your work against the example below before moving on:

Example of a completed Flappy Bird game page showing a title, game instructions, W3 Schools credit, and a centered game canvas inside a student portfolio website.

D. Make the Game Responsive

This section is required for CSCI1450 (Web Fundamentals) and optional for BUS1880 (Developing Web Pages).

Check your game at a mobile size in your browser — you will notice two problems, as you can see in the screenshot below:

This screenshot shows one of the problems on mobile devices. The set size of the game canvas does not fit on a narrow screen.

Problem 1: The canvas is too wide for mobile. The game canvas has a fixed size of 640 × 360 pixels. On a narrow phone screen, that is too wide and breaks your overall page layout.

Problem 2: The button does not respond to touch. Your button uses onmousedown and onmouseup — events that fire when a mouse button is pressed and released. Touchscreens don’t have a mouse, so these events do nothing on a phone. The fix is to add touch equivalents: ontouchstart fires when a finger touches the screen, and ontouchend fires when the finger lifts off.

Step 1: Add Canvas Size Variables

Quick context: what is a variable? A variable is a named container that holds a value you can reuse and change later. Instead of typing the number 640 everywhere you need the canvas width, you store it once in a variable like canvasWidth, then use that name throughout your code. If you ever need to change the value, you only have to update it in one place.

Find these three lines at the very top of the <script> tag, and insert the new code right after them:

/* Find these variables in the code */
var myGamePiece;
var myObstacles = [];
var myScore;

/* Insert this code after those three variables */
var canvasWidth = 640;
var canvasHeight = 360;
/* If the screen is less than 768px wide, use a smaller canvas */
if (window.innerWidth < 768) {
    canvasWidth = 300;
    canvasHeight = 300;
}

Step 2: Use the Variables for Canvas Size

Find these two lines in your code. They are not far below the variables from Step 1 — look inside the myGameArea object, in its start function:

this.canvas.width = 640;
this.canvas.height = 360;

Replace the fixed numbers with the variables you just created:

this.canvas.width = canvasWidth;   /* Changed to variable */
this.canvas.height = canvasHeight; /* Changed to variable */

Step 3: Create Two Versions of the Buttons

Now we are going to make two sets of game buttons: one for a mouse click on a computer, and one for a touchscreen on a tablet or phone. In this step we’ll create the HTML for both, and in Step 4 we’ll add the CSS that makes the right set of buttons appear and disappear based on the device’s screen width.

Find the <div class="game-controls"> section in your HTML (not inside the <script> tag), and replace it entirely with these two versions — one for desktop (mouse) and one for mobile (touch). The code you need to replace looks like this:

Replace the entire game-controls <div> with this code. Use the same button text you chose in Section B — the example below uses [YOUR BUTTON TEXT] as a placeholder, not literal text to copy:

<!-- Desktop controls: uses mouse events -->
<div class="game-controls desktop">
    <button class="myButton" onmousedown="accelerate(-0.2)" onmouseup="accelerate(0.05)">[YOUR BUTTON TEXT]</button>
    <a href=""><button class="myButton">PLAY AGAIN</button></a>
</div>

<!-- Mobile controls: uses touch events -->
<div class="game-controls mobile">
    <button class="myButton" ontouchstart="accelerate(-0.2)" ontouchend="accelerate(0.05)">[YOUR BUTTON TEXT]</button>
    <a href=""><button class="myButton">PLAY AGAIN</button></a>
</div>
This is how the game looks on a mobile device once the responsive changes are made. The CSS media queries recognize the smaller screen width and serve the smaller size game.

Step 4: Add CSS to Switch Between Button Versions

Add this CSS to hide the mobile buttons on desktop and the desktop buttons on mobile. You can either add this code to the end of style.css or the end of the <style> code in the the game.html <head>:

/* Game button switching for mobile */
.mobile {
    display: none;
}
@media only screen and (max-width: 768px) {
    .desktop {
        display: none;
    }
    .mobile {
        display: block;
        touch-action: manipulation;
    }
}

Step 5: Optional Game Play Tweaks

You may also want to make the game easier to play on the smaller canvas:

  • Make the game piece smaller so it is easier to steer through the gaps on a small screen
  • Adjust the score display position so it appears within the smaller canvas area

E. Optional Extra Credit: Add a Custom Game Piece Image (5 Bonus Points)

A cool change you can make is to change the square game piece into an image. If you are a design student, you could make a really cool game with your own background and your own game piece image. If you need inspiration, jump to the gallery at the end of this page.

Step 1. Make an Image

You need a small image. The code will auto resize it, but smaller is better. You may want to make an animated GIF at https://www.piskelapp.com/. You definitely want an image with a transparent background — make it squarish in shape. You can use the sample bird image below if you prefer:

Save your image to your images folder.

Step 2. Insert the Code

One of the students in CSCI1450 (James Finnemann) figured out how to integrate the W3 Schools code into our projects.

Find this line of code:

myGamePiece = new component(30, 30, "black", 10, 120);

This is the code for the game piece. 30, 30 is the width and height, black is the color, and 10 and 120 are the x and y coordinates.

Change this line to put the image location where the color used to be, and add a new parameter at the end to specify that we have an image:

myGamePiece = new component(30, 30, "images/birdie.png", 10, 120, "image");

Next, make some changes to the component function. You only need to insert the highlighted sections — everything else is already there. The first addition checks if the last parameter is an image and inserts it. The second addition draws the image, turning the old if statement into an else if.

Quick context: what is if / else if? An if statement runs a block of code only when its condition is true. An else if checks a different condition, but only if the first one was not true. Here, the code checks: is this component an image? If so, draw it as an image. If not, fall back to the regular drawing logic that was already there.

First addition: insert this new block into the component function — it doesn’t replace anything, it’s added alongside the existing code.

if(type == "image"){
            this.image = new Image();
            this.image.src = color;
          }

Second addition: this replaces the entire existing this.update function in your code, not just a piece of it.

this.update = function() {
              ctx = myGameArea.context;
              if (type == "image"){
                ctx.drawImage(this.image, this.x, this.y, this.width, this.height)
              }
              else if (this.type == "text") {
                  ctx.font = this.width + " " + this.height;
                  ctx.fillStyle = color;
                  ctx.fillText(this.text, this.x, this.y);
              } else {
                  ctx.fillStyle = color;
                  ctx.fillRect(this.x, this.y, this.width, this.height);
              }
          }

Refer to the screenshot below to see the complete code, with both changes in context:

Here are some examples of how you can change the flappy bird game with background images, custom game pieces, and game text that fits your game story. Note that in the third example a student added images to the bars drawn by the game.

Checklist for Grading

Here is a checklist of what I will be looking for when grading the website:

Content:

Design:


Now you’ve built a playable JavaScript game! Move on to Lab 11 to learn Bootstrap columns and Font Awesome icons.

Screenshot of the video game students make in Lab 10.

Need Help?

Using AI for coding is not allowed in this course, so if you get stuck simply Make an Appointment for office hours. In addition, if you run into a bug as you are working, you may email your websites folder to me at susan.metoxen@saintpaul.edu.

Browse All Lessons

CSS Design Tools