Crafty

JavaScript Game Engine

README

# Crafty JS Travis Build Status AppVeyor Build Status Sauce Test Status

Crafty is a JavaScript game library that can help you create games in a structured way…

Key Features:

Entities & Components - A clean and decoupled way to organize game elements. No inheritance needed!
Eventbinding - Event system for custom events that can be triggered whenever, whatever and bound just as easily.
No dom manipulation or custom drawing routines required.

Other Goodies:

Thriving community - Help is readily available in the forum.
Community modules - A growing collection of user-generated code you can use.
Pure JavaScript - No magic. Works in all major browsers and can be combined with your favorite js library.


Using Crafty


A simple game of pong:
  1. ``` js
  2. Crafty.init(600, 300);
  3. Crafty.background('rgb(127,127,127)');

  4. //Paddles
  5. Crafty.e("Paddle, 2D, DOM, Color, Multiway")
  6.     .color('rgb(255,0,0)')
  7.     .attr({ x: 20, y: 100, w: 10, h: 100 })
  8.     .multiway(200, { W: -90, S: 90 });
  9. Crafty.e("Paddle, 2D, DOM, Color, Multiway")
  10.     .color('rgb(0,255,0)')
  11.     .attr({ x: 580, y: 100, w: 10, h: 100 })
  12.     .multiway(200, { UP_ARROW: -90, DOWN_ARROW: 90 });

  13. //Ball
  14. Crafty.e("2D, DOM, Color, Collision")
  15.     .color('rgb(0,0,255)')
  16.     .attr({ x: 300, y: 150, w: 10, h: 10,
  17.             dX: Crafty.math.randomInt(2, 5),
  18.             dY: Crafty.math.randomInt(2, 5) })
  19.     .bind('UpdateFrame', function () {
  20.         //hit floor or roof
  21.         if (this.y <= 0 || this.y >= 290)
  22.             this.dY *= -1;

  23.         // hit left or right boundary
  24.         if (this.x > 600) {
  25.             this.x = 300;
  26.             Crafty("LeftPoints").each(function () {
  27.                 this.text(++this.points + " Points") });
  28.         }
  29.         if (this.x < 10) {
  30.             this.x = 300;
  31.             Crafty("RightPoints").each(function () {
  32.                 this.text(++this.points + " Points") });
  33.         }

  34.         this.x += this.dX;
  35.         this.y += this.dY;
  36.     })
  37.     .onHit('Paddle', function () {
  38.         this.dX *= -1;
  39.     });

  40. //Score boards
  41. Crafty.e("LeftPoints, DOM, 2D, Text")
  42.     .attr({ x: 20, y: 20, w: 100, h: 20, points: 0 })
  43.     .text("0 Points");
  44. Crafty.e("RightPoints, DOM, 2D, Text")
  45.     .attr({ x: 515, y: 20, w: 100, h: 20, points: 0 })
  46.     .text("0 Points");
  47. ```
_Left paddle is controlled by W & S, right paddle by UpArrow & DownArrow._  

Developing


If you want to fix a bug, please submit a pull request against the development branch.  Some guides to help you can be found on the wiki

If you would like to make larger contributions please catch us in the forum and we will help you get started. Much appreciated :-)


Quick build instructions


The easiest way to build crafty is to use gruntjs, which requires node and npm.  If you have grunt, node, and npm already installed, then runnpm install from Crafty's root directory.  (This will pull down about 30MB of node packages.)  From then on, just run grunt to build.

You can also use yarn instead of npm.