c#声明一个二维数组,存放游戏玩家可以行走的四个方向(前后左右)

2018-01-08  本文已影响0人  Class_Lee

string[,] mapArray = new string[5, 5];

//  数组初始化

for (int i = 0; i < mapArray.GetLength (0); i++) {

for (int j = 0; j < mapArray.GetLength (1); j++) {

mapArray [i, j] = "*";

}

}

// 初始化玩家位置

mapArray [mapArray.GetLength (0) - 1, 0] = "#";

//记录当前位置

int x = 0;   // 列

int y = mapArray.GetLength (0) - 1;   // 行

while (true) {

// 打印

for (int i = 0; i < mapArray.GetLength (0); i++) {

for (int j = 0; j < mapArray.GetLength (1); j++) {

Console.Write (mapArray [i, j] + " ");

}

Console.WriteLine ();

}

Console.WriteLine ("请输入移动方向(wasd):");

string dir = Console.ReadLine ();

switch (dir) {

case "w":

if (y - 1 >= 0) {

mapArray [y, x] = "*";

mapArray [y - 1, x] = "#";

// 重新记录当前位置

y--;

} else {

Console.WriteLine ("你想上天啊!小伙子!");

}

break;

case "s":

if (y + 1 < mapArray.GetLength (0)) {

mapArray [y, x] = "*";

mapArray [y + 1, x] = "#";

y++;

} else {

Console.WriteLine ("你想下地狱啊!");

}

break;

case "d":

if (x + 1 < mapArray.GetLength (1)) {

mapArray [y, x] = "*";

mapArray [y, ++x] = "#";

} else {

Console.WriteLine ("这是d的尽头了!");

}

break;

case "a":

if (x - 1 >= 0) {

mapArray [y, x] = "*";

mapArray [y, --x] = "#";

} else {

Console.WriteLine ("小伙子,到头了!傻逼");

}

break;

default:

break;

}

}

上一篇下一篇

猜你喜欢

热点阅读