Step 1

Creating our variables

Let’s first start out creating our variables. we will need to make 3.

  • enemySpeed: This will be how fast our enemies move
  • lane: The current lane our player is in
  • laneYCoords: The different y coordinates for each lane. This will be an array.
  • Understanding arrays

    Now before we move on, we need to go over a new concept to understand the programming in this game, arrays.

    Think of an array as a container that can hold multiple things. Instead of having separate variables for each thing, we can have one variable that holds them all. This keeps everything together very nicely for us.

    However, we need to be able to access the data inside an array individually as well, to do this we use something called an index.

    If we think of an array as a small neighborhood, the index would be the address of a value, where we can find it and access it.

    The important thing we need to notice is that computers count at 0. So the first item inside an array will always have an index of 0.

    Another important thing we need to always know is how big our array is, or its length. This is because if we ever try to get an index that doesn’t exist, say 100, we would get an error. One cool thing to know is that the last index of our array will always be it’s length – 1

    Put into more mathematical terms, our array indices will always have a range of 0 – (length -1).

    All of this may take some time to understand, but hopefully as we complete this project it will make a little more sense.

    Creating our first array

    Complete the code as shown.

    Here we are creating an array with 3 elements. Each of these is a y coordinate of one of the lanes our player can move to.

    The 3 coordinate values we want are 15, 60 and 105.

    Setting the player y position

    Complete the code as shown.

    When we set the position of our player, we want to use one of the values from our laneYCoords array.

    Here we are saying set the y coordinate to whatever y coordinate we have saved at index 1 (since lane has a value of 1). This will start our player on the center lane (the y coordinate of 60, remember, computers start at 0!)

    Creating the tilemap

    Lastly, go ahead and complete the tilemap for your game. Make sure it has 3 visible areas that make it obvious where the lanes are. Don’t forget to make the dimensions 10 x 8!

    Testing the code

    Lastly, test out the code to make sure it’s working. You should see your player start in the middle lane!