本文主要是介绍C#控制台飞行棋游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
游戏截图:
管理类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp1
{class ProgramManage{//随机数public static Random rd = new Random();//定义地图大小public static int[] Maps = new int[100];//public static bool[] PlayerPause = {false,false };//定义玩家A和玩家B的坐标,0是玩家A的坐标;1是玩家B的坐标public static int[] PlayerPos = new int[2];//存储两个玩家的姓名public static string[] PlayerName = new string[2];//定义名字错误类型public static string[] Error = {"正确","不能有数字","重名了","不能为空" };//画游戏头public static void GanmeShow(){Console.ForegroundColor = ConsoleColor.Yellow;Console.WriteLine("*************************");Console.WriteLine("********飞行棋游戏*******");Console.WriteLine("*************************");Console.ForegroundColor = ConsoleColor.Green;}//定义地图关卡布局public static void InitailMap(){int[] luckturn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘◎for (int i = 0; i < luckturn.Length; i++){Maps[luckturn[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 = { 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;}}//定义地图元素。输入坐标值,方法内部判断当前坐标应当放什么符号,返回符号值public static string MapElement(int i){string Element = "0";//如果玩家AB坐标相同并都在当前位置则画一个“<>”if (PlayerPos[0] == i && PlayerPos[1] == i){Element="<>";}//有A画A,有B画B,都没有画地图else if (PlayerPos[0] == i){Element="A";}else if (PlayerPos[1] == i){Element="B";}else{switch (Maps[i]){case 0:Element = "□";break;case 1:Element = "◎";break;case 2:Element = "×";break;case 3:Element = "▲";break;case 4:Element = "卍";break;}}return Element;}//绘制地图public static void DrawMap(){Console.ForegroundColor = ConsoleColor.White;//第一横行for (int i = 0; i < 30; i++){Console.Write(MapElement(i));}//第一竖行for (int i = 30; i < 35; i++){Console.WriteLine();Console.Write(" ");Console.Write(MapElement(i));}//第二横行Console.WriteLine();for (int i = 64; i > 34; i--){Console.Write(MapElement(i));}//第二竖行for (int i = 65; i < 70; i++){Console.WriteLine();Console.Write(MapElement(i));}//第三横行Console.WriteLine();for (int i = 70; i < 100; i++){Console.Write(MapElement(i));}Console.WriteLine();}//判断姓名是否符合要求public static int NameJudge(string str){int result = 0;//判断是否为空if (str == ""){result = 3;return result;}else{result = 0;}//判断是否是字符串for (int i = 0; i < str.Length; i++){if (char.IsNumber(str,i)){result = 1;return result;}else{result = 0;}}//判断是否重名for (int i = 0; i < PlayerName.Length; i++){if (PlayerName[i]==str){result = 2;return result;}else{result = 0;}}return result;}//投骰子public static void Play(int player){PlayerPos[player] += rd.Next(1, 7);}//坐标不能出地图public static void Limit(int player){if (PlayerPos[player] < 0){PlayerPos[player] = 0;}else if (PlayerPos[player] > 99){PlayerPos[player] = 99;}}//执行规则public static void GameRule(int player){//踩到对手if (PlayerPos[player]==PlayerPos[1-player]){PlayerPos[1 - player] -= 6;Console.WriteLine("{0}踩到对手,对手退6格", PlayerName[player]);}//什么也没踩到else if (Maps[PlayerPos[player]] == 0){Console.WriteLine("{0}玩家前进了,什么也没踩到",PlayerName[player]);}//幸运轮盘else if (Maps[PlayerPos[player]] == 1){Console.WriteLine("1,与对手交换位置。2,让对手退回原点");string chose;while (true){chose = Console.ReadLine();if (chose == "1"){int tem = PlayerPos[player];PlayerPos[player] = PlayerPos[1 - player];PlayerPos[1 - player] = tem;Console.WriteLine("{0}与对手交换了位置", PlayerName[player]);break;}else if (chose == "2"){PlayerPos[1 - player] = 0;Console.WriteLine("{0}把对手打回原位", PlayerName[player]);break;}else{Console.WriteLine("请重新输入");}}}//地雷else if (Maps[PlayerPos[player]] == 2){PlayerPos[player] -= 6;Console.WriteLine("{0}踩到地雷了,倒退6格", PlayerName[player]);}//暂停else if (Maps[PlayerPos[player]] == 3){PlayerPause[player] = true;Console.WriteLine("{0}暂停一回合", PlayerName[player]);}//时空隧道else if (Maps[PlayerPos[player]] == 4){PlayerPos[player] += 10;Console.WriteLine("{0}前进10格", PlayerName[player]);}}//判断输赢public static string Winner(){if (PlayerPos[0]>PlayerPos[1]){return PlayerName[0];}else{return PlayerName[1];}}}
}
实现类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApp1
{class Program{static void Main(string[] args){//初始化标题ProgramManage.GanmeShow();//初始化地图关卡ProgramManage.InitailMap();//输入名字string name;for (int i = 0; i < ProgramManage.PlayerName.Length; i++){Console.WriteLine("请输入玩家{0}的名字", i+1);name = Console.ReadLine();while (ProgramManage.NameJudge(name)!=0){Console.WriteLine("名字格式不对,{0},请重新输入", ProgramManage.Error[ProgramManage.NameJudge(name)]);name = Console.ReadLine();}ProgramManage.PlayerName[i] = name;}//刷新界面Console.Clear();ProgramManage.GanmeShow();Console.WriteLine("玩家A名字是{0},玩家B名字{1}", ProgramManage.PlayerName[0], ProgramManage.PlayerName[1]);ProgramManage.DrawMap();//游戏主循环//初始化玩家A开始int player = 0;//两个玩家都没在终点的时候不停的玩游戏while (ProgramManage.PlayerPos[0]<99&&ProgramManage.PlayerPos[1]<99){//判断当前玩家是否暂停,如果暂停了就停一回合if (ProgramManage.PlayerPause[player]==false){Console.WriteLine("轮到玩家{0}敲回车投骰子", ProgramManage.PlayerName[player]);Console.ReadKey();//玩家player投骰子ProgramManage.Play(player);ProgramManage.Limit(player);//清空界面Console.Clear();//执行规则,如果踩到了关卡,一直执行,直到踩到空白点位置while (true){if (ProgramManage.Maps[ProgramManage.PlayerPos[player]] != 0){ProgramManage.GameRule(player);ProgramManage.Limit(player);}ProgramManage.GameRule(player);break;}ProgramManage.Limit(player);//变化玩家player = 1 - player;//刷新地图ProgramManage.DrawMap();}else{//当前玩家恢复暂停ProgramManage.PlayerPause[player] = false;//下一回合让给另一个玩家player = 1 - player;continue;}}//游戏结束Console.WriteLine("玩家{0}赢了", ProgramManage.Winner());Console.ReadKey();}}
}
这篇关于C#控制台飞行棋游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!