C#实现简单飞行棋小游戏
本文实例为大家分享了C#实现简单飞行棋小游戏的具体代码,供大家参考,具体内容如下
目标:实现飞行棋游戏基础功能
玩家在地图触发道具:
1、获得道具,可以进行一次选择1–交换位置2–让对方退随机格子
2、踩到炸弹,让对方暂停一回合
3、乘上了飞机,前进10格
4、进入隧道,将随机从其他隧道口出来
usingSystem;
namespaceFXQGame
{
classProgram
{
//储存地图数组
staticint[]mMaps=newint[120];
//储存两个玩家的坐标
staticint[]mPlayerPos=newint[2];
staticstring[]mPlayerName={"玩家一","玩家二"};
//下标控制当前玩家
staticintindex;
staticvoidMain(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();
}
//游戏逻辑
privatestaticvoidUpdate()
{
index=0;
do
{
Console.ForegroundColor=ConsoleColor.Green;
Console.WriteLine("{0}按任意键开始投掷骰子",mPlayerName[index%2]);
Console.ReadKey(true);
intran=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]])
{
case0:
Console.WriteLine("{0}踩到方块,安全",mPlayerName[index%2]);
Console.ReadKey(true);
break;
case1:
Console.WriteLine("{0}获得道具,可以进行一次选择1--交换位置2--让对方退随机格子",mPlayerName[index%2]);
stringinput=Console.ReadLine();
while(true)
{
if(input=="1")
{
Console.WriteLine("玩家{0}选择跟玩家{1}交换位置",mPlayerName[index%2],mPlayerName[(index+1)%2]);
inttemp=mPlayerPos[0];
mPlayerPos[0]=mPlayerPos[1];
mPlayerPos[1]=temp;
Console.WriteLine("交换完毕,请按任意键进行游戏");
Console.ReadKey(true);
break;
}
elseif(input=="2")
{
Console.WriteLine("玩家{0}选择让玩家{1}退回随机位置",mPlayerName[index%2],mPlayerName[(index+1)%2]);
intr=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;
case2:
Console.WriteLine("{0}踩到炸弹,让对方暂停一回合",mPlayerName[index%2]);
index++;
Console.ReadKey(true);
break;
case3:
Console.WriteLine("{0}乘上了飞机,前进10格",mPlayerName[index%2]);
mPlayerPos[index%2]+=10;
Console.ReadKey(true);
break;
case4:
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);
//当某一位玩家到达终点时,结束游戏
}
//得到随机数
privatestaticintGetRanden(intmin,intmax)
{
Randomrandom=newRandom();
returnrandom.Next(min,max);
}
//游戏头介绍
privatestaticvoidGameTitle()
{
Console.ForegroundColor=ConsoleColor.Green;
Console.WriteLine(DateTime.Now);
Console.WriteLine("飞行棋双人小游戏");
Console.WriteLine("************************************");
}
//初始化游戏地图
privatestaticvoidInitGameMap()
{
//玩家道具在数组中存储为1可以让敌人退后随机距离也可以交换双方位置
int[]planeItem={6,23,40,55,69,83};
for(inti=0;i=35;i--)
{
Draw(i);
}
Console.WriteLine();
//第二竖行
for(inti=65;i<=69;i++)
{
Draw(i);
Console.WriteLine();
}
//第三行
for(inti=70;i<=99;i++)
{
Draw(i);
}
Console.WriteLine();
}
//判断当前点属于什么特殊属性并绘制出来
privatestaticvoidDraw(inti)
{
//当两人重合显示
if(mPlayerPos[0]==mPlayerPos[1]&&mPlayerPos[0]==i)
{
Console.Write("<>");
}
elseif(mPlayerPos[0]==i)
{//玩家一显示A
Console.ForegroundColor=ConsoleColor.Red;
Console.Write("A");
}
elseif(mPlayerPos[1]==i)
{//玩家二显示B
Console.ForegroundColor=ConsoleColor.Red;
Console.Write("B");
}
else
{
switch(mMaps[i])
{
case0:
Console.ForegroundColor=ConsoleColor.Blue;
Console.Write("□");
break;
case1:
Console.ForegroundColor=ConsoleColor.White;
Console.Write("◇");
break;
case2:
Console.ForegroundColor=ConsoleColor.DarkGreen;
Console.Write("●");
break;
case3:
Console.ForegroundColor=ConsoleColor.Yellow;
Console.Write("$");
break;
case4:
Console.ForegroundColor=ConsoleColor.Cyan;
Console.Write("¤");
break;
}
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。