How to make a simple maze game: Part 2


The first post, we initialized all of the variables, set up the game board placing the various items like gold coins, monkeys, etc. We added an event listener for when a key was pressed down to execute a function. This function is more than 1/5 of the code for the whole game, so let’s take a look at it. The meat of the function is two switch statements.

The first switch statement executes statements conditional on which arrow key was pressed. If you are going up, as long as you are not at the top of the screen (playerRow > 0)  then your player is moved up 1 row. If you are at the top, you don’t move. How it happens programmatically is that the value of the cell in the gameObjects map is set to 0 and the value of your new location is set to equal player. Your new location is the old playerRow – 1 and your previous column. Don’t forget the BREAK; statement in your switch or you will end up falling through to the next case.

Similarly, if you press the down arrow the playerRow value is increased by 1 and you move down, if you press the left or right arrows, you move left or right. Since these are the only cases addressed, if you press any other key, you don’t move.

Even if you don’t move, if you are, say, at the top of the game board and you keep pressing the up arrow key, you still lose health points with each move and eventually die. This teaches the student the invaluable lesson, “Don’t be a dumbass.”

 

function keydownHandler(event) {
    switch (event.keyCode) {
        case up:

            //Find out if the Player's move will
            //be within the playing field
            if (playerRow > 0) {
                //If it is,
                //Clear the Player's current cell
                gameObjects[playerRow][playerColumn] = 0;

                //Subtract 1 from the Player's row
                //to move it up one row on the map
                playerRow--;


                //Apply the Player's new updated position to the array
                gameObjects[playerRow][playerColumn] = player;
            }
            break;

        case down:
            if (playerRow < rows - 1) {
                gameObjects[playerRow][playerColumn] = 0;
                playerRow++;
                gameObjects[playerRow][playerColumn] = player;

            }
            break;

        case left:
            if (playerColumn > 0) {
                gameObjects[playerRow][playerColumn] = 0;
                playerColumn--;
                gameObjects[playerRow][playerColumn] = player;

            }
            break;

        case right:
            if (playerColumn < columns - 1) {
                gameObjects[playerRow][playerColumn] = 0;
                playerColumn++;
                gameObjects[playerRow][playerColumn] = player;

            }
            break;
    }

The next part of the function executes statements depending on which type of cell the player has entered. In each case, a sound plays. I just alternated between the walk and run sounds for the empty rooms, just for the heck of it. At each type of cell a different message is displayed, like “Keep going.”.  The playSound function plays and the setMessage function changes the text displayed to the gameMessage value.

For every different type of object, a different sound plays, thanks to our amazing sound guy, Jonah.

    //find out what kind of cell the player is in
    switch (map[playerRow][playerColumn]) {
        case inside:
                walk = 1;
            gameMessage = "Keep going. Not there yet.";
            if (row % 2 == 0) { playSound("footsteps_run") }
            else { playSound("footsteps_walk") ;
                setMessage();

            }
            health--;
            gold--;
                setMessage();

            break;

Note that if if its a rock, squash or coin that the value of that cell is set to “inside”. After you have eaten the squash or picked up the coin, it shouldn’t still be there. Gold gives you more wealth. The monkey kills you, as does walking off into thin air (don’t be a dumbass). If you hit a rock or eat a squash different things can happen so you get sent to those functions . If you make it to your head dress at the top of the pyramid, you win . Yay!

If your wealth or health points drop to 0, you die. Not yay.  If you are running low on gold, you get a warning message.  Finally, the game board is drawn again, by the render function,  to show the new position of the player, the squash, gold or rock disappeared.

        case rock:
                walk = 0 ;
            playSound("hit_rock");
            map[playerRow][playerColumn] = inside ;
            rockEffect(2);
            break;

        case squash:
            walk = 0 ;
            map[playerRow][playerColumn] = inside ;
           eatSquash();
            break;

        case head_dress:
            walk = 0 ;
            endGame();
            break;
        case monkey :
            walk = 0 ;
            playSound("monkey");
            $("#output2").html("The monkey knocked you off the pyramid.");
           death();
            break;
        case air:
            walk = 0 ;
            $("#output2").text("You fell to your death.");
            playSound("fall");
           death();
            break;
        case goldcoin:
            walk = 0 ;
            playSound("coin");
            map[playerRow][playerColumn] = inside ;
            gameMessage = "More gold for bribes.";

            setMessage();
            gold++;
            break;
    }
    
    if (gold <= 0 || health <= 0)
    { endGame(); }
    else if (gold < 4) {
        gameMessage2 = "Running low on gold. Better find some soon."
    }
    //Render the game- this actually draws everything again ;
    render();
}


Maria (our CEO) thinks these posts are a waste of time because people who buy games for their kids are too busy taking their kids to soccer practice, softball and dance class to read blogs on coding games.

Prove her wrong by buying our games. 

Start screen with deer, buffalo and wigwam

or one of our cool shirts – great for wearing while coding.

7 Generation Games shirt

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.