Skip to content

Instantly share code, notes, and snippets.

@keomamallett
Last active April 20, 2019 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keomamallett/83de0d16fdda19d37f69d44c0940663a to your computer and use it in GitHub Desktop.
Save keomamallett/83de0d16fdda19d37f69d44c0940663a to your computer and use it in GitHub Desktop.
This gist was created to share the code for the phaser.io "Making your first Phaser 3 game", with a few adjustments of mine, with one of my students.
const ZERO = 0;
const GAME_WIDTH = 800;
const GAME_HEIGHT = 600;
const TWO = 2;
var platforms;
var player;
var cursors;
var stars;
var bombs;
var score = 0;
var scoreText;
var config = {
type: Phaser.AUTO,
width: GAME_WIDTH,
height: GAME_HEIGHT,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 300 },
debug: false
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
var game = new Phaser.Game(config);
function preload() {
this.load.image('sky', 'assets/sky.png');
this.load.image('ground', 'assets/platform.png');
this.load.image('star', 'assets/star.png');
this.load.image('bomb', 'assets/bomb.png');
this.load.spritesheet(
'dude',
'assets/dude.png',
{
frameWidth: 32,
frameHeight: 48
}
);
}
function create() {
this.add.image(GAME_WIDTH/TWO, GAME_HEIGHT/TWO, 'sky');
platforms = this.physics.add.staticGroup();
platforms.create(400, 568, 'ground').setScale(2).refreshBody();
platforms.create(600, 400, 'ground');
platforms.create(50, 250, 'ground');
platforms.create(750, 220, 'ground');
player = this.physics.add.sprite(100, 450, 'dude');
player.setBounce(0.2);
player.setCollideWorldBounds(true);
this.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers(
'dude',
{
start: 0,
end: 3
}
),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'turn',
frames: [{
key: 'dude',
frame: 4
}],
frameRate: 20
});
this.anims.create({
key: 'right',
frames: this.anims.generateFrameNumbers(
'dude',
{
start: 5,
end: 8
}
),
frameRate: 10,
repeat: -1
});
this.physics.add.collider(player, platforms);
cursors = this.input.keyboard.createCursorKeys();
stars = this.physics.add.group({
key: 'star',
repeat: 11,
setXY: {
x: 15,
y: 0,
stepX: 70
}
});
stars.children.iterate(function(child) {
child.setBounce(Phaser.Math.FloatBetween(0.4, 0.8));
});
this.physics.add.collider(stars, platforms);
this.physics.add.overlap(player, stars, collectStar, null, this);
scoreText = this.add.text(
16,
16,
'score: 0',
{
fontSize: '32px',
fill: '#000'
}
);
bombs = this.physics.add.group();
this.physics.add.collider(bombs, platforms);
this.physics.add.collider(player, bombs, hitBomb, null, this);
}
function update() {
if (cursors.left.isDown)
{
player.setVelocityX(-160);
player.anims.play('left', true);
}
else if (cursors.right.isDown) {
player.setVelocityX(160);
player.anims.play('right', true);
}
else {
player.setVelocityX(0);
player.anims.play('turn');
}
if(cursors.up.isDown && player.body.touching.down) {
player.setVelocityY(-330);
}
}
// other functions
function collectStar(player, star)
{
star.disableBody(true, true);
score += 10;
scoreText.setText('Score: ' + score);
// add bombs
if(stars.countActive(true) === 0 ) {
stars.children.iterate(function(child) {
child.enableBody(true, child.x, 0, true, true);
});
var bombPosX = (player.x < GAME_WIDTH/TWO) ? Phaser.Math.Between(GAME_WIDTH/TWO, GAME_WIDTH) : Phaser.Math.Between(ZERO, GAME_WIDTH/TWO);
var bomb = bombs.create(bombPosX, 16, 'bomb');
bomb.setBounce(1);
bomb.setCollideWorldBounds(true);
bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
}
}
function hitBomb(player, bomb) {
// fix this to set player tint, make player face screen
this.physics.pause();
player.setTint(0xff0000);
player.anims.play(turn);
gameOver = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment