【C#】飞行棋
【摘要】
文章目录
【前言】【正文】一、游戏头二、初始化地图三、画地图四、画地图的逻辑五、提示输入姓名六、投掷色子
欢迎斧正~
【前言】
飞行棋这个小游戏难点在于画图和逻辑的判断,也就是玩...
【前言】
飞行棋这个小游戏难点在于画图和逻辑的判断,也就是玩家如何行走。下面将详细的讲解一下这个小游戏。
【正文】
一、游戏头
首先是显示游戏头,游戏一打开就要显示游戏头,玩的过程中要隐藏游戏头,等到游戏结束的时候又要显示游戏头。
/// <summary>
/// 显示游戏头
/// </summary>
public static void ShowUI()
{
Console.WriteLine(" *********************************************");
Console.WriteLine(" * *");
Console.WriteLine("* 终极骑士飞行棋 *");
Console.WriteLine(" * *");
Console.WriteLine(" *********************************************");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
玩法规则
Console.WriteLine("对战开始……");
Console.WriteLine("{0}的士兵用A表示……", PlayerNames[0]);
Console.WriteLine("{0}的士兵用B表示……", PlayerNames[1]);
- 1
- 2
- 3
- 4
二、初始化地图
初始化地图的时候要注意游戏规则,幸运转盘、地雷、时空隧道和暂停键的位置和碰到它们的反应
/// <summary>
/// 初始化地图
/// </summary>
public static void InitMap()
{
//初始化地图
//我用0表示普通,显示给用户就是 □
//....1...幸运轮盘,显示组用户就 ◎
//....2...地雷,显示给用户就是 ☆
//....3...暂停,显示给用户就是 ▲
//....4...时空隧道,显示组用户就 卐
int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘◎
int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆
int[] pause = { 9, 27, 60, 93 };//暂停▲
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道卐
//把数组中下标为6, 23, 40, 55, 69, 83的地方的值改为1
for (int i = 0; i < luckyturn.Length; i++)
{
//int temp = luckyturn[i];
Map[luckyturn[i]] = 1;
}
for (int i = 0; i < landMine.Length; i++)
{
Map[landMine[i]] = 2;
}
for (int i = 0; i < pause.Length; i++)
{
Map[pause[i]] = 3;
}
for (int i = 0; i < timeTunnel.Length; i++)
{
Map[timeTunnel[i]] = 4;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
三、画地图
画地图的时候需要注意一下地图上具体位置的具体图标应该是什么,要分清楚行列的位置。
当然很多代码都是重复的,可以另外重新写一个方法。
/// <summary>
/// 画地图
/// </summary>
public static void DrawMap()
{
//图例:幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:卐
Console.WriteLine("图例:幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:卐");
#region 画地图第一横行
DrawMapLeftToRight(0, 29);
//Console.Write(DrawStringMap(i));
#endregion
Console.WriteLine();
#region 画地图第一竖行
for (int i = 30; i <= 34; i++)
{
for (int j = 0; j <= 28; j++)
{
Console.Write(" ");
}
//Console.Write();
Console.WriteLine(DrawStringMap(i));
}
#endregion
#region 画第二横行
for (int i = 64; i >= 35; i--)
{
Console.Write(DrawStringMap(i));
}// end for
#endregion
Console.WriteLine();
#region 画第二竖行
for (int i = 65; i <= 69; i++)
{
Console.WriteLine(DrawStringMap(i));
}
#endregion
#region 画第三横行
DrawMapLeftToRight(70, 99);
//for(int i=70;i<=99;i++)
//{
// Console.Write(DrawStringMap (i));
//}
#endregion
Console.WriteLine();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
/// <summary>
/// 从某个坐标到另一个坐标画地图
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
public static void DrawMapLeftToRight(int left, int right)
{
for (int i = left; i <= right; i++)
{
Console.Write(DrawStringMap(i));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
四、画地图的逻辑
/// <summary>
/// 画地图的逻辑
/// </summary>
/// <param name="pos">传过来的坐标</param>
/// <returns></returns>
public static string DrawStringMap(int pos)
{
string temp = "";
#region 画第一行的逻辑
//如果玩家A和玩家B在一起并且在地图上就画<>
if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == pos)
{
Console.ForegroundColor = ConsoleColor.Yellow;
temp = "<>";
}
else if (PlayerPos[0] == pos)//如果玩家A在地图上就画A
{
Console.ForegroundColor = ConsoleColor.Yellow;
temp = "A";
}
else if (PlayerPos[1] == pos)//如果玩家B在地图上就画B
{
Console.ForegroundColor = ConsoleColor.Yellow;
temp = "B";
}
else
{
switch (Map[pos])//如果玩家A和玩家B不在一起也不在这个坐标上就画该显示的地图图标
{
case 0:
Console.ForegroundColor = ConsoleColor.Gray;//显示颜色
temp = "□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Red;
temp = "◎";
break;
case 2:
Console.ForegroundColor = ConsoleColor.Blue;
temp = "☆";
break;
case 3:
Console.ForegroundColor = ConsoleColor.Green;
temp = "▲";
break;
case 4:
Console.ForegroundColor = ConsoleColor.Magenta;
temp = "卐";
break;
}//end switch
}//end else
return temp;
#endregion
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
五、提示输入姓名
输入玩家的姓名,需要在初始化地图的下面
#region 输入玩家的姓名
Console.WriteLine("请输入玩家A的姓名");
PlayerNames[0] = Console.ReadLine();
while (PlayerNames[0] == "")
{
Console.WriteLine("玩家A的姓名不能为空,请重新输入");
PlayerNames[0] = Console.ReadLine();
}
Console.WriteLine("请输入玩家B的姓名");
PlayerNames[1] = Console.ReadLine();
while (PlayerNames[1] == PlayerNames[0] || PlayerNames[1] == "")
{
if (PlayerNames[1] == PlayerNames[0])
{
Console.WriteLine("玩家B的姓名和玩家A的姓名相同", PlayerNames[0]);
}
else
{
Console.WriteLine("玩家B的姓名为空,请重新输入");
}
PlayerNames[1] = Console.ReadLine();
}
#endregion
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
六、投掷色子
在主函数下面调用投掷色子的方法
while (PlayerPos[0] <= 99 && PlayerPos[1] <= 99)//这种情况不成立
{
#region 玩家A投掷色子
if (flag[0]==false)
{
RowTouZi(0);
}
else
{
flag[0] = false;
}
#endregion
if (PlayerPos[0] == 99)
{
Console.WriteLine("恭喜玩家A胜利了");
break;
}
#region 玩家B掷骰子
if (flag[1] == false)
{
RowTouZi(1);
}
else
{
flag[1] = false;
}
#endregion
if (PlayerPos[1] == 99)
{
Console.WriteLine("恭喜玩家B胜利了");
break;
}
Console.WriteLine("行动完毕……");
}
Win();
Console.ReadKey();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
投掷色子的方法
/// <summary>
/// 投掷骰子的代码
/// </summary>
public static void RowTouZi(int playerPos )
{
Random r = new Random();
int num = r.Next(1, 7);
string msg = "";
Console.WriteLine("{0}按任意键开始掷骰色子", PlayerNames[playerPos]);
ConsoleKeyInfo coninfo = Console.ReadKey(true);
if (coninfo.Key == ConsoleKey.Q)
{
coninfo = Console.ReadKey(true);
if (coninfo.Key == ConsoleKey.A)
{
coninfo = Console.ReadKey(true);
if (coninfo.Key == ConsoleKey.Z)
{
num = 50;
}
}
}
//Console.ReadKey(true);
Console.WriteLine("{0}投掷出了{1}", PlayerNames[0], num );
Console.WriteLine("{0}按任意键开始行动……", PlayerNames[playerPos]);
Console.ReadKey(true);
PlayerPos[playerPos] += num;
CheckPos();
//画地图显示玩家在移动
if (PlayerPos[playerPos] == PlayerPos[1 - playerPos])
{
msg = string.Format("玩家{0}踩到了玩家{1},玩家{2}退6格", PlayerNames[playerPos], PlayerNames[1 - playerPos], PlayerNames[1 - playerPos]);
PlayerPos[1 - playerPos] -= 6;
CheckPos();
}
else
{
switch (Map[PlayerPos[playerPos]])
{
case 0: msg = "行动完了"; break;
case 1:
msg = string.Format("{0}走到了幸运轮盘,请选择 1----交换位置,2----轰炸对方", PlayerNames[playerPos]);
int number = ReadInt(msg, 1, 2);
if (number == 1)
{
int temp = 0;
temp = PlayerPos[playerPos];
PlayerPos[playerPos] = PlayerPos[1 - playerPos];
PlayerPos[1 - playerPos] = temp;
msg = string.Format("玩家{0}选择了与玩家{1}交换位置", PlayerNames[playerPos], PlayerNames[1 - playerPos]);
}
else
{
PlayerPos[1 - playerPos] = 0;
msg = string.Format("玩家{0}选择轰炸玩家{1}", PlayerNames[playerPos], PlayerNames[1 - playerPos]);
}
break;
case 2:
//踩到地雷了
msg = "恭喜你,能踩到地雷,百年不遇,退6格";
PlayerPos[playerPos] -= 6;
CheckPos();
break;
case 3:
msg = "踩到暂停了";
flag[playerPos] = true;
break;
case 4:
msg = "恭喜你,这个猥琐家伙竟然穿越了10步";
PlayerPos[playerPos] += 10;
CheckPos();
break;
}
}
Console.Clear();
DrawMap();
Console.WriteLine(msg);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
public static void CheckPos()
{
if (PlayerPos[0] > 99)
{
PlayerPos[0] = 99;
}
if (PlayerPos[1] > 99)
{
PlayerPos[1] = 99;
}
if (PlayerPos[0] < 0)
{
PlayerPos[0] = 0;
}
if (PlayerPos[1] < 0)
{
PlayerPos[1] = 0;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
胜利的结果显示(这里有点乱码)
//胜利的结果显示
public static void Win()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(" ◆ ");
Console.WriteLine(" ■ ◆ ■ ■");
Console.WriteLine(" ■■■■ ■ ■ ◆■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ◆ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■■■■■■ ■■■■■■■ ■ ■ ■");
Console.WriteLine(" ■■■■ ■ ■ ●■● ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ● ■ ● ■ ■ ■");
Console.WriteLine(" ■ ■ ■■■■■■ ● ■ ● ■ ■ ■");
Console.WriteLine(" ■■■■ ■ ● ■ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ■ ■ ■ ■");
Console.WriteLine(" ■ ■ ■ ■ ■ ■ ");
Console.WriteLine(" ■ ■ ■ ■ ● ■ ");
Console.WriteLine(" ■ ■■ ■■■■■■ ■ ● ●");
Console.ResetColor();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
/// <summary>
/// 根据传过来的数字,返回一个数字
/// </summary>
/// <param name="msg"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public static int ReadInt(string msg, int min, int max)
{
while (true)
{
try
{
Console.WriteLine(msg);
int number = Convert.ToInt32(Console.ReadLine());
if (number >= min && number <= max)
{
return number;
}
else
{
Console.WriteLine("你的输入不合法!只能输入{0}到{1}之间的数字!", min, max);
continue;
}
}
catch
{
Console.WriteLine("输入有误,请重新输入!");
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
欢迎斧正~
文章来源: blog.csdn.net,作者:张艳伟_Laura,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/Laura__zhang/article/details/109747292
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)