Array based level help

Hi, I was hoping someone could explain the concept behind using an array for a level in a 2d game. The only tutorial I could find on the subject was not helpfull. I do not see how to take the array and create a grid where I could place a character or wall into an array location. any help is greatly appreciated. Thx for reading

[335 byte] By [cookiemobster8] at [2007-9-27 16:19:19]
# 1
What kind of game? What kind of array? What are you talking about?
Ragnvald at 2007-7-6 0:37:43 > top of Java-index,Other Topics,Java Game Development...
# 2
There is no set game that I have in mind, I would be happy just to display a square and move it with the arrow keys :) once agin any help is greatly appreciated
cookiemobster8 at 2007-7-6 0:37:43 > top of Java-index,Other Topics,Java Game Development...
# 3
oh yeah, as for the type of array, I would assume an int array. I just don't understand if I declare an array say... int array[][], how I could place an object like a square or an image.
cookiemobster8 at 2007-7-6 0:37:43 > top of Java-index,Other Topics,Java Game Development...
# 4

Although all gfx are essencially primative arrays ([] not [][]), through abstraction Java tries to hide this concept from you. In Java, all images that can be rendered to the screen are subclasses of the Image class.

There are subclasses of Image (java.awt.image.BufferedImage) that allow ou to manipulate the underlying primitive representation, but, for the most part you don't need to do this.

Simply creating a BufferedImage with the desired width and height and an appropriate TYPE_ like this..

BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //(the TYPE_ is how the image data will be stored in the underlying primitive [] - in this case it is integer packed RGB values, with no alpha)

when you want to draw the image to a Graphics context, simply use the method in the Graphics class, drawImage(Image,x,y,ImageObserver)

public void paint(Graphics g)

{

g.drawImage(bufferedImage,x,y,null);//in this case you need not worry about the ImageObserver

}

rob,

Abuse at 2007-7-6 0:37:43 > top of Java-index,Other Topics,Java Game Development...
# 5

The array is the data-model in an MVC architecture. You need to have a View object that represent the "stuff" in that array on the screen. As an example - let's say I use an int[][] levelData = new int[100][100] to represent my level. Make a Canvas subclass that "knows about" a level. Your Canvas implements "one array entry is 10x10 pixels". It implements "an array element of value 'x' maps to a 10x10 image 'y'". It draws the various images at the "right" locations on itself. Whenever the array changes, ask your Canvas to redraw, and it'll draw different things (because the model changed).

That's a VERY high-level description - but it should at least point you in the right direction...

ggainey at 2007-7-6 0:37:43 > top of Java-index,Other Topics,Java Game Development...
# 6
ah, is he asking about how to do tiling?im still not sure what the question is :|
Abuse at 2007-7-6 0:37:43 > top of Java-index,Other Topics,Java Game Development...
# 7

Here's what I'm currently doing for my background map... I have a byte array map[][]. I use a seeded Random object to put a number either a 0 or a 3 in each spot... Actually, put a 3 in or leave it alone, but you get the idea. I then have a function that goes through and averages out the neighboring squares so I get a range between 0 and 3 without any 0-3 "gaps"... My GameMap class then takes the created 2D-array, finds the position of the player (located in a Position object), and draws a map based on where the player is. Each map[][] entry corresponds to a 32x32 square on the screen... If map[x][y]==0, that square becomes dark blue... if it's 1, it's a lighter blue, etc.

Actually though... After reading Abuse's comment, I think it might be better to store my 2D array as a BufferedImage and then just copy and expand regions... I've tried this once, but can't seem to get the ColorModel working right.

cbisbee at 2007-7-6 0:37:43 > top of Java-index,Other Topics,Java Game Development...
# 8

ah right - so your using a [][] as a grid map for a whole lot of 32x32 tiles.

ignore what i said in my 1st post, I didn't understand your question(i thought u were trying to draw an arbitary int [] raster using the Graphics object)

stick with the way your doing it atm, its the right way to do it.

so you seem to know what your doing, so whats the problem?

do you want to know how to do the tiling?

Abuse at 2007-7-6 0:37:43 > top of Java-index,Other Topics,Java Game Development...
# 9

I think the original question is how to use an array to show a path that the avatar is allowed to move. For instanance

/*Create a grid of 144 squares */

int grid[][] = new int[12][12] ;

final int VALID_POSITION = 1 ;

final int INVALID_POSITION = 2 ;

Then, by checking his avatars x,y location against the grid, you can see if by moving right,left, up, down is valid.

