这个飞行棋是随便写的也测试后发现没问题,可能比较粗糙,但勉强可以用,很适合喜欢游戏开发的朋友
ps 我加入了登陆系统,所以要先设置账号和密码才能登陆哦
//游戏地图数组
static int[] map = new int[100];
//2个玩家
static int[] player = new int[2];
//登陆游戏入口
static void Main(string[] args)
{
//登陆系统
Console.WriteLine("设置账号");
string str = Console.ReadLine();
Console.WriteLine("设置密码");
string pw = Console.ReadLine();
Console.WriteLine("登陆游戏");
Console.WriteLine("输入账号");
string userName = Console.ReadLine();
Console.WriteLine("输入密码");
string userpw = Console.ReadLine();
while (true)
{
if (userName == str && userpw == pw)
{
#region palyer1 and 2
Random r = new Random();
int step;
int input;
bool[] isStop = { false, false };
string msg = "";
Show();
string[] name = new string[2];
while (true)
{
Console.WriteLine("设置玩家A名字");
name[0] = Console.ReadLine();
if (name[0] == "")
{
Console.WriteLine("名字不能为空");
continue;
}
else
{
break;
}
}
while (true)
{
Console.WriteLine("设置玩家B名字");
name[1] = Console.ReadLine();
if (name[1] == "")
{
Console.WriteLine("名字不能为空");
continue;
}
else if (name[1] == name[0])
{
Console.WriteLine("名字不能相同");
}
else
{
break;
}
}
Console.Clear();
//显示界面
Show();
Console.WriteLine("开始游戏");
Console.WriteLine("{0}玩家A", name[0]);
Console.WriteLine("{0}玩家 B", name[1]);
Console.WriteLine("如果玩家A和玩家B在一起显示ab");
//init map
InitialMap();
//Draw map
Dmap();
Console.WriteLine("开始游戏");
//掷骰子 判断是否越界
while (player[0] < 99 && player[1] < 99)
{
if (isStop[0] == false)
{
Console.WriteLine("按任意键开始掷骰子");
step = r.Next(1, 6);
Console.WriteLine("{0}抛出{1} ", name[0], step);
Console.WriteLine("按任意键抛股子");
Console.ReadKey(true);
player[0] += step;
CheckPos();
if (player[0] == player[1])
{
player[1] = 0;
Console.WriteLine("{0}踩到{1},{1}退回原点", name[0], name[1], name[1]);
}
else
{
//判断没踩到,先判断玩家A现在的位置是否有其他关卡
switch (map[player[0]])
{
case 0:
break;
case 1:
Console.Clear();
//Draw Map
Console.WriteLine("{0}走到了幸运轮盘,选择运气", name[0]);
Console.WriteLine("1--交换位置 2--轰炸对方");
input = ReadInt(1, 7);
if (input == 1)
{
int temp = player[0];
player[0] = player[1];
player[1] = temp;
Console.WriteLine("{0}选择与对方交换位置", name[0]);
}
else
{
//bei hong zang l
player[1] -= 6;
CheckPos();
Console.WriteLine("{0}轰炸{1},{1}退6格", name[0], name[1], name[1]);
}
break;
case 2:
player[1] -= 6;
CheckPos();
Console.WriteLine("{0}踩到地雷,{0}退6格", name[1], name[1]);
break;
case 3:
isStop[1] = true;
Console.WriteLine("{0}暂停一次", name[0]);
break;
case 4:
player[0] += 10;
CheckPos();
Console.WriteLine("{0}进入时空隧道,前进10格", name[1]);
break;
default:
break;
}
if (isStop[1] == false)
{
#region //玩家B掷骰子
Console.WriteLine("{0}按任意键开始掷骰子", name[1]);
ConsoleKeyInfo ren = Console.ReadKey(true);
if (ren.Key == ConsoleKey.Tab) //B的外挂键
{
step = 99;
}
else
{
step = r.Next(1, 7);
}
Console.WriteLine("{0}掷出:{1}", name[1], step);
Console.WriteLine("按任意键开始掷骰子");
Console.ReadKey(true);
player[1] += step;//一旦坐标发生改变,就要判断坐标值是否>99或<0
CheckPos();
if (player[1] == player[0])
{
player[0] = 0;
msg = string.Format("{0}踩到了{1},{1}退回原点", name[1], name[0], name[0]);
}
else
{ //没踩到,先判断玩家B现在的位置是否有其他关卡
switch (map[player[1]])
{
case 0:
//普通
msg = "";
break;
case 1:
//幸运轮盘
Console.Clear();
Dmap();
Console.WriteLine("{0}走到了幸运轮盘,选择运气", name[1]);
Console.WriteLine("1--交换位置 2--轰炸对方");
input = ReadInt(1, 2);
if (input == 1)
{
//与对方交换位置
int temp = player[1];
player[1] = player[0];
player[0] = temp;
msg = string.Format("{0}选择与对方交换位置", name[1]);
}
else
{
//轰炸对方
player[0] -= 6;
CheckPos();
msg = string.Format("{0}轰炸{1},{1}退6格", name[1], name[0], name[0]);
}
break;
case 2:
player[1] -= 6;
CheckPos();
msg = string.Format("{0}踩到地雷,{0}退6格", name[1], name[1]);
//地雷
break;
case 3:
//暂停
isStop[1] = true;
msg = string.Format("{0}暂停一次", name[1]);
break;
case 4:
player[0] += 10;
msg = string.Format("{0}进入时空隧道,前进10格", name[1]);
CheckPos();
//时空隧道
break;
}
}
Console.Clear();
Dmap();
if (msg != "")
{
Console.WriteLine(msg);
}
Console.WriteLine("{0}掷出了{1},行动完成!", name[1], step);
Console.WriteLine("*******玩家A和玩家B的位置如下******");
Console.WriteLine("{0}的位置为:{1}", name[0], player[0] + 1);
Console.WriteLine("{0}的位置为:{1}", name[1], player[1] + 1);
Console.Clear();
Dmap();
#endregion
}
else
{
isStop[1] = false;
}
}
Console.Clear();
//绘地图
Dmap();
if (msg != "")
{
Console.WriteLine(msg);
}
Console.WriteLine("{0}掷出了{1},行动完成!", name[0], step);
Console.WriteLine("*******玩家A和玩家B的位置如下******");
Console.WriteLine("{0}的位置为:{1}", name[0], player[0] + 1);
Console.WriteLine("{0}的位置为:{1}", name[1], player[1] + 1);
}
else
{
isStop[1] = false;
}
if (player[0] >= 99)
{
break;
}
}
#endregion
break;
}
else
{
Console.WriteLine("请输入正确的密码或密码");
userName = Console.ReadLine();
userpw = Console.ReadLine();
continue;
}
}
}
//检查是否越界
static void CheckPos()
{
for (int i = 0; i <= 1; i++)
{
if (player[i] > 99)
{
player[i] = 99;
}
if (player[i] < 0)
{
player[i] = 0;
}
}
}
//初始化地图
static void InitialMap()
{
int[] luckyT = { 5, 24, 37, 47, 87, 90 }; //幸运轮盘
int[] LlandM = { 7, 26, 33, 43, 54, 65, 79, 89 };//地雷
int[] pause = { 12, 21, 35, 60, 70, 74 };//暂停的坐标
int[] TimeT = { 40, 50, 55, 80, 85, 90, };//时空隧道
for (int i = 0; i < luckyT.Length; i++)
{
map[luckyT[i]] = 1;
}
for (int i = 0; i < LlandM.Length; i++)
{
map[LlandM[i]] = 2;
}
for (int i = 0; i < pause.Length; i++)
{
map[pause[i]] = 3;
}
for (int i = 0; i < TimeT.Length; i++)
{
map[TimeT[i]] = 4;
}
}
//得到地图的坐标的值并设置它的值
static string GetMapString(int pos)
{
string res = "";
if (player[0] == pos && player[1] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
res = "ab";
}
else if (player[0] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
res = "А";
}
else if (player[1] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
res = "В";
}
else
{
switch (map[pos])
{
case 0:
Console.ForegroundColor = ConsoleColor.White;
res = "□"; break;
case 1: Console.ForegroundColor = ConsoleColor.Red;
res = "◎"; break;
case 2:
Console.ForegroundColor = ConsoleColor.Magenta;
res = "★"; break;
case 3:
Console.ForegroundColor = ConsoleColor.DarkYellow;
res = "◆"; break;
case 4:
Console.ForegroundColor = ConsoleColor.DarkRed;
res = "→"; break;
}
}
return res;
}
//绘制地图
static void Dmap()
{
for (int i = 0; i <= 29; i++)
{
Console.Write(GetMapString(i));
}
Console.WriteLine();
for (int i = 30; i <= 34; i++)
{
for (int j = 0; j < 29; j++)
{
Console.Write(" ");
}
string str = GetMapString(i);
Console.WriteLine(str);
}
for (int i = 64; i >= 35; i--)
{
Console.Write(GetMapString(i));
}
Console.WriteLine();
for (int i = 65; i <= 70; i++)
{
string s = GetMapString(i);
Console.WriteLine(s);
}
for (int i = 71; i <= 99; i++)
{
Console.Write(GetMapString(i));
}
Console.WriteLine();
}
//传2个输入指定范围的值
static int ReadInt(int num1, int num2)
{
while (true)
{
try
{
int num = Convert.ToInt32(Console.ReadLine());
if (num < num1 || num > num2)
{
Console.WriteLine("只能输入{0}-{1}之间的数,请重新输入", num1, num2);
continue;
}
return num;
}
catch
{
Console.WriteLine("只能输入数字");
}
}
}
//界面
static void Show()
{
Console.WriteLine("*************************************");
Console.WriteLine("* *");
Console.WriteLine("* *");
Console.WriteLine("* *");
Console.WriteLine("* *");
Console.WriteLine("* *");
Console.WriteLine("* 飞 行 棋 *");
Console.WriteLine("* *");
Console.WriteLine("* *");
Console.WriteLine("* *");
Console.WriteLine("* *");
Console.WriteLine("*************************************");
}