简单游戏制作——飞行棋

2024-06-16 14:04
文章标签 简单 游戏 制作 飞行棋

本文主要是介绍简单游戏制作——飞行棋,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

控制台初始化

int w = 50;
int h = 50;
ConsoleInit(w, h);
static void ConsoleInit(int w, int h)
{//基础设置//光标的隐藏Console.CursorVisible = false;//舞台的大小Console.SetWindowSize(w, h);Console.SetBufferSize(w, h);
}

场景选择相关

#region 场景选择相关
//声明一个表示场景标识的变量
E_SceneType nowSceneType = E_SceneType.Begin;
while (true)
{switch (nowSceneType){case E_SceneType.Begin://开始场景逻辑Console.Clear();break;case E_SceneType.Game://游戏场景逻辑Console.Clear();break;case E_SceneType.End://结束场景逻辑Console.Clear();break;default:break;}
}
#endregion
/// <summary>
/// 游戏场景枚举类型
/// </summary>
enum E_SceneType
{/// <summary>/// 开始场景/// </summary>Begin,/// <summary>/// 游戏场景/// </summary>Game,/// <summary>/// 结束场景/// </summary>End
}

开始场景逻辑实现

static void GameScene(int w, int h, ref E_SceneType nowSceneType)
{Console.SetCursorPosition(w / 2 - 3, 8);Console.Write("飞行棋");//当前选项的编号int nowSelIndex = 0;bool isQuitBengin = false;//开始场景逻辑处理函数while (true){Console.SetCursorPosition(w / 2 - 4, 13);Console.ForegroundColor = nowSelIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;Console.Write("开始游戏");Console.SetCursorPosition(w / 2 - 4, 15);Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;Console.Write("退出游戏");//通过ReadKey可以得到一个输入的枚举类型switch (Console.ReadKey(true).Key){case ConsoleKey.W:--nowSelIndex;if (nowSelIndex < 0){nowSelIndex = 0;}break;case ConsoleKey.S:++nowSelIndex;if (nowSelIndex > 1){nowSelIndex = 1;}break;case ConsoleKey.J:if (nowSelIndex == 0){//进入游戏场景//1.改变当前场景的IDnowSceneType = E_SceneType.Game;//退出当前循环isQuitBengin = true;}else{//退出游戏Environment.Exit(0);}break;}//通过标识决定是否跳出开始场景的循环if (isQuitBengin){break;}}
}

游戏场景逻辑实现

不变的红墙

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 - 1);Console.Write('■');//中间的墙Console.SetCursorPosition(i, h - 6);Console.Write('■');Console.SetCursorPosition(i, h - 11);Console.Write('■');}//竖着的墙for (int i = 0; i < h; i++){//最左边Console.SetCursorPosition(0, i);Console.Write('■');Console.SetCursorPosition(w - 2, i);Console.Write('■');}//文字信息Console.ForegroundColor = ConsoleColor.White;Console.SetCursorPosition(2, h - 10);Console.Write("□:普通格子");Console.ForegroundColor = ConsoleColor.Blue;Console.SetCursorPosition(2, h - 9);Console.Write("‖:暂停,一回合不动");Console.ForegroundColor = ConsoleColor.Red;Console.SetCursorPosition(26, h - 9);Console.Write("●:炸弹,倒退5格");Console.ForegroundColor = ConsoleColor.Yellow;Console.SetCursorPosition(2, h - 8);Console.Write("¤:时空隧道,随机倒退,暂停,换位置");Console.ForegroundColor = ConsoleColor.Cyan;Console.SetCursorPosition(2, h - 7);Console.Write("★:玩家");Console.ForegroundColor = ConsoleColor.Magenta;Console.SetCursorPosition(12, h - 7);Console.Write("▲:电脑");Console.ForegroundColor = ConsoleColor.DarkGreen;Console.SetCursorPosition(22, h - 7);Console.Write("◎:玩家和电脑重合");Console.ForegroundColor = ConsoleColor.White;Console.SetCursorPosition(2, h - 5);Console.Write("按任意键开始扔色子");
}

格子结构体和格子枚举

/// <summary>
/// 格子类型枚举
/// </summary>
enum E_Grid_type
{/// <summary>/// 普通格子/// </summary>Normal,/// <summary>/// 炸弹/// </summary>Boom,/// <summary>/// 暂停/// </summary>Pause,/// <summary>/// 时空隧道,随机倒退,暂停,换位置/// </summary>Tunnel,
}struct Vector2
{public int x;public int y;public Vector2(int x, int y){this.x = x;this.y = y;}
}struct Grid
{//格子的类型E_Grid_type type;//格子的位置public Vector2 pos;//初始化构造函数public Grid(int x, int y, E_Grid_type type){pos.x = x;pos.y = y;this.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.Yellow;Console.Write("¤");break;}}
}

