从数组加载地图(对于游戏)的问题

时间:2013-02-08 19:28:25

标签: java android arrays graphics

我正在使用文本文件来存储地图,但加载它们会给我带来一些麻烦。物体被挤压在一起,我不知道为什么。这是“地图”文件(level_1_1.map):

"level_1_1.png", ?600:400?;
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,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,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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0,0,0,0,0,0,0,0,0,0,0,0,0,1,1;
1,1,2,2,2,2,2,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,1;
1,1,2,2,2,2,2,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,1;
1,1,2,2,2,2,2,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,1;
1,1,2,2,2,2,2,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,1;
1,1,2,2,2,2,2,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,1;
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,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,1,1,1,1,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,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,1,1,1,1,0;

以下是我加载它的类:

package org.awesome.Models;



public class Map{
int playerWidth = 50;
int playerHeight = 50;
int platformWidth = 20;
int platformHeight = 20;

int playerX, playerY;
List<Platform> platforms = new ArrayList<Platform>();
Bitmap bg;
int backgroundX, backgroundY;

private Context context;
private SurfaceView game;
public Map(Context context, SurfaceView game){
    this.context = context;
    this.game = game;
}

public void update(){

}

public void draw(Canvas canvas){

    for(Platform p : platforms)
        p.draw(canvas);
}

public boolean loadMap(String filename){
    //load the file into a string
    byte[] buffer = null;
    try {
        AssetManager am = context.getAssets();
        InputStream fis = am.open(filename);
        buffer = new byte[fis.available()];
        fis.read(buffer, 0, fis.available());
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    String s = new String(buffer);
    //seperate the map image, the dimensions and the mapping itself
    s = s.substring(s.indexOf("\"") + 1);
    String bgname = s.substring(0, s.indexOf("\""));    
    bg = BitmapFactory.decodeFile(bgname);
    s = s.substring(s.indexOf(",") + 1);
    s = s.substring(s.indexOf("?") + 1);
    String dimensions = s.substring(0, s.indexOf("?"));
    //actual dimensions
    int width = Integer.parseInt(dimensions.substring(0, s.indexOf(":")));
    int height = Integer.parseInt(dimensions.substring(s.indexOf(":") + 1));

    s = s.substring(s.indexOf(";") + 1);
    String[] mapRows = s.split(";");
    //the actual mapping of things
    String[][] map = new String[mapRows.length][];
    for(int i = 0; i < mapRows.length; i++){
        map[i] = mapRows[i].split(",");
    }

    //now create the stuff
    for(int r = 0; r < map.length; r++){
        for(int c = 0; c < map[r].length; c++){
            map[r][c] = map[r][c].trim();
            int currentDigit = Integer.parseInt(map[r][c]);
            int x = c * 10;
            int y = r * 10;

            //Will create the object at the top left corner of the !!RECTANGULAR!! map representation.
            if(currentDigit == 0){

            }else if(currentDigit == 1 && isTopLeft(1, r, c, map)){
                Bitmap texture = BitmapFactory.decodeResource(game.getResources(), org.awesome.AndroidGame.R.drawable.brown_block_reg);
                Log.w("making platform", "" + x +  ", " + y);
                platforms.add(new Platform(x, y, texture));
            }else if(currentDigit == 2 && isTopLeft(2, r, c, map)){
                playerX = x;
                playerY = y;
            }else if(currentDigit == 3){

            }else if(currentDigit == 4){

            }else if(currentDigit == 5){

            }else if(currentDigit == 6){

            }else if(currentDigit == 7){

            }else if(currentDigit == 8){

            }else if(currentDigit == 9){

            }
        }
    }

    return true;
}

private boolean isTopLeft(int item, int r, int c, String[][] map){
    //width and height measured in # of map "steps" (10px ea)
    int width = 1;
    int height = 1;

    if(item == 0){

    }else if(item == 1){
        width = platformWidth/10;
        height = platformHeight/10;
    }else if(item == 2){
        width = playerWidth/10;
        height = playerHeight/10;
    }else if(item == 3){

    }else if(item == 4){

    }else if(item == 5){

    }else if(item == 6){

    }else if(item == 7){

    }else if(item == 8){

    }else if(item == 9){

    }

    //counts backwards and upwards to see how many blocks of the same type preceed it.
    int curR = r - 1;
    int prevWidthCount = 0;
    int prevHeightCount = 0;
    while(curR >= 0 && map[curR][c].equals("" + item)){
        curR -= 1;
        prevWidthCount += 1;
    }
    int curC = c - 1;
    while(curC >= 0 && map[r][curC].equals("" + item)){
        curC -= 1;
        prevHeightCount += 1;
    }


    if(prevWidthCount % width == 0 && prevHeightCount % height == 0){
        return true; 
    }

    return false;

}

public int countObjectsLeft(String[][] map, int r, int c){
    int counter = 0;
    while(c >= 0){
        if(isTopLeft(Integer.parseInt(map[r][c]), r, c, map))
            counter++;
        c -= 1;
    }
    return counter;
}
public int countObjectsUp(String[][] map, int r, int c){
    int counter = 0;
    while(r >= 0){
        if(isTopLeft(Integer.parseInt(map[r][c]), r, c, map))
            counter++;
        r -= 1;
    }
    return counter;
}

}

结果如下(管道字符是每个块结束的位置),并且球是播放器(当前未从文件加载)。enter image description here

我坚持这一点,似乎块应该适合(0,0)到(19,19)和(20,20)到(39,39)。任何有关这方面的帮助将不胜感激:D:D

编辑1:

这是完整的平台类:

package org.awesome.Models;


public class Platform {
private int x, y;
private Bitmap texture;
public Platform(int x, int y, Bitmap texture){
    this.x = x;
    this.y = y;
    this.texture = texture;

}

public void draw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
      canvas.drawBitmap(texture, x, y, null);
      canvas.drawText("|", x + texture.getWidth(), y - 2, paint);
  }

public int getX(){
    return x;
}

public int getY(){
    return y;
}

public Bitmap getBitmap(){
    return texture;
}
}

1 个答案:

答案 0 :(得分:1)

在你的代码中,我看到你正在使用像素的dimmensions,而BitmapFactory.decodeResource执行你的位图的scalling以适应当前的设备屏幕密度,这使得绘制的图像应该更大。如果你想使用像素,那么你应该使用options(设置inScaled to false应该足够)版本来禁用decodeResource中的scalling:

decodeResource(Resources res, int id, BitmapFactory.Options opts)

或将您的drawables放入

drawable-nodpi

文件夹,什么会阻止加载期间的scalling