HTML5 game engine to create GameBoy blockbuster “Pyramids of Ra”

Read all posts about "" game
Back to early 1990 – 1993 to be accurate – a very interesting game called Pyramids of Ra was published for the Game Boy console. The goal in Pyramids of Ra is to clear each stage of every tile, while also managing to return to the starting point. Players can move one tile in each direction (up, down, left, right), although it is possible to “jump off” the playing field, as well as the possibility of destroying the incorrect tiles to trap the player or render some tiles inaccessible. You can find all information about this fun game in the dedicated Giant Bomb page. pyramid So I made a quick but complete prototype of the game, you will be able to play the first level.
You control the grey circle with a swipe, and you have to walk over all white tiles before returning back to start tile (marked with “S”). The problem is tiles fall down once you stepped over them, so mind your moves. The source code should be quite easy to understand, as all in all it’s just another tile based game:
var game;

// main game options
var gameOptions = {
	gameWidth: 1000, 
	gameHeight: 800, 
	tileSize: 100, 
	fieldSize: {
          rows: 8,
          cols: 10
     }
}

// game levels
var levels = [
     {
          level:[
               [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
               [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
               [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
               [0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
               [0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
               [0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
               [0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
               [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
               ],
          playerPos: new Phaser.Point(5, 2)      
     }
];

// when the window finishes loading...
window.onload = function() {	
	game = new Phaser.Game(gameOptions.gameWidth, gameOptions.gameHeight);
     game.state.add("TheGame", TheGame);
     game.state.start("TheGame");
}

var TheGame = function(){};

TheGame.prototype = {
     
     // preloading assets
     preload: function(){
          game.load.spritesheet("tiles", "assets/sprites/tile.png", gameOptions.tileSize, gameOptions.tileSize);
          game.load.image("player", "assets/sprites/player.png"); 
     },
     
     // when the game starts
  	create: function(){
          game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
		game.scale.pageAlignHorizontally = true;
		game.scale.pageAlignVertically = true; 
          this.levelNumber = 0;  
  		this.createLevel();
          game.input.onDown.add(this.beginSwipe, this);   
  	}, 
     
     // function to create a level
	createLevel: function(){
          this.tilesArray = [];
		this.tileGroup = game.add.group();
          this.tileGroup.x = (game.width - gameOptions.tileSize * gameOptions.fieldSize.cols) / 2;
          this.tileGroup.y = (game.height -  gameOptions.tileSize * gameOptions.fieldSize.rows) / 2;
  		for(var i = 0; i < gameOptions.fieldSize.rows; i++){
               this.tilesArray[i] = [];
			for(var j = 0; j < gameOptions.fieldSize.cols; j++){
                    if(levels[this.levelNumber].level[i][j]!=0){
                         this.addTile(i, j, levels[this.levelNumber].level[i][j]);
                    }
			}
		}
          this.playerPosition = new Phaser.Point(0, 0);
          levels[this.levelNumber].playerPos.clone(this.playerPosition);
          this.player = game.add.sprite(levels[this.levelNumber].playerPos.x * gameOptions.tileSize + gameOptions.tileSize / 2, this.playerPosition.y * gameOptions.tileSize + gameOptions.tileSize / 2, "player");
          this.player.anchor.set(0.5);
          this.tileGroup.add(this.player);
          this.firstMove = true;
	},
     
     // function add a tile at "row" row, "col" column with "val" value
	addTile: function(row, col, val){
		var tileXPos = col * gameOptions.tileSize + gameOptions.tileSize / 2;
		var tileYPos = row * gameOptions.tileSize + gameOptions.tileSize / 2;
		var theTile = game.add.sprite(tileXPos, tileYPos, "tiles");
		theTile.anchor.set(0.5);
          theTile.tileValue = val;
          theTile.falling = false;
          theTile.tilePosition = new Phaser.Point(col, row);
          this.tilesArray[row][col] = theTile;
	     this.tileGroup.add(theTile);	
	},
     
     // start checking for swipes
     beginSwipe: function(e){
		game.input.onDown.remove(this.beginSwipe, this);
     	game.input.onUp.add(this.endSwipe, this);
     },
     
     // end checking for swipes
     endSwipe: function(e){
          game.input.onUp.remove(this.endSwipe, this);
          var swipeTime = e.timeUp - e.timeDown;
          var swipeDistance = Phaser.Point.subtract(e.position, e.positionDown);
          var swipeMagnitude = swipeDistance.getMagnitude();
          var swipeNormal = Phaser.Point.normalize(swipeDistance);
          if(swipeMagnitude &gt; 20 &amp;&amp; swipeTime < 1000 &amp;&amp; (Math.abs(swipeNormal.x) &gt; 0.8 || Math.abs(swipeNormal.y) &gt; 0.8)){
               if(swipeNormal.x &gt; 0.8){
                    this.handleMovement(new Phaser.Point(1, 0));
               }
               if(swipeNormal.x < -0.8){
                    this.handleMovement(new Phaser.Point(-1, 0));
               }
               if(swipeNormal.y &gt; 0.8){
                    this.handleMovement(new Phaser.Point(0, 1));
               }
               if(swipeNormal.y < -0.8){
                    this.handleMovement(new Phaser.Point(0, -1));
               }     
          }
          else{
               game.input.onDown.add(this.beginSwipe, this);  
          }
     },
     
     // handling swipes
     handleMovement: function(p){
          if(this.firstMove){
               this.firstMove = false;
               this.tilesArray[this.playerPosition.y][this.playerPosition.x].tileValue = 2;
               this.tilesArray[this.playerPosition.y][this.playerPosition.x].frame = 1;
          }
          else{
               this.tilesArray[this.playerPosition.y][this.playerPosition.x].falling = true;
               var tween = game.add.tween(this.tilesArray[this.playerPosition.y][this.playerPosition.x]).to({
                    alpha: 0,
                    width: gameOptions.tileSize / 4,
                    height: gameOptions.tileSize / 4,
                    angle: game.rnd.integerInRange(-15, 15)
               }, 500, Phaser.Easing.Linear.None, true);
               tween.onComplete.add(function(target){
                    this.tilesArray[target.tilePosition.y][target.tilePosition.x] = null;
                    target.destroy();
               }, this)
          }
          this.playerPosition.add(p.x, p.y);
          var playerTween = game.add.tween(this.player).to({
               x: this.playerPosition.x * gameOptions.tileSize + gameOptions.tileSize / 2,
               y: this.playerPosition.y * gameOptions.tileSize + gameOptions.tileSize / 2
          }, 100, Phaser.Easing.Linear.None, true);
          playerTween.onComplete.add(function(){
               if(this.tilesArray[this.playerPosition.y][this.playerPosition.x] == null || this.tilesArray[this.playerPosition.y][this.playerPosition.x].falling){
                    this.levelFailed();
               }
               else{
                    if(this.tilesArray[this.playerPosition.y][this.playerPosition.x].tileValue == 2){
                         if(this.isLevelComplete()){
                              alert("ok you completed the level");
                         }
                         else{
                              var tween = game.add.tween(this.tilesArray[this.playerPosition.y][this.playerPosition.x]).to({
                                   alpha: 0,
                              }, 100, Phaser.Easing.Linear.None, true);
                              this.levelFailed();
                         }
                    }
                    else{
                         game.input.onDown.add(this.beginSwipe, this);     
                    }
               }     
          }, this)
          
     },
     
     // function to check if the level is completed
     isLevelComplete: function(){
          for(var i = 0; i < gameOptions.fieldSize.rows; i++){
			for(var j = 0; j < gameOptions.fieldSize.cols; j++){
                    if(this.tilesArray[i][j] != null &amp;&amp; !this.tilesArray[i][j].falling &amp;&amp; this.tilesArray[i][j].tileValue != 2){
                         return false;
                    }
			}
		}
          return true;     
     },
     
     // routine to start when the level is failed
     levelFailed: function(){ 
          var tween = game.add.tween(this.player).to({
               alpha: 0,
               width: gameOptions.tileSize / 4,
               height: gameOptions.tileSize / 4
          }, 500, Phaser.Easing.Linear.None, true); 
          tween.onComplete.add(function(){
               game.state.start("TheGame");     
          }, this);  
     }
}
It would be interesting to port some more levels, you can download the source code and add them on your own.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers