前几天学习的实践:游戏---飞行棋
步骤:
1.画游戏头
2.初始化地图(加载地图所需要的资源)
将整个数组中的数字变成控制台中显示的特殊字符串的过程,就是初始化地图
int[100]代表100个符号--->赋值0,代表普通符号□; 赋值1,代表幸运轮盘◎;赋值2,代表地雷☆;赋值3,代表暂停▲;赋值4代表时空隧道卐。
3.画地图
4.玩游戏
游戏规则:随机一个数X
1. 踩到了对手:对手退6格
2. 踩到了地雷:自己退6格
3. 踩到了幸运:-->1、交换位置;2、轰炸对手,对手退6格
4. 踩到了暂停:暂停一个回合
5. 踩到了时空:进10格
6.踩到了方框:进X格
开始写游戏:
1. 先写游戏头和胜利画面方法:
/// <summary>
/// 画游戏头
/// </summary>
public static void GameHead()
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("********************************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("********************************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("********************************");
Console.WriteLine("***0326版骑士飞行棋游戏--v1.1***");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("********************************");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("********************************");
}
/// <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();
}
2. 初始化地图方法:
为了方便所有方法调用用静态字段模拟了4个全局变量:
//两个玩家的名字
public static string[] PlayerName = new String[2];
//给地图100个字符命声明一个数组
public static int[] maps = new int[100]; //自动初始值为0;
//给两个玩家的坐标(maps的下标)声明一个数组
public static int[] playerPos = new int[2];
//定义一个玩家是否不可玩的bool类型数组
public static bool[] Flag = new bool[2];
也就是说初始化地图是给maps数组所有下标赋值:
/// <summary>
/// 初始化地图地图数组中每一个下标中的值
/// </summary>
public static void InitialMap()
{
//int[100]代表100个符号--->赋值0,代表普通符号□; 赋值1,
//代表幸运轮盘◎;赋值2,代表地雷☆;赋值3,代表暂停▲;赋值4代表时空隧道卐
//显示幸运轮盘的下标为6,23,40,55,69,83,定义一个数组储存它们
int[] luckyturn = {6,23,40,55,69,83};
for(int i = 0; i<luckyturn.Length;i++)
{ //通过循环,给这些下标下的maps赋值
maps[luckyturn[i]] = 1;
}
//显示地雷的下标为5,13,17,33,38,50,64,80,94,定义一个数组储存它们
int[] lineMine = {5,13,17,33,38,50,64,80,94};
for(int i = 0; i<lineMine.Length;i++)
{ //通过循环,给这些下标下的maps赋值
maps[lineMine[i]] = 2;
}
//显示暂停的下标为9,27,60,93,定义一个数组储存它们
int[] Pause = {9,27,60,93};
for(int i = 0; i<Pause.Length;i++)
{ //通过循环,给这些下标下的maps赋值
maps[Pause[i]] = 3;
}
//显示时空隧道的下标为20,25,45,63,72,88,90,定义一个数组储存它们
int[] timeTunnel = {20,25,45,63,72,88,90};
for(int i = 0; i<timeTunnel.Length;i++)
{ //通过循环,给这些下标下的maps赋值
maps[timeTunnel[i]] = 4;
}
}
3. 画地图方法:
1)画地图时需要多次将maps数组中的值转成特殊字符,因此将其定义成一个方法(函数)
/// <summary>
/// 将maps的i下标下的值转成字符
/// </summary>
/// <returns>返回maps[i]的字符</returns>
public static string DrawMap(int i)
{
string str = "";
if( playerPos[0] == i && playerPos[0] == playerPos[1] )
{ //如果玩家A和B在该下标位置
Console.ForegroundColor = ConsoleColor.Red;
str = "<>";
}
else if(playerPos[0] == i) //如果玩家A在该下标位置
{
Console.ForegroundColor = ConsoleColor.Red;
str = "A";
}
else if(playerPos[1] == i) //如果玩家B在该下标位置
{
Console.ForegroundColor = ConsoleColor.Red;
str = "B";
}
//如果不是A或者B在这个位置开始画图案:
//0方框 1幸运轮盘◎ 2地雷☆ 3暂停▲ 4时空隧道卐
else if(maps[i] ==0)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
str = "□";
}
else if(maps[i] == 1)
{
Console.ForegroundColor = ConsoleColor.Green;
str = "◎";
}
else if(maps[i] == 2)
{
Console.ForegroundColor = ConsoleColor.Magenta;
str = "☆";
}
else if(maps[i] == 3)
{
Console.ForegroundColor = ConsoleColor.Blue;
str = "▲";
}
else if(maps[i] == 4)
{
Console.ForegroundColor = ConsoleColor.White;
str = "卐";
}
return str;
}
2)然后开始画地图:
/// <summary>
/// 显示地图
/// </summary>
public static void ShowMap()
{
Console.ForegroundColor = ConsoleColor.Gray;
//显示地图提示:幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:卐
Console.WriteLine("图例: 幸运轮盘◎ 地雷☆ 暂停▲ 时空隧道卐");
//开始显示地图
#region 显示第一个横行
for(int i=0;i<30;i++)
{
Console.Write(DrawMap(i));
}
#endregion
Console.WriteLine(); //画完横行后换行
#region 显示第一个竖行
for(int i=30;i<35;i++)
{
for(int j=0;j<29;j++)
{
Console.Write(" ");
}
Console.WriteLine(DrawMap(i));
}
#endregion
#region 显示第二个竖行
for(int i=64;i>34;i--)
{
Console.Write(DrawMap(i));
}
#endregion
Console.WriteLine(); //画完横行后换行
#region 显示第二个竖行
for(int i = 65; i<70;i++)
{
Console.WriteLine(DrawMap(i));
}
#endregion
#region 显示第三个横行
for(int i = 70; i<100;i++)
{
Console.Write(DrawMap(i));
}
#endregion
Console.WriteLine(); //画完横行后换行
}
/// <summary>
/// 某玩家玩游戏
/// </summary>
/// <param name="playerNumber">玩家0或1</param>
public static void PlayGame(int playerNumber)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("玩家“{0}”按任意键开始掷骰子",PlayerName[playerNumber]);
Console.ReadKey(true);
Random r = new Random(); //创建能产生随机数的对象
int number = r.Next(1,7);
Console.WriteLine("玩家“{0}”掷出了“{1}”",PlayerName[playerNumber],number);
Console.WriteLine("玩家“{0}”按任意键开始行动",PlayerName[playerNumber]);
Console.ReadKey(true);
playerPos[playerNumber] += number;
Console.WriteLine("玩家“{0}”行动完成了",PlayerName[playerNumber]);
Console.ReadKey(true);
if(playerPos[playerNumber] == playerPos[1-playerNumber])
{ //如果踩到了对方
Console.WriteLine("玩家“{0}”踩到了玩家“{1}”,玩家“{2}”退6格",
PlayerName[playerNumber],PlayerName[1-playerNumber],PlayerName[1-playerNumber]);
Console.ReadKey(true);
playerPos[1-playerNumber] -= 6;
}
else if(maps[playerPos[playerNumber]] == 0)
{ //如果该玩家的坐标位置的maps值为0,说明踩到了方框
Console.WriteLine("玩家“{0}”踩到了方框,什么都不发生",PlayerName[playerNumber]);
Console.ReadKey(true);
}
else if(maps[playerPos[playerNumber]] == 1)
{ //如果该玩家的坐标位置的maps值为1,说明踩到了幸运轮盘
Console.WriteLine("玩家“{0}”踩到了方框幸运轮盘,请选择 1--交换位置 2--轰炸对方",PlayerName[playerNumber]);
string choose = Console.ReadLine();
while(true)
{
if(choose =="1")
{
Console.WriteLine("玩家“{0}”选择了跟玩家“{1}”交换位置",
PlayerName[playerNumber],PlayerName[1-playerNumber]);
int temp = playerPos[playerNumber];
playerPos[playerNumber] = playerPos[1-playerNumber];
playerPos[1-playerNumber] = temp;
Console.ReadKey(true);
break;
}
else if(choose =="2")
{
Console.WriteLine("玩家“{0}”选择了轰炸对方,玩家“{1}”退6格",
PlayerName[playerNumber],PlayerName[1-playerNumber]);
playerPos[1-playerNumber] -=6;
Console.ReadKey(true);
break;
}
else
{
Console.WriteLine("输入有误,请重新输入 1--交换位置 2--轰炸对方");
choose = Console.ReadLine();
} //if
} //while
} // else if
else if(maps[playerPos[playerNumber]] == 2)
{ //如果该玩家的坐标位置的maps值为2,说明踩到了地雷
Console.WriteLine("玩家“{0}”踩到了地雷,退6格",PlayerName[playerNumber]);
playerPos[1-playerNumber] -= 6;
Console.ReadKey(true);
}
else if(maps[playerPos[playerNumber]] == 3)
{ //如果该玩家的坐标位置的maps值为3,说明踩到了暂停
Console.WriteLine("玩家“{0}”踩到了暂停,暂停一回合",PlayerName[playerNumber]);
Flag[playerNumber] = true;
Console.ReadKey(true);
}
else if(maps[playerPos[playerNumber]] == 4)
{ //如果该玩家的坐标位置的maps值为4,说明踩到了时空隧道
Console.WriteLine("玩家“{0}”踩到了时空隧道,前进10格",PlayerName[playerNumber]);
playerPos[playerNumber] += 10;
Console.ReadKey(true);
}
ChangPos(); //坐标发生改变后,确保坐标不会出界
Console.Clear(); //每个玩家玩完后清屏
ShowMap(); //重新显示地图
}
5. 最后开始写Main函数,完成游戏:
GameHead(); //画游戏头
Console.ForegroundColor = ConsoleColor.Gray; //改变输入玩家姓名的颜色为灰色
#region 输入玩家姓名
Console.WriteLine("请输入玩家A的姓名");
PlayerName[0] = Console.ReadLine();
while(PlayerName[0] == "")
{
Console.WriteLine("玩家姓名不能为空,请重新输入玩家A的姓名");
PlayerName[0] = Console.ReadLine();
}
Console.WriteLine("请输入玩家B的姓名");
PlayerName[1] = Console.ReadLine();
while(PlayerName[1] == "" || PlayerName[1] == PlayerName[0])
{
if(PlayerName[1] == "")
{
Console.WriteLine("玩家姓名不能为空,请重新输入玩家B的姓名");
PlayerName[1] = Console.ReadLine();
}
else
{
Console.WriteLine("玩家姓名不能一样,请重新输入玩家B的姓名");
PlayerName[1] = Console.ReadLine();
}
}
#endregion
Console.Clear();//输入成功后清屏
GameHead(); //重新画游戏头
Console.ForegroundColor = ConsoleColor.Cyan; //改文本为青色
Console.WriteLine("玩家“{0}”的士兵用A表示",PlayerName[0]);
Console.WriteLine("玩家“{0}”的士兵用B表示",PlayerName[1]);
InitialMap(); //初始化地图字符的内容
ShowMap(); //把初始化好的地图显示出来
while(playerPos[0] <99 && playerPos[1]<99)
{ //开始游戏,两个玩家的坐标一直玩直到到达终点(下标>=99)
if(Flag[0] == false) //A玩家玩
PlayGame(0);
else //如果踩到暂停,下个回合才能玩
Flag[0] = false;
if(playerPos[0] >=99)
{ //A玩家玩完后判断是否到达终点
Console.WriteLine("玩家“{0}”无耻的赢了“{1}”",
PlayerName[0],PlayerName[1]);
break;
}
if(Flag[1] == false) //轮到B玩家玩
PlayGame(1);
else //如果踩到暂停,下个回合才能玩
Flag[1] = false;
if(playerPos[1] >=99)
{ //A玩家玩完后判断是否到达终点
Console.WriteLine("玩家“{0}”无耻的赢了“{1}”",
PlayerName[1],PlayerName[0]);
break;
}
}
Win(); //显示最后胜利的画面
Console.ReadKey();