这篇文章主要为大家详细介绍了C#实现简单飞行棋小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了C#实现简单飞行棋小游戏的具体代码,供大家参考,具体内容如下
目标:实现飞行棋游戏基础功能
玩家在地图触发道具:
1、获得道具,可以进行一次选择 1–交换位置 2–让对方退随机格子
2、踩到炸弹,让对方暂停一回合
3、乘上了飞机,前进10格
4、进入隧道,将随机从其他隧道口出来
using System;
namespace FXQGame
{
class Program
{
//储存地图数组
static int[] mMaps = new int[120];
//储存两个玩家的坐标
static int[] mPlayerPos = new int[2];
static string[] mPlayerName = { "玩家一","玩家二" };
//下标控制当前玩家
static int index;
static void Main(string[] args)
{
//游戏头 展示信息
GameTitle();
Console.WriteLine(mPlayerName[0]);
Console.WriteLine(mPlayerName[1]);
Console.WriteLine("游戏玩法:");
Console.WriteLine("输入任意键开始游戏");
//按下任意键进入下一步骤
Console.ReadKey();
//清屏
Console.Clear();
//初始化地图
InitGameMap();
//绘制地图
DrawMap();
//进入游戏
Update();
//游戏结束
if (mPlayerPos[0] > mPlayerPos[1])
{
Console.WriteLine("{0}获得胜利",mPlayerName[0]);
}
else
{
Console.WriteLine("{0}获得胜利", mPlayerName[1]);
}
Console.ReadKey();
}
//游戏逻辑
private static void Update()
{
index = 0;
do
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("{0}按任意键开始投掷骰子", mPlayerName[index % 2]);
Console.ReadKey(true);
int ran = GetRanden(1, 7);
Console.WriteLine("{0}掷出{1}点", mPlayerName[index % 2], ran);
mPlayerPos[index % 2] += ran;
Console.ReadKey(true);
Console.WriteLine("{0}开始行动", mPlayerName[index % 2]);
//玩家A与玩家B接触
if (mPlayerPos[0] == mPlayerPos[1] && mPlayerPos[0] != 0)
{
Console.WriteLine("{0}走到了{1}的背后进行攻击,{2}后退3格", mPlayerName[index % 2], mPlayerName[(index + 1) % 2], mPlayerName[(index + 1) % 2]);
if (mPlayerPos[(index + 1) % 2] - 3 < 0)
{
mPlayerPos[(index + 1) % 2] = 0;
}
else
{
mPlayerPos[(index + 1) % 2] -= 3;
}
Console.ReadKey(true);
}
else//玩家在地图触发道具
{
switch (mMaps[mPlayerPos[index % 2]])
{
case 0:
Console.WriteLine("{0}踩到方块,安全", mPlayerName[index % 2]);
Console.ReadKey(true);
break;
case 1:
Console.WriteLine("{0}获得道具,可以进行一次选择 1--交换位置 2--让对方退随机格子", mPlayerName[index % 2]);
string input = Console.ReadLine();
while (true)
{
if (input == "1")
{
Console.WriteLine("玩家{0}选择跟玩家{1}交换位置", mPlayerName[index % 2], mPlayerName[(index + 1) % 2]);
int temp = mPlayerPos[0];
mPlayerPos[0] = mPlayerPos[1];
mPlayerPos[1] = temp;
Console.WriteLine("交换完毕,请按任意键进行游戏");
Console.ReadKey(true);
break;
}
else if (input == "2")
{
Console.WriteLine("玩家{0}选择让玩家{1}退回随机位置", mPlayerName[index % 2], mPlayerName[(index + 1) % 2]);
int r = GetRanden(1, 7);
if (mPlayerPos[(index + 1) % 2] - r < 0)
{
mPlayerPos[(index + 1) % 2] = 0;
}
else
{
mPlayerPos[(index + 1) % 2] -= r;
}
Console.WriteLine("玩家{0}本回合得退回{1}位置,按任意键进行游戏", mPlayerName[(index + 1) % 2], r);
Console.ReadKey(true);
break;
}
else
{
Console.WriteLine("输入无效字符,请重新输入");
input = Console.ReadLine();
}
}
break;
case 2:
Console.WriteLine("{0}踩到炸弹,让对方暂停一回合", mPlayerName[index % 2]);
index++;
Console.ReadKey(true);
break;
case 3:
Console.WriteLine("{0}乘上了飞机,前进10格", mPlayerName[index % 2]);
mPlayerPos[index % 2] += 10;
Console.ReadKey(true);
break;
case 4:
Console.WriteLine("{0}进入隧道,将随机从其他隧道口出来", mPlayerName[index % 2]);
int[] tunnel = { 20, 25, 45, 63, 72, 88, 90 };
mPlayerPos[index % 2] = tunnel[GetRanden(0, 7)];
Console.WriteLine("{0}从其他隧道口出来", mPlayerName[index % 2], mPlayerPos[index % 2]);
Console.ReadKey(true);
break;
}
}
index++;
Console.Clear();
DrawMap();
}while (mPlayerPos[0] < 99 && mPlayerPos[1] < 99);
//当某一位玩家到达终点时,结束游戏
}
//得到随机数
private static int GetRanden(int min,int max)
{
Random random = new Random();
return random.Next(min, max);
}
//游戏头介绍
private static void GameTitle()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(DateTime.Now);
Console.WriteLine("飞行棋双人小游戏");
Console.WriteLine("************************************");
}
//初始化游戏地图
private static void InitGameMap()
{
//玩家道具 在数组中存储为1 可以让敌人退后随机距离 也可以交换双方位置
int[] planeItem = { 6,23,40,55,69,83 };
for(int i = 0; i< planeItem.Length; i++)
{
mMaps[planeItem[i]] = 1;
}
//炸弹道具 在数组中存储为2 暂停一回合
int[] bombItem = {5,13,17,33,38,50,64,80,94 };
for (int i = 0; i < bombItem.Length; i++)
{
mMaps[bombItem[i]] = 2;
}
//飞机道具 在数组中存储为3 前进10格
int[] propsItem = {9,27,60,93 };
for (int i = 0; i < propsItem.Length; i++)
{
mMaps[propsItem[i]] = 3;
}
//隧道 在数组中存储为4 从其他随机隧道钻出
int[] tunnelItem = {20,25,45,63,72,88,90 };
for (int i = 0; i < tunnelItem.Length; i++)
{
mMaps[tunnelItem[i]] = 4;
}
}
//绘制地图
private static void DrawMap()
{
Console.WriteLine("玩家一用A表示,玩家二用B表示");
Console.WriteLine("图例:□普通方块 ◇道具 ●炸弹 $飞机 ¤隧道");
//第一行
for (int i = 0; i < 30; i++)
{
Draw(i);
}
//第一竖行
Console.WriteLine();
for (int i = 30; i < 35; i++)
{
for (int j = 0; j <= 28; j++)
{//两个空格
Console.Write(" ");
}
Draw(i);
Console.WriteLine();
}
//第二行
for (int i = 64; i >= 35; i--)
{
Draw(i);
}
Console.WriteLine();
//第二竖行
for(int i = 65; i <= 69; i++)
{
Draw(i);
Console.WriteLine();
}
//第三行
for (int i = 70; i <= 99; i++)
{
Draw(i);
}
Console.WriteLine();
}
//判断当前点属于什么特殊属性 并绘制出来
private static void Draw(int i)
{
//当两人重合显示
if (mPlayerPos[0] == mPlayerPos[1] && mPlayerPos[0] == i)
{
Console.Write("<>");
}
else if (mPlayerPos[0] == i)
{//玩家一显示A
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("A");
}
else if (mPlayerPos[1] == i)
{//玩家二显示B
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("B");
}
else
{
switch (mMaps[i])
{
case 0:
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("□");
break;
case 1:
Console.ForegroundColor = ConsoleColor.White;
Console.Write("◇");
break;
case 2:
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write("●");
break;
case 3:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("$");
break;
case 4:
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("¤");
break;
}
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。
沃梦达教程
本文标题为:C#实现简单飞行棋小游戏
猜你喜欢
- Unity3D实现渐变颜色效果 2023-01-16
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- c# 模拟线性回归的示例 2023-03-14
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- Unity Shader实现模糊效果 2023-04-27
- user32.dll 函数说明小结 2022-12-26
- .NET CORE DI 依赖注入 2023-09-27
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- Oracle中for循环的使用方法 2023-07-04
- 如何使用C# 捕获进程输出 2023-03-10