Book Image

Instant HTML5 2D Platformer

By : Aidan Temple
Book Image

Instant HTML5 2D Platformer

By: Aidan Temple

Overview of this book

Game development has traditionally only been accessible to those with experience in computer science and access to the best or most expensive development tools. However, with the advent of technologies such as HTML5 and the ability to self-publish, web-based games such as those on Facebook and smartphones are becoming more attractive to develop than ever before. Through the use of open technologies such as HTML5, anyone with even a basic understanding of games development can begin to develop video games in their spare time and publish them to the Web or as an application for mobile devices. Instant HTML5 2D Platformer is a practical, hands-on guide that provides you with a number of clear, step-by-step, task-based exercises, which are used to discuss game development and put into practice development techniques through the use of HTML5 and JavaScript. This book looks at the creation of a 2D platform-based game using the HTML5 canvas element. Instant HTML5 2D Platformer introduces you to HTML5 canvas through a number of exercises, which show what the canvas is capable of. The book contains a number of clear, practical, hands-on tasks that incrementally build on the concepts of game creation and result in a 2D HTML5 platform-based game. By undertaking the tasks within this book, you will learn how to develop your own 2D HTML5 game framework that you can use in the creation of your own video games, not just the game developed within this book. Alongside this framework you will learn how to develop and understand 2D animation, game logic, and how to handle user input devices.
Table of Contents (7 chapters)

Adding pickups (Must know)


To allow the player to do more than just jump and avoid enemies, this recipe will focus its attention on the implementation of collectible items known as pickups. These pickups will be similar to the idea of collecting coins in Mario or gold rings in Sonic and will contribute to the player's score and increase the sense of enjoyability, that is, how fun the game is.

How to do it...

  1. In order to implement pickups into our application, we will follow the exact same steps we took when implementing enemies.

  2. We start off by creating a new object that represents a pickup. We begin by creating the pickups object, which is as follows:

    function Pickup() {
    
      this.InitPickup = function(texture, x, y, z, frameCount, frameRate) {
      this.InitAnimationManager(texture, x, y, z, frameCount, frameRate);
        return this;
      }
    
      this.DisposePickup = function() {
        this.DisposeAnimationManager();
      }
      
      this.Update = function(deltaTime, context, deltaX, deltaY) {
        if(this.BoundingBox().Intersects(player.BoundingBox()))
        this.DisposePickup();
      }
    }
    
    Pickup.prototype = new AnimationManager;
  3. The next step involves creating a new pickup array object within the Level object and filling this array with the positions of a number of collectible items, that is, pickups, inside of the Level constructor. Insert the following code at the top of the Level script:

    this.pickup = new Object();
    
    this.tileWidth = 0;
    this.tileHeight = 0;
    
    this.InitLevel = function(width, height) {
    this.tileWidth = tile.width;
      this.tileHeight = tile.height;
        
      for(var i = 0; i < 50; i++) {
        this.tiles[i] = 1;
      }
    
     this.pickup['1'] = 'Berry';
    
           this.AddTile(width, height);
      this.AddPickup(width, height);
    
           return this;
    }
  4. We then need to implement a function that will draw each of the pickups to the canvas at the position specified within the pickup object array.

    this.AddPickup = function(width, height) {
    
      var x, y;
    
    for(var i = 0; i < this.tiles.length; ++i){
    
    if(this.pickup[i]){
          x = i * this.tileWidth + this.tileWidth / 2;
          y = height - this.TerrainHeight(i);
    
    if(this.pickup[i] == 'Berry') {
    new Pickup().InitPickup(berry, x - berry.width / 2, y - berry.height, 6, 1, 1);
           }
        }
      }
    }
  5. The final step required to implement and draw a pickup to the canvas involves loading the necessary texture for the pickup into the Main object.

    var berry = new Image();
    berry.src = "textures/berry.png";

How it works...

The Pickup script creates a new pickup object that represents an animated object similar to the player object. The Pickup constructor takes in a texture parameter, as well as a 2D position, depth position, frame count, and frame rate parameters. The texture is a 2D sprite sheet, which is used to animate the enemy. The x, y, and z positions represent the position of the pickup on the canvas as well as the position of the pickup texture within the list of layered textures within the game.

Both the x and y positions are measured in pixels and the z position represents layer depth. The frame count is used to determine how many frames make up the pickup sprite sheet and finally the frame rate dictates how many frames the enemy texture should be played at each second.

The update function within the Pickup object is used to check for any collisions between the player and a pickup object. If there is a collision then the pickup that collided with the player is removed from the game and the player has part of their score increased. This collision detection makes use of the Rectangle object, which checks whether or not two rectangles are overlapping and if so states that a collision has occurred.

Inside of the Level object, we declare and initialize a new pickup object array. Inside of the Level constructor, we then assign a position for a new pickup object to be drawn to. This position is then stored inside of the pickup object array and passed into the AddPickup function.

This function loops through each tile within the levels tile array and then loops through the pickup object array and draws a pickup at the position above the tile that it corresponds to.

The final code extract refers to loading the pickup texture into the application. This is done exactly as we previously did for each of the textures within the game. A new pickup image object is created within the Main object and an external path that shows the location of the pickup texture is passed into the image object in question.