<!DOCTYPE html>
<html>
<head>
<title>Super Cool First Phaser Demo App</title>
<script data-require="phaser@2.1.3" data-semver="2.1.3" src="//cdnjs.cloudflare.com/ajax/libs/phaser/2.1.3/phaser.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Orbitron' rel='stylesheet' type='text/css' />
<link href="style.css" rel="stylesheet" />
<script src="script.js"></script>
</head>
<body>
<div id="embeddedBody">
<h1>Glen's First HTML5 Game!</h1>
<h3>By working through the <a href="http://phaser.io/tutorials/making-your-first-phaser-game/index">Phaser Demo</a></h3>
<div id="gamediv"></div>
</div>
</body>
</html>
'use strict;'
var game = new Phaser.Game(600, 600, Phaser.AUTO, 'gamediv',
{ preload: preload, create: create, update: update });
var platforms, player, cursors, stars, score = 0, scoreText;
function preload() {
game.load.baseURL = 'http://blogs.bytecode.com.au/glen/images/2015/';
game.load.crossOrigin = 'anonymous';
game.load.image('sky', 'sky.png');
game.load.image('ground', 'platform.png');
game.load.image('star', 'star.png');
game.load.spritesheet('dude', 'dude.png', 32, 48);
}
function create() {
// We're going to be using physics, so enable the Arcade Physics system
game.physics.startSystem(Phaser.Physics.ARCADE);
// A simple background for our game
game.add.sprite(0, 0, 'sky');
// The platforms group contains the ground and the 2 ledges we can jump on
platforms = game.add.group();
// We will enable physics for any object that is created in this group
platforms.enableBody = true;
// Here we create the ground.
var ground = platforms.create(0, game.world.height - 64, 'ground');
// Scale it to fit the width of the game (the original sprite is 400x32 in size)
ground.scale.setTo(2, 2);
// This stops it from falling away when you jump on it
ground.body.immovable = true;
// Now let's create two ledges
var ledge = platforms.create(400, 400, 'ground');
ledge.body.immovable = true;
ledge = platforms.create(-150, 250, 'ground');
ledge.body.immovable = true;
// The player and its settings
player = game.add.sprite(32, game.world.height - 150, 'dude');
// We need to enable physics on the player
game.physics.arcade.enable(player);
// Player physics properties. Give the little guy a slight bounce.
player.body.bounce.y = 0.2;
player.body.gravity.y = 300;
player.body.collideWorldBounds = true;
// Our two animations, walking left and right.
player.animations.add('left', [0, 1, 2, 3], 10, true);
player.animations.add('right', [5, 6, 7, 8], 10, true);
cursors = game.input.keyboard.createCursorKeys();
stars = game.add.group();
stars.enableBody = true;
// Here we'll create 9 of them evenly spaced apart
for (var i = 0; i < 9; i++)
{
// Create a star inside of the 'stars' group
var star = stars.create(i * 70, 0, 'star');
// Let gravity do its thing
star.body.gravity.y = 6;
// This just gives each star a slightly random bounce value
star.body.bounce.y = 0.7 + Math.random() * 0.2;
}
scoreText = game.add.text(16, 16, 'Score: 0', { font: '32px Orbitron', fill: '#000'});
}
function update() {
// Collide the player and the stars with the platforms
game.physics.arcade.collide(player, platforms);
// Reset the players velocity (movement)
player.body.velocity.x = 0;
if (cursors.left.isDown)
{
// Move to the left
player.body.velocity.x = -150;
player.animations.play('left');
}
else if (cursors.right.isDown)
{
// Move to the right
player.body.velocity.x = 150;
player.animations.play('right');
}
else
{
// Stand still
player.animations.stop();
player.frame = 4;
}
// Allow the player to jump if they are touching the ground.
if (cursors.up.isDown && player.body.touching.down)
{
player.body.velocity.y = -350;
}
game.physics.arcade.collide(stars, platforms);
game.physics.arcade.overlap(player, stars, collectStar, null, this);
}
function collectStar (player, star) {
// Removes the star from the screen
star.kill();
// Add and update the score
score += 10;
scoreText.text = 'Score: ' + score;
}
/* Styles go here */
#embeddedBody {
font-family: 'Orbitron', sans-serif;
}
# Glen's first HTML5 Demo Game
This is the [Making Your First Game](http://phaser.io/tutorials/making-your-first-phaser-game) tutorial for Phaser,
but have done it here, so I can share it easily with others to see what you can do in half an hour.