跟着教程写了一个飞行棋控制台小游戏(
效果图
代码参考
namespace 飞行棋项目 {
class Game {
//使用静态字段模拟全局变量
//游戏地图数组
//默认全0
public static int[] Maps = new int[100];
//存储玩家的坐标
public static int[] PlayerPos = new int[2];
//存储图标映射
public static String[] Icon = new String[5] { "□", "◎", "☆", "▲", "卐" };
//存储玩家姓名
public static String[] PlayerNames = new String[2];
//存储玩家游戏暂停状态
public static bool[] PlayerPauseStatus = new bool[2]; //默认false
public static void Main(String[] args) {
MainMenu();
Console.Write("\n请输入玩家A的姓名:");
PlayerNames[0] = Console.ReadLine();
while (PlayerNames[0] == "")
{
Console.Write("玩家A的姓名不能为空,请重新输入:");
PlayerNames[0] = Console.ReadLine();
}
Console.Write("请输入玩家B的姓名:");
PlayerNames[1] = Console.ReadLine();
while (PlayerNames[1] == "" || PlayerNames[1] == PlayerNames[0])
{
if(PlayerNames[1] == "")
Console.Write("玩家B的姓名不能为空,请重新输入:");
if (PlayerNames[1] == PlayerNames[0])
Console.Write("玩家姓名重复,请重新输入:");
PlayerNames[1] = Console.ReadLine();
}
//清屏
Console.Clear();
MainMenu();
Console.WriteLine("{0}玩家用A表示",PlayerNames[0]);
Console.WriteLine("{0}玩家用B表示", PlayerNames[1]);
InitGameMap();
DrawMap();
//开始循环,即当玩家A和玩家B没有一个人在终点时候,两个玩家不停的去玩游戏
while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
{
//判断是否处于暂停状态
if (!PlayerPauseStatus[0])
{
PlayGame(0);
//判断是否获胜
if (PlayerPos[0] >= 99)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("玩家{0}获胜", PlayerNames[0]);
Console.ForegroundColor = ConsoleColor.White;
break;
}
}
else
{
Console.WriteLine("玩家{0}处于坐牢状态,所以这把是{1}的回合", PlayerNames[0], PlayerNames[1]);
//跳过当前回合并重置暂停状态
PlayerPauseStatus[0] = false;
}
if (!PlayerPauseStatus[1])
{
PlayGame(1);
if (PlayerPos[1] >= 99)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("玩家{0}获胜", PlayerNames[1]);
Console.ForegroundColor = ConsoleColor.White;
break;
}
}
else
{
Console.WriteLine("玩家{0}处于坐牢状态,所以这把是{1}的回合", PlayerNames[1], PlayerNames[0]);
PlayerPauseStatus[1] = false;
}
}//while
}
/// <summary>
/// 输出主菜单标题
/// </summary>
public static void MainMenu()
{
Console.ForegroundColor= ConsoleColor.Green ;
Console.WriteLine("+++++++++++++++++++++++++");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("+++++++++++++++++++++++++");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("++++++飞行棋小游戏+++++++");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("+++++++++++++++++++++++++");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("+++++++++++++++++++++++++");
Console.ForegroundColor = ConsoleColor.White;
}
/// <summary>
/// 初始化地图
/// </summary>
public static void InitGameMap() {
int[] luckyturn = { 6, 23, 40, 55, 69, 83 };// 1幸运轮盘😊
for (int i = 0; i < luckyturn.Length; i++)
{
Maps[luckyturn[i]] = 1;
}
int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//2地雷💣
for (int i = 0; i < landMine.Length; i++)
{
Maps[landMine[i]] = 2;
}
int[] pause = { 9, 27, 60, 93 };//3暂停回合😅
for (int i = 0; i < pause.Length; i++)
{
Maps[pause[i]] = 3;
}
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//4时空隧道🚀
for (int i = 0; i < timeTunnel.Length; i++)
{
Maps[timeTunnel[i]] = 4;
}
//遍历
}
/// <summary>
/// 打印地图 □◎☆▲卐
/// </summary>
public static void DrawMap() {
//画图例
Console.WriteLine("图例:幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:卐");
//第一横行
for (int i = 0; i < 30; i++)
{
Console.Write(DrawStringMap(i));
}
//第一竖行
for (int i = 30; i < 35 ; i++)
{
Console.Write("\n");
//使用空格填充
for (int j = 0; j <= 28; j++)
{
Console.Write(" ");
}
//画真正的地图
Console.Write(DrawStringMap(i));
}
//手动换一行
Console.WriteLine();
//第二横行
for (int i = 64; i >= 35; i--)
{
Console.Write(DrawStringMap(i));
}
//换行
Console.WriteLine();
//第二竖行
for (int i = 65; i <= 69; i++)
{
Console.WriteLine(DrawStringMap(i));
}
//第三横行
for (int i = 70; i <= 99; i++)
{
Console.Write(DrawStringMap(i));
}
//换行
Console.WriteLine();
}
/// <summary>
/// 返回字符串地图图标
/// </summary>
/// <param name="i">坐标</param>
/// <returns>返回图标字符串</returns>
public static String DrawStringMap(int i)
{
string s = null;
//如果玩家A和玩家B的坐标相同,且未越界,画一个尖括号
if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)
{
Console.ForegroundColor = ConsoleColor.Green;
s = "<>";
}
else if (PlayerPos[0] == i)
{
Console.ForegroundColor = ConsoleColor.Red;
s = "A";
}
else if (PlayerPos[1] == i)
{
Console.ForegroundColor = ConsoleColor.Blue;
s = "B";
}
else
{
Console.ForegroundColor = ConsoleColor.White;
//输出地图图标
s = Icon[Maps[i]];
}
return s;
}
/// <summary>
/// 玩游戏主方法
/// </summary>
public static void PlayGame(int playerNumber) {
Random rand = new Random();
int rNumber = rand.Next(1, 7);
Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[playerNumber]);
//按下的键不显示在控制台上
Console.ReadKey(true);
Console.WriteLine("{0}掷出了{1}", PlayerNames[playerNumber],rNumber);
ChangePos(playerNumber, rNumber);
Console.ReadKey(true);
Console.WriteLine("{0}按任意键开始行动", PlayerNames[playerNumber]);
Console.ReadKey(true);
Console.WriteLine("{0}行动完成", PlayerNames[playerNumber]);
Console.ReadKey(true);
//玩家A有可能猜到了玩家B 或者方块 地雷等等
if (PlayerPos[playerNumber] == PlayerPos[1-playerNumber])
{
Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退6格", PlayerNames[playerNumber], PlayerNames[1-playerNumber], PlayerNames[1-playerNumber]);
ChangePos(1-playerNumber, -6);
Console.ReadKey();
}
else //踩到了关卡
{
switch (Maps[PlayerPos[playerNumber]])
{
case 0:
Console.WriteLine("玩家{0}踩到了方块,什么也没发生", PlayerNames[playerNumber]);
Console.ReadKey(true);
break;
case 1:
Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择1:交换位置,2:轰炸对方", PlayerNames[playerNumber]);
string input = Console.ReadLine();
while (true)
{
if (input == "1")
{
Console.WriteLine("玩家{0}选择和玩家{1}交换位置", PlayerNames[playerNumber], PlayerNames[1-playerNumber]);
int tmp = PlayerPos[playerNumber];
PlayerPos[playerNumber] = PlayerPos[1-playerNumber];
PlayerPos[1-playerNumber] = tmp;
Console.ReadKey(true);
Console.WriteLine("交换完成,按任意键继续游戏");
Console.ReadKey(true);
break;
}
else if (input == "2")
{
Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{2}退六格", PlayerNames[playerNumber], PlayerNames[1-playerNumber], PlayerNames[1-playerNumber]);
Console.ReadKey(true);
ChangePos(1-playerNumber, -6);
Console.WriteLine("玩家{0}退了6格", PlayerNames[1-playerNumber]);
Console.ReadKey(true);
break;
}
else
{
Console.WriteLine("请输入正确的数值!");
input = Console.ReadLine();
}
}
break;
case 2:
Console.WriteLine("玩家{0}踩到了地雷,退6格", PlayerNames[playerNumber]);
ChangePos(playerNumber, -6);
Console.ReadKey(true);
break;
case 3:
Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerNames[playerNumber]);
PlayerPauseStatus[playerNumber] = true;
Console.ReadKey(true);
break;
case 4:
Console.WriteLine("玩家{0}踩到了时空隧道,前进十格",PlayerNames[playerNumber]);
ChangePos(playerNumber, 10);
Console.ReadKey(true);
break;
}//switch
}//else
Console.Clear();
DrawMap();
}
/// <summary>
/// 当玩家坐标发生改变时调用此方法
/// </summary>
/// <param name="playerNumber">玩家序号</param>
/// <param name="step">前进的步长,如是负数,为后退</param>
public static void ChangePos(int playerNumber,int step)
{
PlayerPos[playerNumber] += step;
if (PlayerPos[0] < 0)
{
PlayerPos[0] = 0;
}
if (PlayerPos[1] < 0)
{
PlayerPos[1] = 0;
}
//如果A越界
if (PlayerPos[0] >= 99)
{
PlayerPos[0] = 99;
}
//如果B越界
if (PlayerPos[1] >= 99)
{
PlayerPos[1] = 99;
}
}
}
}