pong.js
v0.0.7
Published
Configurable HTML5 Pong game for the browser
Downloads
10
Readme
Pong.js
Configurable JavaScript Pong game in the browser. Uses Pixi.js for rendering. Available stand-alone or as npm module for Browserify
Live examples
- Player vs Bot »
- Player vs Player »
- Bot vs Bot »
- Random balls »
- Custom colors »
- Random colors »
- Shrink player »
- Framed »
Usage
Simple Example Usage
var pong = new Pong(document.getElementById('wrapper'));
// Add keyboard controls for player A
pong.players.a.addControls({
'up': 'w',
'down': 's',
});
// Add behaviour for player B
pong.events.on('update', function () {
if (pong.players.b.y < pong.balls[0].y) {
pong.players.b.move(1);
} else if (pong.players.b.y > pong.balls[0].y) {
pong.players.b.move(-1);
}
});
Browserify Example Usage
Installation
npm install pong.js
Example
var Pong = require('pong.js'),
pong = new Pong(document.getElementById('wrapper'));
// Add keyboard controls for player A
pong.players.a.addControls({
'up': 'w',
'down': 's',
});
// Add behaviour for player B
pong.events.on('update', function () {
if (pong.players.b.y < pong.balls[0].y) {
pong.players.b.move(1);
} else if (pong.players.b.y > pong.balls[0].y) {
pong.players.b.move(-1);
}
});
Development
Global Dependencies
- Browserify
npm install -g browserify
- Watchify
npm install -g watchify
Install
git clone [email protected]:KanoComputing/Pong.js.git
cd Pong.js
npm install
Build
npm run build
Watch
npm run watch
Docs
Pong class
Methods
start()
- Start gamestop()
- Stop gameupdate()
- Run next frame in the gameloopupdateIfStill()
- Only update if gameloop not runningresize()
- Resize accordingly to wrapper size. Use for responsive implementationsreset()
- Reset paddles positions, scores and ballssetBackgroundColor([ string ]) - Set background color using hexa string (
#xxxxxx`)setLinesColor([ string ])
- Set lines color using hexa string (#xxxxxx
)setTextStyle([ object ])
- Set text style attributes (E.g.font
,fill
,align
)addBall()
- Add a ball to the game
Properties
events
- Game event emitterplayers
- Object containing Players (a
andb
by default)stage
- Pixi.js stagerenderer
- Pixi.js rendererwrapper
- Wrapping DOM elementballs
- Array containing Ball objectsloop
- GameLoop object
Player class
Methods
addControls([ object ])
- Add keyboard controls (E.g.{ up: 'w', down: 's' }
)move([ number ]) - Move up (
-1) or down (
1`) according to speed at next iterationscreenX()
- Returns screen X positionscreenY()
- Returns screen Y positiongetBoundingBox()
- Returns paddle bounding boxreset()
- Reset player positionaddPoint()
- Increse player scoresetHeight([ number ])
- Set paddle heightsetColor([ string ])
- Set paddle color using hexa string (#xxxxxx
)
Properties
side
-left
orright
width
- Paddle screen widthheight
- Paddle screen heightspeed
- Player speed per second (300 by default)y
- game Y positionscore
- Player scorecolor
- Octal color string
Ball class
Methods
setSize([ number ])
- Set ball radius in pixelssetColor([ string ])
- Set ball color using hexa string (#xxxxxx
)
Properties
color
- Octal color stringsize
- Ball radius in pixelsvelocity
- Object containing X and Y velocity