C#实现简单的飞行棋小游戏
程序员文章站
2022-06-17 20:05:16
本文实例为大家分享了c#实现简单飞行棋小游戏的具体代码,供大家参考,具体内容如下1.玩家姓名的输入2.对屏幕进行清屏3.初始化地图4.玩家a和玩家b玩游戏using system;namespace...
本文实例为大家分享了c#实现简单飞行棋小游戏的具体代码,供大家参考,具体内容如下
1.玩家姓名的输入
2.对屏幕进行清屏
3.初始化地图
4.玩家a和玩家b玩游戏
using system; namespace homework { class data { //静态字段来模拟全局变量 static int[] maps = new int[100]; //声明一个静态数组用来存储玩家a跟玩家b的坐标 static int[] playerpos = new int[2]; static string[] playernames = new string[2]; //两个玩家的标记 static bool[] flags = new bool[2];//flags[0]默认是false public static void main(string[] args) { gameshow(); //玩家姓名的输入 #region console.writeline("请输入玩家a的姓名:"); playernames[0] = console.readline(); while (playernames[0] == "") { console.writeline("玩家a的姓名不能为空,请重新输入"); playernames[0] = console.readline(); } console.writeline("请输入玩家b的姓名:"); playernames[1] = console.readline(); while (playernames[1] == "" || playernames[1] == playernames[0]) { if (playernames[0] == "") { console.writeline("玩家a的姓名不能为空,请重新输入"); playernames[1] = console.readline(); } else { console.writeline("玩家b的姓名不能与玩家a相同"); playernames[1] = console.readline(); } } #endregion //对屏幕进行清屏 #region console.clear();//清屏 gameshow(); console.writeline("{0}的士兵用a表示", playernames[0]); console.writeline("{0}的士兵用b表示", playernames[1]); #endregion //画地图之前 初始化地图 #region initailmap(); drawmap(); #endregion //当玩家a跟玩家b没有一个人在终点时候 两个玩家不停地去玩游戏 #region while (playerpos[0] < 99 && playerpos[0] < 99) { if (flags[0] == false) { playgame(0); } else { flags[0] = false; } if (playerpos[0] > 99) { console.writeline("玩家{0}赢了玩家{1}", playernames[0], playernames[1]); break; } if (flags[1] == false) { playgame(1); } else { flags[1] = false; } if (playerpos[0] > 99) { console.writeline("玩家{0}赢了玩家{1}", playernames[0], playernames[1]); break; } }//while #endregion console.writeline("游戏结束!!!"); console.readline(); } //画地图头 #region public static void gameshow() { console.foregroundcolor = consolecolor.gray; console.writeline("*************************************"); console.writeline("*************************************"); console.writeline("****************飞行棋***************"); console.writeline("*************************************"); console.writeline("*************************************"); } #endregion //初始化地图 #region public static void initailmap() { int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘 for (int i = 0; i < luckyturn.length; i++) { maps[luckyturn[i]] = 1; } int[] landmine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷 for (int i = 0; i < landmine.length; i++) { maps[landmine[i]] = 2; } int[] pause = { 2,3,4,9, 27, 60, 93 };//暂停 for (int i = 0; i < pause.length; i++) { maps[pause[i]] = 3; } int[] timetunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道 for (int i = 0; i < timetunnel.length; i++) { maps[timetunnel[i]] = 4; } } #endregion //画地图 #region public static void drawmap() { console.writeline("图例:幸运圆圈:○ 地雷:☆ 暂停:▽ 时空隧道:◎"); //第一横行 for (int i = 0; i < 30; i++) { //如果玩家a跟玩家b的坐标相同,画一个尖括号 console.write(drawstringmap(i)); }//for console.writeline(); // 第一竖行 for (int i = 30; i < 35; i++) { for (int j = 0; j <= 28; j++) { console.write(" "); } console.writeline(drawstringmap(i)); } //第二横行 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(); } #endregion //画地图中抽象出一个方法 #region public static string drawstringmap(int i) { string str = ""; if (playerpos[0] == playerpos[1] && playerpos[1] == i) { str = "<>"; } else if (playerpos[0] == i) { str = "a"; } else if (playerpos[1] == i) { str = "b"; } else { switch (maps[i]) { case 0: str = "□"; break; case 1: str = "○"; break; case 2: str = "☆"; break; case 3: str = "▽"; break; case 4: str = "◎"; break; }//switch }//else return str; } #endregion //双方掷骰子过程 #region public static void playgame(int playernumber) { random r = new random(); int rnumber = r.next(1, 7); console.writeline("{0}按人任意键开始掷骰子", playernames[playernumber]); console.readkey(true); console.writeline("{0}掷骰子掷出了{1}", playernames[playernumber], rnumber); playerpos[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},玩家{1}退6格", playernames[playernumber], playernames[1 - playernumber]); playerpos[1 - playernumber] -= 6; console.readkey(true); } 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]); console.readkey(true); int temp = playerpos[playernumber]; playerpos[playernumber] = playerpos[1 - playernumber]; playerpos[1 - playernumber] = temp; console.writeline("交换完成!!!按任意键继续游戏!!!"); console.readkey(true); break; } else if (input == "2") { console.writeline("玩家{0}选择轰炸玩家{1},玩家{1}退6格", playernames[playernumber], playernames[1 - playernumber]); console.readkey(true); playerpos[1] -= 6; console.writeline("玩家{0}退了6格", playernames[1 - playernumber]); console.readkey(true); break; } else { console.writeline("只能输入1或者2 1--交换位置 2--轰炸对方"); input = console.readline(); } } console.readkey(true); break; case 2: console.writeline("玩家{0}踩到了地雷,退6格", playernames[playernumber]); console.readkey(true); playerpos[1 - playernumber] -= 6; console.writeline("玩家{0}退了6格", playernames[playernumber]); console.readkey(true); break; case 3: console.writeline("玩家{0}踩到了暂停,暂停一回合", playernames[playernumber]); flags[playernumber] = true; console.readkey(true); break; case 4: console.writeline("玩家{0}踩到了时空隧道,前进10格", playernames[playernumber]); console.readkey(true); playerpos[playernumber] += 10; break; }//switch }//else changepos(); console.clear(); drawmap(); } #endregion //当玩家坐标发生改变需要判断两者是否超出范围 #region public static void changepos() { if (playerpos[0] < 0) { playerpos[0] = 0; } if (playerpos[0] >= 99) { playerpos[0] = 99; } if (playerpos[1] < 0) { playerpos[1] = 0; } if (playerpos[1] > 99) { playerpos[1] = 99; } } #endregion } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: 黑无常叫什么?关于黑无常有哪些传说故事?