Simple Grid/Tile based layout

I am trying to create a simple game, where there is the human controlled character and some enemy placed in a maze. When you take one move, the enemy takes one move, until he reaches you or you reach the exit.

What I'm having trouble figuring out is how I can lay all this out.

What I'm envisioning is a 2 dimensional array, with each position either a 0 or 1 depending on if the user can move there or not.

Is there a way in java to implement a grid style playing field, such that the player moves from tile to tile, and incoporate collision detection with the use of the array values?

I could realy use some help here, I was thinking of using a layout manager, but I don't think the two ideas are compatible. Please help!

[758 byte] By [curiousdevelopera] at [2007-10-2 12:05:30]
# 1

Assuming your not going to use a special-purpose gaming library, you'll probably have to build most from scratch.

A simple way of doing it would probably be to to create a 2-dimensional Tile array, in which Tile is a class you'll write yourself to hold all relevant information (painting info, wether it can be moved to, etc).

If you're going for a 'square' looking game (as opposed to isometric) you might be able to use GridLayout to some extent. But in that case (or when using any other layout manager) your Tiles will have to subclass Component. My guess is that it will quickly become more work to cram everything into Component-shape than it's worth...

Short skeleton-example of what I might do:

public class Game extends JFrame {

private Tile[][] tiles;

private Monster[] monster;

private Player player;

private GameCanvas canvas;

public Game() {

canvas = new GameCanvas();

add(canvas);

//other initialization, listeners, etc.

}

//you could call something like this when you detect user input that implies a move

private void move(int x, int y) {

if (tiles[x][y].isPassable()) {

player.moveTo(x, y);

for (int i = 0; i < monsters.length; i++) {

monsters[i].makeMove();

}

canvas.repaint();

} else {

//inform user that he can't move to (x, y)

}

}

private static class GameCanvas extends JPanel {

public void paintComponent(Graphics g) {

for (int i = 0; i < tiles.length; i ++) {

for (int j = 0; j < tiles[i].length; j++) {

//OtherPaintingInfo is stuff like coordinates, width, height, etc.

//All of that more or less depends on how you want tiles to look

tiles[i][j].paint(Graphics g, OtherPaintingInfo I);

}

}

//do something similar for monsters and player

}

}

private static class Tile {

boolean isPassable() { ... }

//Again, OtherPaintingInfo is just a placeholder for anything your impl. needs

void paint(Graphics g, OtherPaintingInfo I() { ... }

}

}

DaanSa at 2007-7-13 8:32:23 > top of Java-index,Other Topics,Java Game Development...