C#控制台实现飞行棋游戏
更新时间:2019年08月15日 16:31:18 作者:取名字真的很难
这篇文章主要为大家详细介绍了C#控制台实现飞行棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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#操作文件路径(Directory)的常用静态方法详解
这篇文章主要给大家介绍了关于C#操作文件路径(Directory)的常用静态方法,Directory类位于System.IO 命名空间,Directory类提供了在目录和子目录中进行创建移动和列举操作的静态方法,需要的朋友可以参考下2021-08-08C#版免费离线人脸识别之虹软ArcSoft V3.0(推荐)
本文只是简单介绍了如何使用虹软的离线SDK,进行人脸识别的方法,并且是图片的方式,本地离线识别最大的好处就是没有延迟,识别结果立马呈现,对C#离线人脸识别虹软相关知识感兴趣的朋友一起看看吧2021-12-12
最新评论