C#实现经典飞行棋游戏的脚本怎么写
发表于:2024-10-22 作者:千家信息网编辑
千家信息网最后更新 2024年10月22日,今天小编给大家分享一下C#实现经典飞行棋游戏的脚本怎么写的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们
千家信息网最后更新 2024年10月22日C#实现经典飞行棋游戏的脚本怎么写
今天小编给大家分享一下C#实现经典飞行棋游戏的脚本怎么写的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
效果展示
主函数
static void Main(string[] args) { int w = 50; int h = 30; ConsoleInit(w, h); E_SceneType nowSceneType = E_SceneType.Begin; while (true) { switch (nowSceneType) { case E_SceneType.Begin: Console.Clear(); GameEndOrBegin(w, h, ref nowSceneType); break; case E_SceneType.Game: Console.Clear(); GameScene(w, h, ref nowSceneType); break; case E_SceneType.End: Console.Clear(); GameEndOrBegin(w, h, ref nowSceneType); break; default: break; } } }
场景类型枚举
enum E_SceneType { Begin, Game, End, }
控制台基础设置
static void ConsoleInit(int w, int h) { //控制台设置 Console.CursorVisible = false; Console.SetWindowSize(w, h); Console.SetBufferSize(w, h); }
开始及结束场景逻辑
static void GameEndOrBegin(int w, int h, ref E_SceneType nowSceneType) { Console.ForegroundColor = ConsoleColor.White; Console.SetCursorPosition(nowSceneType == E_SceneType.Begin ? w / 2 - 3 : w / 2 - 4, 8); Console.Write(nowSceneType == E_SceneType.Begin ? "飞行棋" : "游戏结束"); //当前选项的编号 int count = 0; bool IsOver = false; while (true) { Console.SetCursorPosition(nowSceneType ==E_SceneType.Begin? w/2-4:w/2-5, 11); Console.ForegroundColor = count == 0 ? ConsoleColor.Red : ConsoleColor.White; Console.Write(nowSceneType == E_SceneType.Begin? "游戏开始":"回到主菜单"); Console.SetCursorPosition(w/2-4, 13); Console.ForegroundColor = count == 1 ? ConsoleColor.Red : ConsoleColor.White; Console.Write("退出游戏"); switch (Console.ReadKey(true).Key) { case ConsoleKey.W: --count; if (count < 0) { count = 0; } break; case ConsoleKey.S: ++count; if (count > 1) { count = 1; } break; case ConsoleKey.J: if(count == 0) { nowSceneType = nowSceneType ==E_SceneType.Begin? E_SceneType.Game:E_SceneType.Begin; IsOver = true; } else { Environment.Exit(0); } break; } if (IsOver) break; } }
游戏场景逻辑
static void GameScene(int w, int h, ref E_SceneType nowSceneType) { DrawWall(w, h); Map map = new Map(14, 3, 80); map.Draw(); Player player = new Player(0, E_Player_Type.Player); Player computer = new Player(0, E_Player_Type.Computer); DrawPlayer(map, player, computer); while (true) { if (PlayerRandomMove(w, h, ref player, ref computer, map, ref nowSceneType)) { break; } if (PlayerRandomMove(w, h, ref computer, ref player, map, ref nowSceneType)) { break; } } } static bool PlayerRandomMove(int w, int h, ref Player p, ref Player otherP, Map map, ref E_SceneType nowSceneType) { //之后的游戏逻辑 //玩家扔色子逻辑 //检测输入 Console.ReadKey(true); //扔色子的逻辑 bool isGameOver = RandomMove(w, h, ref p, ref otherP, map); //绘制地图 map.Draw(); //绘制玩家 DrawPlayer(map, p, otherP); //判断是否要结束游戏 if(isGameOver) { //卡住程序 让玩家按任意键 Console.ReadKey(true); nowSceneType = E_SceneType.End; } return isGameOver; }
固定打印的信息
static void DrawWall(int w, int h) { Console.ForegroundColor = ConsoleColor.Red; //横着的墙 for (int i = 0; i < w; i+=2) { //最上面一行 Console.SetCursorPosition(i, 0); Console.Write("■"); //中间一行 Console.SetCursorPosition(i, h-6); Console.Write("■"); Console.SetCursorPosition(i, h - 11); Console.Write("■"); //最下面一行 Console.SetCursorPosition(i, h-1); Console.Write("■"); } //竖着的墙 for(int i = 0; i < h; i++) { //左边的墙 Console.SetCursorPosition(0, i); Console.Write("■"); //右边的墙 Console.SetCursorPosition(w-2, i); Console.Write("■"); } Console.SetCursorPosition(2, h - 5); Console.ForegroundColor = ConsoleColor.White; Console.Write("按任意键开始扔色子"); Console.SetCursorPosition(2, h - 10); Console.Write("□:普通格子"); Console.SetCursorPosition(2, h - 9); Console.ForegroundColor = ConsoleColor.Blue; Console.Write("■:暂停,一回合不动"); Console.SetCursorPosition(22,h - 9); Console.ForegroundColor = ConsoleColor.Red; Console.Write("●:炸弹,倒退5格"); Console.SetCursorPosition(2, h - 8); Console.ForegroundColor = ConsoleColor.White; Console.Write("×:时空隧道,随机倒退,暂停,交换位置"); Console.SetCursorPosition(2, h - 7); Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("★:玩家 "); Console.SetCursorPosition(11, h - 7); Console.ForegroundColor = ConsoleColor.Magenta; Console.Write("▲:电脑 "); Console.SetCursorPosition(20, h - 7); Console.ForegroundColor = ConsoleColor.Blue; Console.Write("◎:玩家电脑重合"); }
格子类型枚举和格子结构体
enum E_Grid_Type { Normal, Boom, Pause, Tunnel, } ////// 位置信息结构体 /// struct Vector2 { public int x; public int y; public Vector2(int x, int y) { this.x = x; this.y = y; } } struct Grid { //格子的类型 public E_Grid_Type _type; //格子的位置 public Vector2 pos; //构造函数 public Grid(int x, int y, E_Grid_Type type) { pos.x = x; pos.y = y; _type = type; } //画一个格子 public void Draw() { Console.SetCursorPosition(pos.x, pos.y); switch(_type) { case E_Grid_Type.Normal: Console.ForegroundColor = ConsoleColor.White; Console.Write("□"); break; case E_Grid_Type.Boom: Console.ForegroundColor = ConsoleColor.Red; Console.Write("●"); break; case E_Grid_Type.Pause: Console.ForegroundColor = ConsoleColor.Blue; Console.Write("■"); break; case E_Grid_Type.Tunnel: Console.ForegroundColor = ConsoleColor.White; Console.Write("×"); break; } } }
地图结构体
struct Map { public Grid[] grids; public Map(int x, int y, int num) { grids = new Grid[num]; int indexX = 0; int indexY = 0; int stepNum = 2; Random r = new Random(); int randomNum; for(int i = 0; i < num; i++) { randomNum = r.Next(0, 101); if(randomNum < 85 || i == 0 || i == num - 1) { //普通格子 grids[i]._type = E_Grid_Type.Normal; } else if(randomNum < 90 && randomNum >=85) { //炸弹 grids[i]._type = E_Grid_Type.Boom; } else if(randomNum < 95 && randomNum >=90) { //暂停 grids[i]._type = E_Grid_Type.Pause; } else { //时空隧道 grids[i]._type = E_Grid_Type.Tunnel; } grids[i].pos = new Vector2(x, y); if(indexX == 10) { y += 1; indexY++; if(indexY == 2) { indexX = 0; indexY = 0; stepNum = -stepNum; } } else { x += stepNum; indexX++; } } } public void Draw() { for (int i = 0; i < grids.Length; i++) { grids[i].Draw(); } } }
玩家和电脑结构体
enum E_Player_Type { Player, Computer, } struct Player { public E_Player_Type type; public int nowIndex; //是否暂停的标识 public bool isPause; public Player(int index, E_Player_Type type) { nowIndex = index; this.type = type; isPause = false; } public void Draw(Map mapInfo) { //从传入的地图中得到格子信息 Grid grid = mapInfo.grids[nowIndex]; Console.SetCursorPosition(grid.pos.x, grid.pos.y); switch(type) { case E_Player_Type.Player: Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("★"); break; case E_Player_Type.Computer: Console.ForegroundColor = ConsoleColor.Magenta; Console.Write("▲"); break; } } }
绘制玩家
static void DrawPlayer(Map map, Player player, Player computer) { //重合时 if(player.nowIndex == computer.nowIndex) { //得到重合的位置 Grid grid = map.grids[player.nowIndex]; Console.SetCursorPosition(grid.pos.x, grid.pos.y); Console.ForegroundColor = ConsoleColor.DarkGreen; Console.Write("◎"); } //不重合时 else { player.Draw(map); computer.Draw(map); } }
扔骰子逻辑
//擦除提示的函数 static void ClearInfo(int h) { Console.SetCursorPosition(2, h - 5); Console.Write(" "); Console.SetCursorPosition(2, h - 4); Console.Write(" "); Console.SetCursorPosition(2, h - 3); Console.Write(" "); Console.SetCursorPosition(2, h - 2); Console.Write(" "); } ////// 扔色子函数 /// > /// 窗口的宽 /// 窗口的高 /// 扔色子的对象 /// 地图信息 ///默认返回false 代表没有结束 static bool RandomMove(int w, int h, ref Player p, ref Player otherP, Map map) { //擦除之前显示的提示信息 ClearInfo(h); //根据扔色子的玩家类型,决定信息的颜色 Console.ForegroundColor = p.type == E_Player_Type.Player ? ConsoleColor.Cyan : ConsoleColor.Magenta; //扔色子之前判断玩家是否处于暂停状态 if(p.isPause) { Console.SetCursorPosition(2, h - 5); Console.Write("处于暂停状态,{0}需要暂停一回合", p.type == E_Player_Type.Player ? "你" : "电脑"); Console.SetCursorPosition(2, h - 4); Console.Write("请按任意键,让{0}开始扔色子", p.type == E_Player_Type.Player ? "电脑" : "你"); //停止暂停 p.isPause = false; return false; } //扔色子目的是改变玩家或电脑的位置 计算位置的变化 //扔色子 随机一个1到6的数字,加上去 Random r = new Random(); int randomNum = r.Next(1, 7); p.nowIndex += randomNum; //打印扔的点数 Console.SetCursorPosition(2, h - 5); Console.Write("{0}扔出的点数为:{1}", p.type == E_Player_Type.Player ? "你" : "电脑", randomNum); //首先判断是否到终点了 if(p.nowIndex >= map.grids.Length - 1) { p.nowIndex = map.grids.Length - 1; Console.SetCursorPosition(2, h - 4); if(p.type == E_Player_Type.Player) { Console.Write("恭喜你,率先到达了终点"); } else { Console.Write("很遗憾,电脑率先到达了终点"); } Console.SetCursorPosition(2, h - 3); Console.Write("请按任意键结束"); return true; } else { //没有到终点 就判断当前对象到了一个什么类型的格子 Grid grid = map.grids[p.nowIndex]; switch(grid._type) { case E_Grid_Type.Normal: Console.SetCursorPosition(2, h - 4); Console.Write("{0}到达了一个安全位置", p.type == E_Player_Type.Player ? "你" : "电脑"); Console.SetCursorPosition(2, h - 3); Console.Write("请按任意键,让{0}开始扔色子", p.type == E_Player_Type.Player ? "电脑" : "你"); break; case E_Grid_Type.Boom: p.nowIndex -= 5; if(p.nowIndex < 0) { p.nowIndex = 0; } Console.SetCursorPosition(2, h - 4); Console.Write("{0}踩到了炸弹,退后5格", p.type == E_Player_Type.Player ? "你" : "电脑"); Console.SetCursorPosition(2, h - 3); Console.Write("请按任意键,让{0}开始扔色子", p.type == E_Player_Type.Player ? "电脑" : "你"); break; case E_Grid_Type.Pause: p.isPause = true; Console.SetCursorPosition(2, h - 4); Console.Write("{0}到达了暂停点,你需要暂停一回合", p.type == E_Player_Type.Player ? "你" : "电脑"); Console.SetCursorPosition(2, h - 3); Console.Write("请按任意键,让{0}开始扔色子", p.type == E_Player_Type.Player ? "电脑" : "你"); break; case E_Grid_Type.Tunnel: Console.SetCursorPosition(2, h - 4); Console.Write("{0}踩到了时空隧道", p.type == E_Player_Type.Player ? "你" : "电脑"); //随机 randomNum = r.Next(1, 91); if(randomNum <= 30) { //倒退 p.nowIndex -= 5; if(p.nowIndex < 0) { p.nowIndex = 0; } Console.SetCursorPosition(2, h - 5); Console.Write("触发倒退5格"); } else if(randomNum <= 60) { p.isPause = true; Console.SetCursorPosition(2, h - 3); Console.Write("触发暂停一回合"); } else { int tmp = p.nowIndex; p.nowIndex = otherP.nowIndex; otherP.nowIndex = tmp; Console.SetCursorPosition(2, h - 3); Console.Write("惊喜,双方交换位置"); } Console.SetCursorPosition(2, h - 2); Console.Write("请按任意键,让{0}开始扔色子", p.type == E_Player_Type.Player ? "电脑" : "你"); break; } } //默认没有结束 return false; }
以上就是"C#实现经典飞行棋游戏的脚本怎么写"这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注行业资讯频道。
电脑
色子
玩家
格子
位置
逻辑
信息
类型
函数
回合
地图
知识
篇文章
终点
结构
飞行棋
飞行
一行
场景
时空
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
魔兽世界服务器bl
广州软件开发国企公司有哪
我的世界服务器好管理吗
服务器dhcp自动分配ip
网络安全宣传专题栏目
阿里云 服务器 远程管理
网络安全问题英文作文80词
服务器电源atx支架
数据库中主题所涵盖
我国制定了网络安全的目的是为了
桌面软件开发语言推荐
济南凌刚网络技术有限公司
物联网数据库与数据储存技术
爱奇艺服务器瘫痪
海南网络营销软件开发定制优势
富士康云端网络技术学院是什么
计算机网络技术 域名
最右服务器错误
pubg超级服务器
进口网络技术网上价格
网络安全问答
数据库对身份证号加密
熊瑞金数据库原理与应用出版的书
做软件开发的工作工资多少
顶级域名的根服务器
沈阳新恒基软件开发
梁溪区现代软件开发产品介绍
火山视频软件开发成本
上海库存网络技术厂家现货
获取原始数据数据库的选择原则是