您的位置:首页 > 移动开发 > Unity3D

关于游戏《挑战巅峰》中铺砖算法的猜测 -Unity

2015-11-07 22:34 441 查看

关于游戏《挑战巅峰》中铺砖算法的猜测 -Unity

首先,附上一张游戏图



可以看到图中的砖块从左到右,然后从右到左依次上升呈现阶梯排列(可以无视金币数),那么这个地图有点类似于棋盘格子的铺设算法,但是又有点不太一样。我们来分析一下这个游戏中砖块所遵从的规则。

假设砖的高和宽都是1,那么相临两块砖在横向和纵向上相差都是1。如果我们在横向上成功铺出来,只要在每一个砖块生成的时候纵向加1即可。下面来讨论一下横向上的铺砖算法。

来简化一下问题,假设最左边的砖和最右边的砖的横向位置(砖块的X坐标)是固定的,我们用整型数a,b来代替,此问题可以看成是一个数X从a开始++, 加到b时开始–,减到a时再++,如此循环。因而转换成一个数在a,b之间振荡并打印输出的问题。

对流程进行分析,如图:



代码:

int min = 0;
int max = 10;
int current = 0;
bool isAdd = true;

for (int index = 0; index < 30; index++)
{
print(current);
if (isAdd)
{
current++;
if (current > 10)
{
current = max;
isAdd = false;
}

}
else
{
current--;
if (current < 0)
{
current = min;
isAdd = true;
}
}

}


结果会打印两个10,两个0,我们对代码进行修改

for (int index = 0; index < 30; index++)
{
print(current);
if (isAdd)
{
current++;
if (current > 10)
{
current = max-1; // 此处修改
isAdd = false;
}

}
else
{
current--;
if (current < 0)
{
current = min+1; // 此处修改
isAdd = true;
}
}
}


这样,打印输出正常了。

接下来我们让两边的数字a,b都随机产生,再次修改代码:

int min = Random.Range(0,5);//修改
int max = Random.Range(5,10);//修改
int current = min;//修改
bool isAdd = true;

for (int index = 0; index < 30; index++)
{
print(current);
if (isAdd)
{
current++;
if (current > max)//修改
{
current = max-1;
isAdd = false;
min = Random.Range(0, 11);
while (min >= current)
min = Random.Range(0, 11);
}

}
else
{
current--;
if (current < min)//修改
{
current = min+1;
isAdd = true;
max = Random.Range(0, 11);
while (max <= current)
max = Random.Range(0, 11);
}
}
}


请自行测试打印

到此,a,b振荡的命题就解决了。。。以此为砖块横向坐标改变,再添加 纵向坐标改变,即可完成游戏砖块的铺设

for (int currentRow = 0; currentRow < maxRow; currentRow++)
{
if (!isAdd)   //   砖从右往左排时
{
currentColumn--;
if (currentColumn < min)
{
currentColumn = min + 1;
isAdd = true;
max = Random.Range(0, 6);
while (max <= currentColumn)
max = Random.Range(0, 6);
}
}
else  //   砖从左往右排时
{
currentColumn++;
if (currentColumn > max)
{
currentColumn = max - 1;
isAdd = false;
min = Random.Range(0, 6);
while (min >= currentColumn)
{
min = Random.Range(0, 6);
}
}
}
GameObject go = (GameObject)Instantiate(brick);
go.transform.parent = this.transform;

//The "current" is the X position,and the "index" is the Y position
go.GetComponent<Brick>().fixedPosition(currentColumn, currentRow);
}


Brick类中的fixedPosition方法

public void fixedPosition(int rowIndex, int columnIndex)
{
this.rowIndex = rowIndex;
this.columnIndex = columnIndex;
transform.localPosition = new Vector3(rowIndex * 40, columnIndex * 20, 0);
transform.localScale = Vector3.one;
}


效果截图:



附unity包

http://download.csdn.net/detail/liushida00/9251191
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: