Down the Mountain movement HTML5 prototype: going down the mountain

Read all posts about "" game
Last week I showed you the hexagonal concept behind iOS blockbuster Down The Mountain based on my tutorial how to find adjacent tiles in hexagonal maps – ALL and EVERY case explained, now it’s time to make the player actually go down the mountain. You will play with left and right arrow keys, to go down respectively on the left or on the right of the current block/hexagon. If you reach the bottom of the mountain or fall from the left or right edges, you will restart. Have a look at what we are going to do:
Move the player with left and right arrow keys. The code is not optimised at all so it’s still uncommented, but I am adding comments – and features – next week, meanwhile have a look at the code:
window.onload = function() {
	
	var game = new Phaser.Game(480, 480, Phaser.CANVAS, "", {preload: onPreload, create: onCreate, update: onUpdate});                

	var hexagonWidth = 70;
	var hexagonHeight = 80;
	var gridSizeX = 9;
	var gridSizeY = 7;
	var columns = [Math.ceil(gridSizeX / 2),Math.floor(gridSizeX / 2)];
	var moveIndex;
	var sectorWidth = hexagonWidth;
	var sectorHeight = hexagonHeight / 4 * 3;
	var gradient = (hexagonHeight / 4) / (hexagonWidth / 2);
	var marker;
	var hexagonGroup;
	var hexagonArray = [];
	var cursors;
	var playerCol = 2;
	var playerRow = 0;
	var playerMove = true;
    
	function onPreload() {
		game.load.image("hexagon", "hexagon.png");
		game.load.image("marker", "marker.png");
	}

	function onCreate() {
		hexagonGroup = game.add.group();
		game.stage.backgroundColor = "#ffffff";
		cursors = game.input.keyboard.createCursorKeys();
		for(var i = 0; i < gridSizeY / 2; i ++){
	     	hexagonArray[i] = [];
			for(var j = 0; j < gridSizeX; j ++){
				if(gridSizeY % 2 == 0 || i + 1 < gridSizeY / 2 || j % 2 == 0){
					var hexagonX = hexagonWidth * j / 2;
					var hexagonY = hexagonHeight * i * 1.5 + (hexagonHeight / 4 * 3) * (j % 2);	
					var hexagon = game.add.sprite(hexagonX, hexagonY, "hexagon");
					hexagonGroup.add(hexagon);
					hexagonArray[i][j] = hexagon;
					var hexagonText = game.add.text(hexagonX + hexagonWidth / 3 + 5, hexagonY + 15, (i * 2 + j % 2) + "," + Math.floor(j / 2));
					hexagonText.font = "arial";
					hexagonText.fontSize = 12;
					hexagonGroup.add(hexagonText);
				}
			}
		}
		hexagonGroup.x = (game.width - hexagonWidth * Math.ceil(gridSizeX / 2)) / 2;
		if(gridSizeX % 2 == 0){
			hexagonGroup.x -= hexagonWidth / 4;
        }
		hexagonGroup.y = (game.height - Math.ceil(gridSizeY / 2) * hexagonHeight - Math.floor(gridSizeY / 2) * hexagonHeight / 2) / 2;
        if(gridSizeY % 2 == 0){
        	hexagonGroup.y -= hexagonHeight / 8;
        }
		marker = game.add.sprite(0, 0, "marker");
		marker.anchor.setTo(0.5);
		marker.visible=false;
		hexagonGroup.add(marker);
        placeMarker(playerCol, playerRow);  		
	}
	
	function onUpdate(){
		if(playerMove){
			if(cursors.left.isDown &amp;&amp; cursors.right.isUp){
				playerCol -= (1 - playerRow % 2);
				playerRow ++;	
				placeMarker(playerCol, playerRow);
				playerMove = false;
			}
			if(cursors.right.isDown  &amp;&amp; cursors.left.isUp){				
				playerCol += (playerRow % 2);
				playerRow ++;	
				placeMarker(playerCol, playerRow);
				playerMove = false;
			}
		}
		else{
			if(cursors.right.isUp  &amp;&amp; cursors.left.isUp){
				playerMove = true;
			}
		}
	}     
     
	function placeMarker(posX,posY){
		if(posY &gt; 6 || posX < 0 || posX &gt; Math.floor(gridSizeX / 2) - posY % 2){
			posX = 2;
			posY = 0;
			playerCol = 2;
			playerRow = 0;
		}
     	for(var i = 0; i < gridSizeY / 2; i ++){
			for(var j = 0; j < gridSizeX; j ++){
				if(gridSizeY % 2 == 0 || i + 1 < gridSizeY / 2 || j % 2 == 0){
					hexagonArray[i][j].tint = 0xffffff;
				}
			}
		}
		if(posX < 0 || posY < 0 || posY &gt;= gridSizeY || posX &gt; columns[posY % 2] - 1){
			marker.visible = false;
		}
		else{
			marker.visible = true;
			marker.x = hexagonWidth * posX;
			marker.y = hexagonHeight / 4 * 3 * posY + hexagonHeight / 2;
			if(posY % 2 == 0){
				marker.x += hexagonWidth / 2;
			}
			else{
				marker.x += hexagonWidth;
			}
			var markerX = posX * 2 + posY % 2;
			var markerY = Math.floor(posY / 2);
			hexagonArray[markerY][markerX].tint = 0x00ff00;
			if(markerY + markerX % 2 < gridSizeY / 2 &amp;&amp; (gridSizeY % 2 == 0 || markerY < Math.floor(gridSizeY / 2))){
				// left
				if(markerX - 1 &gt;= 0){
					hexagonArray[markerY + markerX % 2][markerX - 1].tint = 0xff0000;
				}
				// right
				if(markerX + 1 < gridSizeX){
					hexagonArray[markerY + markerX % 2][markerX + 1].tint = 0xff0000;
				}
			} 
		}
	}	
}
Next time I am going to add deadly tiles and scrolling, to turn this prototype into a full endless runner game, meanwhile you can download the source code of the entire project.

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