固定资源
我们在写游戏的时候,界面上显示的有些东西是不变的,比如游戏头,这样我们可以封装成一个方法。
Console.ForegroudColor属性表示控制台的前景色;
- 画游戏头
- 初始化地图(加载地图所需要的资源)
- 画地图
- 玩游戏
初始化地图
我们用int型数组存这个地图,把数字变成地图上的字符串的过程,就是初始化地图的过程
//我用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 };//时空隧道卐
方法里面尽量不要出现Console.Write等;这样可以实现更大的兼容性;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 飞行棋游戏
{
class Program
{
//我们用静态字段模拟全局变量
public static int[] maps=new int[100];
public static int[] playerpos = new int[2];
public static string[] playerNames = new string[2];
static void Main(string[] args)
{
GameShow();
Initial();
GetName();
//下面清屏;
Console.Clear();
GameShow();
Console.WriteLine("{0}的士兵用A表示", playerNames[0]);
Console.WriteLine("{0}的士兵用B表示", playerNames[1]);
ShowMaps();
Console.ReadKey();
}
public static void GetName()
{
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的名字不能为空,请重新输入");
}
else
{
Console.WriteLine("玩家B的名字不能和玩家A相同,请重新输入");
}
playerNames[1] = Console.ReadLine();
}
}
public static void GameShow()
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("************************");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("************************");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("************************");
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("*** 飞 行 棋 游 戏 ***");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("************************");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("************************");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("************************");
}
public static void Initial()
{
//我用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 };//时空隧道卐
for (int i = 0; i < luckyturn.Length; i++)
{
maps[luckyturn[i]] = 1;
}
for (int i = 0; i < landMine.Length; i++)
{
maps[landMine[i]] = 2;
}
for (int i = 0; i < pause.Length; i++)
{
maps[pause[i]]=3;
}
for (int i = 0; i < pause.Length; i++)
{
maps[pause[i]] = 4;
}
}
public static void ShowPos(int i)
{
if (i == playerpos[0])
{
if (playerpos[0] == playerpos[1])
{
Console.Write("<>");
}
else
{
Console.Write("A");
//return "A";
}
}
else if (i == playerpos[1])
{
Console.Write("B");
//return "B";
}
else
{
switch (maps[i])
{
case 0: Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("□");
//return "□";
break;
case 1:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("◎");
break;
case 2:
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("☆");
break;
case 3:
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("▲");
break;
case 4:
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.Write("卐");
break;
}//switch
}
}
public static void ShowMaps()
{
//我用0表示普通,显示给用户就是 □
//....1...幸运轮盘,显示组用户就◎
//....2...地雷,显示给用户就是 ☆
//....3...暂停,显示给用户就是 ▲
//....4...时空隧道,显示组用户就 卐
Console.WriteLine("图例,□表示普通位,◎表示幸运轮盘,☆表示地雷,▲表示暂停,卐表示时空隧道");
#region 第一条横线
int i=0;
for ( i= 0; i < 30; i++)
{
ShowPos(i);
}//for
#endregion
#region 第一条竖线
for (; i < 35; i++)
{
Console.WriteLine();
for (int j = 0; j < 29; j++)
{
Console.Write(" ");
}
ShowPos(i);
}
#endregion
#region 第二条横线
Console.WriteLine();
for (i=64; i >34; i--)
{
ShowPos(i);
}
#endregion
#region 第三条竖线
Console.WriteLine();
for(i=65;i<70;i++)
{
ShowPos(i);
Console.WriteLine();
}
#endregion
#region 第四条横线
for (; i < 100; i++)
{
ShowPos(i);
}
#endregion
Console.WriteLine();
}
}
}