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)

Preface

To begin with, the book will guide you through an introduction of the HTML5 canvas and some of the functionality that it provides to web and games developers. With an understanding of the HTML5 canvas, the book then gears its attention towards the creation of the 2D platform game in question by guiding you through the steps required to implement a basic game framework for any 2D HTML5 game.

The book will then progress iteratively through a number of components you might find within a 2D platform-based game. These components include monitoring and updating user input through means of a keyboard. Also covered are implementing a parallax background, adding enemies, and collectible items similar to the concept behind the coins in Mario and gold rings in Sonic. We will also look at implementing a basic form of physics, which will be used to allow the player to jump as well as introducing sound effects to the game to enhance its appeal.

What this book covers

Setting up your HTML5 canvas (Should know) gives an overview of how to set up the HTML5 canvas as well as some of the basic functionality the canvas has to offer and how we would go about implementing that functionality.

Implementing the game framework (Should know) is a step-by-step guide to the creation of a basic 2D game framework that takes advantage of polymorphism and which will be incrementally improved in each of the recipe. This framework will be responsible for updating and rendering game objects.

Creating the player (Must know) will look at the steps taken to implement a player class that will fit seamlessly with our game framework in order to load and draw the necessary assets to the canvas.

Handling user input (Must know) will improve upon the game framework introduced in an earlier task as well as enhance our player class to respond to the user's input. By responding to the user's input, we can move our player freely around the canvas.

Animating the player (Must know) will introduce an animation manager, which will allow us to load and render sprite sheet animations. We will also adjust the player class to take advantage of the animation manager so that we can animate the player.

Creating the level (Must know) will introduce a level class that will be responsible for loading and drawing a 2D tile-based level. With this level we can specify the layout of our level as well as the positioning of collectible items and the spawn locations of enemies.

Implementing a parallax background (Must know) will implement a new class, which will be responsible for drawing and updating each of the textures that make up the background. This updating will involve moving each of the background layers at differing speeds to produce a sense of depth within our game.

Implementing physics (Must know) explains how no game would be complete without some form of physics therefore we will implement this in the basic form of jumping. By introducing jumping we can implement more imaginative levels for the player to traverse as well as a means of avoiding enemy units.

Creating enemies (Must know) will improve upon the level class and implement an enemy class therefore allowing for the spawning of enemy units and updating their positions as they move around the level.

Adding pickups (Must know) will introduce the ability to collect items as a means of scoring points, which can also be a great way to compare your score with your friends.

Adding sounds (Must know) will look at implementing audio into our game that will be played each time the player collects an item or when the player take damage from an enemy.

Creating a graphical user interface (Must know) will draw a basic user interface to the canvas, which will be updated in real time to show the player's score and remaining health.

What you need for this book

As this book is intended to be used for the creation of a web-based game it is therefore necessary to have some form of text editor or an integrated development environment to work with. Readers who use a Microsoft-based operating system will be able to use Notepad whereas Mac readers will be able to use TextEdit. However, there are alternative development tools freely available online that are recommended rather than using a basic text editor. These tools include:

  • Brackets is an open source editor for web design and development built on top of web technologies such as HTML, CSS, and JavaScript and is available for both Microsoft and Mac operating systems. (www.brackets.io)

  • Notepad++ is a free to use open source code editor that supports several programming languages and that is a replacement for Microsoft's Notepad. (www.notepad-plus-plus.org)

  • Aptana Studio is a complete integrated development environment for Microsoft, Mac, and Linux operating systems that include extensive capabilities for the creation of applications along with support for HTML, CSS, and JavaScript support. (www.aptana.com)

Who this book is for

This book has been written as a means of showing those with an interest in games development and who have a basic to intermediate understanding of the workings of HTML and JavaScript how to produce a 2D HTML5 platformer-based game. Therefore this book will not discuss the inner workings of either of these languages but will instead attempt to explain how the code within this book functions.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: " In order for our scripts to have any effect on our canvas we must create a separate file called canvas example."

A block of code is set as follows:

context.beginPath();
context.arc(350,150,40,0,2 * Math.PI);
context.stroke(); 

New terms and important words are shown in bold.

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.