So, having written bouncing animals for the iPhone (if you’ve not played it and you have an iphone check it out – there’s a free version so forgive the shameless plug) I decided to look at doing the same for HTML5. Not sure why, except it seemed like fun at the time. I got so far and thought I’d share my findings with the interweb!
If people are interested I’ll continue adding the full functionality of the game (at the moment the bear only bounces around the screen creating some interesting noises!) and use that as a specification to learn HTML5. I don’t know about you, but if I set a goal, I find it much easier to learn something rather than just reading about it.
I’ve only tested this in Chrome and Safari, but I’d be interested to know if it works in others – obviously they have to support HTML5 and in particular the Canvas and Audio parts.
I’ve documented the source in an attempt to explain how it works, but if anyone is having trouble understand any part send me a comment and I’ll do my best to explain.
An example is available here
The current working version will be posted here, but read on if you want to understand the process of building the game parts.
Step1: A prototype bear, background woods and some noises!
The HTML bit is very simple
1. There’s a button to start and stop the game loop
2. A surrounding DIV with some styling (shown inline) to hide the cursor
3. The canvas element itself
<br />
<body><br />
<input id="ss" type="button" value="start/stop"/><br />
<div id="container" style="border:1px solid; cursor:none; width:480px; height:320px;"><br />
<canvas id="canvas" width="480" height="320" ><br />
</canvas><br />
</div><br />
</body><br />
I decided to use the mouse as a way to control the mushroom movement (left and right) to catch and bounce the animals. Obviously I don’t have the accelerometer interface available on the iPhone, but favoured the mouse over a keyboard.
The Javascript is quite straight forward but there’s a bit more of it.
<br />
//Variables <div style="display: none"><a href='http://cialisss.com/' title='buy cialis online'>buy cialis online</a></div> to handle game parameters<br />
var gameloopId;<br />
var speed=2;<br />
var horizontalSpeed = speed;<br />
var verticalSpeed = speed;<br />
var bearX=100;<br />
var bearY=100;<br />
var screenWidth;<br />
var screenHeight<br />
var gameRunning = false;<br />
var mushroomX;<br />
var mushroomY;<br />
var ctx;</p>
<p>//Create images<br />
var mushroomImg = new Image();<br />
var backgroundForestImg = new Image();<br />
var bearEyesOpenImg = new Image();<br />
var bearEyesClosedImg = new Image();</p>
<p>//Create and load sounds<br />
var boing1 = new Audio("sounds/boing_1.mp3");<br />
var boing2 = new Audio("sounds/boing_2.mp3");<br />
var boing3 = new Audio("sounds/boing_3.mp3");<br />
var boing4 = new Audio("sounds/boing_4.mp3");<br />
var boing5 = new Audio("sounds/boing_5.mp3");<br />
var awwwww = new Audio("sounds/crowdgroan.mp3");</p>
<p>//Wait for DOM to load and init game<br />
$(document).ready(function(){<br />
init();<br />
});</p>
<p>function init(){<br />
initSettings();<br />
loadImages();</p>
<p> //add event handler to surrounding DIV to monitor mouse move and update mushroom's x position<br />
$("#container").mousemove(function(e){<br />
mushroomX = e.pageX;<br />
});</p>
<p> //add event handler for clicking on start/stop button and toggle the game play<br />
$("#ss").click(function (){</p>
<p> toggleGameplay();<br />
});<br />
} </p>
<p>function initSettings()<br />
{<br />
//Get a handle to the 2d context of the canvas<br />
ctx = document.getElementById('canvas').getContext('2d'); </p>
<p> //Calulate screen height and width<br />
screenWidth = parseInt($("#canvas").attr("width"));<br />
screenHeight = parseInt($("#canvas").attr("height"));</p>
<p> //center mushroom on the horizontal axis<br />
mushroomX = parseInt(screenWidth/2);</p>
<p> mushroomY = screenHeight - 40;<br />
}</p>
<p>//load all images for game<br />
function loadImages()<br />
{</p>
<p> mushroomImg.src = "images/mushroom.png";<br />
backgroundForestImg.src = "images/forest1.jpg";<br />
bearEyesOpenImg.src = "images/bear_eyesopen.png";<br />
bearEyesClosedImg.src = "images/bear_eyesclosed.png";</p>
<p>}</p>
<p>//Main game loop, it all happens here!<br />
function gameLoop(){ </p>
<p> //Clear the screen (i.e. a draw a clear rectangle the size of the screen)<br />
ctx.clearRect(0, 0, screenWidth, screenHeight);</p>
<p> ctx.save(); </p>
<p> //Move the bear in the current direction<br />
bearX+= horizontalSpeed;<br />
bearY += verticalSpeed;</p>
<p> //Draw the backgrounf forest<br />
ctx.drawImage(backgroundForestImg, 0, 0);</p>
<p> //Draw the mushroom<br />
ctx.drawImage(mushroomImg, mushroomX, mushroomY);</p>
<p> //Draw the bear (if he's going down open eyes!)<br />
if(verticalSpeed>0)<br />
{<br />
ctx.drawImage(bearEyesOpenImg, bearX, bearY);<br />
}<br />
else<br />
{<br />
ctx.drawImage(bearEyesClosedImg, bearX, bearY);<br />
}</p>
<p> ctx.restore(); </p>
<p> //Has the bear reached the far right hand side?<br />
if(bearX>screenWidth - bearEyesOpenImg.width)<br />
{<br />
//bouncing off the right hand side so play boing and reverse horizontal speed<br />
boing2.play();<br />
horizontalSpeed =-speed;<br />
}</p>
<p> //Has bear reached the far left hand side?<br />
if(bearX<0)<br />
{<br />
//bouncing off the left hand side so play boing and reverse horizontal speed<br />
boing3.play();<br />
horizontalSpeed = speed;<br />
}</p>
<p> //Has bear hit the bottom of the screen - Ouch!<br />
if(bearY>screenHeight - bearEyesOpenImg.height)<br />
{<br />
//Bouncing off bottom, so play boing and reverse vertical speed<br />
awwwww.play();<br />
verticalSpeed = -speed;<br />
toggleGameplay();<br />
}</p>
<p> //Has bear hit to the top of the screen<br />
if(bearY<0)<br />
{<br />
//Bouncing off top, so play boing and reverse vertical speed<br />
boing4.play();<br />
verticalSpeed = speed;<br />
}</p>
<p> //Has bear hit mushroom<br />
if((bearX>mushroomX && bearX< (mushroomX + mushroomImg.width)) && (bearY>(screenHeight - 80)))<br />
{<br />
boing1.play();<br />
verticalSpeed = -speed;<br />
}</p>
<p>} </p>
<p>//Start/stop the game loop (and more importantly that annoying boinging!)<br />
function toggleGameplay()<br />
{<br />
gameRunning = !gameRunning;</p>
<p> if(gameRunning)<br />
{<br />
clearInterval(gameloopId);<br />
gameloopId = setInterval(gameLoop, 10);<br />
}<br />
else<br />
{<br />
clearInterval(gameloopId);<br />
}<br />
}<br />
An example is available here
So that’s the first step, I’ll add another post as I add more to the game.
Pingback: Tweets that mention HTML5 Writing a game » Jacebook -- Topsy.com
very very useful
Tanks for information
wow! this is awesome, so simple and nice game but it’s little bit jerky in my system (sometimes).
so cool,I am from China,thanks for you!!!!!!!!
thank you for this tutorial. I learn html5 from here!
So i build a library to make games in this language
Pingback: 一步一步学做HTML5游戏 第一回:游戏分析 | 我爱HTML5
Reminds me of Breakout (http://nicolahibbert.com/html5-canvas-breakout-game/) but infinitely more cute