这是用C#做的第一个项目,用C#写项目第一感觉就是这个开发工具功能好强大,比起VB来简直就是一个天上一个地上。哈哈,不说了,直接看图:
不管怎么样,看着还是挺好看的。其实这游戏没有多少代码,都是方法调方法。如图:这就是这个游戏的所有代码,C#让你看着代码特别清晰简洁。
下面说一下游戏制作流程:
首先游戏上面有个游戏头,我们用一个ShowUI 方法来写,方便以后调用
public static void ShowUI()
{
Console.WriteLine("*************************************");
Console.WriteLine("* *");
Console.WriteLine("* 终极骑士飞行棋 V1.0 *");
Console.WriteLine("* *");
Console.WriteLine("*************************************");
} //游戏头
然后游戏开始,我们输入玩家A和玩家B,名字不能相等和为空
Console.WriteLine("请输入玩家A的姓名");
PlayerNames[0] = Console.ReadLine();
while (PlayerNames[0] == "")
{
Console.WriteLine("玩家A的姓名不能为空,请重新输入");
PlayerNames[0] = Console.ReadLine();
}
Console.WriteLine("请输入玩家B的姓名");
PlayerNames[1] = Console.ReadLine();
while (PlayerNames[1] == PlayerNames[0] || PlayerNames[1] == "")
{
if (PlayerNames[1] == PlayerNames[0])
{
Console.WriteLine("玩家B的姓名和玩家A的姓名[{0}]不能相同", PlayerNames[0]);
}
else
{
Console.WriteLine("玩家B的名字为空,请重新输入");
}
PlayerNames[1] = Console.ReadLine();
}
Console.Clear();
ShowUI();
Console.WriteLine("对战开始.....");
Console.WriteLine("{0}的士兵用A表示......", PlayerNames[0]);
Console.WriteLine("{0}的士兵用B 表示......", PlayerNames[1]);
初始化地图,地图是由100个格子组成,上面有四种标志,地雷、幸运转盘、时空隧道、暂停四类,用四个数组表示他们,只要玩家走到其中的每一个上,就做出相应的动作,定义InitMap()方法来写。
public static void InitMap()
{
int[] luckyturn = { 6, 23, 40, 55, 69, 83 };
int[] landMine = { 5, 13, 17, 33, 50, 64, 80, 94 };
int[] pause = { 9, 27, 60, 93 };
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };
for (int i = 0; i < luckyturn.Length; i++)
{
//int temp = luckyturn[i];
Map[luckyturn[i]] = 1;
}
for (int i = 0; i < landMine.Length; i++)
{
Map[landMine[i]] = 2;
}
for (int i = 0; i < pause.Length; i++)
{
Map[pause[i]] = 3;
}
for (int i = 0; i < timeTunnel.Length; i++)
{
Map[timeTunnel[i]] = 4;
}
} //初始化地图
地图初始化好,判断玩家的位置,如果两个人同时在一起,图形为<> ,否则为A和B,如果玩家A和玩家B不在一起也不在这个坐标上就画该画的地图图标。
public static int[] PlayerPos = new int[] { 0, 0 }; //声明一个数组用来存玩家A和玩家B
public static string[] PlayerNames = new string[2];
public static string DrawStringMap(int pos)
{
string temp = "";
#region 画第一行逻辑
if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
temp = "<>";
}
else if (PlayerPos[0] == pos) //玩家A在地图上就画A
{
Console.ForegroundColor = ConsoleColor.Yellow;
temp = "A";
}
else if (PlayerPos[1] == pos) //玩家B在地图上就画B
{
Console.ForegroundColor = ConsoleColor.Yellow;
temp = "B";
}
else
{
switch (Map[pos]) //如果玩家A和玩家B不在一起也不在这个坐标上就画该画的地图图标
{
case 0:
Console.ForegroundColor = ConsoleColor.White;
temp = "□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Red;
temp = "●";
break;
case 2:
Console.ForegroundColor = ConsoleColor.Blue;
temp = "★";
break;
case 3:
Console.ForegroundColor = ConsoleColor.Green;
temp = "▲";
break;
case 4:
Console.ForegroundColor = ConsoleColor.Magenta;
temp = "卍";
break;
}//end switch
}//end else
return temp;
#endregion
} //画第一行逻辑
画地图,定义DrawMap()方法
public static void DrawMap()
{
Console.WriteLine("图例:幸运轮盘:● 地雷:★ 暂停:▲ 时空隧道:卍");
#region 画第一横行
DrawMapLeftToRight(0, 30);
#endregion
Console.WriteLine();
#region 画地图第一竖行
for (int i = 30; i <= 34; i++)
{
for (int j = 0; j <= 28; j++)
{
Console.Write(" ");
}
Console.WriteLine(DrawStringMap(i));
}
#endregion
#region 画第二横行
for (int i = 64; i >= 35; i--)
{
Console.Write(DrawStringMap(i));
}
#endregion
Console.WriteLine();
#region 画第二竖行
for (int i = 65; i <= 69; i++)
{
Console.WriteLine(DrawStringMap(i));
}
#endregion
#region 画第三横行
DrawMapLeftToRight(70, 100);
#endregion
Console.WriteLine();
} //画地图
画完地图,我们就要掷骰子,我们都知道骰子都是1-6个数,我们产生一个随机数在1-6之内。然后做出相应的动作。
public static void RowTouZi(int playerPos)
{
Random r = new Random();
int num = r.Next(1, 7);
string msg = "";
Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[playerPos]);
ConsoleKeyInfo coninfo = Console.ReadKey(true);
if (coninfo.Key == ConsoleKey.Q)
{
coninfo = Console.ReadKey(true);
if (coninfo.Key == ConsoleKey.A)
{
coninfo = Console.ReadKey(true);
if (coninfo.Key == ConsoleKey.Z)
{
num = 50;
}
}
}
//Console.ReadKey(true);
Console.WriteLine("{0}掷出了{1}", PlayerNames[playerPos], num);
Console.WriteLine("{0}按任意键开始行动.....", PlayerNames[playerPos]);
Console.ReadKey(true);
PlayerPos[playerPos] += num;
CheckPos();
if (PlayerPos[playerPos] == PlayerPos[1 - playerPos])
{
msg = string.Format("玩家{0}踩到了玩家{1},玩家{2}退6格", PlayerNames[playerPos], PlayerNames[1 - playerPos], PlayerNames[1 - playerPos]);
PlayerPos[1 - playerPos] -= 6;
CheckPos();
}
else
{
switch (Map[PlayerPos[playerPos]])
{
case 0: msg = "行动完"; break;
case 1:
msg = string.Format("{0}走到幸运轮盘,请选择1----交换位置,2--轰炸对方", PlayerNames[playerPos]);
int number = ReadInt(msg, 1, 2);
if (number == 1)
{
int temp = 0;
temp = PlayerPos[playerPos];
PlayerPos[playerPos] = PlayerPos[1 - playerPos];
PlayerPos[1 - playerPos] = temp;
msg = string.Format("玩家{0}选择了与玩家{1}交换位置", PlayerNames[playerPos], PlayerNames[1 - playerPos]);
}
else
{
PlayerPos[1 - playerPos] = 0;
msg = string.Format("玩家{0}选择轰炸玩家{1}", PlayerNames[playerPos], PlayerNames[1 - playerPos]);
}
break;
case 2:
msg = "恭喜你踩到地雷,退6格";
PlayerPos[playerPos] -= 6;
CheckPos();
break;
case 3:
msg = "踩到暂停了";
flag[playerPos] = true;
break;
case 4:
msg = "恭喜你踩到时空隧道了,你爸告诉你前进10步";
PlayerPos[playerPos] += 10;
CheckPos();
break;
}
}
Console.Clear();
DrawMap();
Console.WriteLine(msg);
} //掷骰子
在掷骰子的过程中,谁先走到最后,谁就胜利,不能超范围,所以定义一个方法来判断是否越界。
public static void CheckPos()
{
if (PlayerPos[0] > 99)
{
PlayerPos[0] = 99;
}
if (PlayerPos[1] > 99)
{
PlayerPos[1] = 99;
}
if (PlayerPos[0] < 0)
{
PlayerPos[0] = 0;
}
if (PlayerPos[1] < 0)
{
PlayerPos[1] = 0;
}
} //判断游戏是否越界
当我们踩到幸运轮盘的时候,我们需要输入1和2来让用户做出相应的动作:
public static int ReadInt(string msg, int min, int max)
{
while (true)
{
try
{
Console.WriteLine(msg);
int number = Convert.ToInt32(Console.ReadLine());
if (number >= min && number <= max)
{
return number;
}
else
{
Console.WriteLine("你的输入不合法!{0}到{1}之间的数字", min, max);
continue;
}
}
catch (Exception)
{
Console.WriteLine("输入有误,请重新输入");
}
}
}
好了,飞行棋就这么做完了,其实分开做,一步一步的,只要逻辑搞清了,就可以了,想要提前体验一把的,可以来找我。哈哈。