Kilobolt
  • Home
  • Tutorials
    • Game Development Tutorial >
      • Unit 1: Beginning Java >
        • Before you begin...
        • Day 1: Setting Up
        • Day 2: Java Basics
        • Day 3: More Basics
        • Day 4: Java Math
        • Day 5: More Math
        • Day 6: If... else...
        • Day 7: More Control Flow
        • Day 8: Looping
        • Day 9: More on Looping
        • Day 10: Inheritance, Interface
        • Day 11: Threads and Graphics
      • Unit 2: Creating a Game I >
        • Day 1: Foundations
        • Day 2: Basic Framework
        • Day 3: Taking User Input
        • Day 4: Enter the Robot
        • Day 5: Background and Sprites
        • Day 6: Adding Enemies
        • Day 7: Shooting Bullets
        • Day 8: Animations
        • Day 9: 2D-Arrays
        • Day 10: Painting the Tilemap
      • Unit 3: Creating a Game II >
        • Day 1: Level Creation - Part 1
        • Day 2: Level Creation - Part 2
        • Day 3: Level Creation - Part 3
        • Collision Detection Basics
        • Day 4: Collision Detection Part 1
        • Day 5: Collision Detection Part 2
        • Day 6: Collision Detection Part 3
        • Day 7: Health System & Death
        • Day 8: Basic AI & Final Touches
      • Unit 4: Android Game Development >
        • Day 1: Introduction to Android
        • Day 2: Setting up for Development
        • Day 3: Creating our First Android Application
        • Day 4: Parts of an Android Application
        • Day 5: The Android Game Framework: Part I
        • Day 6: The Android Game Framework: Part II
        • Create an Android Game From Scratch (or port your existing game)
        • Day 7: Creating an Android Game (From Start to Finish)
      • Reference Sheet
    • Zombie Bird Tutorial (Flappy Bird Remake) >
      • Unit 1: Building the Game >
        • Introduction
        • Day 1: Flappy Bird - An In-depth Analysis
        • Day 2: Setting up libGDX
        • Day 3: Understanding the libGDX Framework
        • Day 4: GameWorld and GameRenderer and the Orthographic Camera
        • Day 5: The Flight of the Dead - Adding the Bird
        • Day 6: Adding Graphics - Welcome to the Necropolis
        • Day 7: The Grass, the Bird and the Skull Pipe
        • Day 8: Collision Detection and Sound Effects
        • Day 9: Finishing Gameplay and Basic UI
        • Day 10: GameStates and High Score
        • Day 11: Supporting iOS/Android + SplashScreen, Menus and Tweening
        • Day 12: Completed UI & Source Code
    • Android Application Development Tutorial >
      • Unit 1: Writing Basic Android Apps >
        • Before you begin...
        • Day 1: Android 101
        • Day 2: Getting to Know the Android Project
        • Day 3: The Development Machine
        • Day 4: Building a Music App - Part 1: Building Blocks
        • Day 5: Building a Music App - Part 2: Intents
        • Day 6: Building a Music App - Part 3: Activity Lifecycles
  • New Forum
  • About Us
    • Contact Us
  • Our Games
    • TUMBL: FallDown
  • Facebook
  • Twitter

GAME DEVELOPMENT TUTORIAL: DAY 2-9: Two-Dimensional Arrays

11/11/2012

32 Comments

 
Picture
Welcome to Day 9! Today's lesson will be short, but full of information. We are building towards loading pre-made Level designs and rendering tiles to the screen. To do so, we must first go over some basic fundamentals. Therefore, we will be creating a new project to practice and study these new topics!
Lesson #2-26: Arrays
One of the most powerful tools in a programmer's arsenal is the array. You can think of an array as a list of items. Let's say you had three favorite things:

1. Playing
2. Eating
3. Sleeping

These three things could easily be categorized into one list so that you can refer people to this list rather than three separate variables. That's what an array does, in principle.

1. To see this in action, create a new Project, and name it whatever you want.
2. Within the src folder, create a new Java class called: Array.
3. Add the main method:

  public static void main(String args[]) {

}

4. Within the main method, declare the following:

String[] favoriteThings = new String [3];


