Skip to content

Commit

Permalink
Updated games examples
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinsight committed Mar 13, 2014
1 parent 39add47 commit 20d7ebd
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 55 deletions.
42 changes: 26 additions & 16 deletions examples/games/breakout.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ var s;
function create() {

// This tells the world to include walls on the left, right and top, but not the bottom - so the ball can 'fall' away.
game.physics.setBoundsToWorld(true, true, true, false);
game.physics.setBoundsToWorld();

s = game.add.tileSprite(0, 0, 800, 600, 'starfield');

var brick;
bricks = game.add.group();
bricks.enableBody = true;
bricks.physicsBodyType = Phaser.Physics.ARCADE;

for (var y = 0; y < 4; y++)
{
Expand All @@ -45,12 +47,14 @@ function create() {

paddle = game.add.sprite(game.world.centerX, 500, 'breakout', 'paddle_big.png');
paddle.anchor.setTo(0.5, 0.5);
game.physics.enable(paddle, Phaser.Physics.ARCADE);
paddle.body.collideWorldBounds = true;
paddle.body.bounce.setTo(1, 1);
paddle.body.immovable = true;

ball = game.add.sprite(game.world.centerX, paddle.y - 16, 'breakout', 'ball_1.png');
ball.anchor.setTo(0.5, 0.5);
game.physics.enable(ball, Phaser.Physics.ARCADE);
ball.body.collideWorldBounds = true;
ball.body.bounce.setTo(1, 1);
ball.animations.add('spin', [ 'ball_1.png', 'ball_2.png', 'ball_3.png', 'ball_4.png', 'ball_5.png' ], 50, true, false);
Expand All @@ -70,25 +74,29 @@ function update () {
// Fun, but a little sea-sick inducing :) Uncomment if you like!
// s.tilePosition.x += (game.input.speed.x / 2);

paddle.x = game.input.x;
paddle.body.x = game.input.x;

if (paddle.x < 24)
if (paddle.body.x < 24)
{
paddle.x = 24;
paddle.body.x = 24;
}
else if (paddle.x > game.width - 24)
else if (paddle.body.x > game.width - 24)
{
paddle.x = game.width - 24;
paddle.body.x = game.width - 24;
}

if(ball.body.y > game.height - 20){
ballLost();
}

if (ballOnPaddle)
{
ball.x = paddle.x;
ball.body.x = paddle.body.x;
}
else
{
game.physics.collide(ball, paddle, ballHitPaddle, null, this);
game.physics.collide(ball, bricks, ballHitBrick, null, this);
game.physics.arcade.collide(ball, paddle, ballHitPaddle, null, this);
game.physics.arcade.collide(ball, bricks, ballHitBrick, null, this);
}

}
Expand Down Expand Up @@ -116,11 +124,13 @@ function ballLost () {
}
else
{
livesText.content = 'lives: ' + lives;
console.log('Hi');
livesText.text = 'lives: ' + lives;
ballOnPaddle = true;
ball.x = paddle.x + 16;
ball.y = paddle.y - 16;
ball.body.reset();
ball.body.x = paddle.body.x + 16;
ball.body.y = paddle.y - 22;

ball.animations.stop();
}

Expand All @@ -130,7 +140,7 @@ function gameOver () {

ball.body.velocity.setTo(0, 0);

introText.content = 'Game Over!';
introText.text = 'Game Over!';
introText.visible = true;

}
Expand All @@ -141,15 +151,15 @@ function ballHitBrick (_ball, _brick) {

score += 10;

scoreText.content = 'score: ' + score;
scoreText.text = 'score: ' + score;

// Are they any bricks left?
if (bricks.countLiving() == 0)
{
// New level starts
score += 1000;
scoreText.content = 'score: ' + score;
introText.content = '- Next Level -';
scoreText.text = 'score: ' + score;
introText.text = '- Next Level -';

// Let's move the ball back to the paddle
ballOnPaddle = true;
Expand Down
44 changes: 31 additions & 13 deletions examples/games/invaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,25 @@ var livingEnemies = [];

function create() {




// The scrolling starfield background
starfield = game.add.tileSprite(0, 0, 800, 600, 'starfield');

// Our bullet group
bullets = game.add.group();
bullets.enableBody = true;
bullets.physicsBodyType = Phaser.Physics.ARCADE;
bullets.createMultiple(30, 'bullet');
bullets.setAll('anchor.x', 0.5);
bullets.setAll('anchor.y', 1);
bullets.setAll('outOfBoundsKill', true);

// The enemy's bullets
enemyBullets = game.add.group();
enemyBullets.enableBody = true;
enemyBullets.physicsBodyType = Phaser.Physics.ARCADE;
enemyBullets.createMultiple(30, 'enemyBullet');
enemyBullets.setAll('anchor.x', 0.5);
enemyBullets.setAll('anchor.y', 1);
Expand All @@ -52,22 +59,25 @@ function create() {
// The hero!
player = game.add.sprite(400, 500, 'ship');
player.anchor.setTo(0.5, 0.5);
game.physics.enable(player, Phaser.Physics.ARCADE);

// The baddies!
aliens = game.add.group();
aliens.enableBody = true;
aliens.physicsBodyType = Phaser.Physics.ARCADE;

createAliens();

// The score
scoreString = 'Score : ';
scoreText = game.add.text(10, 10, scoreString + score, { fontSize: '34px', fill: '#fff' });
scoreText = game.add.text(10, 10, scoreString + score, { font: '34px Arial', fill: '#fff' });

// Lives
lives = game.add.group();
game.add.text(game.world.width - 100, 10, 'Lives : ', { fontSize: '34px', fill: '#fff' });
game.add.text(game.world.width - 100, 10, 'Lives : ', { font: '34px Arial', fill: '#fff' });

// Text
stateText = game.add.text(game.world.centerX,game.world.centerY,'', { fontSize: '84px', fill: '#fff' });
stateText = game.add.text(game.world.centerX,game.world.centerY,' ', { font: '84px Arial', fill: '#fff' });
stateText.anchor.setTo(0.5, 0.5);
stateText.visible = false;

Expand All @@ -79,14 +89,22 @@ function create() {
ship.alpha = 0.4;
}


// An explosion pool
explosions = game.add.group();
explosions.createMultiple(30, 'kaboom');
explosions.forEach(setupInvader, this);


// And some controls to play the game with
cursors = game.input.keyboard.createCursorKeys();
fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);






}

function createAliens () {
Expand Down Expand Up @@ -155,8 +173,8 @@ function update() {
}

// Run collision
game.physics.overlap(bullets, aliens, collisionHandler, null, this);
game.physics.overlap(enemyBullets, player, enemyHitsPlayer, null, this);
game.physics.arcade.overlap(bullets, aliens, collisionHandler, null, this);
game.physics.arcade.overlap(enemyBullets, player, enemyHitsPlayer, null, this);

}

Expand All @@ -168,20 +186,20 @@ function collisionHandler (bullet, alien) {

// Increase the score
score += 20;
scoreText.content = scoreString + score;
scoreText.text = scoreString + score;

// And create an explosion :)
var explosion = explosions.getFirstDead();
var explosion = explosions.getFirstExists(false);
explosion.reset(alien.body.x, alien.body.y);
explosion.play('kaboom', 30, false, true);

if (aliens.countLiving() == 0)
{
score += 1000;
scoreText.content = scoreString + score;
scoreText.text = scoreString + score;

enemyBullets.callAll('kill',this);
stateText.content = " You Won, \n Click to restart";
stateText.text = " You Won, \n Click to restart";
stateText.visible = true;

//the "click to restart" handler
Expand All @@ -202,7 +220,7 @@ function enemyHitsPlayer (player,bullet) {
}

// And create an explosion :)
var explosion = explosions.getFirstDead();
var explosion = explosions.getFirstExists(false);
explosion.reset(player.body.x, player.body.y);
explosion.play('kaboom', 30, false, true);

Expand All @@ -212,7 +230,7 @@ function enemyHitsPlayer (player,bullet) {
player.kill();
enemyBullets.callAll('kill');

stateText.content=" GAME OVER \n Click to restart";
stateText.text=" GAME OVER \n Click to restart";
stateText.visible = true;

//the "click to restart" handler
Expand All @@ -238,14 +256,14 @@ function enemyFires () {
if (enemyBullet && livingEnemies.length > 0)
{

var random=game.rnd.integerInRange(0,livingEnemies.length);
var random=game.rnd.integerInRange(0,livingEnemies.length-1);

// randomly select one of them
var shooter=livingEnemies[random];
// And fire the bullet from this enemy
enemyBullet.reset(shooter.body.x, shooter.body.y);

game.physics.moveToObject(enemyBullet,player,120);
game.physics.arcade.moveToObject(enemyBullet,player,120);
firingTimer = game.time.now + 2000;
}

Expand Down
10 changes: 5 additions & 5 deletions examples/games/matching pairs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload:
function preload() {

game.load.tilemap('matching', 'assets/tilemaps/maps/phaser_tiles.json', null, Phaser.Tilemap.TILED_JSON);
game.load.tileset('tiles', 'assets/tilemaps/tiles/phaser_tiles.png', 100, 100, -1, 1, 1);
game.load.image('tiles', 'assets/tilemaps/tiles/phaser_tiles.png');
}

var timeCheck = 0;
Expand Down Expand Up @@ -41,11 +41,11 @@ function create() {

map = game.add.tilemap('matching');

tileset = game.add.tileset('tiles');

layer = game.add.tilemapLayer(0, 0, 600, 600, tileset, map, 0);
map.addTilesetImage('tiles', 'tiles');
layer = map.createLayer('World1');

//layer.resizeWorld();
layer.resizeWorld();

marker = game.add.graphics();
marker.lineStyle(2, 0x00FF00, 1);
Expand Down
2 changes: 2 additions & 0 deletions examples/games/simon.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function create() {
{
item = simon.create(150 + 168 * i, 150, 'item', i);
// Enable input.
item.inputEnabled = true;
item.input.start(0, true);
item.events.onInputDown.add(select);
item.events.onInputUp.add(release);
Expand All @@ -41,6 +42,7 @@ function create() {
{
item = simon.create(150 + 168 * i, 318, 'item', i + 3);
// Enable input.
item.inputEnabled = true;
item.input.start(0, true);
item.events.onInputDown.add(select);
item.events.onInputUp.add(release);
Expand Down
17 changes: 9 additions & 8 deletions examples/games/starstruck.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ function create() {

layer.resizeWorld();

game.physics.gravity.y = 250;
game.physics.setBoundsToWorld();
game.physics.arcade.gravity.y = 250;
game.physics.arcade.setBoundsToWorld();

player = game.add.sprite(32, 32, 'dude');
game.physics.enable(player, Phaser.Physics.ARCADE);
player.body.bounce.y = 0.2;
player.body.minVelocity.y = 5;
player.body.collideWorldBounds = true;
Expand All @@ -68,7 +69,7 @@ function create() {

function update() {

game.physics.collide(player, layer);
game.physics.arcade.collide(player, layer);

player.body.velocity.x = 0;

Expand Down Expand Up @@ -121,10 +122,10 @@ function update() {

function render () {

if (player.debug)
{
game.debug.physicsBody(player.body);
game.debug.bodyInfo(player, 16, 24);
}
// if (player.debug)
// {
// game.debug.physicsBody(player.body);
// game.debug.bodyInfo(player, 16, 24);
// }

}
Loading

0 comments on commit 20d7ebd

Please sign in to comment.