首页 > 代码库 > 12月28日 二维数组的应用:第一个小游戏(推箱子)
12月28日 二维数组的应用:第一个小游戏(推箱子)
小游戏:******推箱子********
static void Main(string[] args)
{
int i, j;
int[,] a = new int[10, 10] //二维数组的定义 类型[,] 数组名 = new 类型 [行数, 列数] {赋值}; 或单个赋值 a[i,j]=1;
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,2,0,0,8,0,0,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,0,0,0,1,0,0,0,1}, // 二维数组的格式为表格形式
{1,0,0,1,1,1,0,0,0,1},
{1,0,1,1,0,0,0,0,0,1},
{1,0,0,0,0,0,9,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
};
int hang = 2, lie = 2, temp = 0;
while (true)
{
//做地图
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (a[i, j] == 1)
{
Console.Write("■"); //二维数组的取值Console.Write(a[i,j]);
}
if (a[i, j] == 0)
{
Console.Write(" ");
}
if (a[i, j] == 2)
{
Console.Write("♀");
} if (a[i, j] == 9)
{
Console.Write("★");
} if (a[i, j] == 8)
{
Console.Write("□");
}
}
Console.WriteLine();
}
Console.Write("请按键盘上的前后左右键行走:");
Console.WriteLine();
ConsoleKey s = Console.ReadKey().Key;
Console.Clear();
// 向右
if (s.ToString() == "RightArrow")
{
if (a[hang, lie+1] == 0)
{
temp = a[hang, lie];
a[hang, lie] = a[hang, (lie + 1)];
a[hang, (lie + 1)] = temp;
lie++;
}
else if (a[hang, lie + 1] == 1)
{
Console.Write("撞墙了");
}
else if (a[hang, lie + 1] == 8)
{
if (a[hang, lie + 2] != 1 && a[hang, lie - 2] != 9)
{
temp = a[hang, (lie + 2)];
a[hang, lie + 2] = a[hang, lie + 1];
a[hang, lie + 1] = a[hang, lie];
a[hang, lie] = temp;
lie++;
}
else if (a[hang, lie + 2] == 9)
{
a[hang, lie + 2] = a[hang, lie + 1];
a[hang, lie + 1] = a[hang, lie];
a[hang, lie] = 0;
Console.Write("你赢了,请进入下一关");
break;
}
else { Console.Write("撞墙了");}
}
}
//向左
else if (s.ToString() == "LeftArrow")
{
if (a[hang, lie-1] == 0)
{
temp = a[hang, lie];
a[hang, lie] = a[hang, (lie - 1)];
a[hang, (lie - 1)] = temp;
lie--;
}
else if (a[hang, lie - 1] == 1)
{
Console.Write("撞墙了");
}
else if (a[hang, lie - 1] == 8 )
{
if (a[hang, lie - 2] != 1 && a[hang, lie - 2] != 9)
{
temp = a[hang, (lie - 2)];
a[hang, lie - 2] = a[hang, lie - 1];
a[hang, lie - 1] = a[hang, lie];
a[hang, lie] = temp;
lie--;
}
else if (a[hang, lie - 2] == 9)
{
a[hang, lie - 2] = a[hang, lie - 1];
a[hang, lie - 1] = a[hang, lie];
a[hang, lie] = 0;
Console.Write("你赢了,请进入下一关");
break;
}
else { Console.Write("撞墙了"); }
}
}
//向上
else if (s.ToString() == "UpArrow")
{
if (a[hang-1, lie] == 0)
{
temp = a[hang, lie];
a[hang, lie] = a[(hang - 1), lie];
a[(hang - 1), lie] = temp;
hang--;
}
else if (a[hang-1, lie] == 1)
{
Console.Write("撞墙了");
}
else if (a[hang-1, lie] == 8)
{
if (a[hang - 2, lie] != 1 && a[hang - 2, lie] != 9)
{
temp = a[hang - 2, lie];
a[hang - 2, lie] = a[hang - 1, lie];
a[hang - 1, lie] = a[hang , lie];
a[hang, lie] = temp;
hang--;
}
else if (a[hang - 2, lie] == 9)
{
a[hang - 2, lie] = a[hang - 1, lie];
a[hang - 1, lie] = a[hang, lie];
a[hang, lie] = 0;
Console.Write("你赢了,请进入下一关");
break;
}
else { Console.Write("撞墙了"); }
}
}
//向下
else if (s.ToString() == "DownArrow")
{
if (a[hang+1, lie] == 0)
{
temp = a[hang, lie];
a[hang, lie] = a[(hang + 1), lie];
a[(hang + 1), lie] = temp;
hang++;
}
else if (a[hang + 1, lie] == 1)
{
Console.Write("撞墙了");
}
else if (a[hang + 1, lie] == 8)
{
if (a[hang + 2, lie] != 1 && a[hang + 2, lie] != 9)
{
temp = a[hang + 2, lie];
a[hang + 2, lie] = a[hang + 1, lie];
a[hang + 1, lie] = a[hang , lie];
a[hang, lie] = temp;
hang++;
}
else if (a[hang + 2, lie] == 9)
{
a[hang + 2, lie] = a[hang + 1, lie];
a[hang + 1, lie] = a[hang, lie];
a[hang, lie] = 0;
Console.Write("你赢了,请进入下一关");
break;
}
else { Console.Write("撞墙了"); }
}
}
Console.WriteLine();
}
12月28日 二维数组的应用:第一个小游戏(推箱子)