马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 china25qd 于 2017-6-9 17:35 编辑
python的函数竟然无需返回类型!!!
真是个绝佳的利好~
因为同样的功能用python写出来就更简单啦!
以下是一个寻路算法,从中心点开始寻找一定距离内可行走的所有路线,以前我是用c#写,现在听了小甲鱼的课,我又用python写了一遍//for c#:
private List<GameObject> cube_reachable (GameObject start, int movement) {
List<GameObject> visited = new List<GameObject> ();
visited .Add(start);
List<GameObject> fringes = new List<GameObject> ();
fringes.Add (start);
for (int k= 1; k<= movement; k++) {
GameObject[] fringe = fringes.ToArray();
fringes.Clear ();
foreach (GameObject cube in fringe) {
for (int dir = 0; dir< 6; dir++) {
Debug.Log ("k =" + k + ", dir= " + dir);
GameObject neighbor = cube_neighbor (dir, cube );
if (visited.Contains (neighbor) == false) {
if (neighbor.GetComponent<cd_cooride> ().cooride_type != 1) {
if (neighbor.GetComponent<cd_cooride> ().cooride_type != -1) {
visited.Add (neighbor);
fringes.Add (neighbor);
}
}
}
}
}
}
return visited;
}
#for python:
function cube_reachable(start, movement):
var visited = set()
add start to visited
var fringes = []
fringes.append([start])
for each 1 < k ≤ movement:
fringes.append([])
for each cube in fringes[k-1]:
for each 0 ≤ dir < 6:
var neighbor = cube_neighbor(cube, dir)
if neighbor not in visited, not blocked:
add neighbor to visited
fringes[k].append(neighbor)
return visited
对比下来是不是短了很多呀 |