This creates an Array (list) of String objects called stringArray, and allocates memory to accommodate three (3) items.

Alternatively, this same line can be written as below, by shifting the brackets:
String favoriteThings[] = new String [3];

Astute observers will recognize this syntax. If you examine the parameter of the main method:
String args[], you will now see that the main method takes in an array of Strings as a parameter. These arrays are passed in at a lower level, and you will not encounter them in Eclipse.

Your code should be looking like this:

Figure 2-34: Array Class

 public class Array {

         public static void main(String args[]) {
String[] favoriteThings = new String [3];
}

}
Now let's talk about how to fill this Array.

We can refer to positions in the array as indices. The first item in the list will have an index of 0, the second will have 1, and so on.

To refer to a specific index of an Array, we can write:
array[index].

Going back to our code, we can list our three favorite things as follows:

...
public static void main(String args[]) {
String[] favoriteThings = new String [3];

favoriteThings[0] = "Playing";
favoriteThings[1] = "Eating";
favoriteThings[2] = "Sleeping";

}
...

What if we now want to print all these items?
It's tempting to use System.out.println(favoriteThings), but this will return what seems to be nonsense.

Instead, we must use a for loop, and iterate over an index to print each line.

This is how I would approach it:

for (int i = 0; i < favoriteThings.length; i++) {
System.out.print(i+1 + ". ");
System.out.println(favoriteThings[i]);
}


This code introduces nothing new. It is just application of previously-covered topics. Make sure you understand this before moving on!

If we place this for loop at the bottom of the main method, we will see the following output:
Picture
Understand how it works? Then you are ready for the next level.
Lesson #2-27: Two-Dimensional Arrays
The most practical application of a two-dimensional Array is when you need to store a list of values with (x, y) positions. 

For our game's purposes, we will use a 2D array to hold a value at each position, and then paint the object/character/platform associated with said value at its (x, y) coordinate.


2D Arrays are very similar to single dimensional arrays, but we add a second dimension (like adding a width).

Two create a 2D array, we simply state:
int[][] intArray = new int[30][50]; //Creates a 2D array of integers with height 30 and width 50.

1. Create a new class called Tilemap. 
2. Add the following code:

Figure 2-35: Tilemap Class

import java.util.Random;

public class Tilemap {


public static void main(String args[]) {
Tilemap tp = new Tilemap();

}

public Tilemap() {
int[][] tilemap = new int[30][50];

System.out.println("New Tilemap created.");
Random r = new Random();

int rows = tilemap.length;
int columns = tilemap[1].length;

printTiles(rows, columns, tilemap, r);

}

private void printTiles(int rows, int columns, int[][] tilemap, Random r) {

for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
tilemap[i][j] = r.nextInt(5);
System.out.print(" " + tilemap[i][j]);
}

System.out.println("");

}
}

}
Before you run this code, let's talk about what each segment of code does.

1. We import the Random class (to be used later)
2. In the main method, we create a new Tilemap object called tp. This calls the constructor below the main method.

3. In the constructor, we create a new two dimensional array called tilemap, with height 30, and width 50.
- We also create a Random object called r

Two-dimensional arrays can be thought of as Arrays of Arrays. Meaning that it is a list of lists. The larger list has 30 empty spots. Each of those 30 spots has 50 spots across.

Therefore, when we refer to tilemap.length, we get the length of the larger list, which is 30.
When we refer to tilemap[1].length, we get the length of the small list with index 1 in the large list, which is the second small list in the large list. This will have a value of 50.

4. We then call the printTiles() method, which takes in various parameters, uses two for loops (one for the columns and another for the rows).

This printTiles() method gives each space a value between 0 to 4 (at random). Then we print each row and column.

The result should look something like this:
Picture
In the next lesson, we will associate each integer from 0 to 4 with a colored rectangle and fill the screen. This will then segue into a Unit 3, beginning with a series of lessons on Level design/rendering and collision physics!

Stay Tuned!
Go to Day 8: Animations
Go to Day 10: Painting the Tilemap
32 Comments

    Author

    James Cho is the lead developer at Kilobolt Studios. He is a college student at Duke University and loves soccer, music, and sharing knowledge. 


© 2014 Kilobolt, LLC. All rights reserved.