C#实现一个控制台飞行棋小游戏(附源码)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;namespace 飞行棋小游戏
{class Program{/// <summary>/// 地图/// </summary>static int[] Maps = new int[100];/// <summary>/// 用户姓名/// </summary>static string[] playerName = new string[2];/// <summary>/// 两名玩家的位置/// </summary>static int[] playerPosition = new int[2];/// <summary>/// 标记两个玩家谁该进行投掷筛子/// </summary>static bool[] playerFlag = new bool[2];static void Main(string[] args){//游戏标题ShowTitle();#region///用户名注册Console.WriteLine("请两名玩家先进行用户注册!");Console.WriteLine("注册规则:不能为空,并且不能是纯数字,玩家AB姓名不能相同");Console.WriteLine("请输入玩家A的姓名:");string pattern = @"^\d+$";bool isok = true;while (isok){playerName[0] = Console.ReadLine();if (string.IsNullOrWhiteSpace(playerName[0])){Console.WriteLine("玩家A的姓名不能为空,请重新输入");}else if (Regex.IsMatch(playerName[0], pattern)){Console.WriteLine("玩家A的姓名不能是纯数字,请重新输入");}else{isok = false;}}isok = true;Console.WriteLine("请输入玩家B的姓名:");while (isok){playerName[1] = Console.ReadLine();if (string.IsNullOrWhiteSpace(playerName[1])){Console.WriteLine("玩家B的姓名不能为空,请重新输入");}else if (Regex.IsMatch(playerName[1], pattern)){Console.WriteLine("玩家B的姓名不能是纯数字,请重新输入");}else if (playerName[0] == playerName[1]){Console.WriteLine("玩家B的姓名已被占用,请重新输入");}else{isok = false;}}#endregionConsole.Clear();ShowTitle();ShowRule();InitialMap();DrawMap();//开始进入游戏//判断两名玩家都未到达终点则游戏继续while (playerPosition[0] < 99 && playerPosition[1] < 99){if (playerFlag[0] == false){PlayGame(0);}else{playerFlag[0] = false;}if (playerFlag[1] == false){PlayGame(1);}else{playerFlag[1] = false;}if (playerPosition[0] >= 99){Console.WriteLine("恭喜玩家【{0}】获胜!", playerName[0]);break;}else if (playerPosition[1] >= 99){Console.WriteLine("恭喜玩家【{0}】获胜!", playerName[1]);break;}}Console.ReadLine();}/// <summary>/// 绘制游戏标题/// </summary>static void ShowTitle(){Console.ForegroundColor = ConsoleColor.Cyan;Console.WriteLine("****************************************");Console.ForegroundColor = ConsoleColor.Green;Console.WriteLine("****************************************");Console.ForegroundColor = ConsoleColor.Blue;Console.WriteLine("****************************************");Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine("*****************飞行棋*****************");Console.ForegroundColor = ConsoleColor.Blue;Console.WriteLine("****************************************");Console.ForegroundColor = ConsoleColor.Green;Console.WriteLine("****************************************");Console.ForegroundColor = ConsoleColor.Cyan;Console.WriteLine("****************************************");}/// <summary>/// 绘制游戏规则/// </summary>static void ShowRule(){Console.ForegroundColor = ConsoleColor.Yellow;Console.WriteLine("------------------------------------------------------------");Console.WriteLine("玩家【{0}】使用A表示", playerName[0]);Console.WriteLine("玩家【{0}】使用B表示", playerName[1]);Console.WriteLine("规则说明:");Console.WriteLine("1.玩家A先掷骰子,A、B玩家轮流投掷骰子");Console.WriteLine("2.“□”普通格子对于玩家没有任何奖惩!");Console.WriteLine("3.“◎”幸运轮盘玩家具有两种选择:a.选择与对方交换位置;b.选择轰炸对方使对方倒退6步");Console.WriteLine("4.“★”地雷对于玩家惩罚使玩家倒退6步");Console.WriteLine("5.“▲”暂停惩罚玩家下一轮暂停操作");Console.WriteLine("6.“卍”时空隧道奖励玩家直接前进10步");Console.WriteLine("7.如果踩到对方则惩罚对方直接倒退6步");Console.WriteLine("8.“<>”代表玩家AB位置重合");Console.WriteLine("------------------------------------------------------------");}/// <summary>/// 初始化地图/// </summary>static void InitialMap(){//确定“◎”=1int[] lunckturn = { 6, 23, 40, 55, 69, 83 };for (int i = 0; i < lunckturn.Length; i++){int index = lunckturn[i];Maps[index] = 1;}//确定“★”=2int[] landmine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };for (int i = 0; i < landmine.Length; i++){Maps[landmine[i]] = 2;}//确定“▲”=3int[] pause = { 9, 27, 60, 93 };for (int i = 0; i < pause.Length; i++){Maps[pause[i]] = 3;}//确定“卍”=4int[] timeTunnel = { 20, 25, 45, 63, 88, 90 };for (int i = 0; i < timeTunnel.Length; i++){Maps[timeTunnel[i]] = 4;}}/// <summary>/// 绘制地图/// </summary>static void DrawMap(){//第一行for (int i = 0; i < 30; i++){Console.Write(DrawString(i));}Console.WriteLine();//第一列for (int i = 30; i < 35; i++){for (int k = 0; k < 29; k++){Console.Write(" ");}Console.Write(DrawString(i));Console.WriteLine();}//第二行for (int i = 64; i > 34; i--){Console.Write(DrawString(i));}Console.WriteLine();//第二列for (int i = 65; i < 70; i++){Console.WriteLine(DrawString(i));}//第三行for (int i = 70; i < 100; i++){Console.Write(DrawString(i));}Console.WriteLine();}/// <summary>/// 根据地图数组每个位置的值选择绘制的图案/// </summary>/// <param name="type">地图数组中位置数字代表地图位置索引</param>/// <returns>返回最终选择的图案</returns>static string DrawString(int index){string str = "";//两名玩家的位置重合,并且保证两个玩家的位置都在地图中if (playerPosition[0] == playerPosition[1] && playerPosition[0] == index){Console.ForegroundColor = ConsoleColor.White;str = "<>";}else if (playerPosition[0] == index){Console.ForegroundColor = ConsoleColor.DarkYellow;str = "A";}else if (playerPosition[1] == index){Console.ForegroundColor = ConsoleColor.DarkCyan;str = "B";}else{switch (Maps[index]){case 0:Console.ForegroundColor = ConsoleColor.Cyan;str = "□";break;case 1:Console.ForegroundColor = ConsoleColor.Magenta;str = "◎";break;case 2:Console.ForegroundColor = ConsoleColor.Red;str = "★";break;case 3:Console.ForegroundColor = ConsoleColor.DarkBlue;str = "▲";break;case 4:Console.ForegroundColor = ConsoleColor.Green;str = "卍";break;default:break;}}return str;}/// <summary>/// 实现游戏功能的方法/// </summary>/// <param name="playerIndex">通过玩家索引判断本次游戏操作是由哪个玩家发起的</param>static void PlayGame(int playerIndex){#region ///为了实现玩家掷骰子算出移动步数Random r = new Random();Console.WriteLine("本回合由玩家【{0}】按下任意键掷骰子!", playerName[playerIndex]);Console.ReadKey(true);int number = r.Next(1, 7);Console.WriteLine("玩家【{0}】掷出<{1}>点", playerName[playerIndex], number);Console.WriteLine("玩家【{0}】按下任意键开始移动!", playerName[playerIndex]);Console.ReadKey(true);playerPosition[playerIndex] += number;Console.WriteLine("玩家【{0}】移动完成!", playerName[playerIndex]);#endregionCheckPosition();#region///进行对移动结果的格子奖惩判断//先判断两个玩家是否踩到对方if (playerPosition[0] == playerPosition[1]){Console.WriteLine("玩家【{0}】踩到玩家【{1}】,玩家【{1}】退6格!", playerName[playerIndex], playerName[1 - playerIndex]);playerPosition[1 - playerIndex] -= 6;}else{switch (Maps[playerPosition[playerIndex]]){//踩到普通格子case 0:Console.WriteLine("玩家【{0}】踩到安全地带!无奖惩!按下任意键刷新界面!", playerName[playerIndex]);Console.ReadKey(true);break;//踩到幸运轮盘case 1:Console.WriteLine("玩家【{0}】踩到幸运轮盘!请选择:a--交换位置,b--轰炸对方", playerName[playerIndex]);string type = Console.ReadLine();while (true){if (type == "a"){Console.WriteLine("玩家【{0}】选择与玩家【{1}】交换位置", playerName[playerIndex], playerName[1 - playerIndex]);int temp = playerPosition[0];playerPosition[0] = playerPosition[1];playerPosition[1] = temp;Console.WriteLine("玩家【{0}】与玩家【{1}】交换位置完成,按下任意键刷新界面!", playerName[playerIndex], playerName[1 - playerIndex]);Console.ReadKey(true);break;}else if (type == "b"){Console.WriteLine("玩家【{ 0}】选择轰炸玩家【{ 1}】", playerName[playerIndex], playerName[1 - playerIndex]);playerPosition[1 - playerIndex] -= 6;Console.WriteLine("玩家【{ 0}】轰炸玩家【{ 1}】完成,按下任意键刷新界面!", playerName[playerIndex], playerName[1 - playerIndex]);Console.ReadKey(true);break;}else{Console.WriteLine("请输入正确的指令!请选择:a--交换位置,b--轰炸对方");type = Console.ReadLine();}}break;//踩到地雷case 2:Console.WriteLine("玩家【{0}】踩到地雷,退6格!按下任意键刷新界面", playerName[playerIndex]);playerPosition[playerIndex] -= 6;Console.ReadKey(true);break;//踩到暂停case 3:Console.WriteLine("玩家【{0}】踩到暂停,下一回合暂停!按下任意键刷新界面", playerName[playerIndex]);playerFlag[playerIndex] = true;break;//踩到时空隧道case 4:Console.WriteLine("玩家【{0}】踩到时空隧道,前进10步!按下任意键刷新界面", playerName[playerIndex]);playerPosition[playerIndex] += 10;Console.ReadKey(true);break;default:break;}}CheckPosition();Console.Clear();ShowTitle();ShowRule();InitialMap();DrawMap();#endregion}/// <summary>/// 断是否超越终点或者超出起点/// </summary>static void CheckPosition(){if (playerPosition[0] < 0){playerPosition[0] = 0;}if (playerPosition[0] > 99){playerPosition[0] = 99;}if (playerPosition[1] < 0){playerPosition[1] = 0;}if (playerPosition[1] > 99){playerPosition[1] = 99;}}}
}
C#实现一个控制台飞行棋小游戏(附源码)相关推荐
- 【Python游戏】用Python 和 Pyglet 编写一个我的世界小游戏 | 附源码
相关文件 想学Python的小伙伴可以关注小编的公众号[Python日志] 有很多的资源可以白嫖的哈,不定时会更新一下Python的小知识的哈!! 需要源码的小伙伴可以在公众号回复我的世界 Pytho ...
- 用C语言easyx库来写一个简单的翻翻乐小游戏(附源码素材)
简明目录 写在前面 easyx库 准备工作 新建项目文件 分析 素材分析 上代码吧 地图表示 开始界面 地图初始化(打乱) 游戏过程实现 主函数的实现 测试 优化 1.游戏分数 2.游戏时间 3.nu ...
- html实现扫雷小游戏(附源码)
文章目录 实现功能 1.扫雷设计 1.1 主界面 1.2 扫雷难度 1.3 附带功能 2.效果和源码 2.1 动态效果 2.2 源代码 源码下载 作者:xcLeigh 文章地址:https://blo ...
- 【Python游戏】基于pygame实现的一个Dino Rush 恐龙宝贝冲冲冲的小游戏 | 附源码
前言 halo,包子们晚上好 很久没有更新啦,主要是小编这边最近有点小忙 今天给大家整一个Dino Rush 恐龙宝贝冲冲冲的小游戏 还是一个比较记经典的小游戏,还记这可谷歌浏览器上没有网也能打发时间 ...
- 【Python游戏】基于化学方程式的基础上,用Python实现一个消灭泡泡小游戏 | 附源码
前言 halo,包子们下午好 今天实现的这个小游戏呀,说实话化学不太好的小伙伴可能看起来会有点懵逼 不过不用担心,咱们今天不是来学化学的,我们是来学习Python的 所以呀,不要太担心啦,大家先好好看 ...
- 飞行棋程序(附源码)
下面是自己写的飞行棋的小程序,代码写的简单,希望各路大神多多指教----话不多说,直接上代码 一共有三个类,第一个GameManager: 1 using System; 2 using System ...
- 【Python游戏】Python基于pygame实现的人机大战的斗兽棋小游戏 | 附源码
前言 有粉丝说要我出一期Python版本的斗兽棋,今天宠粉狂魔的我不就来啦!! 虽然是一个简单的小游戏,但是对于新手小伙伴来说还是有一定的小难度的哟!要是不理解都可以找到小编的哈!! 相关文件 关注小 ...
- 整理了30款Python小游戏附源码,五一有的玩了
快到五一了,整理了 30 款 Python 小游戏源码分享给大家,具体内容可以点击下方视频号查看: 点击上方视频后,源码获取方式:①关注上方视频号.②点赞当前视频.③在当前视频评论区扣1 友情提示:获 ...
- 【博主推荐】html好看的拼图小游戏(附源码)
拼图目录 html好看的拼图小游戏 1.拼图效果示意图 1.1 第一级 九宫格拼图 1.2 第二级 十六宫格拼图 1.3 第三级 三十二宫格拼图 14 第三级 八十一宫格拼图 2.图片切图说明 3.实 ...
最新文章
- 设计模式 之 命令模式
- [转]MySQL Explain详解
- Delphi的实数计算结果中只保留2位小数
- 【机器学习算法-python实现】svm支持向量机(1)—理论知识介绍
- java的整数扩展,浮点数扩展,字符扩展,转义字符,布尔值扩展
- 浮点数向零舍入(信息学奥赛一本通-T1019)
- IE9 新功能 五大新特点
- 如何开启和使用windows 10中的Hyper-v
- nosql----redis数据恢复方案
- POJ2676Sudoku
- BZOJ 2176 Strange string 最小表示法
- 吉林大学计算机学院刘衍衍教授,周柚-吉林大学计算机科学与技术学院
- php通过header发送自定义数据
- 计算机找不到970pro,缝缝补补又三年,老机器更新---三星970 pro SSD简测
- 高效获得准确的中国地图数据并进行可视化
- java电力巡检项目讲解,电力巡检-东软平台产品官网
- python实现qq空间自动点赞
- 如何编写一份高质量的测试报告
- pandas的重复值的处理
- 英文缩写400个速查
热门文章
- ibm刀片服务器虚拟化,IBM刀片服务器虚拟化方案
- location.href跳转url链接失败,原来是零宽字符导致的
- 为什么国家政府发的国债,被央行自己印的钱大量购买时,利率会降低呢?
- Android实现国际化
- Java项目:SSM员工考勤管理系统
- matlab画气象要素,基于MATLAB实现3种气象数据读取和绘图.pdf
- jmeter正则表达式
- [附源码]java毕业设计网上购物商城系统
- apache IOUtils的读取文件并转化为clazz
- Direct3D 9.0 SDK 文档(中文版)