static void BeginScene(int width, int height, ref E_SceneType e_SceneType) { //设置标题 Console.SetCursorPosition(width / 2 - 2, height / 3 - 3); Console.ForegroundColor = ConsoleColor.White; Console.Write("飞行棋"); int btnID = 0; bool quit = false; while (true) { Console.SetCursorPosition(width / 2 - 3, height / 2 - 3); Console.ForegroundColor = btnID == 0 ? ConsoleColor.Red : ConsoleColor.White; Console.Write("开始游戏"); Console.SetCursorPosition(width / 2 - 3, height / 2 - 1); Console.ForegroundColor = btnID == 1 ? ConsoleColor.Red : ConsoleColor.White; Console.Write("退出游戏"); switch (Console.ReadKey(true).Key) { case ConsoleKey.W: btnID--; if (btnID < 0) { btnID = 0; } break; case ConsoleKey.S: btnID++; if (btnID > 1) { btnID = 1; } break; case ConsoleKey.Enter: if (btnID == 0) { e_SceneType = E_SceneType.Game; quit = true; } else { Environment.Exit(0); } break; default: break; } if (quit) { break; } } }
static void GameScene(int width, int height, ref E_SceneType sceneType) { //绘制红墙、不变的文字信息 BuildRedWall(width, height); Hint(height); //绘制飞行棋地图 Map map = new Map(80, 15, 3); //初始化玩家及回合所需参数 Character mPlayer = new Character(E_PlayerType.player, 0); Character cPlayer = new Character(E_PlayerType.computer, 0); bool playerRound = true; bool over = false; //绘制地图 map.DrawMap(); //绘制玩家和电脑 DrawPlayer(mPlayer, cPlayer, map); //提示按键开始游戏 Console.SetCursorPosition(2, height - 5); Console.ForegroundColor = ConsoleColor.White; Console.Write("请按任意键开始游戏!"); //游戏回合循环 while (true) { //绘制地图 map.DrawMap(); //绘制玩家和电脑 DrawPlayer(mPlayer, cPlayer, map); //玩家输入按键 Console.ReadKey(true); //回合内容 Play(ref mPlayer, ref cPlayer, map, ref playerRound, ref over, height); //回合结束逻辑 if (over) { sceneType = E_SceneType.End; //绘制地图 map.DrawMap(); //绘制玩家和电脑 DrawPlayer(mPlayer, cPlayer, map); Console.ReadKey(true); break; } } }
static void Main(string[] args) { #region 控制台初始化 int w = 50, h = 30; ConsoleInit(w, h); #endregion #region 场景选择相关 //进入时默认为0开始场景 E_SceneType sceneType = E_SceneType.Begin; //游戏主循环 while (true) { switch (sceneType) { case E_SceneType.Begin: Console.Clear(); BeginScene(w, h, ref sceneType); break; case E_SceneType.Game: Console.Clear(); GameScene(w, h, ref sceneType); break; case E_SceneType.End: //结束场景循环 Console.Clear(); EndScene(w, h, ref sceneType); break; default: break; } } #endregion }
static void EndScene(int width, int height, ref E_SceneType e_SceneType) { //设置标题 Console.SetCursorPosition(width / 2 - 2, height / 3 - 3); Console.ForegroundColor = ConsoleColor.White; Console.Write("游戏结束"); int btnId = 0; bool quit = false; while (true) { //回到主菜单 Console.SetCursorPosition(width / 2 - 3, height / 2 - 3); Console.ForegroundColor = btnId == 0? ConsoleColor.Red: ConsoleColor.White; Console.Write("回到主菜单"); //退出游戏 Console.SetCursorPosition(width / 2 - 2, height / 2 - 1); Console.ForegroundColor = btnId == 1 ? ConsoleColor.Red : ConsoleColor.White; Console.Write("退出游戏"); switch (Console.ReadKey(true).Key) { case ConsoleKey.W: btnId--; if (btnId < 0) { btnId = 0; } break; case ConsoleKey.S: btnId++; if (btnId > 1) { btnId = 1; } break; case ConsoleKey.Enter: if (btnId == 0) { e_SceneType = 0; quit = true; } else { Environment.Exit(0); } break; default: break; } if (quit) { break; } } }