Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Down The Mountain game, Game development, HTML5, Javascript and Phaser.

About two years ago I released the latest part in my Down the Mountain prototype. At that time the game was performing very well. Today I saw my daughter playing on her brand new iPad Pro and I said “hey I made a prototype of that game”. So I decided to continue in the making of the game, and the first thing I did was updating Phaser to the latest version, adding tap controls rather than old boring arrow keys controls and making player and camera movements faster and smoother. Let’s have a look at the game:
Run down the mountain by tapping to the left or right half on the vancas, see how the mountain scrolls and how higher mountain blocks are removed from the game. Also, I don’t let the player move down the canvas too much, in this case I just increase scrolling speed. I really recommend you to have a look at previous steps to see how the “mountain” actually is a series of hexagons and the fake “jump” movement is made with a Bezier curve. I am publishing the source code, more optizmization has to be made, starting from an object pooling. Then we’ll start adding enemies and stuff. And comments of course.
var game;
var hexagonWidth = 70;
var hexagonHeight = 80;
var minRow = 0;
var gridSizeX = 5;
var gridSizeY = 9;
var marker;
var hexagonGroup;
var hexagonArray = [];
var playerCol = 2;
var playerRow = 0;
var playerMove = true;

window.onload = function() {
    game = new Phaser.Game(480, 480);
    game.state.add("PlayGame", playGame);
    game.state.start("PlayGame");
}
var playGame = function(game){}
playGame.prototype = {
    preload: function(){
        game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
        game.scale.pageAlignHorizontally = true;
        game.scale.pageAlignVertically = true;
        game.load.image("hexagon", "hexagon.png");
        game.load.image("marker", "marker.png");
    },
    create: function(){
        hexagonGroup = game.add.group();
        game.stage.backgroundColor = "#ffffff";
        for(var i = 0; i < gridSizeY; i ++){
            addHexagonRow(i);
        }
        hexagonGroup.x = (game.width - hexagonWidth * gridSizeX) / 2;
        hexagonGroup.y = 20;
        marker = game.add.sprite(hexagonGroup.width / 2, 20, "marker");
        marker.anchor.setTo(0.5);
        hexagonGroup.add(marker);
        game.input.onDown.add(function(e){
            if(playerMove){
                if(e.x < (game.width / 2) && (playerCol > 0 || (playerRow % 2 == 1))){
                    placeMarker(playerCol - (1 - playerRow % 2), playerRow + 1);
                }
                if(e.x >= (game.width / 2) &&  playerCol < gridSizeX - 1){
                    placeMarker(playerCol + (playerRow % 2), playerRow + 1);
                }
            }
        }, this)
    },
    update: function(){
        if(playerMove){
            if(gridSizeY - playerRow < 8){
                addHexagonRow(gridSizeY);
                gridSizeY ++;
            }
        }
        if(marker.world.y > 60){
            var distance = 60 - marker.world.y
            hexagonGroup.y += distance / 10;
        }
        var destroyedRow = false;
        for(var i = minRow; i < gridSizeY; i ++){
            for(var j = 0; j < gridSizeX; j ++){
                if((i % 2 == 0 || j < gridSizeX - 1) && hexagonArray[i][j].world.y < 0){
                    var destroyTween = game.add.tween(hexagonArray[i][j]).to({
                        alpha:0,
                        y: hexagonArray[i][j].y + hexagonHeight / 2
                    }, 200,  Phaser.Easing.Quadratic.Out, true);
                    destroyTween.onComplete.add(function(e){
                        e.destroy();
                    })
                    destroyedRow = true;
                }
            }
        }
        if(destroyedRow){
            minRow ++;
        }
    }
}
function placeMarker(posX, posY){
    playerRow = posY;
    playerCol = posX;
    var nextX = hexagonWidth * (2 * posX + 1 + posY % 2) / 2;
    var nextY = hexagonHeight * (3 * posY + 1) / 4;
    playerMove = false;
    var bezierX = hexagonWidth;
    if(marker.x > nextX){
        bezierX *= -1;
    }
    var playerTween = game.add.tween(marker).to({
        x: [marker.x, marker.x + bezierX, nextX, nextX],
        y: [marker.y, marker.y, nextY, nextY]
    }, 100, Phaser.Easing.Linear.None, true).interpolation(function(v, k){
        return Phaser.Math.bezierInterpolation(v, k);
    });
    playerTween.onComplete.add(function(){
        playerMove = true;
    });
    marker.bringToTop();
}
function addHexagonRow(i){
    hexagonArray[i] = [];
    for(var j = 0; j < gridSizeX - i % 2; j ++){
        var hexagonX = hexagonWidth * j + (hexagonWidth / 2) * (i % 2);
        var hexagonY = hexagonHeight * i / 4 * 3;
        var hexagon = game.add.sprite(hexagonX, hexagonY, "hexagon");
        var hexagonText = game.add.text(0 + hexagonWidth / 3 + 5, 0 + 15, i + "," + j);
        hexagonText.font = "arial";
        hexagonText.align = "center";
        hexagonText.fontSize = 10;
        hexagon.addChild(hexagonText);
        hexagonGroup.add(hexagon);
        hexagonArray[i][j] = hexagon;
    }
}
It was really a nice surprise to see how this game is still fun, download the source code while you wait for next steps.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.