C#实现骑士飞行棋
作者:「已注销」 时间:2022-01-13 18:50:29
前言
飞行棋小游戏是学习C#以来,接触的第一个游戏项目,根据小杨老师的思路引导,自己的代码也实现了功能,经过思路的梳理,试着不借助代码自己去实现功能,感触就是不管想的多明白,实践起来完全不一样,所以,还得多多实践,培养严谨的逻辑思维。下面看看我梳理的思路~
游戏中界面
飞行棋流程思路
掷骰子流程
游戏运行流程
掷骰子代码
public static void RowShaiZi(int xy)
{
Random r = new Random();//随机数
int num = r.Next(1, 7);
string str = "";
Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[xy]);
Console.ReadKey(true);//不显示输入内容,开始掷骰子
Console.WriteLine("{0}掷出了{1}", PlayerNames[xy], num);
Console.WriteLine("{0}按任意键开始行动……", PlayerNames[xy]);
Console.ReadKey(true);
PlayerPos[xy] += num;//玩家坐标累加
CheckPos();//检验玩家坐标是否超出范围方法
if (PlayerPos[xy] == PlayerPos[1- xy])//传进来玩家0,1-0是玩家1;传进来是玩家1,1-0是玩家0
{
str = string.Format ("玩家{0}踩到了玩家{1},玩家{2}退6格", PlayerNames[xy], PlayerNames[1 - xy], PlayerNames[1 - xy]);
PlayerPos[1- xy] -= 6;
CheckPos();
}
else
{
switch (map[PlayerPos[xy]])
{
case 0: str = "行动完毕"; break;
case 1:
str = string.Format("{0}走到了幸运轮盘,请选择1---交换位置,2---轰炸对方", PlayerNames[xy]);
int number = ReadInt(str, 1, 2);//调用走到幸运轮盘,判断输入数字的方法
if (number == 1)//幸运轮盘中输入1
{
int temp = xy;
temp = PlayerPos[xy];
PlayerPos[xy] = PlayerPos[1- xy];
PlayerPos[1- xy] = temp;
str = string.Format("玩家{0}选择了与玩家{1}交换位置", PlayerNames[xy], PlayerNames[1- xy]);
Console.WriteLine(str);
}
else//幸运轮盘中输入2
{
PlayerPos[1- xy] = 0;
str = string.Format("玩家{0}选择了轰炸玩家{1}", PlayerNames[xy], PlayerNames[1- xy]);
Console.WriteLine(str);
}
break;
case 2:
str = "恭喜你,踩到地雷了,退6格";
PlayerPos[xy] -= 6;
CheckPos();
Console.WriteLine(str);
break;
case 3:
str = "踩到暂停了";
Console.WriteLine(str);
flag[xy] = true;
break;
case 4:
str = "恭喜你,幸运转盘让你前进10格";
Console.WriteLine(str);
PlayerPos[xy] += 10;
CheckPos();
break;
}
}
Console.ReadKey();
Console.Clear();
DrawMap();
运行游戏代码
static void Main(string[] args)
{
ShowUI();//游戏头
IntMap();//初始化地图
do//输入玩家A姓名
{
Console.WriteLine("请输入玩家A姓名");
PlayerNames[0] = Console.ReadLine();
if (PlayerNames[0] =="")
{
Console.Write("玩家A姓名不能为空");
}
}
while (PlayerNames[0] == "");
do//输入玩家B姓名
{
Console.WriteLine("请输入玩家B的姓名");
PlayerNames[1] = Console.ReadLine();
if(PlayerNames[1]=="")
{
Console.Write("玩家B姓名不能为空,");
}
if(PlayerNames[0]==PlayerNames[1])
{
Console.Write("玩家B姓名不能与玩家A姓名相同,");
}
} while (PlayerNames[1] == "" || PlayerNames[0] == PlayerNames[1]);
Console.Clear();
ShowUI(); //游戏头
DrawMap();//画地图
Console.ReadKey();
Console.WriteLine("对战开始……");
Console.WriteLine("{0}的士兵用A表示", PlayerNames[0]);
Console.WriteLine("{0}的士兵用B表示", PlayerNames[1]);
while (PlayerPos [0]<=99 && PlayerPos [1]<=99)
{
//玩家1掷骰子
if(flag[0]==false )
{
RowShaiZi(0);
}
else
{
flag[0] = false;
}
//判断玩家1是否胜利
if(PlayerPos[0]==99)
{
Console.WriteLine("恭喜玩家{0}胜利了!!!", PlayerNames[0]);
break;
}
//玩家2掷骰子
if(flag[1]==false )
{
RowShaiZi(1);
}
else
{
flag[1] = false;
}
//判断玩家2是否胜利
if(PlayerPos[1]==99)
{
Console.WriteLine("恭喜玩家{0}胜利了!!!", PlayerNames[1]);
break;
}
Console.WriteLine("行动完毕……");
}
Console.ReadKey();
}
来源:https://blog.csdn.net/Hellen0708/article/details/85016886
标签:C#,飞行棋
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现的最大匹配分词算法详解
2021-12-30 00:50:04
![](https://img.aspxhome.com/file/2023/3/71873_0s.jpg)
基于Java利用static实现单例模式
2021-12-20 06:07:32
Windows10系统下JDK1.8的下载安装及环境变量配置的教程
2022-03-18 18:14:03
![](https://img.aspxhome.com/file/2023/7/62507_0s.jpg)
Java Config下的Spring Test几种方式实例详解
2022-12-17 00:31:36
java实现支付宝退款功能
2021-09-25 14:00:00
![](https://img.aspxhome.com/file/2023/0/62240_0s.jpg)
SpringBoot常用注解详细整理
2022-12-15 18:53:01
![](https://img.aspxhome.com/file/2023/3/75763_0s.jpg)
Java特性队列和栈的堵塞原理解析
2023-10-13 14:15:55
springboot集成与使用Sentinel的方法
2023-08-31 11:22:30
![](https://img.aspxhome.com/file/2023/2/74762_0s.png)
Maven如何打入依赖中指定的部分jar包
2023-09-22 02:50:33
SpringBoot停止启动时测试检查rabbitmq操作
2023-04-06 10:05:05
![](https://img.aspxhome.com/file/2023/1/76321_0s.png)
基于java查找并打印输出字符串中字符出现次数
2022-07-28 19:45:47
Java 实现判定顺序表中是否包含某个元素(思路详解)
2023-09-11 01:03:04
![](https://img.aspxhome.com/file/2023/4/58474_0s.png)
Mybatis的详细使用教程
2022-02-15 21:08:20
Maven 错误找不到符号的解决方法
2021-07-19 09:03:02
![](https://img.aspxhome.com/file/2023/7/62387_0s.png)
java中用String.Join美化代码的实例讲解
2022-03-04 08:17:04
![](https://img.aspxhome.com/file/2023/7/64947_0s.jpg)
利用java开发简易版扫雷游戏
2023-11-07 14:35:15
![](https://img.aspxhome.com/file/2023/9/63489_0s.png)
Java Socket实现单线程通信的方法示例
2022-04-22 15:43:02
Spring Boot 文件上传与下载的示例代码
2021-08-30 02:04:53
![](https://img.aspxhome.com/file/2023/0/62470_0s.png)
java中Memcached的使用实例(包括与Spring整合)
2021-11-28 13:29:06
flutter实现appbar下选项卡切换
2023-06-21 13:35:24
![](https://img.aspxhome.com/file/2023/3/87313_0s.gif)