您的位置:首页 > 其它

飞行棋思路整理

2013-10-27 08:55 302 查看

飞行棋 笔记

输出游戏说明

一、显示游戏名称 ShowUI()

 

static void ShowUI()
{
Console.WriteLine("*********************************************");
Console.WriteLine("*                                           *");
Console.WriteLine("*          骑   士   飞   行   棋           *");
Console.WriteLine("*                                           *");
Console.WriteLine("*********************************************");
}


二、输入游戏用户名

 

//获得玩家A、B名字
Console.WriteLine("请输入玩家A的姓名?");
names[0] = Console.ReadLine();
while (names[0] == "")
{
Console.WriteLine("玩家A的姓名不能为空请重新输入!");
names[0] = Console.ReadLine();
}
Console.WriteLine("请输入玩家B的姓名");
names[1] = Console.ReadLine();
while (names[1] == "" || names[1] == names[0])
{
if (names[1] == "")
{
Console.WriteLine("玩家B的姓名不能为空请重新输入!");
names[1] = Console.ReadLine();
}
else
{
Console.WriteLine("此名字已经被玩家A占用!请重新输入");
names[1] = Console.ReadLine();
}
}


三、清屏及游戏说明

 

Console.Clear();//清屏
ShowUI();//重绘LOGO
Console.WriteLine("对战开始......................");
Console.WriteLine("玩家{0}用A表示,玩家{1}用B表示", names[0], names[1]);
Console.WriteLine("如果AB在同一位置,则用<>来表示");
InitialMap();
DrawMap();
Console.WriteLine("对战开始......");


 

 制作游戏地图

一、定义数组存储地图上的关卡

 

 

