这段时间在家没事干做了一个2048的小游戏,主要还是看大佬的博客做的,那为什么我还要再写一个呢,因为大佬博客有些代码没有放出来QAQ,自己捣鼓了半天,先放大佬博客地址
https://blog.csdn.net/stevenkylelee/article/details/51930080
跟评论一样,这是我找到的结构最好的一个2048游戏,很有学习价值!!(确定方向那个地方大佬貌似写错了正负,大家自己实践下哈)
这里放下大佬省略的拖动方块移动的代码
(这里只放两方向的,剩余两个方向建议大家自己写着试下,不会的话最后我会把源代码放出来)
//逻辑
以向上滑动举例,大体逻辑就是循环除了最上面一行所有的背景方块,如果背景方块里面有实体,那么在那个方块上发射一条射线检测,是BG(边界)的话移动到最上面的位置,是另一个实体的话在检测自己跟射线检测到的方块是不是同一个数值,不是的话移动到检测到方块的下一个位置,是的话消除两个方块,在检测到方块的位置在生成一个新的方块,方块数值*2.
自己写完觉得有点乱QAQ,大佬们多担待,也希望可以多提下意见。
private void MoveUp()
{
for (int i = 1; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (Map.Inatance.Cells[i][j].Entity != null)
{
GameObject theObj = Map.Inatance.Cells[i][j].Entity;
EntityProperty theEntity = theObj.GetComponent<EntityProperty>();
RaycastHit2D rayhit = Physics2D.Raycast(theObj.transform.position+Vector3.up, Vector2.up, 10);
if (rayhit.collider == null)
continue;
if(rayhit.collider.tag == "BG")
{
Map.Inatance.Cells[i][j].Entity = null;
theObj.transform.position = Map.Inatance.Cells[0][j].Position;
theEntity.AssociatedCell = Map.Inatance.Cells[0][j];
Map.Inatance.Cells[0][j].Entity = theObj;
}
if(rayhit.collider.tag == "Entity")
{
if(theEntity.Score == rayhit.collider.GetComponent<EntityProperty>().Score)
{
//生成新的实体
GameObject newEntity = FactoryEntity.Create(theEntity.Score * 2);
newEntity.transform.position = rayhit.collider.gameObject.transform.position;
newEntity.GetComponent<EntityProperty>().AssociatedCell = rayhit.collider.gameObject.GetComponent<EntityProperty>().AssociatedCell;
//销毁合成的两个
rayhit.collider.gameObject.GetComponent<EntityProperty>().AssociatedCell.Entity = newEntity;
Destroy(Map.Inatance.Cells[i][j].Entity);
Map.Inatance.Cells[i][j].Entity = null;
Destroy(rayhit.collider.gameObject);
}
else
{
//移动到射线检测物体下方
for (int k = 1; k <i; k++)
{
if(Map.Inatance.Cells[i-k][j].Entity != null)
{
Map.Inatance.Cells[i][j].Entity = null;
theObj.transform.position = Map.Inatance.Cells[i-k+1][j].Position;
theEntity.AssociatedCell = Map.Inatance.Cells[i-k+1][j];
Map.Inatance.Cells[i-k+1][j].Entity = theObj;
break;
}
}
}
}
}
}
}
}
private void MoveLeft()
{
for (int i = 0; i < 4; i++)
{
for (int j = 1; j < 4; j++)
{
if (Map.Inatance.Cells[i][j].Entity != null)
{
GameObject theObj = Map.Inatance.Cells[i][j].Entity;
EntityProperty theEntity = theObj.GetComponent<EntityProperty>();
RaycastHit2D rayhit = Physics2D.Raycast(theObj.transform.position + Vector3.left, Vector2.left, 10);
if (rayhit.collider == null)
continue;
if (rayhit.collider.tag == "BG")
{
Map.Inatance.Cells[i][j].Entity = null;
theObj.transform.position = Map.Inatance.Cells[i][0].Position;
theEntity.AssociatedCell = Map.Inatance.Cells[i][0];
Map.Inatance.Cells[i][0].Entity = theObj;
}
if (rayhit.collider.tag == "Entity")
{
if (theEntity.Score == rayhit.collider.GetComponent<EntityProperty>().Score)
{
//生成新的实体
GameObject newEntity = FactoryEntity.Create(theEntity.Score * 2);
newEntity.transform.position = rayhit.collider.gameObject.transform.position;
newEntity.GetComponent<EntityProperty>().AssociatedCell = rayhit.collider.gameObject.GetComponent<EntityProperty>().AssociatedCell;
//销毁合成的两个
rayhit.collider.gameObject.GetComponent<EntityProperty>().AssociatedCell.Entity = newEntity;
Destroy(Map.Inatance.Cells[i][j].Entity);
Map.Inatance.Cells[i][j].Entity = null;
Destroy(rayhit.collider.gameObject);
}
else
{
//移动到射线检测物体下方
for (int k = 1; k <j; k++)
{
if (Map.Inatance.Cells[i][j-k].Entity != null)
{
Map.Inatance.Cells[i][j].Entity = null;
theObj.transform.position = Map.Inatance.Cells[i][j-k + 1].Position;
theEntity.AssociatedCell = Map.Inatance.Cells[i][j-k + 1];
Map.Inatance.Cells[i][j-k + 1].Entity = theObj;
break;
}
}
}
}
}
}
}
}
文件源文件地址:https://download.csdn.net/download/ztysmile/12334817