Phaser exercise 2

by theodedeken

HTML

<!DOCTYPE html>
<!-- HTML base to get Phaser to work, no changes here are needed -->
<html>
<head>
    <script src="https://cdn.jsdelivr.net/npm/phaser@3.15.1/dist/phaser-arcade-physics.min.js"></script>
</head>
<body>
</body>
</html>

JavaScript

/**
 * WARNING: TRY TO FIGURE THE EXERCISES OUT ON YOUR OWN! 
 * ONLY USE MY SOURCE CODE AS A LAST RESORT, GOOD LUCK!
 */

// Configuration for the game
// Other options that can be set in this config:
// https://photonstorm.github.io/phaser3-docs/Phaser.Core.Config.html
let config = {
    type: Phaser.AUTO,
    // width of the game
    width: 800,
    // height fo the game
    height: 450,
    // background color of the game
    backgroundColor: 0x6dc4f2,
    // function names for the default engine events
    scene: {
        create: create,
        update: update
    }
};

// Create a new Phaser Game
let game = new Phaser.Game(config);
// Declare our variables
let space,
    ball,
    ground,
    pillar,
    scoretext;
let jumping = false;
let keylock = false;
let playing = false;
// Declare a variable to keep score
let score = 0;

/**
 * Create function
 * This function gets executed once after the preload and creates the game scene
 * 
 * Use this function to create and configure all your game elements in the scene
 */
function create() {
    // Create and add the bouncy ball
    ball = this.add.circle(100, 370, 20, 0x931f9c);
    // Create and add the ground
    ground = this.add.rectangle(400, 420, 800, 60, 0x38963c);
    // Create and add the pillar where we will bounce over
    pillar = this.add.rectangle(1000, 340, 50, 100, 0xff0000);

    space = this
        .input
        .keyboard
        .addKey(Phaser
            .Input
            .Keyboard
            .KeyCodes
            .SPACE);

    // displaying score
    scoretext = this.add.text(400, 50, score, {
        fontFamily: "\"Arial\"",
        fontSize: "50pt"
    });
    // displaying info at the start of the game
    infotext = this.add.text(50, 400, "Press space to start game", {
        fontFamily: "\"Arial\"",
        fontSize: "20pt"
    })
}

/**
 * Main update loop
 * The code in this function gets executed on every game update (default 60 times per second)
 *
 * Use this function for...