C#飞行棋小程序设计代码
程序员文章站
2022-03-26 08:29:12
飞行棋游戏大家应该都玩过吧,如何使用c#语言进行编写,本文实例就为大家分享了飞行棋c#实现代码,供大家参考,具体内容如下
using system;
using...
飞行棋游戏大家应该都玩过吧,如何使用c#语言进行编写,本文实例就为大家分享了飞行棋c#实现代码,供大家参考,具体内容如下
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace pachee { class program { #region 静态字段 // 关卡数量 public static int[] maps = new int[100]; // 玩家坐标 public static int[] playerpos = new int[2]; // 玩家名称 public static string[] playernames = new string[2]; // 判断玩家是否暂停 public static bool[] flags = new bool[2]; #endregion /// <summary> /// 输出游戏头 /// </summary> public static void showgame() { console.foregroundcolor = consolecolor.green; console.writeline("****************************"); console.foregroundcolor = consolecolor.blue; console.writeline("****************************"); console.foregroundcolor = consolecolor.white; console.writeline("***c#基础练习:飞行棋项目***"); console.foregroundcolor = consolecolor.yellow; console.writeline("****************************"); console.foregroundcolor = consolecolor.red; console.writeline("****************************"); } /// <summary> /// 接受用户输入的游戏名称,判断是否合法 /// </summary> /// <returns>游戏名称</returns> public static string[] inputplayernames() { playernames[0] = ""; playernames[1] = ""; console.foregroundcolor = consolecolor.white; while (playernames[0] == "") { console.write("please enter the name of game a player: "); playernames[0] = console.readline().trim(); if (playernames[0] == "") { console.writeline("a player name cannot be empty, please enter again."); continue; } break; } while (playernames[1] == "" || playernames[0] == playernames[1]) { console.write("please enter the name of game b player: "); playernames[1] = console.readline().trim(); if (playernames[1] == "") { console.writeline("b player name cannot be empty, please enter again."); continue; } else if (playernames[1] == playernames[0]) { console.writeline("the player name cannot be the same as the player a b, please enter again."); continue; } break; } return playernames; } /// <summary> /// 初始化地图,改变默认的地图坐标类型 /// 0:方块 /// 1:轮盘 /// 2:地雷 /// 3:暂停 /// 4:隧道 /// </summary> public static void initailmap() { #region 轮盘 int[] lucktrun = { 6, 23, 40, 55, 69, 83 }; for (int i = 0; i < lucktrun.length; i++) { maps[lucktrun[i]] = 1; } #endregion #region 地雷 int[] landmine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; for (int i = 0; i < landmine.length; i++) { maps[landmine[i]] = 2; } #endregion #region 暂停 int[] pause = { 9, 27, 60, 93 }; for (int i = 0; i < pause.length; i++) { maps[pause[i]] = 3; } #endregion #region 隧道 int[] timetunnel = { 20, 25, 45, 63, 72, 88, 90 }; for (int i = 0; i < timetunnel.length; i++) { maps[timetunnel[i]] = 4; } #endregion } /// <summary> /// 设定当前坐标的类型 /// </summary> /// <param name="i">坐标</param> /// <returns>坐标类型</returns> public static string drawstringmap(int i) { string str = null; if (playerpos[0] == playerpos[1] && playerpos[0] == i) { str = "<>"; } else if (playerpos[0] == i) { str = "a"; } else if (playerpos[1] == i) { str = "b"; } else { switch (maps[i]) { case 0: console.foregroundcolor = consolecolor.yellow; str = "□"; break; case 1: console.foregroundcolor = consolecolor.blue; str = "◎"; break; case 2: console.foregroundcolor = consolecolor.green; str = "☆"; break; case 3: console.foregroundcolor = consolecolor.red; str = "▲"; break; case 4: console.foregroundcolor = consolecolor.cyan; str = "*"; break; } } return str; } /// <summary> /// 生成所有坐标 /// </summary> public static void drawmap() { console.writeline("legend: lucktrun<◎> landmine<☆> pause<▲> timetunnel<*>"); #region 第一橫行 for (int i = 0; i < 30; i++) { console.write(drawstringmap(i)); } console.writeline(); #endregion #region 第一竖行 for (int i = 30; i < 35; i++) { for (int j = 0; j <= 28; j++) { console.write(" "); } console.write(drawstringmap(i)); console.writeline(); } #endregion #region 第二橫行 for (int i = 64; i >= 35; i--) { console.write(drawstringmap(i)); } console.writeline(); #endregion #region 第二竖行 for (int i = 65; i < 70; i++) { console.writeline(drawstringmap(i)); } #endregion #region 第三橫行 for (int i = 70; i <= 99; i++) { console.write(drawstringmap(i)); } console.writeline(); #endregion } /// <summary> /// 判断坐标是否超出范围 /// </summary> public static void changepos() { #region player a if (playerpos[0] < 0) { playerpos[0] = 0; } if (playerpos[0] > 99) { playerpos[0] = 99; } #endregion #region player b if (playerpos[1] < 0) { playerpos[1] = 0; } if (playerpos[1] > 99) { playerpos[1] = 99; } #endregion } /// <summary> /// 踩到轮盘时,选择功能 /// </summary> /// <param name="input">玩家的选择</param> /// <param name="player">玩家标示</param> public static void playerselect(string input, int player) { while (true) { if (input == "1") { console.writeline("player {0} select and {1} swap places.", playernames[player], playernames[1 - player]); int temp = playerpos[player]; playerpos[player] = playerpos[1 - player]; playerpos[1 - player] = temp; console.writeline("swap complete, press any key continue."); console.readkey(true); break; } else if (input == "2") { console.writeline("player {0} select bombing {1}, player {2} back to 6.", playernames[player], playernames[1 - player], playernames[1 - player]); playerpos[1 - player] -= 6; console.readkey(true); break; } else { console.writeline("can only select: 1--swap places 2--bombing: "); input = console.readline(); } } } /// <summary> /// 进行游戏 /// </summary> /// <param name="player">玩家标示位</param> public static void playgame(int player) { random r = new random(); int next = r.next(1, 7); console.writeline("{0} press any key to start rolling the dice.", playernames[player]); console.readkey(true); console.writeline("{0} throw out of {1}", playernames[player], next); playerpos[player] += next; changepos(); console.readkey(true); console.writeline("{0} press any key to start action.", playernames[player]); console.readkey(true); console.writeline("{0} action complete.", playernames[player]); console.readkey(true); // player a 有可能踩到: player b、方块、轮盘、地雷、暂停、隧道 if (playerpos[player] == playerpos[1 - player]) { console.writeline("player {0} step on {1}, {2} back to 6.", playernames[player], playernames[1 - player], playernames[1 - player]); playerpos[1 - player] -= 6; console.readkey(true); } else { switch (maps[playerpos[player]]) { case 0: console.writeline("player {0} step on square, safe.", playernames[player]); console.readkey(true); break; case 1: console.writeline("player {0} step on a lucktrun, please select: 1--swap places 2--bombing: ", playernames[player]); string input = console.readline().trim(); playerselect(input, player); console.readkey(true); break; case 2: console.writeline("player {0} step on a landmine, back to 6", playernames[player]); playerpos[player] -= 6; console.readkey(true); break; case 3: console.writeline("player {0} step on a pause, to suspend a round.", playernames[player]); console.readkey(true); flags[player] = true; break; case 4: console.writeline("player {0} step on a timetunnel, forward 10.", playernames[player]); playerpos[player] += 10; console.readkey(); break; } } changepos(); console.clear(); drawmap(); } static void main(string[] args) { showgame(); inputplayernames(); console.writeline("player {0} is a.", playernames[0]); console.writeline("player {0} is b.", playernames[1]); initailmap(); drawmap(); while (playerpos[0] < 99 && playerpos[1] < 99) { #region a if (flags[0] == false) { playgame(0); } else { flags[0] = false; } #endregion #region b if (flags[1] == false) { playgame(1); } else { flags[1] = false; } #endregion } #region 判断玩家胜利 if (playerpos[0] == 99) { console.clear(); console.writeline("player {0} win.", playernames[0]); } if (playerpos[1] == 99) { console.clear(); console.writeline("player {0} win.", playernames[1]); } #endregion console.readkey(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: asp智能脏话过滤系统v1.0第1/2页