我正在尝试制作一个简单的蛇梯游戏。我有一个像这样绘制的主矩形(所有代码都在以下onDraw
方法的内部View
:
//Main BOX rectangle
RectMainBox = new Rect();
RectMainBox.set(getLeft() + 25, getTop() + 25, getRight() - 25, getBottom() - 25);
canvas.drawRect(RectMainBox, PaintMainBox);
这样就可以从该视图的父对象的每个角绘制一个填充为25的黑盒。
现在,我要在主框内有一个正方形(游戏单元可以有蛇或梯子)。我正在寻找这样的东西(不要介意正方形的不平等,它的涂漆仅供参考):
所以我试图m by n
在主盒子里有一个相等的正方形:
//initiate Cell map
_mapCell = new HashMap<Integer, Cell>();
int cols = 6;
int rows = 8;
//Start and End points of main game box
float left = RectMainBox.left;
float top = RectMainBox.top;
float right = RectMainBox.right / 2f;
float bottom = RectMainBox.bottom / 2f;
float boxWidth = RectMainBox.width() / (float)cols;
float boxHeight = RectMainBox.height() / (float) rows;
PointF start = new PointF(left, top);
PointF end = new PointF(right, bottom);
int id = 0;
for(int r = 1; r <= rows; r++){
for(int c = 1; c <= cols; c++){
RectF rect = new RectF();
rect.set(start.x, start.y, boxWidth - end.x, boxHeight - end.y);
canvas.drawRect(rect, PaintCell);
if(id == 0){
Cell cell = new Cell(id, CellType.START);
cell.setRectangle(rect);
_mapCell.put(id, cell);
} else if(id == rows*cols){
Cell cell = new Cell(id, CellType.END);
cell.setRectangle(rect);
_mapCell.put(id, cell);
}else{
Cell cell = new Cell(id, CellType.NORMAL);
cell.setRectangle(rect);
_mapCell.put(id, cell);
}
id++;
}
start.y += boxHeight;
}
但是不幸的是,这些代码并没有带我到任何地方。请给我一些建议,我如何才能达到上述效果。
也许您可以将网格视图用作特殊布局。这也为您提供了向每个单元添加特殊侦听器的优势。另外,尺寸对齐由布局管理器进行,您不必自己进行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句