地图结构体

struct Map
{public Grid[] grids;//初始化中初始了各个格子类型和位置public Map(int x, int y, int num){grids = new Grid[num];//用于位置变化计数的变量//表示X变化的次数int indexX = 0;//表示Y变化的次数int indexY = 0;//x的步长int stepNum = 2;Random r = new Random();int randomNum;for (int i = 0; i < num; i++){//应该初始化格子类型randomNum = r.Next(0, 101);//设置类型,普通格子//有85%几率是普通格子(首尾两个格子,必为普通格子)if (randomNum < 85 || i == 0 || i == num - 1){grids[i].type = E_Grid_type.Normal;}//有5%的几率是炸弹else if (randomNum >= 85 && randomNum < 90){grids[i].type = E_Grid_type.Boom;}//有5%的几率是暂停else if (randomNum >= 90 && randomNum < 95){grids[i].type = E_Grid_type.Pause;}//有5%的几率是时空隧道else{grids[i].type = E_Grid_type.Tunnel;}//位置应该如何设置grids[i].pos = new Vector2(x, y);//每次循环都应该按一定规则去变化位置//加十次if (indexX == 10){y += 1;//加一次Y记一次数++indexY;if (indexY == 2){//y加了2次过后,把x加的次数记0indexX = 0;indexY = 0;//反向步长stepNum = -stepNum;}}else{x += stepNum;//加一次x记一次数++indexX;}}}public void Draw(){for (int i = 0; i < grids.Length; i++){grids[i].Draw();}}
}

玩家枚举和玩家结构体

