// Import the Phaser library
import * as Phaser from "phaser";

// Create a new game instance
const game = new Phaser.Game({
  type: Phaser.AUTO,
  width: 800,
  height: 600,
  scene: {
    key: "main",
    create: create,
    update: update,
  },
});

// The create function is called when the game first starts
function create() {
  // Create the player sprite
  this.player = this.add.sprite(400, 300, "player");

  // Add a physics body to the player
  this.player.body.setSize(64, 64);
  this.player.body.setGravityY(300);

  // Create the ground object
  this.ground = this.add.tilemap("ground");

  // Set the ground's position to be off the screen
  this.ground.x = -1000;

  // Set the ground's speed to be very slow
  this.ground.speed.x = -1;

  // Add some enemies
  this.add.enemy(100, 200, "enemy");
  this.add.enemy(300, 400, "enemy");
  this.add.enemy(500, 600, "enemy");
}

// The update function is called every frame
function update() {
  // Move the player
  this.player.body.x += this.input.x;

  // Check for collisions with the ground
  if (this.player.body.bottom > this.ground.height) {
    this.player.body.y = this.ground.height - 64;
  }

  // Check for collisions with enemies
  for (let i = 0; i < this.enemies.length; i++) {
    if (this.player.body.collidesWith(this.enemies[i].body)) {
      // Game over!
      this.game.over();
    }
  }

  // Move the ground
  this.ground.x += this.ground.speed.x;

  // If the ground has gone off the left side of the screen, move it back to the right side
  if (this.ground.x < -1000) {
    this.ground.x = 800;
  }
} 

NodeJS Online Compiler

Write, Run & Share NodeJS code online using OneCompiler's NodeJS online compiler for free. It's one of the robust, feature-rich online compilers for NodeJS language,running on the latest LTS version NodeJS 16.14.2. Getting started with the OneCompiler's NodeJS editor is easy and fast. The editor shows sample boilerplate code when you choose language as NodeJS and start coding. You can provide the dependencies in package.json.

About NodeJS

Node.js is a free and open-source server environment. Node.js is very popular in recent times and a large number of companies like Microsoft, Paypal, Uber, Yahoo, General Electric and many others are using Node.js.

Key features

  • Built on Google chrome's javascript engine V8 and is pretty fast.
  • Node.js was developed by Ryan Dahl in 2009.
  • Server-side platform for building fast and scalable applications.
  • Node.js is Asynchronous, event-driven and works on single-thread model thus eliminating the dis-advantages of multi-thread model.
  • Supports various platforms like Windows, Linux, MacOS etc.
  • Provides rich library of java script modules which simplifies the development efforts.
  • Released under MIT license.

Express Framework

Express is one of the most popular web application framework in the NodeJS echosystem.

  • Pretty fast
  • Minimalist
  • Unopinionated
  • Very flexible

Syntax help

Examples

Using Moment

let moment = require('moment');

console.log(moment().format('MMMM Do YYYY, h:mm:ss a'));

Using Lodash

const _ = require("lodash");

let colors = ['blue', 'green', 'yellow', 'red'];

let firstElement = _.first(colors);
let lastElement = _.last(colors);

console.log(`First element: ${firstElement}`);
console.log(`Last element: ${lastElement}`);

Libraries supported

Following are the libraries supported by OneCompiler's NodeJS compiler.

  • lodash
  • moment
  • underscore
  • uuid
  • ejs
  • md5
  • url