Configurable JavaScript Pong game in the browser. Uses Pixi.js for rendering. Available stand-alone or as npm module for Browserify
Current Version: 0.0.9
- Player vs Bot »
- Player vs Player »
- Custom colors »
- Random colors »
- Custom images »
- Bot vs Bot »
- Custom ball »
- Random balls »
- Shrink player »
- Framed »
- Winning Screen »
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.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);
}
});
npm install pong.js
var Pong = require('pong.js'),
pong = new Pong(document.getElementById('wrapper'));
bower install pong.js
<script type="text/javascript" src="bower_components/pong.js/build/Pong.js"></script>
// Add keyboard controls for player A
pong.players.a.addControls({
'up': 'w',
'down': 's',
});
// Add behaviour for player B
pong.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);
}
});
// Use a custom image for the ball
pong.setBallImage('./assets/ball.png');
// Use a background color
pong.setBackgroundColor('#ff0000');
- Browserify
npm install -g browserify
- Watchify
npm install -g watchify
git clone git@github.com:KanoComputing/Pong.js.git
cd Pong.js
npm install
npm run build
npm run watch
start()
- Start gamepause()
- Pause game (Showing pause screen)resume()
- Resume paused gametogglePaused()
- Pause or resume gamerefresh()
- Re-render screenupdate()
- Run next frame in the gameloopupdateIfStill()
- Only update if gameloop not runningresize()
- Resize accordingly to wrapper size. Use for responsive implementationsresetBalls( add_one )
- Remove all balls, add one iftrue
is passedrestart()
- Reset position of players and ballreset()
- Reset game (Restore start screen, scores, ..)setBackgroundColor( string )
- Set background color using hexa string (#xxxxxx
)setBackgroundImage( string )
- Set background image asset url / relative pathsetLinesColor( string )
- Set lines color using hexa string (#xxxxxx
)setTextStyle( object )
- Set text style attributes (E.g.font
,fill
,align
)setBallColor( string )
- Set the color of balls currently on stage and future onessetBallImage( string )
- Set ball image asset url / relative path for all balls on stage (Recommended if 1:1 ratio assets)setBallSize( number )
- Set the size of balls currently on stage and future onessetBallSpeed( number )
- Set the speed of balls currently on stage and future onesaddBall()
- Add a ball to the gameon( event , callback )
- Bind callback to a game eventemit( event , [ params.. ])
- Emit a game eventwin( string )
- Stops game, display message to screen
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 objectbounces
- Number of ball bounces since last pointtotalBounces
- Number of ball bounces since the start of the current gamehits
- Number of times a player hit the ball since last pointtotalHits
- Number of times a player hit the ball since the start of the current game
start
- Triggered when game is startedstop
- Triggered when game is stoppedpause
- Triggered when game is pausedresume
- Triggered when game is resumedbeforeupdate
- Triggered before every gameloop iterationupdate
- Triggered after every gameloop iterationresize
- Triggered when the game is resizedpoint
- Triggered by every player when a point is scoredsetLinesColor
- Used by UI children, triggered when.setLinesColor
is calledsetTextStyle
- Used by UI children, triggered when.setTextStyle
is calledsetBallColor
- Used by Ball instances, triggered when.setBallColor
is calledsetBallSize
- Used by Ball instances, triggered when.setBallSize
is calledsetBallSpeed
- Used by Ball instances, triggered when.setBallSpeed
is calledbounce
- Fired every time a ball bounceshit
- Fired every time a ball hits a player
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 heightsetY( number )
- Set player Y positionsetColor( string )
- Set paddle color using hexa string (#xxxxxx
)on( event, callback )
- Bind callback to a player eventemit( event , [ params.. ])
- Emit a player event
point
- Triggered when player scores a pointhit
- Triggered when player hits a ball
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
setSize( number )
- Set ball radius in pixelssetColor( string )
- Set ball color using hexa string (#xxxxxx
)setImage( string )
- Set the ball image asset url / relative path (Recommended if 1:1 ratio assets)rebound( direction )
- Reset ball position, pointing right whendirection
is positive, left when negative
color
- Octal color stringsize
- Ball radius in pixelsvelocity
- Object containing X and Y velocityimage
- Background image url / relative path