如何在Android中设计简单的游戏地图?

时间:2015-08-22 20:26:53

标签: java android xml

我在Android中制作了一个非常简单的游戏,我没有使用OpenGL或任何其他库,只是Java!

现在我一直在设计我的地图......

我想设计这样的东西:

Working with Paint is hard! :|

(那些线条应该被视为墙壁,玩家无法摆脱它们)

如何使用XML设计这样的东西?或者我应该开始使用Unity3D?

1 个答案:

答案 0 :(得分:1)

这不是用xml完成​​的,但你不需要任何库。

public class Background extends View {

    private static final int BORDER_WIDTH = 2; //in px

    private int[] points; //2n = x, 2n+1 = y

    public Background(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for(Drawable drawable : drawBackground()){
            drawable.draw(canvas);
        }
    }

    private Drawable[] drawBackground() {
        Drawable[] drawables = new Drawable[2];

        int width = getWidth();
        int height = getHeight();
        int corner = width / 4;

        //draw border
        Path path = new Path();
        path.moveTo(0, corner);
        path.lineTo(corner, 0);
        path.lineTo(width, 0);
        path.lineTo(width, height - corner);
        path.lineTo(width - corner, height);
        path.lineTo(0, height);
        path.close();

        drawables[0] = new ShapeDrawable(new PathShape(path, width, height));
        drawables[0].setBounds(0, 0, width, height);

        //draw inside the border
        points = {
            BORDER_WIDTH, corner,
            corner, BORDER_WIDTH,
            width - BORDER_WIDTH, BORDER_WIDTH,
            width - BORDER_WIDTH, height - corner,
            width - corner, height - BORDER_WIDTH,
            BORDER_WIDTH, height - BORDER_WIDTH
        }
        path = new Path();
        path.moveTo(points[0], points[1]);
        for (int i = 2, i < points.length, i++) {
            path.lineTo(points[i], points[++i]);
        }
        path.close();

        ShapeDrawable shapeDrawable = new ShapeDrawable(new PathShape(path, width, height));
        shapeDrawable.getPaint().setColor(Color.rgb(238, 238, 238));
        shapeDrawable.setBounds(0, 0, width, height);
        drawables[1] = shapeDrawable;

        return drawables;
    }

    public Point[] getPoints() {
        return points;
    }
}

结果:

Result of the class