由于本人学习时间不长,到现在为止只是学到了I/O操作方面,想编写一个控制台小游戏,类似简易版的魔塔。前期从网上搜了很多资料想搞清楚控制台游戏如何控制人物的移动,但是搜了很久,找到的都是一些超出自己目前学习进度的知识点,个人感觉对于新手来说有一定的难度,最后通过我的不断思索,想到了一种很简易的方法,特别适合目前自己的水平。
在C#中通过Console.ReadKey()是能读取用户输入的,所以现在问题就是在地图中人物的移动如何与用户的输入联动起来。下面的代码详细说明了自己的思路
首先创建一个二维数组,提前设计好规则,每个数字所对应的涵义。这里1代表墙,2代表英雄的初始位置,3代表怪物,4代表出口
//用数组绘制地图
int[,] map = new int[20, 40]
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
{1,0,0,0,0,0,3,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
{1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
{1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
{1,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
{1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
{1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
{1,0,0,0,0,0,0,0,0,1,0,0,0,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于存放人物移动后的情况
int[,] Newmap = new int[20, 40];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
Newmap[i, j] = map[i, j];
}
}
//绘制旧地图
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
if (map[i, j] == 1) Console.Write("■");
else if (map[i, j] == 2) Console.Write("〇");
else if (map[i, j] == 3) Console.Write("♥ ");
else Console.Write(" ");
}
Console.WriteLine();
}
//获取用户输入用以控制人物移动
var key = Console.ReadKey().Key;
switch (key)
{
case ConsoleKey.W:
case ConsoleKey.UpArrow:
//当用户按下前进键后清除旧地图内容,为绘制新地图做准备
Console.Clear();
//向上移动则行数减1
int newPosition = aa - 1;
//如果碰到墙则回到原先位置
if (Newmap[newPosition, bb] == 1)
{
newPosition = aa;
Newmap[newPosition, bb] = 2;
aa = newPosition;
}
//如果前方没有障碍刷新英雄的位置,将人物位置向上移动
else if (Newmap[newPosition, bb] == 0)
{
Newmap[newPosition, bb] = 2;
Newmap[aa, bb] = 0;
aa = newPosition;
}
//如果遭遇怪物则触发战斗系统
else if (Newmap[newPosition, bb] == 3)
{
//触发战斗系统
FightSystem(newPosition,bb,Newmap);
}
//如果到达出口则结束程序
else if (Newmap[newPosition, bb] == 4)
{
return;
}
break;
case ConsoleKey.S:
case ConsoleKey.DownArrow:
Console.Clear();
int newPosition1 = aa + 1;
if (Newmap[newPosition1, bb] == 1 || Newmap[newPosition1, bb] == 2)
{
newPosition1 = aa;
Newmap[newPosition1, bb] = 2;
aa = newPosition1;
}
else if (Newmap[newPosition1, bb] == 0)
{
Newmap[newPosition1, bb] = 2;
Newmap[aa, bb] = 0;
aa = newPosition1;
}
else if (Newmap[newPosition1, bb] == 3)
{
//触发战斗系统
FightSystem(newPosition1, bb, Newmap);
}
else if (Newmap[newPosition1, bb] == 4)
{
return;
}
break;
case ConsoleKey.A:
case ConsoleKey.LeftArrow:
Console.Clear();
int newPosition2 = bb - 1;
if (Newmap[aa, newPosition2] == 1 || Newmap[aa, newPosition2] == 2)
{
newPosition2 = bb;
Newmap[aa, newPosition2] = 2;
bb = newPosition2;
}
else if (Newmap[aa, newPosition2] == 0)
{
Newmap[aa, newPosition2] = 2;
Newmap[aa, bb] = 0;
bb = newPosition2;
}
else if (Newmap[aa, newPosition2] == 3)
{
//触发战斗系统
FightSystem(aa, newPosition2, Newmap);
}
else if (Newmap[aa, newPosition2] == 4)
{
return;
}
break;
case ConsoleKey.D:
case ConsoleKey.RightArrow:
Console.Clear();
int newPosition3 = bb + 1;
if (Newmap[aa, newPosition3] == 1 || Newmap[aa, newPosition3] == 2)
{
newPosition3 = bb;
Newmap[aa, newPosition3] = 2;
bb = newPosition3;
}
else if (Newmap[aa, newPosition3] == 0)
{
Newmap[aa, newPosition3] = 2;
Newmap[aa, bb] = 0;
bb = newPosition3;
}
else if (Newmap[aa, newPosition3] == 3)
{
//触发战斗系统
FightSystem(aa, newPosition3, Newmap);
}
else if (Newmap[aa, newPosition3] == 4)
{
return;
}
break;
}
//绘制新地图的内容
for (int i = 0; i < Newmap.GetLength(0); i++)
{
for (int j = 0; j < Newmap.GetLength(1); j++)
{
if (map[i, j] == 1) Console.Write("■");
else if (map[i, j] == 2) Console.Write("〇");
else if (map[i, j] == 3) Console.Write("♥ ");
else Console.Write(" ");
}
Console.WriteLine();
}
//清空控制台以便下次循环显示地图
System.Threading.Thread.Sleep(100); Console.Clear();
//把新地图的内容给到旧地图以便下次循环时人物位置不变
for (int i = 0; i < Newmap.GetLength(0); i++)
{
for (int j = 0; j < Newmap.GetLength(1); j++)
{
map[i, j] = Newmap[i, j];
}
}
}
这样的话通过不停擦除二维数组再重新绘制二维数组,人物也能实现不停的移动。
当然肯定还有很多更好的方法能实现人物的移动,但是以自己目前的水平感觉这是最适合的方式。