c#飞行棋项目
下面我来给大家主要讲解一下C#飞行棋项目。
先来展示下游戏界面
完整版游戏界面:
飞行棋这个游戏的游戏规则我就不多说了,下面主要介绍一下这个游戏的玩法。
1.进入欢迎界面。
2.设置玩家姓名(玩家姓名不能为空,并且不能重复)
3.然后两名玩家分别掷骰子,掷出几点向前移动几步,玩家踩到相应的障碍会执行相应的操作。谁先到达终点谁就获得胜利。
为了让大家更好的理解这个游戏的实现过程,先为大家缕一下整体的思路
1. 画标题
2. 输入玩家姓名
3. 画地图
3.1将地图各个障碍的位置信息储存到map里面
3.2设置每一个位置该画什么图标
3.3绘制地图形状
4.玩游戏的方法
5.修正坐标
我来为大家详细分步介绍一下:
1.画标题
// 改变前景颜色(字体颜色)
关键代码: Console.ForegroundColor = ConsoleColor.DarkRed;
2.输入玩家姓名
1. 首先要定义一个长度为2的数组来储存玩家的姓名信息
static string[] playNames = new string[2];
2. 判断当玩家A的姓名输入的为空的话,将会一直提示不能让用户输入姓名为空直到输入姓名后才
可以跳出循环,并把姓名储存到数组里。
3. 判断当玩家B的姓名输入的为空的话,将会一直提示不能让用户输入姓名为空,并且输入的用户名不能跟玩家A相同直到输入姓名后才可以跳出循环,并把姓名储存到数组里。
3.1将地图各个障碍的位置信息储存到map里面
1.设置map数组来存储地图信息
static int[] map=new int[100];
2.设置各个障碍的位置信息
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 };//时空隧道卐
3.遍历数组将障碍的位置信息储存到map数组里面
3.2设置每一个位置该画什么图标
3.3绘制地图形状
4.玩游戏的方法
1.声明一个玩家坐标数组来储存玩家当前坐标
static int[] playZhuoBiao = new int[2];
2.生成一个1~6的随机数来模拟掷骰子的过程
3.每一次掷完骰子更新玩家坐标,更新地图
5.修正坐标
当玩家坐标小于0的时候让坐标等于0
当玩家坐标大于99的时候让坐标等于99
6.在Main方法里面的主要调用流程
****************************************************************************************
基础版:游戏源码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 飞2
{
class Program
{
//储存玩家姓名
static string[] playNames=new string[2];
//地图信息
static int[] map=new int[100];
//玩家坐标
static int[] playZhuoBiao = new int[2];
//玩家状态
static bool[] ZhuangTai = new bool[2];
static void Main(string[] args)
{
//画标题的方法
HuaBiaoTi();
//设置玩家姓名的方法
ShuRuPlayName();
//设置地图障碍
SheZhiDiTuZhangAi();
// 画地图
HuaDiTu();
while (true)
{
if (ZhuangTai[0]==false)
{
//玩游戏的方法
PlayGame(0);
}
else
{
ZhuangTai[0] = false;
}
if (playZhuoBiao[0]==99)
{
Console.WriteLine("玩家{0}赢得了比赛",playNames[0]);
break;
}
if (ZhuangTai[1]==false)
{
PlayGame(1);
}
else
{
ZhuangTai[1]=false;
}
PlayGame(1);
if (playZhuoBiao[1] == 99)
{
Console.WriteLine("玩家{0}赢得了比赛", playNames[1]);
break;
}
}
Console.ReadKey();
}
/// <summary>
/// 画标题
/// </summary>
static void HuaBiaoTi() {
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine("**********************************************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("**********************************************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("*****************欢迎来到飞行棋***************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("**********************************************");
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine("***********************************LYC********");
Console.ForegroundColor = ConsoleColor.White;
}
/// <summary>
/// 设置玩家姓名
/// </summary>
static void ShuRuPlayName() {
Console.WriteLine("请输入玩家A的姓名");
playNames[0] = Console.ReadLine();
while (true)
{
if (playNames[0] == "")
{
Console.WriteLine("玩家的姓名不能为空请从新输入");
playNames[0] = Console.ReadLine();
}
else
{
Console.WriteLine("玩家{0},已上线", playNames[0]);
break;
}
}
Console.WriteLine("请输入玩家B的姓名");
playNames[1] = Console.ReadLine();
while (true)
{
if (playNames[1] == "")
{
Console.WriteLine("玩家的姓名不能为空请从新输入");
playNames[1] = Console.ReadLine();
}
else if (playNames[0]==playNames[1])
{
Console.WriteLine("玩家姓名不能相同请从新输入");
playNames[1] = Console.ReadLine();
}
else
{
Console.WriteLine("玩家{0},已上线", playNames[1]);
break;
}
}
}
/// <summary>
/// 设置地图障碍
/// </summary>
static void SheZhiDiTuZhangAi() {
//幸运轮盘◎
int[] luckyTurn = { 6, 23, 40, 55, 69, 83 };
for (int i = 0; i < luckyTurn.Length; i++)
{
map[luckyTurn[i]] = 1;
}
//地雷☆
int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };
for (int i = 0; i < landMine.Length; i++)
{
map[landMine[i]] = 2;
}
//暂停▲
int[] pause = { 9, 27, 60, 93 };
for (int i = 0; i < pause.Length; i++)
{
map[pause[i]] = 3;
}
//时空隧道卐
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };
for (int i = 0; i < timeTunnel.Length; i++)
{
map[timeTunnel[i]] = 4;
}
}
/// <summary>
/// 设置地图信息
/// </summary>
static string SheZhiDiTuXinXi(int i) {
string str="";
if (playZhuoBiao[0]==playZhuoBiao[1]&&playZhuoBiao[0]==i)
{
str = "<>";
}
else if (playZhuoBiao[0]==i)
{
Console.ForegroundColor = ConsoleColor.White;
str = "A";
}
else if (playZhuoBiao[1]==i)
{
Console.ForegroundColor = ConsoleColor.White;
str = "B";
}
else
{
switch (map[i])
{
case 0:
Console.ForegroundColor = ConsoleColor.White;
str = "□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Yellow;
str = "◎";
break;
case 2:
Console.ForegroundColor = ConsoleColor.Red;
str = "☆";
break;
case 3:
Console.ForegroundColor = ConsoleColor.Green;
str = "▲";
break;
case 4:
Console.ForegroundColor = ConsoleColor.Blue;
str = "卐";
break;
}
}
return str;
}
/// <summary>
/// 画地图的方法
/// </summary>
static void HuaDiTu() {
for (int i = 0; i < 30; i++)
{
Console.Write(SheZhiDiTuXinXi(i));
}
Console.WriteLine();
for (int i = 30; i < 35; i++)
{
for (int j = 0; j < 29; j++)
{
Console.Write(" ");
}
Console.WriteLine(SheZhiDiTuXinXi(i));
}
for (int i = 64; i > 34; i--)
{
Console.Write(SheZhiDiTuXinXi(i));
}
Console.WriteLine();
for (int i = 65; i < 70; i++)
{
Console.WriteLine(SheZhiDiTuXinXi(i));
}
for (int i = 70; i < 100; i++)
{
Console.Write(SheZhiDiTuXinXi(i));
}
Console.WriteLine();
}
/// <summary>
/// 玩游戏的方法
/// </summary>
static void PlayGame(int i) {
Console.WriteLine("请玩家{0}按掷骰子(按任意键继续)",playNames[i]);
Console.ReadKey(true);
//生成1~6的随机数
Random r = new Random();
int rNum = r.Next(1, 7);
Console.WriteLine("请玩家{0}掷c出的点数为{1}。",playNames[i],rNum);
playZhuoBiao[i] += rNum;
//修正坐标方法
XiuZheng();
switch (map[playZhuoBiao[i]])
{
case 0:
Console.WriteLine("玩家{0}踩中了方块什么也没发生",playNames[i]);
break;
case 1:
#region 玩家{0}踩中了幸运轮盘。请选择:1.双方交换位置。2.轰炸对方使对方后退6步。
Console.WriteLine("玩家{0}踩中了幸运轮盘。请选择:1.双方交换位置。2.轰炸对方使对方后退6步。", playNames[i]);
string ShuRu = Console.ReadLine();
while (true)
{
if (ShuRu == "1")
{
Console.WriteLine("您选择了1.双方交换位置。");
int temp = playZhuoBiao[i];
playZhuoBiao[i] = playZhuoBiao[1 - i];
playZhuoBiao[1 - i] = temp;
break;
}
else if (ShuRu == "2")
{
Console.WriteLine("您选择了:2.轰炸对方使对方后退6步。");
playZhuoBiao[1 - i] -= 6;
break;
}
else
{
Console.WriteLine("您输入的格式不正确请从新输入:1.双方交换位置。2.轰炸对方使对方后退6步。");
ShuRu = Console.ReadLine();
}
}
#endregion
break;
case 2:
Console.WriteLine("玩家{0}踩中了地雷,后退6步", playNames[i]);
playZhuoBiao[i] -= 6;
break;
case 3:
Console.WriteLine("玩家{0}踩中了暂停,暂停11回合", playNames[i]);
ZhuangTai[i] = true;
break;
case 4:
Console.WriteLine("玩家{0}踩中了时空隧道,前进10步", playNames[i]);
playZhuoBiao[i] += 10;
break;
}
//修正坐标的方法
XiuZheng();
Console.ReadKey(true);
//清屏
Console.Clear();
//更新地图
HuaDiTu();
}
static void XiuZheng() {
if (playZhuoBiao[0]<0)
{
playZhuoBiao[0] = 0;
}
if (playZhuoBiao[0]>99)
{
playZhuoBiao[0] = 99;
}
if (playZhuoBiao[1] < 0)
{
playZhuoBiao[1] = 0;
}
if (playZhuoBiao[1] > 99)
{
playZhuoBiao[1] = 99;
}
}
}
}
*********************************************************************************************
完整版:游戏代码
添加的新功能
1.可以选择与电脑对战
2.可以选择游戏难度
3.优化了游戏界面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 飞3
{
class Program
{
static string[] playNames = new string[2];
static int[] map=new int[100];
static int[] playZhuoBiao=new int[2];
static bool[] ZhuangTai = new bool[2];
static int[] moShi =new int[2];
static string nandunName;
static bool dianNao;
static void Main(string[] args)
{
HuaBiaoTi();
XuanZhe();
NanDu();
SheZhiWanJiaName();
Console.Clear();
SheZhiZhangAi();
HuaDiTu();
while (true)
{
if (ZhuangTai[0]==false)
{
PlayGame(0);
}
else
{
ZhuangTai[0] = false;
}
if (playZhuoBiao[0]==99)
{
Console.WriteLine("玩家{0}获得游戏胜利",playNames[0]);
break;
}
if (playZhuoBiao[1] == 99)
{
Console.WriteLine("玩家{0}获得游戏胜利", playNames[1]);
break;
}
if (ZhuangTai[1] == false)
{
PlayGame(1);
}
else
{
ZhuangTai[1] = false;
}
}
Console.ReadKey();
}
/// <summary>
/// 画标题的方法
/// </summary>
static void HuaBiaoTi()
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷");
Console.WriteLine("∷ ∷ ∷ ∷");
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine("∷ ∷ ∷ ∷");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("∷∷∷∷∷∷∷∷ ");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write("欢迎来到飞行棋");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(" ∷∷∷∷∷∷∷∷");
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine("∷ ∷ ∷ ∷");
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("∷ ∷ ");
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("LYC");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" ∷ ∷");
Console.WriteLine("∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷∷");
Console.ForegroundColor = ConsoleColor.White;
}
/// <summary>
/// 设置角色名字的方法
/// </summary>
static void SheZhiWanJiaName()
{
Console.WriteLine("请输入玩家A的姓名");
playNames[0] = Console.ReadLine();
while (true)
{
if (playNames[0] == "")
{
Console.WriteLine("玩家的姓名不能为空请从新输入");
playNames[0] = Console.ReadLine();
}
else
{
Console.WriteLine("玩家{0}已上线", playNames[0]);
break;
}
}
if (dianNao)
{
Console.WriteLine("请输入玩家B的姓名");
playNames[1] = Console.ReadLine();
while (true)
{
if (playNames[1] == "")
{
Console.WriteLine("玩家的姓名不能为空请从新输入");
playNames[1] = Console.ReadLine();
}
else if (playNames[0] == playNames[1])
{
Console.WriteLine("玩家名不能相同,请从新输入");
playNames[1] = Console.ReadLine();
}
else
{
Console.WriteLine("玩家{0}已上线", playNames[1]);
break;
}
}
}
}
/// <summary>
/// 设置障碍的方法
/// </summary>
static void SheZhiZhangAi()
{
int[] luckyTurn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘◎
for (int i = 0; i < luckyTurn.Length; i++)
{
map[luckyTurn[i]]=1;
}
int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆
for (int i = 0; i < landMine.Length; i++)
{
map[landMine[i]] = 2;
}
int[] pause = { 9, 27, 60, 93 };//暂停▲
for (int i = 0; i < pause.Length; i++)
{
map[pause[i]] = 3;
}
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道卐
for (int i = 0; i < timeTunnel.Length; i++)
{
map[timeTunnel[i]] = 4;
}
}
/// <summary>
/// 设置地图信息
/// </summary>
static string SheZhiDiTuXinXi(int i) {
string str = "";
if (playZhuoBiao[0]==playZhuoBiao[1]&&playZhuoBiao[0]==i)
{
str = "<>";
}
else if (playZhuoBiao[0]==i)
{
str = "Α";
}
else if (playZhuoBiao[1]==i)
{
str = "Β";
}
else
{
switch (map[i])
{
case 0:
Console.ForegroundColor = ConsoleColor.White;
str = "□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Yellow;
str = "◎";
break;
case 2:
Console.ForegroundColor = ConsoleColor.Red;
str = "☆";
break;
case 3:
Console.ForegroundColor = ConsoleColor.Green;
str = "▲";
break;
case 4:
Console.ForegroundColor = ConsoleColor.Blue;
str = "卐";
break;
}
}
return str;
}
/// <summary>
/// 画地图的方法
/// </summary>
static void HuaDiTu() {
Random r = new Random();
int rNnmC = r.Next(1, 9);
switch (rNnmC)
{
case 1:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case 2:
Console.ForegroundColor = ConsoleColor.Blue;
break;
case 3:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case 4:
Console.ForegroundColor = ConsoleColor.Red;
break;
case 5:
Console.ForegroundColor = ConsoleColor.Magenta;
break;
case 6:
Console.ForegroundColor = ConsoleColor.DarkGreen;
break;
case 7:
Console.ForegroundColor = ConsoleColor.DarkCyan;
break;
case 8:
Console.ForegroundColor = ConsoleColor.Cyan;
break;
}
Console.WriteLine("ΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞ");
Console.WriteLine("Э A 代表:{0} ", playNames[0]);
Console.WriteLine("Э B 代表:{0} ◎幸运轮盘 ☆地雷 ▲暂停 卐时空隧道", playNames[1]);
Console.WriteLine("Э 游戏模式:{0} ", nandunName);
Console.WriteLine("ΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞΞ");
Console.ForegroundColor = ConsoleColor.White;
for (int i = 0; i < 30; i++)
{
Console.Write(SheZhiDiTuXinXi(i));
}
Console.WriteLine();
for (int i = 30; i < 35; i++)
{
for (int j = 0; j < 29; j++)
{
Console.Write(" ");
}
Console.WriteLine(SheZhiDiTuXinXi(i));
}
for (int i = 64; i >34; i--)
{
Console.Write(SheZhiDiTuXinXi(i));
}
Console.WriteLine();
for (int i = 65; i < 70; i++)
{
Console.WriteLine(SheZhiDiTuXinXi(i));
}
for (int i = 70; i < 100; i++)
{
Console.Write(SheZhiDiTuXinXi(i));
}
Console.WriteLine();
}
/// <summary>
/// 玩游戏的方法
/// </summary>
static void PlayGame(int i ) {
Console.WriteLine("玩家{0}开始掷骰子(按任意键继续)",playNames[i]);
if (dianNao==true||i==0)
{
Console.ReadKey(true);
}
Random r = new Random();
int rNum = r.Next(1, 7);
Console.WriteLine("玩家{0}掷出的点数为:{1}.(按任意键继续)",playNames[i],rNum);
playZhuoBiao[i] += rNum;
XiuZheng();
if (playZhuoBiao[0]==playZhuoBiao[1])
{
Console.WriteLine("玩家{0}攻击了玩家{1},玩家{2}退后6步",playNames[i],playNames[1-i],playNames[1-i]);
playZhuoBiao[1 - i] -= 6;
}
switch (map[playZhuoBiao[i]])
{
case 0:
Console.WriteLine("玩家{0}踩中了方块什么也没发生", playNames[i]);
break;
case 1:
if (dianNao==false&&i==1)
{
Console.WriteLine("{0}踩中了幸运轮盘。请选择:1.双方交换位置。2.轰炸对方使对方后退{1}步。", playNames[i], moShi[0]);
if (playZhuoBiao[i]>playZhuoBiao[1-i])
{
Console.WriteLine("电脑选择了2.轰炸对对方,对方后退{0}步。。", moShi[0]);
playZhuoBiao[1 - i] -= moShi[0];
}
else
{
Console.WriteLine("电脑选择了:1.双方交换位置");
int temp = playZhuoBiao[i];
playZhuoBiao[i] = playZhuoBiao[1 - i];
playZhuoBiao[1 - i] = temp;
}
Console.WriteLine("按任意键继续");
Console.ReadKey();
}
else
{
Console.WriteLine("玩家{0}踩中了幸运轮盘。请选择:1.双方交换位置。2.轰炸对方使对方后退6步。", playNames[i]);
string ShuRu = Console.ReadLine();
while (true)
{
if (ShuRu == "1")
{
Console.WriteLine("您选择了1.双方交换位置。");
int temp = playZhuoBiao[i];
playZhuoBiao[i] = playZhuoBiao[1 - i];
playZhuoBiao[1 - i] = temp;
break;
}
else if (ShuRu == "2")
{
Console.WriteLine("您选择了:2.轰炸对方使对方后退6步。");
playZhuoBiao[1 - i] -=6;
break;
}
else
{
Console.WriteLine("您输入的格式不正确请从新输入:1.双方交换位置。2.轰炸对方使对方后退6步。");
ShuRu = Console.ReadLine();
}
}
}
break;
case 2:
if (dianNao == false && i == 1)
{
Console.WriteLine("{0}踩中了地雷,后退6步", playNames[i]);
playZhuoBiao[i] -= 6;
}
else
{
Console.WriteLine("玩家{0}踩中了地雷,后退{1}步", playNames[i],moShi[0]);
playZhuoBiao[i] -= moShi[0];
}
break;
case 3:
Console.WriteLine("玩家{0}踩中了暂停,暂停1回合", playNames[i]);
ZhuangTai[i] = true;
break;
case 4:
if (dianNao == false && i == 1)
{
Console.WriteLine("{0}踩中了时空隧道,前进10步", playNames[i]);
playZhuoBiao[i] += 10;
}
else
{
Console.WriteLine("玩家{0}踩中了时空隧道,前进{1}步", playNames[i], moShi[1]);
playZhuoBiao[i] += moShi[1];
}
break;
}
XiuZheng();
Console.ReadKey(true);
Console.Clear();
HuaDiTu();
}
/// <summary>
/// 修正方法
/// </summary>
static void XiuZheng() {
if (playZhuoBiao[0]<0)
{
playZhuoBiao[0] = 0;
}
if (playZhuoBiao[0]>99)
{
playZhuoBiao[0] = 99;
}
if (playZhuoBiao[1] < 0)
{
playZhuoBiao[1] = 0;
}
if (playZhuoBiao[1] > 99)
{
playZhuoBiao[1] = 99;
}
}
/// <summary>
/// 选择
/// </summary>
static void XuanZhe() {
Console.WriteLine("请选择:1.与电脑对战。2.与玩家对战");
string xuanZhe = Console.ReadLine();
while (true)
{
if (xuanZhe == "1")
{
dianNao = false;
playNames[1] = "电脑";
break;
}
else if (xuanZhe == "2")
{
dianNao = true;
break;
}
else
{
Console.WriteLine("您输入有误,请从新输入:1.与电脑对战。2.与玩家对战");
xuanZhe = Console.ReadLine();
}
}
}
/// <summary>
/// 设置难度
/// </summary>
static void NanDu(){
if ( dianNao == false)
{
Console.WriteLine("请选择难度:1.普通 2.困难");
string nanDu = Console.ReadLine();
while (true)
{
if (nanDu == "1")
{
Console.WriteLine("您选择的是普通模式");
moShi[0] = 6;
moShi[1] = 10;
nandunName = "普通模式";
break;
}
else if (nanDu == "2")
{
Console.WriteLine("您选择的是困难模式");
moShi[0] = 10;
moShi[1] = 5;
nandunName = "困难模式";
break;
}
else
{
Console.WriteLine("您输入的格式有误请从新输入");
nanDu = Console.ReadLine();
}
}
}
else
{
moShi[0] = 6;
moShi[1] = 10;
nandunName = "普通模式";
}
}
}
}
本作品主要是为了跟大家交流和分享一下学习经验,如有什么不对的地方还请大家见谅。
原创作品(纯手打)