推箱子Unity
逻辑首先有控制台写了一下。
需要注意的地方不少:
- 进一步,需要考虑3个层面的问题。
- 前面的位置是空地,成功点,墙,还是箱子。
- 前面是箱子的时候,箱子的前面是空地,墙,成功点,还是箱子。
- 当移动的时候,原先人的地点是空地,成功点,需要恢复。
- 需要后退一步的时候,需要考虑两种情况。
- 恢复两个点的节点原先值。
- 由于上面2的情况,需要恢复3个点的原先值。
Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; struct State
{
public int posX, posY, mapx;
}; namespace PushBox
{ class Program
{
// 1 表示 强
// 2 表示 人
// 3 表示 箱子
// 4 表示 成功点
static int[,] map = new int[, ] {
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,}
};
static int posX, posY; static State[] list = new State[];
static int list_cnt = ;
//箱子个数
static int cube_cnt;
static int _cube_cnt;
//步数
static int cnt; static void pre_clear()
{
list_cnt = ;
}
static void pre_save(int POSX,int POSY)
{
list[list_cnt].posX = POSX;
list[list_cnt].posY = POSY;
list[list_cnt].mapx = map[POSX, POSY];
list_cnt++;
} static bool victory()
{
int flag_cube_cnt = ;
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
if (map[i, j] == )
flag_cube_cnt++;
}
}
return flag_cube_cnt == cube_cnt;
} static void init()
{
cube_cnt = ;
cnt = ;
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
if (map[i, j] == )
{
cube_cnt++;
}
if (map[i, j] == )
{
posX = i;
posY = j;
pre_save(i, j);
}
}
}
} static void draw()
{
Console.Clear();
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
if (map[i, j] == ) //#
{
System.Console.Write("■");
}
else if(map[i,j]==)
System.Console.Write(" ");
else if (map[i, j] == ) //pepole
{
System.Console.Write("♀");
}
else if(map[i,j] == )
{
System.Console.Write("△");
}
else if (map[i, j] == )
{
System.Console.Write("☆");
}
else if (map[i,j] == ) // 人 + 成功点
{
System.Console.Write("◎");
}
else if (map[i, j] == )
{
_cube_cnt++;
System.Console.Write("◆"); } }
Console.WriteLine(""); }
/*
System.Console.Write("步数:{0}\n保存的链表",cnt); for (int i = 0; i < list_cnt; i++)
{
Console.WriteLine("{0} {1} {2}", list[i].posX, list[i].posY, list[i].mapx); }*/
System.Console.WriteLine("按z键退回一步哦!");
} //向上移动
static void moveUp()
{
if (posX - >= )
{
if (map[posX - , posY] == ) // 空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY); map[posX, posY] = ;
map[posX-,posY] = ;
posX--;
//now_save();
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX-, posY);
map[posX, posY] = ;
map[posX-, posY] = ;
posX--;
}
}
else if (map[posX - , posY] == ) //成功点
{ if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
}
else if (map[posX - , posY] == ) // 箱子
{
if (posX - >= )
{
if (map[posX - , posY] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{ // pre_save();
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
}
else if (map[posX - , posY] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
} }
}
}
else if (map[posX - , posY] == ) // 箱子+成功点
{
if (posX - >= )
{
if (map[posX - , posY] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
}
else if (map[posX - , posY] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
} }
}
}
}
} //向下移动
static void moveDown()
{
if (posX + < )
{
if (map[posX + , posY] == ) // 空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY); map[posX, posY] = ;
map[posX + , posY] = ;
posX++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY); map[posX, posY] = ;
map[posX + , posY] = ;
posX++;
}
}
else if (map[posX + , posY] == ) //成功点
{ if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
}
else if (map[posX + , posY] == ) // 箱子
{
if (posX + < )
{
if (map[posX + , posY] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
}
else if (map[posX + , posY] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
} }
}
}
else if (map[posX + , posY] == ) // 箱子+成功点
{
if (posX + < )
{
if (map[posX + , posY] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
}
else if (map[posX + , posY] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
} }
}
}
}
} static void moveLeft()
{
if (posY - >= )
{
if (map[posX , posY-] == ) // 空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY-); map[posX, posY] = ;
map[posX, posY-] = ;
posY--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - ); map[posX, posY] = ;
map[posX, posY-] = ;
posY--;
}
}
else if (map[posX, posY-] == ) //成功点
{ if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - ); map[posX, posY - ] = ;
map[posX, posY] = ;
posY--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - ); map[posX, posY - ] = ;
map[posX, posY] = ;
posY--;
}
}
else if (map[posX, posY-] == ) // 箱子
{
if (posY - >= )
{
if (map[posX, posY-] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX , posY-] = ;
map[posX, posY-] = ;
map[posX, posY] = ;
posY--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX , posY-] = ;
map[posX , posY-] = ;
map[posX, posY] = ;
posY--;
}
}
else if (map[posX , posY-] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX, posY-] = ;
map[posX, posY-] = ;
map[posX, posY] = ;
posY--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX , posY-] = ;
map[posX , posY-] = ;
map[posX, posY] = ;
posY--;
} }
}
}
else if (map[posX, posY-] == ) // 箱子+成功点
{
if (posY - >= )
{
if (map[posX, posY-] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX, posY-] = ;
map[posX, posY-] = ;
map[posX, posY] = ;
posY--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX, posY-] = ;
map[posX, posY-] = ;
map[posX, posY] = ;
posY--;
}
}
else if (map[posX, posY-] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX, posY-] = ;
map[posX, posY-] = ;
map[posX, posY] = ;
posY--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX, posY-] = ;
map[posX, posY-] = ;
map[posX, posY] = ;
posY--;
} }
}
}
}
} static void moveRight()
{
if (posY + < )
{
if (map[posX, posY + ] == ) // 空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + ); map[posX, posY] = ;
map[posX, posY + ] = ;
posY++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + ); map[posX, posY] = ;
map[posX, posY + ] = ;
posY++;
}
}
else if (map[posX, posY + ] == ) //成功点
{ if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
}
else if (map[posX, posY + ] == ) // 箱子
{
if (posY + < )
{
if (map[posX, posY + ] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
}
else if (map[posX, posY + ] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
} }
}
}
else if (map[posX, posY + ] == ) // 箱子+成功点
{
if (posY + < )
{
if (map[posX, posY + ] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
}
else if (map[posX, posY + ] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
} }
}
}
}
} static bool move()
{
var info = Console.ReadKey();
if (info.Key == ConsoleKey.UpArrow||info.Key == ConsoleKey.W)
{
cnt++;
moveUp(); } else if (info.Key == ConsoleKey.DownArrow||info.Key == ConsoleKey.S)
{
cnt++;
moveDown();
} else if (info.Key == ConsoleKey.LeftArrow || info.Key == ConsoleKey.A)
{
cnt++;
moveLeft();
} else if (info.Key == ConsoleKey.RightArrow || info.Key == ConsoleKey.D)
{
cnt++;
moveRight();
}
else if (info.Key == ConsoleKey.Z)
{
if (list_cnt > )
{
cnt--;
for (int i = ; i < list_cnt; i++)
{
int tx = list[i].posX;
int ty = list[i].posY;
int tmax = list[i].mapx; map[tx, ty] = tmax; }
posX = list[].posX;
posY = list[].posY;
pre_clear();
} }
return victory();
} static void Main(string[] args)
{
// System.Console.WriteLine("Hello");
for (int i = ; i < ; i++)
{
init();
while (true)
{ draw();
if (move()==true)
{
break;
}
}
Console.Clear(); Console.WriteLine("恭喜!");
Console.ReadKey();
} }
}
}
PushBox
基本上整个推箱子的逻辑部分就已经完成了,现在只要在Unity下进行实例化就可以了。
Source Code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI; struct State
{
public int posX, posY, mapx;
}
public class Map : MonoBehaviour
{ public GameObject wallPre;
public GameObject Box;
public GameObject Point;
public GameObject EndPoint;
public GameObject man; int posX, posY; State[] list = new State[];
int list_cnt = ;
//箱子个数
int cube_cnt;
int _cube_cnt;
//步数
int cnt; void pre_clear()
{
list_cnt = ;
}
void pre_save(int POSX, int POSY)
{
list[list_cnt].posX = POSX;
list[list_cnt].posY = POSY;
list[list_cnt].mapx = map[POSX, POSY];
list_cnt++;
}
bool victory()
{
int flag_cube_cnt = ;
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
if (map[i, j] == )
flag_cube_cnt++;
}
}
return flag_cube_cnt == cube_cnt;
} static int[,] map = new int[, ]
{
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
};
static int[,] map_1 = new int[, ] {
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
}; static int[,] map_2 = new int[, ] {
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
}; GameObject x;
// Use this for initialization
void Start()
{
x = GameObject.Find("victory_UI");
x.SetActive(false);
init();
draw();
}
// Update is called once per frame
void Update()
{
draw();
if (move())
{
print("xxxxxxxxxxxxxxxxxxxxx\n"); x.SetActive(true); //init();
// draw();
}
} Vector3 getPoint(int x, float y, int z)
{
return new Vector3(x - 5.5f, y, z - 5.5f);
} //关卡
static int level;
void init()
{
cube_cnt = ;
cnt = ; GameObject x = GameObject.Find("level");
string tmp;
tmp = "第" + (level+).ToString() + "关";
x.GetComponent<Text>().text = tmp; if (level == )
{
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
if (map[i, j] == )
{
cube_cnt++;
}
if (map[i, j] == )
{
posX = i;
posY = j;
pre_save(i, j);
}
}
}
}
else if (level >= )
{
map = new int[, ] {
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
{,,,,,,,,,},
};
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
if (map[i, j] == )
{
cube_cnt++;
}
if (map[i, j] == )
{
posX = i;
posY = j;
pre_save(i, j);
}
}
}
}
level++;
Game_UI_Manage.Level++;
} void draw()
{
destroyMap();
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
if (map[i, j] == ) //#
{
Instantiate(wallPre, getPoint(i, 0.5f, j), Quaternion.identity);
//System.Console.Write("■");
}
else if (map[i, j] == )
{ } //System.Console.Write(" ");
else if (map[i, j] == ) //pepole
{
Instantiate(man, getPoint(i, 0.5f, j), Quaternion.identity);
//System.Console.Write("♀");
}
else if (map[i, j] == )
{
Instantiate(Box, getPoint(i, 0.5f, j), Quaternion.identity);
//System.Console.Write("△");
}
else if (map[i, j] == )
{
Instantiate(Point, getPoint(i, 0.5f, j), Quaternion.identity);
//System.Console.Write("☆");f
}
else if (map[i, j] == ) // 人 + 成功点
{
Instantiate(man, getPoint(i, 0.5f, j), Quaternion.identity);
}
else if (map[i, j] == )
{
Instantiate(EndPoint, getPoint(i, 0.5f, j), Quaternion.identity); } } } } void destroyMap()
{
GameObject[] gos = GameObject.FindGameObjectsWithTag("brick");
foreach (var go in gos)
{
Destroy(go);
}
} bool move()
{
//上
if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
{
cnt++;
moveUp();
}
//下
else if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
{
cnt++;
moveDown(); }
//左
else if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S))
{
cnt++;
moveLeft();
}
//右
else if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W))
{
cnt++;
moveRight();
}
else if (Input.GetKeyDown(KeyCode.Z))
{
if (list_cnt > )
{
cnt--;
for (int i = ; i < list_cnt; i++)
{
int tx = list[i].posX;
int ty = list[i].posY;
int tmax = list[i].mapx; map[tx, ty] = tmax; }
posX = list[].posX;
posY = list[].posY;
pre_clear();
}
}
return victory();
} void moveUp()
{
if (posX - >= )
{
if (map[posX - , posY] == ) // 空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY); map[posX, posY] = ;
map[posX - , posY] = ;
posX--;
//now_save();
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
map[posX, posY] = ;
map[posX - , posY] = ;
posX--;
}
}
else if (map[posX - , posY] == ) //成功点
{ if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
}
else if (map[posX - , posY] == ) // 箱子
{
if (posX - >= )
{
if (map[posX - , posY] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{ // pre_save();
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
}
else if (map[posX - , posY] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
} }
}
}
else if (map[posX - , posY] == ) // 箱子+成功点
{
if (posX - >= )
{
if (map[posX - , posY] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
}
else if (map[posX - , posY] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX - , posY);
pre_save(posX - , posY); map[posX - , posY] = ;
map[posX - , posY] = ;
map[posX, posY] = ;
posX--;
} }
}
}
}
} //向下移动
void moveDown()
{
if (posX + < )
{
if (map[posX + , posY] == ) // 空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY); map[posX, posY] = ;
map[posX + , posY] = ;
posX++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY); map[posX, posY] = ;
map[posX + , posY] = ;
posX++;
}
}
else if (map[posX + , posY] == ) //成功点
{ if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
}
else if (map[posX + , posY] == ) // 箱子
{
if (posX + < )
{
if (map[posX + , posY] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
}
else if (map[posX + , posY] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
} }
}
}
else if (map[posX + , posY] == ) // 箱子+成功点
{
if (posX + < )
{
if (map[posX + , posY] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
}
else if (map[posX + , posY] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX + , posY);
pre_save(posX + , posY); map[posX + , posY] = ;
map[posX + , posY] = ;
map[posX, posY] = ;
posX++;
} }
}
}
}
} void moveLeft()
{
if (posY - >= )
{
if (map[posX, posY - ] == ) // 空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - ); map[posX, posY] = ;
map[posX, posY - ] = ;
posY--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - ); map[posX, posY] = ;
map[posX, posY - ] = ;
posY--;
}
}
else if (map[posX, posY - ] == ) //成功点
{ if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - ); map[posX, posY - ] = ;
map[posX, posY] = ;
posY--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - ); map[posX, posY - ] = ;
map[posX, posY] = ;
posY--;
}
}
else if (map[posX, posY - ] == ) // 箱子
{
if (posY - >= )
{
if (map[posX, posY - ] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX, posY - ] = ;
map[posX, posY - ] = ;
map[posX, posY] = ;
posY--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX, posY - ] = ;
map[posX, posY - ] = ;
map[posX, posY] = ;
posY--;
}
}
else if (map[posX, posY - ] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX, posY - ] = ;
map[posX, posY - ] = ;
map[posX, posY] = ;
posY--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX, posY - ] = ;
map[posX, posY - ] = ;
map[posX, posY] = ;
posY--;
} }
}
}
else if (map[posX, posY - ] == ) // 箱子+成功点
{
if (posY - >= )
{
if (map[posX, posY - ] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX, posY - ] = ;
map[posX, posY - ] = ;
map[posX, posY] = ;
posY--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX, posY - ] = ;
map[posX, posY - ] = ;
map[posX, posY] = ;
posY--;
}
}
else if (map[posX, posY - ] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX, posY - ] = ;
map[posX, posY - ] = ;
map[posX, posY] = ;
posY--;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY - );
pre_save(posX, posY - ); map[posX, posY - ] = ;
map[posX, posY - ] = ;
map[posX, posY] = ;
posY--;
} }
}
}
}
} void moveRight()
{
if (posY + < )
{
if (map[posX, posY + ] == ) // 空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + ); map[posX, posY] = ;
map[posX, posY + ] = ;
posY++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + ); map[posX, posY] = ;
map[posX, posY + ] = ;
posY++;
}
}
else if (map[posX, posY + ] == ) //成功点
{ if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
}
else if (map[posX, posY + ] == ) // 箱子
{
if (posY + < )
{
if (map[posX, posY + ] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
}
else if (map[posX, posY + ] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
} }
}
}
else if (map[posX, posY + ] == ) // 箱子+成功点
{
if (posY + < )
{
if (map[posX, posY + ] == ) // 箱子上面是空地
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
}
else if (map[posX, posY + ] == ) //箱子上面是成功点
{
if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
}
else if (map[posX, posY] == )
{
pre_clear();
pre_save(posX, posY);
pre_save(posX, posY + );
pre_save(posX, posY + ); map[posX, posY + ] = ;
map[posX, posY + ] = ;
map[posX, posY] = ;
posY++;
} }
}
}
}
} static public void Reset(int Level)
{
level = Level;
if (Level == )
{
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
map[i,j] = map_1[i,j]; }
else if (Level >= )
{
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
map[i, j] = map_2[i, j]; } } public static void set_level(int Level)
{
level = Level;
if (Level == )
{
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
map[i, j] = map_1[i, j]; }
else if (Level >= )
{
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
map[i, j] = map_2[i, j]; }
} }
Map.cs
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement; public class UI_Manage : MonoBehaviour { // Use this for initialization
void Start () { } // Update is called once per frame
void Update () { } public void onStartBtnClick()
{
//跳转场景 SceneManager.LoadScene("Game");
//退出游戏
//Application.Quit();
//显示与隐藏
//游戏物体.setActive(true / false); } public void onEndBtnClick()
{
Application.Quit();
System.Console.WriteLine("Exit!");
} }
UI_Manage.cs
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement; public class Game_UI_Manage : MonoBehaviour { // Use this for initialization
void Start () { } // Update is called once per frame
void Update () { } public void onHomeBtnClick()
{
Level = ;
print(Level);
Map.set_level(Level);
SceneManager.LoadScene("Start"); } public static int Level;
public void onResetClick()
{
print(Level);
Map.set_level(--Level);
SceneManager.LoadScene("Game");
} public void victory_Home()
{
Level = ;
Map.set_level(Level);
SceneManager.LoadScene("Start");
} public void victory_next()
{
SceneManager.LoadScene("Game");
} }
结构:
UI_Manage管理开始界面的所有UI
Map:控制绘图,和行走
Game_UI_Manage:控制游戏界面的所有UI
推箱子Unity的更多相关文章
- OC推箱子
#include<stdio.h> #include<stdlib.h> int main(void) { char sr;//存储用户输入的指令 //绘制地图 char a[ ...
- c语言游戏推箱子
前两天做了推箱子小游戏,看似简单的一个小游戏背后却 有巨大的秘密,这秘密就是一大堆逻辑. 自从学习了函数过后,的确是解决了很多问题,而且调用很方便,尽管我现在都不是很会调用. 写完一个函数,准备测试一 ...
- JavaScript写一个小乌龟推箱子游戏
推箱子游戏是老游戏了, 网上有各种各样的版本, 说下推箱子游戏的简单实现,以及我找到的一些参考视频和实例: 推箱子游戏的在线DEMO : 打开 如下是效果图: 这个拖箱子游戏做了移动端的适配, 我使用 ...
- 用C#制作推箱子小游戏
思路分析: 一.制作一个地图 二.地图中放置墙.箱子.人.目标等 三.让小人动起来完成推箱子动作 游戏制作: 1.按照上述地图制作一个地图 (12行×13列) 地图可以看做是行和列组成的,即可以看做 ...
- hdu.1254.推箱子(bfs + 优先队列)
推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- [游戏模版17] Win32 推箱子 迷宫
>_<:Here introduce a simple game: >_<:resource >_<:only can push a box and finally ...
- [转]Flash ActionScript2.0面向对象游戏开发-推箱子
本文转自:http://www.alixixi.com/Dev/W3C/Flash/2007/2007070868666.html 概述: Flash ActionScript2.0是一种面向对向的编 ...
- c#部分---网吧充值系统;简易的闹钟;出租车计费;简单计算器;对战游戏;等额本金法计算贷款还款利息等;随机生成10个不重复的50以内的整数;推箱子;
网吧充值系统namespace ConsoleApplication1 { class Program { struct huiyuan { public string name; public st ...
- c++、c实现推箱子小游戏
经过四次的修改和优化,终于将推箱子这个游戏完整的写出来了,今天就像大家分享一下这个游戏的编写. 这个游戏界面的编写总的来说不困难,主要是推动箱子的算法. (1)利用数组和windows api 即可写 ...
随机推荐
- 盒子绝对定位 position:foxed ; 居中
方法1 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- Yii2 场景scenario的应用
首先,我们在使用模型类中的验证,rules的时候,会出现以下情况: 假设有一个字段type, 当type
- Python获取当前时间及格式化
1.导入time模块 # 导入time模块 import time 2.打印时间戳-time.time() # 导入time模块 import time # 打印时间戳 print(time.time ...
- oracle 基础知识(七)----动态性能视图
一,动态性能视图介绍 动态性能视图属于数据字典,系统管理员用户 SYS 可以访问它们.在数据库打开和使用时不断进行更新,而且它们的内容主要与性能有关.虽然这些视图很像普通的数据库表,但它们不允许用户直 ...
- URL篇之不安全字符处理
URL是可移植的.它要统一命名因特网上的所有资源,就需要通过各种不同的协议来传送这些资源.这些协议在传输数据时会使用不同的机制,所以,设计URL,使其可以通过任意因特网协议安全地传输是很重要的. UR ...
- Java基础25-静态代码块
/* 静态代码块 格式: static{ 静态代码块中执行语句 } 特点:随着类的加载而执行,并且只会执行一次,并且还优先于主函数. 作用:用于给类进行初始化 */ public class Test ...
- Thinkphp基于规则的Auth权限认证类
PS:onethink是基于该权限认证类实现,Auth类作为官方类库,在Library\Think里面. 其实Auth类也是基于角色访问控制RBAC扩展的,具体到节点的权限校验方式还是需要根据业务 ...
- Ubuntu 12.04 搭建 SAMBA-SWAT(Samba Web 管理工具)
参考了:http://linux.chinaunix.net/techdoc/net/2007/03/14/952274.shtml,对其进行了部分修改完善. 依次执行 1.sudo apt-get ...
- (转)shell命令:echo命令详解
shell命令:echo命令详解 原文:https://www.cnblogs.com/xyz0601/archive/2015/04/23/4450736.html 功能说明:显示文字. 语 法:e ...
- TOJ 1883 Domino Effect
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...