Ember
Level 0
|
|
« Reply #3 on: November 06, 2010, 04:26:22 PM » |
|
Perhaps I should have been more specific. This is purely reading a file and loading it in the engine. Not procedural generation(I'm definitely not ready for that!) After reading up on two dimensional arrays, I found that you can iterate through them, and I tried doing that with drawing tiles. My temporary system, a 2d array with 1s and 0s: int level[][] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, }; My paint method: public void paint() { Graphics g = (Graphics) buffer.getDrawGraphics(); g.setColor(Color.black); g.fillRect(0,0,800,600); p1.draw(g); //draw tiles g.setColor(Color.blue); { //set i to 0, when it finishes all the columns in a row, increment the row //stop when you run out of rows for(int i = 0;i < level.length; i++) { //set j to 0, when it finished all the columns in a row, loop back and increase the row number for(int j = 0; j < level[ 0] .length; j++) { if(level[i][j] == 1) { //draw a 32x32 tile g.drawRect(j*32, i*32, 32, 32); } } } } Which does work...However the problem is, its really taxing to do this every frame. What can I do to optimize this? Is there a better way? Also, the image trick is neat, but I don't understand how I would read an image's per pixel color values from the standard Image class. Would I have to write code to separate the image into pixels, read the values, then blit it back together again?
|
|
« Last Edit: November 06, 2010, 04:49:54 PM by Ember »
|
Logged
|
|
|
|