class Program
{
//用下面的数组存储游戏地图各个关卡
//在数组中用  ◎表示幸运轮盘=1
//            ☆表示地雷=2
//            ▲表示暂停=3
//            卐表示时空隧道=4
//            □表示普通=0
static int[] Map = new int[100];//游戏地图的坐标
static int[] playerPos = { 0, 0 };//玩家A、B的初始坐标为0
static string[] names = new string[2];//声明一个长度为2的数组存放两个玩家的名字
static bool[] isRest = { false, false };//isRest[0],isRest[1]分别表示玩家A、B上一次是否走到了暂停

static void Main(string[] args)
{


二、地图初始化InitialMap()

 

static void InitialMap()
{
//绘制普通图标
for (int i = 0; i < Map.Length; i++)
{
Map[i] = 0;
}
int[] luckTurn = { 19, 24, 33, 47, 66, 78, 93 };//幸运轮盘的坐标
int[] landMine = { 5, 13, 26, 44, 57, 69, 87 };//地雷的坐标
int[] pause = { 9, 27, 60, 93 };//暂停的坐标
int[] timeTunnel = { 13, 25, 45, 63, 72, 88 };//时空隧道的坐标
for (int i = 0; i < luckTurn.Length; i++)
{
int pos = luckTurn[i];
Map[pos] = 1;
}
for (int i = 0; i < landMine.Length; i++)
{
int pos = landMine[i];
Map[pos] = 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;
}

}


 

三、地图走势示意步骤说明

 

四、初步绘制地图 DrawMap()

 

 

static void DrawMap()
{
Console.WriteLine("图例:幸运轮盘◎ 地雷☆ 暂停▲ 时空隧道卐");
# region//画第一行
for (int i = 0; i < 30; i++)
{
Console.Write(GetMapString(i));
}
Console.WriteLine();

#endregion
#region//画第第一行之后的列
for (int i = 30; i < 35; i++)
{
for (int j = 0; j < 29; j++)
{
Console.Write("  ");
}
Console.WriteLine(GetMapString(i));

}

#endregion
# region//画第二行
for (int i = 64; i >= 35; i--)
{
Console.Write(GetMapString(i));
}
Console.WriteLine();

#endregion
#region//画第第二行之后的列
for (int i = 65; i < 70; i++)
{
Console.WriteLine(GetMapString(i));
}
#endregion
# region//画最后一行
for (int i = 70; i < 100; i++)
{
Console.Write(GetMapString(i));

}
Console.WriteLine("");
#endregion
Console.ResetColor();
}


 

五、绘制地图上的图案GetMapString() 

注意理解:用返回值,不用参数

 

六、重新绘制地图DrawMap()

 

/// <summary>
/// 获得pos坐标上要绘制的图案
/// </summary>
/// <param name="pos">要绘制的坐标</param>
/// <returns></returns>
static string GetMapString(int pos)
{
string result = "";
if (playerPos[0] == pos && playerPos[1] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
result = "<>";
}
else if (playerPos[0] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
result = "A";
}
else if (playerPos[1] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
result = "B";
}
else
{
switch (Map[pos])
{
case 0:
Console.ForegroundColor = ConsoleColor.White;
result = "□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Red;
result = "◎";
break;
case 2:
Console.ForegroundColor = ConsoleColor.Green;
result = "☆";
break;
case 3:
Console.ForegroundColor = ConsoleColor.Blue;
result = "▲";
break;
case 4:
Console.ForegroundColor = ConsoleColor.Magenta;
result = "卐";
break;
}
}
return result;

}


七、输出图例说明

 

Console.Clear();//清屏
ShowUI();//重绘LOGO
Console.WriteLine("对战开始......................");
Console.WriteLine("玩家{0}用A表示,玩家{1}用B表示", names[0], names[1]);
Console.WriteLine("如果AB在同一位置,则用<>来表示");
InitialMap();
DrawMap();
Console.WriteLine("对战开始......");


 

八、调色

 Console.ResetColor();

注意这里可能出现的bug

产生随机数Random

改坐标后,重绘地图(循环)

 

一、按任意键掷骰子的条件

 

Console.ReadKey(true);

 

二、如何产生随机数Random

   Random r = new Random();

   step = r.Next(1, 7);//产生一个1-6之间的随机整数  

三、随机数后台代码

 

Console.WriteLine("{0}按任意键开始掷骰子......", names[0]);
ConsoleKeyInfo rec = Console.ReadKey(true);//游戏后门代码
if (rec.Key == ConsoleKey.Tab)//如玩家按下了Tab键
{
step = 20;//玩家前进2步
}
else
{
step = r.Next(1, 7);//产生一个1-6之间的随机整数
}
Console.WriteLine("{0}掷出了{1}", names[0], step);


玩家A掷骰子全过程

一、坐标越界判断CheckPos();

static void CheckPos()
{
for (int i = 0; i < 2; i++)
{
if (playerPos[i] > 99)
{
playerPos[i] = 99;
}
if (playerPos[i] < 0)
{
playerPos[i] = 0;
}
}
}


二、判断玩家的位置情况

 

static string GetMapString(int pos)
{
string result = "";
if (playerPos[0] == pos && playerPos[1] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
result = "<>";
}
else if (playerPos[0] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
result = "A";
}
else if (playerPos[1] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
result = "B";
}
else
{
switch (Map[pos])
{
case 0:
Console.ForegroundColor = ConsoleColor.White;
result = "□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Red;
result = "◎";
break;
case 2:
Console.ForegroundColor = ConsoleColor.Green;
result = "☆";
break;
case 3:
Console.ForegroundColor = ConsoleColor.Blue;
result = "▲";
break;
case 4:
Console.ForegroundColor = ConsoleColor.Magenta;
result = "卐";
break;
}
}
return result;

}


三、幸运轮盘 ReadInt()

 

case 1:
//走到了 幸运轮盘
Console.Clear();
DrawMap();
Console.WriteLine("你走到了幸运轮盘,请选择运气1/2");
Console.WriteLine("1—交换位置,2—轰炸对方");
input = Console.ReadLine();
while (input != "1" && input != "2")
{

Console.WriteLine("只能输入1或2!请重新输入");
input = Console.ReadLine();

}
if (input == "1")
{
middle = playerPos[0];
playerPos[0] = playerPos[1];
playerPos[1] = middle;
msg = string.Format("{0}选择了与对方交换位置!", names[0]);
}
else
{
playerPos[1] -= 6;
CheckPos();
msg = string.Format("{0}选择了轰炸对方,{1}后退六格!", names[0], names[1]);
}
break;


四、踩到地雷和时空隧道

 

case 2:
//踩到了地雷
playerPos[0] -= 6;
CheckPos();
msg = string.Format("{0}踩到了地雷,后退六格!", names[0]);
break;

case 4:
//时空隧道
playerPos[0] += 10;
CheckPos();
msg = string.Format("{0}进入了时空隧道,前进十步!", names[0]);
break;


五、报告玩家位置

 

Console.WriteLine("{0}掷出了{1}行动完成!!!", names[0], step);
Console.WriteLine("************************玩家位置分别如下*************************");
Console.WriteLine("{0}的位置为{1},{2}的位置为{3}", names[0], playerPos[0] + 1, names[1], playerPos[1] + 1);


六、Main()执行过程

 

            ShowUI();
#region//获得玩家A、B名字 Console.WriteLine("请输入玩家A的姓名?"); names[0] = Console.ReadLine(); while (names[0] == "") { Console.WriteLine("玩家A的姓名不能为空请重新输入!"); names[0] = Console.ReadLine(); } Console.WriteLine("请输入玩家B的姓名"); names[1] = Console.ReadLine(); while (names[1] == "" || names[1] == names[0]) { if (names[1] == "") { Console.WriteLine("玩家B的姓名不能为空请重新输入!"); names[1] = Console.ReadLine(); } else { Console.WriteLine("此名字已经被玩家A占用!请重新输入"); names[1] = Console.ReadLine(); } }
#endregion
Console.Clear();//清屏
ShowUI();//重绘LOGO
Console.WriteLine("对战开始......................");
Console.WriteLine("玩家{0}用A表示,玩家{1}用B表示", names[0], names[1]);
Console.WriteLine("如果AB在同一位置,则用<>来表示");
InitialMap();
DrawMap();
Console.WriteLine("对战开始......");
//这个循环中让玩家A和B轮流掷骰子 当玩家A或者玩家B的坐标大于99时,游戏结束
while (playerPos[0] < 99 && playerPos[1] < 99)
{
//玩家A掷骰子
if (isRest[0] == false)
{
Action(0);

}

else
{
isRest[0] = false;
}
if (playerPos[0] >= 99)
{ break; }
#region//玩家B掷骰子 if (isRest[1] == false) { Action(1); } else { isRest[1] = false; }
#endregion
}
if (playerPos[0] >= 99)
{
Console.WriteLine("游戏结束!{0}胜利了!!!!!!!!", names[0]);
}
else
{
Console.WriteLine("游戏结束!{0}胜利了!!!!!!!!", names[1]);
}
Console.ReadKey();
}


七、输出关卡信息

 

