Simple Game Engine Documentation

Overview

The simpleGame.js library adds game programming features to HTML5-compliant browsers. The library is designed primarily for 2D Sprite-based games, but it can be used for other game types as well. The library is designed to be easy enough for beginning game developers to use.

The library is object-oriented, and features two primary objects, the sprite and the scene. Each game is based on a scene, and all of the moving objects in the game are sprites. A scene can contain as many sprites as you wish. Each sprite is based on a standard image, so any image file you create can become a sprite for your game.

Primary features

The basic game

A game written with the SimpleGame library is essentially a web page. You should be familiar with basic HTML and JavaScript syntax. Examine the following simple code (simpleCar.html). It provides the skeleton for all simpleGame games.

<!DOCTYPE HTML>
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title>simpleCar.html</title>
	<script type = "text/javascript"
		src = "simpleGame.js"></script>
	<script type = "text/javascript">

	var scene;
	var car;
	
	function init(){
		scene = new Scene();
		
		car = new Sprite(scene, "car.gif", 50, 30);
		car.setSpeed(3);
		car.setAngle(0);
		car.setPosition(20, 25);

		scene.start();
	} // end init
	
	function update(){
		scene.clear();
		car.update();
	} // end update
	
	</script>
</head>
<body onload = "init()">
	<h1>Simple Car</h1>
</body>
</html>
  

Template code explanation

While this example is not very long, there is quite a bit happening. Here's the details:

Baby you can drive my car

So far this is a good animation, but it's not much of a game. To make a game, you need some sort of user interaction. With a few changes, it's not hard to let the user drive the car with the keyboard. Take a look at drive.html to see how it works.

Here's the code. Most of it's the same, but the update() function has been improved.

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>Drive.html</title>
    <script type = "text/javascript"
            src = "simpleGame.js"></script>
    <script type = "text/javascript">
      var scene;
      var car;
      function init(){
        scene = new Scene();
        car = new Sprite(scene, "car.gif", 50, 30);
        
        car.setSpeed(3);
        car.setAngle(0);
        scene.start();
      } // end init
      
      function update(){
        scene.clear();
        if (keysDown[K_LEFT]){
          car.changeAngleBy(-5);
        }
        if (keysDown[K_RIGHT]){
          car.changeAngleBy(5);
        }
        if (keysDown[K_UP]){
          car.changeSpeedBy(1);
        }
        if (keysDown[K_DOWN]){
          car.changeSpeedBy(-1);
        }
        car.update();
      } // end update
    </script>
</head>

<body onload = "init()">
  <title>Drive.html</title>
  <h1>A basic car you can drive</h1>
</body>
</html>  

The new code isn't terribly shocking. In the update() function, you simply check to see if any keys have been pressed. SimpleGame has a large array which shows the current status of any key. You can use special constants (K_UP, K_DOWN, K_SPACE, K_A, and so on) to simplify checking for a particular key press.

If the player presses the up key, accellerate the car by using the sprite's changeSpeedBy() method. The down key works the same way, but change the car's speed by a negative value. The left and right keys are the steering control. You can change the car's direction with the changeAngleBy() method.