/// <summary>
/// 玩家枚举类型
/// </summary>
enum E_PlayType
{/// <summary>/// 玩家/// </summary>Player,/// <summary>/// 电脑/// </summary>Computer,
}struct Player
{//玩家类型public E_PlayType type;//当前所在地图哪一个索引的格子public int nowIndex;public Player(int index, E_PlayType type){nowIndex = index;this.type = type;}public void Draw(Map mapInfo){//必需要先得到地图才能够得到玩家在地图上的哪一个格子//从传入的地图中得到格子信息Grid grid = mapInfo.grids[nowIndex];//设置位置Console.SetCursorPosition(grid.pos.x, grid.pos.y);//画,设置颜色,设置图标switch (type){case E_PlayType.Player:Console.ForegroundColor = ConsoleColor.Cyan;Console.Write("★");break;case E_PlayType.Computer:Console.ForegroundColor = ConsoleColor.Magenta;Console.Write("▲");break;default:break;}}
}
static void DrawPlayer(Player player, Player computer, Map map)
{//重合时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_PlayType.Player ? ConsoleColor.Cyan : ConsoleColor.Magenta;//扔色子之前判断玩家是否处于暂停状态if (p.isPause){Console.SetCursorPosition(2, h - 5);Console.Write("处于暂停点,{0}需要暂停一回合", p.type == E_PlayType.Player ? "你" : "电脑");Console.SetCursorPosition(2, h - 4);Console.Write("请按任意键,让{0}开始扔色子", p.type == E_PlayType.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_PlayType.Player ? "你" : "电脑", randomNum);//首先判断是否到终点了if (p.nowIndex >= map.grids.Length - 1){p.nowIndex = map.grids.Length - 1;Console.SetCursorPosition(2, h - 4);if (p.type == E_PlayType.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_PlayType.Player ?"你" : "电脑");Console.SetCursorPosition(2, h - 3);Console.Write("请按任意键,让{0}开始扔色子", p.type == E_PlayType.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_PlayType.Player ?"你" : "电脑");Console.SetCursorPosition(2, h - 3);Console.Write("请按任意键,让{0}开始扔色子", p.type == E_PlayType.Player ? "你" : "电脑");break;case E_Grid_type.Pause://暂停一回合//暂停目的,只有加一个对象的暂停标识,才能知道下一回合它是不是不能扔色子p.isPause = true;Console.SetCursorPosition(2, h - 4);Console.Write("{0}踩到了炸弹,退后5格", p.type == E_PlayType.Player ?"你" : "电脑");Console.SetCursorPosition(2, h - 3);Console.Write("请按任意键,让{0}开始扔色子", p.type == E_PlayType.Player ? "你" : "电脑");break;case E_Grid_type.Tunnel:Console.SetCursorPosition(2, h - 4);Console.Write("{0}踩到了时空隧道", p.type == E_PlayType.Player ?"你" : "电脑");Console.SetCursorPosition(2, h - 3);Console.Write("请按任意键,让{0}开始扔色子", p.type == E_PlayType.Player ? "你" : "电脑");//随机randomNum = r.Next(1, 91);//触发倒退if (randomNum <= 30){p.nowIndex -= 5;if (p.nowIndex < 0){p.nowIndex = 0;}Console.SetCursorPosition(2, h - 3);Console.Write("触发倒退5格");}//触发暂停else if (randomNum <= 60){p.isPause = true;Console.SetCursorPosition(2, h - 3);Console.Write("触发暂停一回合");}//触发交换位置else{int temp = p.nowIndex;p.nowIndex = otherp.nowIndex;otherp.nowIndex = temp;Console.SetCursorPosition(2, h - 3);Console.Write("惊喜,惊喜,双方交换位置");}break;default:break;}}//默认没有结束return false;}

结束场景逻辑实现

static void BeginOrEndScene(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 nowSelIndex = 0;bool isQuitBengin = false;//开始场景逻辑处理函数while (true){Console.SetCursorPosition(nowSceneType == E_SceneType.Begin ? w / 2 - 4 : w / 2 - 5, 13);Console.ForegroundColor = nowSelIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;Console.Write(nowSceneType == E_SceneType.Begin ? "开始游戏" : "回到主菜单");Console.SetCursorPosition(w / 2 - 4, 15);Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;Console.Write("退出游戏");//通过ReadKey可以得到一个输入的枚举类型switch (Console.ReadKey(true).Key){case ConsoleKey.W:--nowSelIndex;if (nowSelIndex < 0){nowSelIndex = 0;}break;case ConsoleKey.S:++nowSelIndex;if (nowSelIndex > 1){nowSelIndex = 1;}break;case ConsoleKey.J:if (nowSelIndex == 0){//进入游戏场景//1.改变当前场景的IDnowSceneType = nowSceneType == E_SceneType.Begin ? E_SceneType.Game : E_SceneType.Begin;//退出当前循环isQuitBengin = true;}else{//退出游戏Environment.Exit(0);}break;}//通过标识决定是否跳出开始场景的循环if (isQuitBengin){break;}}
}

这篇关于简单游戏制作——飞行棋的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1066644

相关文章

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

usaco 1.3 Prime Cryptarithm(简单哈希表暴搜剪枝)

思路: 1. 用一个 hash[ ] 数组存放输入的数字,令 hash[ tmp ]=1 。 2. 一个自定义函数 check( ) ,检查各位是否为输入的数字。 3. 暴搜。第一行数从 100到999,第二行数从 10到99。 4. 剪枝。 代码: /*ID: who jayLANG: C++TASK: crypt1*/#include<stdio.h>bool h

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

poj 1113 凸包+简单几何计算

题意: 给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。 解析: 韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。 用convexhull算法把凸包求出来,然后加加减减就A了。 计算见下图: 好久没玩画图了啊好开心。 代码: #include <iostream>#include <cstdio>#inclu

uva 10130 简单背包

题意: 背包和 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>

国产游戏崛起:技术革新与文化自信的双重推动

近年来,国产游戏行业发展迅猛,技术水平和作品质量均得到了显著提升。特别是以《黑神话:悟空》为代表的一系列优秀作品,成功打破了过去中国游戏市场以手游和网游为主的局限,向全球玩家展示了中国在单机游戏领域的实力与潜力。随着中国开发者在画面渲染、物理引擎、AI 技术和服务器架构等方面取得了显著进展,国产游戏正逐步赢得国际市场的认可。然而,面对全球游戏行业的激烈竞争,国产游戏技术依然面临诸多挑战,未来的

用Unity2D制作一个人物,实现移动、跳起、人物静止和动起来时的动画:中(人物移动、跳起、静止动作)

上回我们学到创建一个地形和一个人物,今天我们实现一下人物实现移动和跳起,依次点击,我们准备创建一个C#文件 创建好我们点击进去,就会跳转到我们的Vision Studio,然后输入这些代码 using UnityEngine;public class Move : MonoBehaviour // 定义一个名为Move的类,继承自MonoBehaviour{private Rigidbo

JAVA用最简单的方法来构建一个高可用的服务端,提升系统可用性

一、什么是提升系统的高可用性 JAVA服务端,顾名思义就是23体验网为用户提供服务的。停工时间,就是不能向用户提供服务的时间。高可用,就是系统具有高度可用性,尽量减少停工时间。如何用最简单的方法来搭建一个高效率可用的服务端JAVA呢? 停工的原因一般有: 服务器故障。例如服务器宕机,服务器网络出现问题,机房或者机架出现问题等;访问量急剧上升,导致服务器压力过大导致访问量急剧上升的原因;时间和

火柴游戏java版

代码 /*** 火柴游戏* <p>* <li>有24根火柴</li>* <li>组成 A + B = C 等式</li>* <li>总共有多少种适合方式?</li>* <br>* <h>分析:</h>* <li>除去"+"、"="四根,最多可用火柴根数20根。</li>* <li>全部用两根组合成"1",最大数值为1111。使用枚举法,A和B范围在0~1111,C为A+B。判断</li>** @