飞行项目基本上涵盖了C#所有的基础的内容,给飞行棋项目搞透彻了,C#的基础基本上是没啥问题的了。
综合飞行棋的项目,主要有一下几个点:
1.画游戏的头
2.初始化地图(InitialMap())
3.根据地图的位置,画出不同的内容(DrawStringMap() ; 我理解这个方法就像是画画使用的画笔一样,在哪个位置应该画什么样的图形)
4.使用“画笔”进行真正的画地图 (DrawMap() )
5.玩家名称的输入
6.游戏中每个“关卡”的判断效果(有普通,幸运轮盘,地雷,暂停,以及时空隧道等)
7.玩家进行游戏以及关卡效果 的判断
8.输出胜利图标
</pre>下面是代码大集合:</p><p><pre name="code" class="csharp"><span style="font-size:14px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 骑士飞行棋
{
class Program
{
//静态字段
public static int[] Map = new int[100];//存储地图
public static int[] PlayPos = new int[2] { 0, 0 };//存储玩家的坐标位置
public static string[] PlayerNames = new string[2];//存储玩家姓名
public static bool[] IsStop = new bool[2] { false, false };//是否暂停//IsStop[0]为玩家1标记,IsStop[1]为玩家2标记。
static void Main(string[] args)
{
ShowUI();//画游戏头
#region 输入玩家姓名
Console.WriteLine("请输入玩家A的姓名:");
PlayerNames[0] = Console.ReadLine().Trim();
while (PlayerNames[0] == "")
{
Console.WriteLine("玩家A的姓名不能为空,请重新输入:");
PlayerNames[0] = Console.ReadLine().Trim();
}
Console.WriteLine("请输入玩家B的姓名:");
PlayerNames[1] = Console.ReadLine().Trim();
while (PlayerNames[1] == "" || PlayerNames[1] == PlayerNames[0])//玩家B的姓名不能为空,并且不能和玩家A的姓名相同
{
if (PlayerNames[1] == "")
{
Console.WriteLine("玩家B的姓名不能为空,请重新输入:");
PlayerNames[1] = Console.ReadLine().Trim();
}
else
{//玩家B的姓名和玩家A 的姓名相同了
Console.WriteLine("玩家B的姓名不能和玩家A的姓名相同,请重新输入玩家B的姓名:");
PlayerNames[1] = Console.ReadLine().Trim();
}
}
#endregion
//接受正确玩家姓名之后,需要清屏,并且重新画游戏头,然后输出玩家信息,初始化地图,画地图。
Console.Clear();
ShowUI();//重新画游戏头
Console.WriteLine("对战开始..........");
Console.WriteLine("{0}的士兵用A表示", PlayerNames[0]);
Console.WriteLine("{0}的士兵用B表示", PlayerNames[1]);
Console.WriteLine("图例:普通□ 幸运轮盘◎ 地雷☆ 暂停▲ 时空隧道卐");//关卡中的简介
InitialMap();//初始化地图
//调用画地图方法
DrawMap();
#region 开始游戏
while (PlayPos[0] < 99 && PlayPos[1] < 99)
{
#region 封装方法前的实现做法
//Console.WriteLine("{0}按任意键开始掷骰子......", PlayerNames[0]);
//Console.ReadKey(true);//不显示用户按下的键值
//Console.WriteLine("{0}掷出了{1}", PlayerNames[0], 4);
//Console.WriteLine("{0}按任意键开始行动", PlayerNames[0]);
//Console.ReadKey(true);
//PlayPos[0] += 4;
//#region 游戏规则判定
//if (PlayPos[0] == PlayPos[1])
//{//玩家A踩到了玩家B
// Console.WriteLine("玩家A踩到了玩家B,玩家B退6格");
// PlayPos[1] -= 6;
//}
//else
//{//玩家A踩到了其他的关卡
// switch (Map[PlayPos[0]])
// {
// case 0:
// Console.WriteLine("玩家{0}行动结束了", PlayerNames[0]);
// Console.ReadKey(true);
// break;
// case 1://踩到了幸运轮盘
// Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择:1--交换位置 2---轰炸对方", PlayerNames[0]);
// string input = Console.ReadLine();
// while (true)
// {
// if (input == "1")
// {
// Console.WriteLine("玩家{0}选择了交换位置", PlayerNames[0]);
// Console.ReadKey(true);
// int temp = PlayPos[0];
// PlayPos[0] = PlayPos[1];
// PlayPos[1] = temp;
// Console.WriteLine("交换完成...");
// Console.ReadKey(true);
// break;
// }
// else if (input == "2")
// {
// Console.WriteLine("玩家{0}选择了轰炸对方,玩家{1}退6格", PlayerNames[0], PlayerNames[1]);
// Console.ReadKey(true);
// PlayPos[1] -= 6;
// break;
// }
// else
// {//输入的既不是1也不少2
// Console.WriteLine("输入有误,请重新输入:1--交换位置 2---轰炸对方");
// input = Console.ReadLine();
// }
// }
// break;
// case 2://踩到地雷
// Console.WriteLine("玩家{0}踩到了地雷,退6格", PlayerNames[0]);
// PlayPos[0] -= 6;
// Console.ReadKey(true);
// break;
// case 3://踩到暂停
// Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerNames[0]);
// break;
// case 4://踩到了时空隧道
// Console.WriteLine("玩家{0}踩到了时空隧道,进10格", PlayerNames[0]);
// PlayPos[0] += 10;
// break;
// }//Switch
//}//if else
//#endregion
清屏
//Console.Clear();
//Console.WriteLine("图例:普通□ 幸运轮盘◎ 地雷☆ 暂停▲ 时空隧道卐");
重画
//DrawMap();
#endregion
if (IsStop[0] == false)//没有踩到暂停
{
//玩家A玩游戏
PlayGame(0);
}
else
{//踩到暂停了
IsStop[0] = false;
}
if (PlayPos[0] >= 99)
{
Console.WriteLine("玩家{0}无耻的赢了玩家{1}", PlayerNames[0], PlayerNames[1]);
break;
}
if (IsStop[1] == false)
{
//玩家B玩游戏
PlayGame(1);
}
else
{
//玩家B踩到了暂停
IsStop[1] = false;
}
if (PlayPos[1] >= 99)
{
Console.WriteLine("玩家{0}无耻的赢了玩家{1}", PlayerNames[1], PlayerNames[0]);
break;
}
}//While_END
#endregion
//调用胜利的输出的方法
Win();
Console.ReadKey();
}//Main_End
/// <summary>
/// 1. 画游戏头
/// </summary>
public static void ShowUI()
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("****骑士飞行棋游戏 v1.01 ****");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("******************************");
}
//初始化地图
//初始化地图:-- Map[6]=1 ---- 0-99
//用0表示普通,显示给用户的是 □
//用1 幸运轮盘,显示给用户的是◎
//用2 地雷,显示给用户的就是☆
//用3 暂停,显示给用户的就是▲
//用4 时空隧道,显示给用户的就是卐
/// <summary>
/// 2. 初始化地图,定义地图中关卡在Map数组中的值
/// </summary>
public static void InitialMap()
{
int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘◎
for (int i = 0; i < luckyturn.Length; i++)
{
Map[luckyturn[i]] = 1;
}
int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆
for (int i = 0; i < landMine.Length; i++)
{
Map[landMine[i]] = 2;
}
int[] pause = { 9, 27, 60, 93 };//暂停▲
for (int i = 0; i < pause.Length; i++)
{
Map[pause[i]] = 3;
}
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道卐
for (int i = 0; i < timeTunnel.Length; i++)
{
Map[timeTunnel[i]] = 4;
}
}//InitialMap_END
//画地图的方法:
/// <summary>
/// 2. 抽象出来的画地图的方法,根据地图Map中坐标pos对应存储的值,对应的字符串画出来
/// </summary>
/// <param name="i">地图中的坐标</param>
/// <returns>返回画出来的字符</returns>
public static string DrawStringMap(int i)
{
string str = "";
if (PlayPos[0] == PlayPos[1] && PlayPos[1] == i)//玩家A和玩家B坐标相同,并且都在地图上
{
str = "<>";
}
else if (PlayPos[0] == i)//如果玩家1的坐标是i
{
Console.ForegroundColor = ConsoleColor.Yellow;
str = "A";
}
else if (PlayPos[1] == i)//如果玩家2的坐标是i
{
Console.ForegroundColor = ConsoleColor.Yellow;
str = "B";
}
else
{//是其他的关卡,
switch (Map[i])
{
case 0:
Console.ForegroundColor = ConsoleColor.Magenta;
str = "□";//普通
break;
case 1:
Console.ForegroundColor = ConsoleColor.Red;
str = "◎";//幸运轮盘
break;
case 2:
Console.ForegroundColor = ConsoleColor.Gray;
str = "☆";//地雷
break;
case 3:
Console.ForegroundColor = ConsoleColor.Green;
str = "▲";//暂停
break;
case 4:
Console.ForegroundColor = ConsoleColor.Blue;
str = "卐";//时空隧道
break;
}
}
return str;
}
//画地图
/// <summary>
/// 3. 画地图
/// </summary>
public static void DrawMap()
{
#region 画第一横行
for (int i = 0; i < 30; i++)
{
//调用画地图的方法
Console.Write(DrawStringMap(i));
}
Console.WriteLine();//第一行输出完成后就要换行
#endregion
#region 画第一竖行
for (int i = 30; i <= 34; i++)
{//画坐标对应图像
for (int j = 0; j < 29; j++)
{//竖行左侧的空格
Console.Write(" ");
}
Console.Write(DrawStringMap(i));//每行左侧空格输出完成后就要输出一个图形
Console.WriteLine();//每行输出完成后就要进行 换行
}
#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 画第三横行
for (int i = 70; i <= 99; i++)
{
Console.Write(DrawStringMap(i));
}
#endregion
Console.WriteLine();//换行
}
/// <summary>
///4. 玩游戏的方法
/// </summary>
/// <param name="PlayNumber">游戏玩家</param>
public static void PlayGame(int PlayNumber)
{
Random r = new Random();
int rNumber = r.Next(1, 7);
Console.WriteLine("{0}按任意键开始掷骰子......", PlayerNames[PlayNumber]);
Console.ReadKey(true);//不显示用户按下的键值
Console.WriteLine("{0}掷出了{1}", PlayerNames[PlayNumber], rNumber);
Console.WriteLine("{0}按任意键开始行动", PlayerNames[PlayNumber]);
Console.ReadKey(true);
PlayPos[PlayNumber] += rNumber;
CheckPos();
#region 游戏规则判定
if (PlayPos[PlayNumber] == PlayPos[1 - PlayNumber])
{//玩家A踩到了玩家B
Console.WriteLine("玩家{0}踩到了玩家{1},玩家{1}退6格", PlayerNames[PlayNumber], PlayerNames[1 - PlayNumber], PlayerNames[1 - PlayNumber]);
PlayPos[1 - PlayNumber] -= 6;
CheckPos();
}
else
{//玩家A踩到了其他的关卡
switch (Map[PlayPos[PlayNumber]])
{
case 0:
Console.WriteLine("玩家{0}行动结束了", PlayerNames[PlayNumber]);
Console.ReadKey(true);
break;
case 1://踩到了幸运轮盘
Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择:1--交换位置 2---轰炸对方", PlayerNames[PlayNumber]);
string input = Console.ReadLine();
while (true)
{
if (input == "1")
{
Console.WriteLine("玩家{0}选择了交换位置", PlayerNames[PlayNumber]);
Console.ReadKey(true);
int temp = PlayPos[PlayNumber];
PlayPos[PlayNumber] = PlayPos[1 - PlayNumber];
PlayPos[1 - PlayNumber] = temp;
Console.WriteLine("交换完成...");
Console.ReadKey(true);
CheckPos();
break;
}
else if (input == "2")
{
Console.WriteLine("玩家{0}选择了轰炸对方,玩家{1}退6格", PlayerNames[PlayNumber], PlayerNames[1 - PlayNumber]);
Console.ReadKey(true);
PlayPos[1 - PlayNumber] -= 6;
CheckPos();
break;
}
else
{//输入的既不是1也不少2
Console.WriteLine("输入有误,请重新输入:1--交换位置 2---轰炸对方");
input = Console.ReadLine();
}
}
break;
case 2://踩到地雷
Console.WriteLine("玩家{0}踩到了地雷,退6格", PlayerNames[PlayNumber]);
PlayPos[PlayNumber] -= 6;
Console.ReadKey(true);
CheckPos();
break;
case 3://踩到暂停
Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerNames[PlayNumber]);
IsStop[PlayNumber] = true;//踩到暂停了,就给标志设置为true.
break;
case 4://踩到了时空隧道
Console.WriteLine("玩家{0}踩到了时空隧道,进10格", PlayerNames[PlayNumber]);
PlayPos[PlayNumber] += 10;
CheckPos();
break;
}//Switch
}//if else
#endregion
//清屏
Console.Clear();
Console.WriteLine("图例:普通□ 幸运轮盘◎ 地雷☆ 暂停▲ 时空隧道卐");
//重画
DrawMap();
}
//检查玩家的坐标是否越界
/// <summary>
/// 5. 当每次玩家的坐标移动之后,检查玩家的坐标是否越界,并进行操作
/// </summary>
public static void CheckPos()
{
if (PlayPos[0] < 0)
{
PlayPos[0] = 0;
}
if (PlayPos[0] >= 99)
{
PlayPos[0] = 99;
}
if (PlayPos[1] < 0)
{
PlayPos[1] = 0;
}
if (PlayPos[1] >= 99)
{
PlayPos[1] = 99;
}
}
//游戏的胜利图标输出
/// <summary>
/// 游戏胜利图标输出
/// </summary>
public static void Win()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(" ◆ ");
Console.WriteLine(" ■ ◆ ■ ■");
Console.WriteLine(" ■■■■ ■ ■ ◆■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ◆ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■■■■■■ ■■■■■■■ ■ ■ ■");
Console.WriteLine(" ■■■■ ■ ■ ●■● ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ● ■ ● ■ ■ ■");
Console.WriteLine(" ■ ■ ■■■■■■ ● ■ ● ■ ■ ■");
Console.WriteLine(" ■■■■ ■ ● ■ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ■ ■ ");
Console.WriteLine(" ■ ■ ■ ■ ● ■ ");
Console.WriteLine(" ■ ■■ ■■■■■■ ■ ● ●");
Console.ResetColor();
}
}//Class_E
}</span>