当前位置:网站首页>C#编写飞行棋游戏
C#编写飞行棋游戏
2022-08-08 23:55:00 【无悔青春_j进无止境】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;//绘图
namespace PlaneDemo3
{
class Program
{
public static string[] playerName=new string[2];//定义玩家的名称的数组
public static int[] Maps=new int[100];//定义整型的地图数组
public static int[] playerPos = new int[2];//定义玩家坐标
public static bool[] flags=new bool[2];//定义标识符
static void Main(string[] args)
{
GameHead();
#region 玩家输入
Console.WriteLine("请输入第一位玩家的昵称:");
playerName[0] = Console.ReadLine();
while (playerName[0]=="") //playerName[0]==""可以替换为 string.IsNullOrWhiteSpace(playerName[0])
{
Console.WriteLine("用户名不能为空,请重新输入.");
playerName[0] = Console.ReadLine();
}
Console.WriteLine("请输入第二位玩家的昵称:");
playerName[1] = Console.ReadLine();
while (playerName[1]==playerName[0]||playerName[1]=="")
{
if (playerName[1]==playerName[0])
{
Console.WriteLine("昵称重复出现,请重新输入.");
playerName[1] = Console.ReadLine();
}
else
{
Console.WriteLine("用户名不能为空,请重新输入.");
playerName[1] = Console.ReadLine();
}
}
#endregion
Console.Clear();//清空
GameHead();
Console.WriteLine("游戏开始啦");
Console.WriteLine("我们用A代表第一位玩家{0}",playerName[0]);
Console.WriteLine("我们用B代表第二位玩家{0}", playerName[1]);
Console.ReadKey(true);
InitialMap();//初始化地图
drawMap();//画地图
#region 依据游戏规则开始
while (playerPos[0]<99||playerPos[1]<99)
{
#region 玩家1
if (flags[0]==false)
{
PlayGame(0);
}
else
{
flags[0] = false;
}
if (playerPos[0]>=99)
{
Console.WriteLine("玩家{0}最终获得了胜利!!!",playerName[0]);
break;
}
#endregion
#region 玩家2
if (flags[1]==false)
{
PlayGame(1);
}
else
{
flags[1] = false;
}
if (playerPos[1] >= 99)
{
Console.WriteLine("玩家{0}最终获得了胜利!!!", playerName[1]);
break;
}
#endregion
}
#endregion
Win();
Console.ReadKey();
}
/// <summary>
/// 绘制游戏头
/// </summary>
public static void GameHead()
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("*******************************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("***********飞行棋5.1***********");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("*******************************");
Console.ForegroundColor = ConsoleColor.White;
}
//初始化地图
public static void InitialMap()
{
//幸运轮盘
int[] luckyTurn = { 6,16,36,66};
for (int i = 0; i < luckyTurn.Length; i++)
{
Maps[luckyTurn[i]] = 1;
}
//地雷
int[] landMine = { 4,25,48,74,89};
for (int i = 0; i < landMine.Length; i++)
{
Maps[landMine[i]] = 2;
}
//暂停
int[] pause = { 12,15,58,69,80,90};
for (int i = 0; i < pause.Length; i++)
{
Maps[pause[i]] = 3;
}
//时空隧道
int[] space = { 30,59,76,90};
for (int i = 0; i < space.Length; i++)
{
Maps[space[i]] = 4;
}
}
//画地图
public static void drawMap()
{
#region 第一横行
for (int i = 0; i <= 29; i++)
{
Console.Write(drawStringMap(i));
}
Console.WriteLine();
#endregion
#region 第一竖行
for (int i = 30; i <= 34; i++)
{
for (int j = 0; j <= 29; j++)
{
Console.Write(" ");
}
Console.WriteLine(drawStringMap(i));
}
#endregion
#region 第二横行
for (int i = 64; i >= 35; i--)
{
Console.Write(drawStringMap(i));
}
Console.WriteLine();
#endregion
#region 第二竖行
for (int i = 65; i < 70; i++)
{
Console.WriteLine(drawStringMap(i));
}
#endregion
#region 第三横行
for (int i = 70; i < 100; i++)
{
Console.Write(drawStringMap(i));
}
Console.WriteLine();
#endregion
}
//抽象出地图的形状
public static string drawStringMap(int i)
{
// □ ◎ * ▲ 卐
string str = "";
if (playerPos[0]==playerPos[1]&&playerPos[1]==i)
{
Console.Write("<>");
}
else if (playerPos[0]==i)
{
Console.Write("A");
}
else if (playerPos[1]==i)
{
Console.Write("B");
}
else
{
switch (Maps[i])
{
case 0:
Console.ForegroundColor = ConsoleColor.DarkGreen;
str = "□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.DarkRed;
str = "◎";
break;
case 2:
Console.ForegroundColor = ConsoleColor.DarkBlue;
str = "*";
break;
case 3:
Console.ForegroundColor = ConsoleColor.DarkYellow;
str = "▲";
break;
case 4:
Console.ForegroundColor = ConsoleColor.DarkCyan;
str = "卐";
break;
default:
break;
}
}
return str;
}
//玩游戏
public static void PlayGame(int playerNumber)
{
Console.WriteLine("请按任意键开始游戏");
Console.ReadKey(true);
#region 掷骰子
Random r = new Random();
int rNumber=r.Next(1,7);
#endregion
Console.WriteLine("玩家{0}掷出了{1}.",playerName[playerNumber],rNumber);
Console.ReadKey(true);
Console.WriteLine("玩家{0}开始行动",playerName[playerNumber]);
Console.ReadKey(true);
playerPos[playerNumber] += rNumber;
chanPos();
Console.WriteLine("玩家{0}前进了{1}格",playerName[playerNumber],rNumber);
if (playerPos[1]==playerPos[0])
{
Console.ReadKey(true);
Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}需要后退6格.",playerName[playerNumber],playerName[1-playerNumber],playerName[1-playerNumber]);
playerPos[1 - playerNumber] -= 6;
chanPos();
Console.ReadKey(true);
Console.WriteLine("玩家{0}已经行动结束",playerName[1-playerNumber]);
}
else
{
Console.ReadKey(true);
switch (Maps[playerPos[playerNumber]])
{
case 0:
//Console.ReadKey(true);
Console.WriteLine("玩家踩到方格,什么事也不会发生");
//Console.ReadKey(true);
break;
case 1:
Console.WriteLine("玩家{0}踩到幸运轮盘,可以选择: 1 前进6格, 2 轰炸对方.",playerName[playerNumber]);
string input = Console.ReadLine();
while (input=="")
{
Console.ReadKey(true);
Console.WriteLine("请重新输入");
input = Console.ReadLine();
}
if (input=="1")
{
Console.ReadKey(true);
Console.WriteLine("玩家{0}可以前进6格",playerName[playerNumber]);
playerPos[playerNumber] += 6;
chanPos();
Console.WriteLine("行动完成");
}
if (input=="2")
{
Console.ReadKey(true);
Console.WriteLine("玩家{0}选择轰炸对方,玩家{1}需要后退8格",playerName[playerNumber],playerName[1-playerNumber]);
playerPos[1 - playerNumber] -= 8;
chanPos();
Console.WriteLine("行动完成");
}
break;
case 2:
Console.ReadKey(true);
Console.WriteLine("玩家{0}踩到地雷,需要后退6格",playerName[playerNumber]);
playerPos[playerNumber] -= 6;
chanPos();
break;
case 3:
Console.ReadKey(true);
Console.WriteLine("玩家{0}被暂停一局",playerName[playerNumber]);
flags[playerNumber] = true;
break;
case 4:
Console.ReadKey(true);
Console.WriteLine("玩家{0}进行时空穿梭,瞬间传送10格",playerName[playerNumber]);
playerPos[playerNumber] += 10;
chanPos();
break;
default:
break;
}
}
Console.Clear();
drawMap();
}
//判断玩家是否出界
public static void chanPos()
{
if (playerPos[0]<=0)
{
playerPos[0] = 0;
}
if (playerPos[0]>=99)
{
playerPos[0] = 99;
}
if (playerPos[1] <= 0)
{
playerPos[1] = 0;
}
if (playerPos[1] >= 99)
{
playerPos[1] = 99;
}
}
//获胜
public static void Win()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("*****************************");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("************胜利************");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("*****************************");
}
}
}
边栏推荐
猜你喜欢
一 C#中通过USB操作手机文件
Risk Control Modeling II: Modeling Scheme formulation
测试用例的原则、缺陷报告怎么写你都知道吗?
Introduction to basic grammar and the foundation of freemarker
Excel 2013 下拉为“快速分拆”调整为“填充序号”
【深度学习】TensorFlow学习之路一:TensorFlow简介及线性回归、逻辑回归实现
不躺平,然后做到极致,就是最大的“安全感”
HCIP2---第一天实验
03 Spark on 读取内部数据分区策略(源码角度分析)
SAP ABAP debug的七种方法及错误消息定位
随机推荐
Kubernetes web网站无法访问
并发编程第11篇,线程池的一些常用用法和使用
51nod 2887 抓小偷 平面图最小割转换成最短路
第六章 数据库安全与保护
11 Spark on RDD CheckPoint
08 Spark on RDD 依赖关系
05 Spark on 读取内部数据分区存储策略(源码角度分析)
风控建模四:逻辑回归评分卡开发
OSPF总结作业
JS基础-数组
linux环境安装mysql和使用中的常见问题
八 Node.js中使用MySQL
记录一些 PostgreSQL问题分析思路
一命令删除所有指定进程
360“卸载不下去”引热议 周鸿祎重申是谣言:步骤繁琐出于安全考虑
51nod1656 合并trie树
MES对接Simba实现展讯平台 IMEI 写号与耦合测试
机器学习建模高级用法!构建企业级AI建模流水线
Get the time n-1 a week ago including the current day 7 days a week 7-1
测试用例的原则、缺陷报告怎么写你都知道吗?