Use HTML5 to create a physical game that can be played in chrome

Source: Internet
Author: User

Introduction

HTML5 technology provides a very broad space for today's web applications to play in the browser. Its powerful features make it no longer difficult for us to develop and play games on the browser. With canvas and powerful JavaScript engine, we can easily develop casual games. Players just need to open their browsers and enjoy the game without installing plug-ins.

This article was made by the ntfusion team, combining the paper saved Papa published on Google Chrome Web store to share with you the experiences of using HTML5 to develop physical games.

Before reading this article, you can try it out by installing save Papa in Chrome Web store.

Build a physical world

All objects in a physical game operate in a set physical world. To create a physical world, we now use the Javascript version of The box2d physical engine. The box2d engine has a good function, debugdraw, which can simulate our preset physical world and show it to us. It is very suitable for rapid game prototype development.

It is the physical world instance in saving Papa drawn by debugdraw:

When building a physical world, we first need to define the relevant physical objects, and then set the relevant physical parameters according to the characteristics of the game.

// Create a physical world instance var world = new b2world (New b2vec2 (0, 9.8), true); var scale = 1/30; /* length unit conversion between pixels and box2d * // create the ground and physical settings var bodydef = new b2bodydef (); var body = World. createbody (bodydef); var fixturedef = new b2fixturedef (); fixturedef. density = 10; fixturedef. friction = 0.3; fixturedef. restitution = 0.1; var polygonshape = new b2polygonshape (); polygonshape. setasbox (800 * scale, 20 * scale); fixturedef. shape = polygonshape; body. settype (b2body. b2_staticbody); body. createfixture (fixturedef); body. setposition (New b2vec2 (400 * scale, 490 * scale); // create the papa block and its physical settings var bodydef = new b2bodydef (); var body = World. createbody (bodydef); var fixturedef = new b2fixturedef (); fixturedef. density = 10;/* density is the density */fixturedef. friction = 0.3;/* friction is the friction coefficient */fixturedef. restitution = 0.8;/* restitution is the elastic coefficient */var polygonshape = new b2polygonshape (); polygonshape. setasbox (30 * scale, 30 * scale); fixturedef. shape = polygonshape; body. settype (b2body. b2_dynamicbody); body. createfixture (fixturedef); body. setposition (New b2vec2 (400 * scale, 100 * scale ));

After the physical world is built, we can see the prototype of the game through the debugdraw function, so that we can adjust the object parameters and create checkpoints on this basis. After these tasks are completed, we need to "Paste" the film on the physical object so that players can see the real game picture.

Synchronous Display object

First, we need to create a displayobject class, which is similar to the display class in the Flash display list mechanism. This class has corresponding properties such as X, Y, and rotation, and has its own drawing method, the list is displayed in array format.

Next, add the displayobject to the display list, and synchronize its position and rotation angle with the corresponding object in box2d:

var position =body.GetPosition();var angle =body.GetAngle();displayObject.x = position.x / scale;displayObject.y = position.y / scale;displayObject.rotation = angle * 180 / Math.PI;

Based on the physical world in the image, we can see the following images:

Animation

After the displayobject is synchronized with the physical object, we can fill it with the image to be displayed, and then change the image at a certain interval to achieve the animation effect. Generally, an animation consists of many images. To reduce the number of reads, we combine all the images in the animation into a large image called spritesheet. Spritesheet:

Then, we can obtain the image to be displayed in the current frame according to the coordinates in spritesheet:

VaR canvas = document. createelement ("canvas"); canvas. width = width;/* width indicates the width of the image */canvas. height = height;/* height indicates the height of the image */canvas. getcontext ('2d '). drawimage (sheet, X, Y, width, height, 0, 0, width, height );

Summary

HTML5 technology is gradually being improved, and our programmers are constantly working hard to create more good works. The construction of the physical world, display objects, and animation playing technologies discussed in this article are very useful for HTML5 games. We are looking forward to seeing the continuous development of HTML5 technology and more innovative HTML5 web games.

Welcome to the ntfusion team's HTML5 physical game save Papa!

This article is from: Enjoy HTML5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.