 注意:为什么这里不能输出显示?

因为后面清屏了,用户看不到

 

 

所以,这样写:

 

string input = "", msg = "";//input 存放玩家输入,msg存放关卡消息,重新绘制地图后显示

最后,输出显示msg

 

 

第5课 输出游戏结果

一、玩家B掷骰子

 

//玩家B掷骰子
if (isRest[1] == false)
{
Action(1);
}
else
{
isRest[1] = false;
}


二、控制跳出循环输出游戏结果

if (playerPos[0] >= 99)
{ break; }


if (playerPos[0] >= 99)
{
Console.WriteLine("游戏结束!{0}胜利了!!!!!!!!", names[0]);
}
else
{
Console.WriteLine("游戏结束!{0}胜利了!!!!!!!!", names[1]);
}


三、游戏后门代码(上面随机数处可见) 

四、暂停一次代码

 

static bool[] isRest = { false, false };//isRest[0],isRest[1]分别表示玩家A、B上一次是否走到了暂停
if (isRest[0] == false)
{
Action(0);

}

else
{
isRest[0] = false;
}


第6课 掷骰子方法Action()

一、调用Action ()

 

static void Action(int playerNumber)
{
string input = "", msg = "";//input 存放玩家输入,msg存放关卡消息,重新绘制地图后显示
Random r = new Random();//new一个随机数
int step = 0, middle = 0;//step玩家掷出的数,middle玩家踩到幸运轮盘时坐标互换的中间数
Console.WriteLine("{0}按任意键开始掷骰子......", names[playerNumber]);
ConsoleKeyInfo rec = Console.ReadKey(true);//游戏后门代码
if (rec.Key == ConsoleKey.Tab)//如玩家按下了Tab键
{
step = 20;//玩家前进2步
}
else
{
step = r.Next(1, 7);//产生一个1-6之间的随机整数
}
Console.WriteLine("{0}掷出了{1}", names[playerNumber], step);
Console.WriteLine("按任意键开始行动.....");
Console.ReadKey(true);
playerPos[playerNumber] += step;//坐标发生改变,重新绘制地图
CheckPos();//检查坐标是否越界
if (playerPos[playerNumber] == playerPos[1 - playerNumber])//玩家A踩到玩家B
{
msg = string.Format("玩家{0}踩到了玩家{1},玩家{2}退回原点!!", names[playerNumber], names[1 - playerNumber], names[1 - playerNumber]);
playerPos[1 - playerNumber] = 0;
}
else//判断玩家是否踩到其他关卡
{
switch (Map[playerPos[playerNumber]])
{
case 0:
//普通,没有效果0
msg = "";
break;
case 1:
//走到了 幸运轮盘
Console.Clear();
DrawMap();
break;***
case 2:
//踩到了地雷
playerPos[playerNumber] -= 6;
CheckPos();
msg = string.Format("{0}踩到了地雷,后退六格!", names[playerNumber]);
break;
case 3:
//暂停一次
isRest[playerNumber] = true;
msg = string.Format("{0}到了暂停点,休息一回合!", names[playerNumber]);
break;
case 4:
//时空隧道
playerPos[playerNumber] += 10;
CheckPos();
msg = string.Format("{0}进入了时空隧道,前进十步!", names[playerNumber]);
break;
}


二、调整Action ()的变量

三、设置Action ()的参数

调试运行

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: