C#实现经典飞行棋游戏的示例代码
程序员文章站
2022-06-10 10:16:57
目录效果展示主函数场景类型枚举控制台基础设置开始及结束场景逻辑游戏场景逻辑固定打印的信息格子类型枚举和格子结构体地图结构体玩家和电脑结构体绘制玩家扔骰子逻辑效果展示主函数 static voi...
效果展示
主函数
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, } /// <summary> /// 位置信息结构体 /// </summary> 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(" "); } /// <summary> /// 扔色子函数 /// </summary>> /// <param name="w">窗口的宽</param> /// <param name="h">窗口的高</param> /// <param name="p">扔色子的对象</param> /// <param name="map">地图信息</param> /// <returns>默认返回false 代表没有结束</returns> 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#实现经典飞行棋游戏的示例代码的详细内容,更多关于c#飞行棋的资料请关注其它相关文章!