C#小游戏–飞行棋
这个项目是我初学C#时,跟随老赵写的项目。这个小游戏,是一个综合案例,包含面向对象以前的全面内容,初学者建议自己写一下这个项目,会有很大的收获。
using System;
namespace 飞行棋
{
class Program
//用静态字段模拟变量
{ public static int[] Maps = new int[100];
//声明一个静态数组用来存储玩家A跟玩家B的坐标;
public static int[] PlayPos = new int[2];
//存储两个玩家的姓名
public static string[] PlayerNames = new string[2];
//玩家标记
public static bool[] flag = new bool[2];//flag[0]是false,flag[1]默认也是false
static void Main(string[] args)
{
//调用游戏头
GameShow();
#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[1] == PlayerNames[0])
{
if (PlayerNames[1] == "")
{
Console.WriteLine("玩家B的姓名不能为空,请重新输入");
PlayerNames[1] = Console.ReadLine();
}
if (PlayerNames[1] == PlayerNames[0])
{
Console.WriteLine("玩家A和玩家B的姓名不能相同,请重新输入");
PlayerNames[1] = Console.ReadLine();
}
}
#endregion
//玩家姓名输入OK之后,我们首先应该清屏
Console.Clear();
GameShow();
Console.WriteLine("{0}的士兵用A表示", PlayerNames[0]);
Console.WriteLine("{0}的士兵用B表示", PlayerNames[1]);
//在画地图之前,应该初始化地图
InitailMap();
DrawMap();
//当玩家A跟玩家B没有一个在终点的时候 两个玩家不停的玩游戏
while(PlayPos[0]<99&&PlayPos[1]<99)
{
if(flag[0]==false)
{
Playgame(0);
}
else
{
flag[0] = false;
}
0 if(PlayPos[0]>=99)
{
Console.WriteLine("玩家{0}无耻的赢了玩家{1}", PlayerNames[0], PlayerNames[1]);
break;
}
if(flag[0]==false)
{
Playgame(1);
}
else
{
flag[1] = false;
}
if(PlayPos[1]>=99)
{
Console.WriteLine("玩家{0}无耻的赢了玩家{1}", PlayerNames[1], PlayerNames[0]);
break;
}
}
Win();
Console.ReadKey();
}
///游戏头
public static void GameShow()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("*******第一个飞行棋项目*******");
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("******************************");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("******************************");
}
//初始化地图
public static void InitailMap()
{
int[] Luckyturn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘○
for (int i=0;i<Luckyturn.Length;i++)
{
int index = Luckyturn[i];
Maps[index] = 1;
}
int[] LandMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆
for (int i = 0; i < LandMine.Length; i++)
{
int index = LandMine[i];
Maps[index] = 2;
}
int[] pause = { 9, 27, 60, 93 };//暂停▲
for (int i = 0; i < pause.Length; i++)
{
int index =pause[i];
Maps[index] = 3;
}
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道ㄨ
for (int i = 0; i < timeTunnel.Length; i++)
{
int index = timeTunnel[i];
Maps[index] = 4;
}
}
//画地图
public static void DrawMap()
{
Console.WriteLine("图例:幸运轮盘:○ 地雷:☆ 暂停:▲ 时空隧道:ㄨ");
//第一横行
for (int i = 0; i < 30; i++)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine();
//第一竖行
for(int i=30;i<35;i++)
{
for(int j=0;j<=28;j++)
{
Console.Write(" ");
}
Console.Write(DrawStringMap(i));
Console.WriteLine();
}
//第二横行
for(int i=64;i>=35;i--)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine();
//第二竖行
for(int i=65;i<=69;i++)
{
Console.WriteLine(DrawStringMap(i));
}
//第三横行
for(int i=70;i<=99;i++)
{
Console.Write(DrawStringMap(i));
}
Console.WriteLine();
}
//从画地图的方法中抽象出来的一个方法
public static string DrawStringMap(int i)
{
string str ="";
//画图
if (PlayPos[0] == PlayPos[1] && PlayPos[0] == i)
{
str = "<>";
}
else
if (PlayPos[0] == i)
{
str="A";
}
else
if (PlayPos[1] == i)
{
str="B";
}
else
{
switch (Maps[i])
{
case 0: Console.ForegroundColor = ConsoleColor.DarkCyan; 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.Magenta; str="ㄨ"; break;
}
}
return str;
}
//玩游戏
public static void Playgame(int playernuber)
{
Random r = new Random();
int rNuber = r.Next(1, 7);
Console.WriteLine("玩家{0}按任意键开始掷骰子", PlayerNames[playernuber]);
Console.ReadKey(true);
Console.WriteLine("{0}掷出了{1}", PlayerNames[playernuber],rNuber);
PlayPos[playernuber] += rNuber;
ChangePos();
Console.ReadKey(true);
Console.WriteLine("{0}按任意键开始行动", PlayerNames[playernuber]);
Console.ReadKey(true);
Console.WriteLine("{0}行动完了", PlayerNames[playernuber]);
Console.ReadKey(true);
//玩家A有可能踩到了玩家B 方块 幸运轮盘 地雷 暂停 时空隧道
if (PlayPos[playernuber] == PlayPos[1- playernuber])
{
Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退6格", PlayerNames[playernuber], PlayerNames[1- playernuber], PlayerNames[1- playernuber]);
PlayPos[1- playernuber] -= 6;
Console.ReadKey(true);
}
else //踩到了关卡
{
//玩家的坐标
switch (Maps[PlayPos[playernuber]])
{
case 0: Console.WriteLine("玩家{0}踩到了方块□,安全", PlayerNames[playernuber]); Console.ReadKey(true); break;
case 1:
Console.WriteLine("玩家{0}踩到了幸运轮盘○,请选择1--交换位置 2--轰炸对方", PlayerNames[playernuber]);
string input = Console.ReadLine();
while (true)
{
if (input == "1")
{
Console.WriteLine("玩家{0}选择跟{1}交换位置", PlayerNames[playernuber], PlayerNames[1- playernuber]);
Console.ReadKey(true);
int temp = PlayPos[playernuber];
PlayPos[playernuber] = PlayPos[1- playernuber];
PlayPos[1- playernuber] = temp;
Console.WriteLine("交换完成!!!按任意键继续游戏!!!");
Console.ReadKey(true);
break;
}
else
if (input == "2")
{
Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{2}退6格", PlayerNames[playernuber], PlayerNames[1- playernuber], PlayerNames[1- playernuber]);
Console.ReadKey(true);
PlayPos[1- playernuber] -= 6;
Console.WriteLine("玩家{0}退了6格", PlayerNames[1- playernuber]);
Console.ReadKey(true);
break;
}
else
{
Console.WriteLine("只能输入1或者2 1-交换位置 2--轰炸对方");
input = Console.ReadLine();
}
}
break;
case 2:
Console.WriteLine("玩家{0}踩到了地雷☆ ,退六格", PlayerNames[playernuber]);
Console.ReadKey(true);
PlayPos[playernuber] -= 6;
break;
case 3:
Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerNames[playernuber]);
flag[playernuber] = true;
Console.ReadKey(true);
break;
case 4:
Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayerNames[playernuber]);
PlayPos[playernuber] += 10;
Console.ReadKey(true);
break;
}
}
ChangePos();
Console.Clear();
DrawMap();
}
public static void ChangePos()
{
if(PlayPos[0]<0)
{
PlayPos[0] = 0;
}
if(PlayPos[0]>=99)
{
PlayPos[0] = 99;
}
if (PlayPos[1] < 0)
{
PlayPos[1] = 0;
}
if (PlayPos[1] >= 99)
{
PlayPos[1] = 99;
}
}
public static void Win()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("W WW W");
Console.WriteLine(" W W W W");
Console.WriteLine(" W W W W");
Console.WriteLine(" W W W W");
Console.WriteLine(" W W W W");
Console.WriteLine(" W W");
Console.ResetColor();
}
}
}