markkid at 2007-7-6 0:37:43 > top of Java-index,Other Topics,Java Game Development...
# 10
lol - another interpretation of the questionso now this is a collision map question?I think this needs clarifying, what *exactly* are you having problems with?rob,
Abuse at 2007-7-6 0:37:43 > top of Java-index,Other Topics,Java Game Development...
# 11
Perhaps we should bet some dukes on who's interpretation of the question is correct? I smell an idea for yet another forum. A bad idea, but an idea, none the less.
markkid at 2007-7-6 0:37:43 > top of Java-index,Other Topics,Java Game Development...
# 12

Well, the way I read it, the OP (cookiemobster8, btw, not cbisbee - that may have contributed even more to the confusion!) was after the real basics - how to map GUI events to his model and map the model to the screen.

That's one thing about these forums - it's sometimes difficult to tell the general ability-lvl and experience of a poster based on the first, often general, statement of the problem...

ggainey at 2007-7-6 0:37:43 > top of Java-index,Other Topics,Java Game Development...
# 13
LOL... my message was supposed to be HELP, not the question... I was just giving the guy an example of how to do it.
cbisbee at 2007-7-6 0:37:43 > top of Java-index,Other Topics,Java Game Development...
# 14
lol - this is getting to confusing.
Abuse at 2007-7-6 0:37:43 > top of Java-index,Other Topics,Java Game Development...
# 15

Thanks all for the responses. I am relativly new to the Java language. I had no idea that there was so much to trying to do this :) I really dont have any idea what a collision map is, but with the info I should be able to do some research and get it. The question I posted must have been cryptic, so I will try to rephrase it. If I was to create an applet say 500x500, and I wanted to make a grid where a square could be placed initially in the middle. I could just draw a square in the middle and control the posistion by using the arrow keys, but I have seen people who use an array(how I have no concept) for dealing with the places the square could move. so if i craete an array[], and create a square how could the value of say 1 make a square appear in the corrisponding grid location to the 1 in the array? thx all :)

cookiemobster8a at 2007-7-18 13:42:01 > top of Java-index,Other Topics,Java Game Development...
# 16

Did you mean something like this?

[1] [2] [3]

[4] [5] [6]

[7] [8] [9]

[10][11][12]

int WATHER=2;

int GROUND=1;

int PATH=3;

int ground[]=new int[13];

ground[1]=WATHER;

ground[2]=GROUND;

ground[3]=PATH;

place something on position(x=0, y=1) 4 and call for example method up()

void UP(){x=x;y=y-1;

c=checkPOS(x,y);

if(c==2){

System.out.println("I HATE WATER. (some thought about sadistic player and maso... character)");

x=x;y=y+1;wet+=2;}

}

int checkPOS(int x, int y){

int a=y*3+x+1; //I started array from 1 so +1

return ground[a];

}

Raghara at 2007-7-18 13:42:01 > top of Java-index,Other Topics,Java Game Development...
# 17

i was able to use the array to display things, and this is what i came up with...

import java.util.*;

import java.applet.*;

import java.awt.*;

import javax.swing.*;

public class Adventure extends JApplet implements Runnable

{

Image grass;

Image player;

int array[][]={ {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

public void init()

{

load_image();

setBackground(Color.black);

}

public void run()

{

}

public void paint(Graphics g)

{

checkBackground();

int col;

int row;

for(row=0;row<15;row++)

{

for(col=0;col<20;col++)

{

if(array[row][col]==0)

{

g.drawImage(grass,(col*31),(row*31),this);

}

else if(array[row][col]==1)

{

g.drawImage(player,(col*31),(row*31),this);

}

}

}

}

public void update()

{

}

public void checkBackground()

{

}

public void load_image()

{

grass = getImage(getCodeBase(),"grass.png");

player = getImage(getCodeBase(),"player.png");

}

}

that is kinda what I was trying to do. Is there a better way? If so any help is appreciated :)

cookiemobster8a at 2007-7-18 13:42:01 > top of Java-index,Other Topics,Java Game Development...
# 18

efficiency wise, yes there are much better ways of doing it

your 2 for loops in your paint method should be changed so they dont involve all those multiplies (having 2 extra ints, and incrementing them by 32 each loop would be faster than performing x*y*2 multiply operations every paint)

the collision map itself is rather wasteful, at the moment your using a 32bit primitive to store either a 0 or 1, that is 32bits to store the information of 1bit! To solve this, you should pack the collision values into the ints.

Another issue more fundamental to your solution, if your background is not going to be very big ( say less than 4096x3072) it would be more speed efficient to prerender all the background tiles to a single large image - then each repaint, simply redraw the background image at the desired offset (if scrolling is what is intended)

However, this different solution is less flexible (as it imposes a max. background size) and also uses more memory (if you were using Volatile Images for instance, this solution would not be practical)

rob,

Abusea at 2007-7-18 13:42:01 > top of Java-index,Other Topics,Java Game Development...