3
votes

I've been trying to figure out now if i have a map in my platformer game and i want my character be able to jump on some platform for example.. Is there any easy way to tell the game where the platforms are and where the player can jump and start??

Also the games like Super Mario Bros and such are their maps made in .txt files ? Because i watched a tutorial for maps and he said all proffesional game developers make a .txt file for the map and write numbers for example:

111111111111111111111

111111111111111111111

111111111111111111111

111111111111111111111

222222222222222222222

To create a map.. Is this correct or how did the developers do maps in the Super mario bros or Mega man games ??

What i am wondering is.. If i want my player to be able to jump up on some platform or something.. Thats what am looking for.

2
"all proffesional game developers make a .txt file for the map and write numbers" That is not at all correct. Many developers use map editors of some sort, and the format for those maps varies a lot.thedaian
But if would have a map finished from a map editor how would i solve that creating a platform for example and making so if player jumps he stays at that platform not go through it.. ?Rakso
@thedaian - It's actually quite common for 2D games to use a matrix of tileID numbers to represent their maps. Obviously this isn't done by hand, It's generated by a map editor. It's not a text document either as that would be slow. A binary file would be more likely.MGZero

2 Answers

2
votes

A very simple example:

You can represent the map by a matrix of e.g. numbers. A number of one will represent an obstacle and a number of zero will represent open space.

                   [000000]              [      ]
int[] map    ==    [000000]      ==      [      ]
                   [000111]              [   xxx]
                   [001111]              [  xxxx]

The player object must also have a coordinate inside the matrix, where horizontal position (X) is the column and vertical position (Y) is the row, and a velocity (direction and speed). The velocity is controlled by user input, like for example pressing the right arrow will set the X-direction to speed +1.

int xPos;
int yPos

int xSpeed;
int ySpeed;

while(true) {
    // Check user input
    if (upArrow()) {
        ySpeed = 1;
    }
    if (downArrow()) {
        ySpeed = -1;
    }
    if (leftArrow()) {
        ySpeed = -1;
    }
    if (rightArrow()) {
        ySpeed = 1;
    }

    // Update player position
    xPos = xPos + xSpeed;
    yPos = yPos + ySpeed;

// ...

Now you just have to check the number in the matrix for the current position the player object has in the matrix. If it's a zero do nothing, if it's a 1: set the speed to 0.

    int mapObstacle = map[xPos, yPos];
    if (mapObstacle == 1) {
        // Stop player movement
        xSpeed = 0;
        ySpeed = 0;
    }
}

To keep the map in txt file you must get your game to read/write the map matrix from the file. Below is an example for reading.

int n = 0;
while (mapFile.hasNextLine()) {
    String mapLine = mapFile.nextLine();

    for (int i = 0, n = mapLine.length; i < n; i++) {
        int mapObstacle = Integer.parseInt(mapLine.charAt(i));
        map[n, i] = mapObstacle; // Read map layout
    }
    n++;
}
0
votes

Using text files are okay, but using PNG or Bitmap graphics are better. A pixel can potentially store up to 4 bytes of information, and can be easily be created using photo manipulation tools.

First, before all, you should have an "Entity" or "GameObject" class that everything in your scene can inherit. Example:

public class Entity
{
    public float x, y, width, height;

    public void onStep() {}
    public void onDraw() {}

    public RectF getRectF()
    {
        RectF rect = new RectF();
        rect.left = x;
        rect.top = y;
        rect.right = x + width;
        rect.bottom = y + height;
        return rect;
    }
}

Second, you should have a handful of different classes that extend the Entity class, such as a Player class, a Wall class, and maybe a Coin class:

public class Player extends Entity {...}
public class Wall extends Entity {...}
public class Coin extends Entity {...}

Your next step would be to override the Player's step event, and in that step event, iterate through all of the wall entities like so:

@Override public void onStep()
{
    for (Entity e : my_active_entities)
    {
        if (e instanceof Wall)
        {
            Wall wall = (Wall) e;

            if (RectF.intersects(wall.getRectF(), this.getRectF()))
            {
                // We now know that our player is intersecting with "e"
                // We also know that "e" is a wall

                wall.doSomething();
                this.doSomething();
            }
        }
    }
}

Lastly, would be to create your own stage loader. This will iterate through your graphic and plot objects on the screen based on the color of the current pixel.

Bitmap bitmap = getOurBitmapFromSomewhere();

for (int x = 0; x < bitmap.getWidth(); x++)
    for (int y = 0; y < bitmap.getHeight(); y++)
    {
        int color = bitmap.getPixel(x, y);

        switch (color)
        {
            case 0xFF00FF00:
                // First byte is Alpha
                // Second byte is Red
                // Third byte is Green
                // Fourth byte is Blue

                Player player = new Player();
                player.x = x * TILE_SIZE; // For example, 16 or 32
                player.y = y * TILE_SIZE;

                my_active_entities.add(player);
                break;
        }
    }

This wraps up my two cents of game object handling, collision detection, and level loading. Anyone who stumbles by this is more than welcome to use this code or this concept of code in their software.