C# 简单 控制台 飞行棋 附源码
简单C# 控制台飞行棋,使用 int数组 用于创建地图。图例: 幸运转盘: ◎ 地雷: ☆ 暂停: ▲ 时空隧道: 卐
using System;
namespace 飞行棋
{
class Program
{
static int[] Maps = new int[100];//定义 地图 数组
static string[] PlayerNames = new string[2];//定义 玩家名字数组
static int[] PlayerPos = new int[2];//定义玩家坐标数组
static bool[] Flags = new bool[2];//定义玩家布尔数组
static void Main(string[] args)
{
GameHead();//调用 游戏头 方法
SetPlayer();//调用 玩家名字 方法
DrawMap();//绘制地图 方法
while (PlayerPos[0] < 99 && PlayerPos[1] < 99)//判断玩家1和玩家2是否在地图数组中
{
if (Flags[0] == false)//玩家1 标志
{
PlayGame(0);//调用开始游戏方法
}
else
{
Flags[0] = false;
}
if (PlayerPos[0]>=99)
{
Console.WriteLine("玩家【{0}】胜利了!!",PlayerNames[0]);//提示
Console.ReadKey();
break;//退出
}
if (Flags[1] == false)
{
PlayGame(1);//调用开始游戏方法
}
else
{
Flags[1] = false;
}
if (PlayerPos[1] >= 99)
{
Console.WriteLine("玩家【{0}】胜利了!!", PlayerNames[1]);//提示
Console.ReadKey();
break;//退出
}
}
Console.ReadKey();
}
#region 游戏头
static void GameHead()//“游戏头” 方法
{
Console.ForegroundColor = ConsoleColor.Blue;//设置 控制台前景色
Console.WriteLine("************************************************************");//输出
Console.WriteLine("************************************************************");//输出
Console.ForegroundColor = ConsoleColor.Red;//设置 控制台前景色
Console.WriteLine("*************************飞行棋练习*************************");//输出
Console.ForegroundColor = ConsoleColor.Blue;//设置 控制台前景色
Console.WriteLine("************************************************************");//输出
Console.WriteLine("************************************************************");//输出
Console.ForegroundColor = ConsoleColor.Yellow;//设置 控制台前景色
Console.WriteLine("*********************************************小道博客.制作**");//输出
}
#endregion
#region 设置玩家
static void SetPlayer() //输入 玩家 名字 方法
{
bool sr = true;// 布尔变量
while (sr)//循环
{
Console.ForegroundColor = ConsoleColor.Magenta;//设置 控制台前景色
Console.WriteLine("请输入玩家A的姓名:");//提示
PlayerNames[0] = Console.ReadLine();//接收字符串
Console.WriteLine("请输入玩家B的名字:");//提示
PlayerNames[1] = Console.ReadLine();//接收字符串
if (PlayerNames[0] == "" || PlayerNames[1] == "" || PlayerNames[0] == PlayerNames[1])
{
Console.ForegroundColor = ConsoleColor.Cyan;//设置 控制台前景色
Console.WriteLine("玩家名字不能为空或相同!!!");//提示
}
else
{
sr = false;//设置
Console.Clear();//清除控制台内容
GameHead();//游戏头 方法
}
}
}
#endregion
#region 初始化地图
static void DrawMap()
{
#region 初始地图
int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘◎ 1
for (int i = 0; i < luckyturn.Length; i++) //循环
{
Maps[luckyturn[i]] = 1;//设置Maps数组的 幸运轮盘 的值
}
int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆ 2
for (int i = 0; i < landMine.Length; i++)//循环
{
Maps[landMine[i]] = 2;//设置Maps数组的 地雷 的值
}
int[] pause = { 9, 27, 60, 93 };//暂停▲ 3
for (int i = 0; i < pause.Length; i++)//循环
{
Maps[pause[i]] = 3;//设置Maps数组的 暂停 的值
}
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道卐 4
for (int i = 0; i < timeTunnel.Length; i++)//循环
{
Maps[timeTunnel[i]] = 4;//设置Maps数组的 时空隧道 的值
}
#endregion
#region 第一行
Console.ForegroundColor = ConsoleColor.Red;//设置 控制台前景色
Console.WriteLine("玩家【{0}】的士兵用A表示,当前坐标:{1},距终点还剩{2}步.", PlayerNames[0],PlayerPos[0],99-PlayerPos[0]);//显示 玩家A的信息
Console.WriteLine("玩家【{0}】的士兵用B表示,当前坐标:{1},距终点还剩{2}步.", PlayerNames[1], PlayerPos[1], 99 - PlayerPos[1]);//显示 玩家B的信息
Console.ForegroundColor = ConsoleColor.Gray;//设置 控制台前景色
Console.WriteLine("图例: 幸运转盘: ◎ 地雷: ☆ 暂停: ▲ 时空隧道: 卐");//提示说明
for (int i = 0; i < 30; i++)//绘制 第一行
{
Console.Write(DrawStringMap(i));//绘制 第一行
}
Console.WriteLine();//换行
#endregion
#region 第一列
for (int j = 29; j < 35; j++)//循环 第一列
{
for (int i = 0; i < 29; i++)//循环28个空格
{
Console.Write(" ");//输出28个空格
}
Console.WriteLine(DrawStringMap(j));//绘制输出 第一列
}
#endregion
#region 第二行
for (int i = 64; i > 34; i--) //循环 第二行
{
Console.Write(DrawStringMap(i));//绘制 第二行
}
Console.WriteLine();//换行
#endregion
#region 第二列
for (int i = 65; i < 70; i++)//循环 第二列
{
Console.WriteLine(DrawStringMap(i));//绘制输出第二列
}
#endregion
#region 第三行
for (int i = 70; i < 100; i++)//循环 第三行
{
Console.Write(DrawStringMap(i));//绘制输出 第三行
}
Console.WriteLine();//换行
#endregion
}
#endregion
#region 绘制玩家坐标显示
static string DrawStringMap(int i)
{
string str = "";//定义 字符串 变量
if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)//判断 玩家1和玩家2坐标是否相同,并且玩家1在地图上
{
Console.ForegroundColor = ConsoleColor.Yellow;//设置 控制台前景色
str = "<>";//赋值
}
else if (PlayerPos[0] == i)//判断玩家1 在地图上
{
Console.ForegroundColor = ConsoleColor.Yellow;//设置 控制台前景色
str = "A";//赋值
}
else if (PlayerPos[1] == i)//判断玩家2 在地图上
{
Console.ForegroundColor = ConsoleColor.Yellow;//设置 控制台前景色
str = "B";//赋值
}
else
{
switch (Maps[i])//判断 地图数组 的值为几。
{
case 0://值为0
Console.ForegroundColor = ConsoleColor.Gray;//设置 控制台前景色
str = "□";//赋值
break;
case 1://值为1
Console.ForegroundColor = ConsoleColor.Green;//设置 控制台前景色
str = "◎";//赋值
break;
case 2://值为2
Console.ForegroundColor = ConsoleColor.Red;//设置 控制台前景色
str = "☆";//赋值
break;
case 3://值为3
Console.ForegroundColor = ConsoleColor.White;//设置 控制台前景色
str = "▲";//赋值
break;
case 4://值为4
Console.ForegroundColor = ConsoleColor.Blue;//设置 控制台前景色
str = "卐";//赋值
break;
}
}
return str;//返回 str 字符串的值
}
#endregion
static void PlayGame(int PN)
{
Random r = new Random();//实例化一个随机
int a = r.Next(1, 7);//随机(1~6)之间的一个数
Console.WriteLine("请玩家【{0}】按任意键开始掷骰子.", PlayerNames[PN]);//提示
Console.ReadKey(true);
Console.WriteLine("玩家【{0}】掷出了 {1} 点.", PlayerNames[PN], a);//提示
PlayerPos[PN] += a;//玩家的坐标加上 随机的步数
Console.ReadKey(true);
Console.WriteLine("按任意键使玩家【{0}】向前行走 {1} 步.", PlayerNames[PN],a);//提示
Console.ReadKey(true);
Console.WriteLine("玩家【{0}】行走完成.", PlayerNames[PN]);//提示
Console.ReadKey(true);
if (PlayerPos[PN] == PlayerPos[1 - PN])//判断玩家是否 走到了一个空格上
{
Console.WriteLine("玩家【{0}】撞到了玩家【{1}】,玩家【{2}】退6格.", PlayerNames[PN], PlayerNames[1 - PN], PlayerNames[1 - PN]);//提示
PlayerPos[1 - PN] -= 6;//玩家 减去6
Console.ReadKey(true);
}
else
{
ChangePos();//调用 判断 玩家的坐标是否在地图上.
switch (Maps[PlayerPos[PN]])//判断 玩家 的坐标在地图数组上的值是多少。
{
case 0:
Console.WriteLine("玩家【{0}】踩到了方格,什么也没发生.", PlayerNames[PN]);//提示
Console.ReadKey(true);
break;
case 1:
Console.WriteLine("玩家【{0}】踩到了幸运轮盘,请选择 1--交换位置 2--轰炸对方", PlayerNames[PN]);//提示
string input = Console.ReadLine();//接收用户输入的值
while (true)
{
if (input == "1")//输入的值 是 1
{
Console.WriteLine("玩家【{0}】选择与玩家【{1}】交换位置.", PlayerNames[PN], PlayerNames[1 - PN]);//提示
Console.ReadKey(true);
int temp;
temp = PlayerPos[PN];
PlayerPos[PN] = PlayerPos[1 - PN];
PlayerPos[1 - PN] = temp; //交换值。
Console.WriteLine("交换完成!!!按任意键继续.");//提示
Console.ReadKey(true);
break;
}
else if (input == "2")//输入的值 是 2
{
Console.WriteLine("玩家【{0}】选择轰炸玩家【{1}】.", PlayerNames[PN], PlayerNames[1 - PN]);//提示
Console.ReadKey(true);
PlayerPos[1 - PN] -= 6;//坐标减去6
Console.WriteLine("轰炸完成!!!玩家【{0}】退后6格,按任意键继续.", PlayerNames[1 - PN]);//提示
Console.ReadKey(true);
break;
}
else
{
Console.WriteLine("只能输入 1 或者 2 请选择 1--交换位置 2--轰炸对方");//提示
input = Console.ReadLine();//接收用户输入的值
}
}
break;
case 2:
Console.WriteLine("玩家【{0}】踩到了地雷,退6格.", PlayerNames[PN]);//提示
Console.ReadKey(true);
PlayerPos[PN] -= 6;//坐标减去6
break;
case 3:
Console.WriteLine("玩家【{0}】踩到了暂停,暂停一回合.", PlayerNames[PN]);//提示
Flags[PN] = true;//设置玩家 标致
Console.ReadKey(true);
break;
case 4:
Console.WriteLine("玩家【{0}】踩到了时空隧道,前进10格.", PlayerNames[PN]);//提示
Console.ReadKey(true);
PlayerPos[PN] += 10;//玩家坐标加10
break;
}
}
ChangePos();//调用 判断 玩家的坐标是否在地图上.
Console.Clear();//清空 控制台 内容
DrawMap();//绘制地图 方法
}
static void ChangePos()
{
if (PlayerPos[0] < 0)//判断玩家1坐标 是否小于0
{
PlayerPos[0] = 0;//坐标赋值为0
}
if (PlayerPos[0] >= 99)//判断玩家1坐标 是否大于或等于99
{
PlayerPos[0] = 99;//坐标赋值为99
}
if (PlayerPos[1] < 0)//判断玩家2坐标 是否小于0
{
PlayerPos[1] = 0;//坐标赋值为0
}
if (PlayerPos[1] >= 99)//判断玩家2坐标 是否大于或等于99
{
PlayerPos[1] = 99;//坐标赋值为99
}
}
}
}输出结果:
