需求分析
目的在于——理清项目的功能需求
整理——流程图
学习面向对象之后——还要制作UML类图
通过流程图来理清 游戏程序 的思路
一个主循环 三个游戏场景内部的循环
开始场景
游戏场景
while循环之外
不变的基本信息——红墙,玩家形象和属性等
while循环内部
枚举
结束场景(和开始场景的代码逻辑相似)
公司的工作流程
对流程图,和类图的逻辑的绘制。——程序思路。
需求分析
目的在于——理清项目的功能需求
整理——流程图
学习面向对象之后——还要制作UML类图
通过流程图来理清 游戏程序 的思路
一个主循环 三个游戏场景内部的循环
开始场景
游戏场景
while循环之外
不变的基本信息——红墙,玩家形象和属性等
while循环内部
枚举
结束场景(和开始场景的代码逻辑相似)
公司的工作流程
对流程图,和类图的逻辑的绘制。——程序思路。
namespace 飞行棋
{
enum E_Scene
{
StartGame,
InGame,
FinishGame
}
enum E_PlayerType
{
Player,
PC
}
enum E_CellType
{
Normal,
Bomb,
PauseOneTurn,
TimeTunnel,
}
#region Vecor2
struct Vector2 //位置信息也是一个集合,可以直接写成结构
{
public int x;
public int y;
public Vector2(int x, int y)
{
this.x = x;
this.y = y;
}
}
#endregion
#region 格子构造体
struct Cell
{
public E_CellType cellType;
public Vector2 pos;//格子的位置
//初始化构造函数
public Cell(E_CellType cellType, int x, int y)
{
this.cellType = cellType;
pos.x = x;
pos.y = y;
}
public void DrawCell()
{
Console.SetCursorPosition(pos.x, pos.y);
switch (cellType)
{
case E_CellType.Normal:
Console.ForegroundColor = ConsoleColor.White;
Console.Write("□ ");
break;
case E_CellType.PauseOneTurn:
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.Write("‖ ");
break;
case E_CellType.Bomb:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("☢ ");
break;
case E_CellType.TimeTunnel:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("¤ ");
break;
}
}
}
#endregion
#region 棋盘
struct Map
{ //直接格子构造体作为数组
public Cell[] A_Cells;
public Map(int num , int x , int y)
{
A_Cells = new Cell[num];
Random randomize = new Random();
for (int i = 0; i < num; i++)
{
int probability = randomize.Next(1, 101);
if (probability < 85 || i == 0 || i > num - 1)
{
A_Cells[i].cellType = E_CellType.Normal;
}
else if (probability < 90 )
{
A_Cells[i].cellType = E_CellType.Bomb;
}
else if (probability < 95)
{
A_Cells[i].cellType = E_CellType.PauseOneTurn;
}
else
{
A_Cells[i].cellType = E_CellType.TimeTunnel;
}
}
int cellPosX = x;
int cellPosY = y;
int cellCalcX = 0;
int cellCalcY = 0;
int mapPosXChange = 2;
//横轴加10后纵轴加2,横轴减10后纵轴+2
for (int i = 0; i < num; i++)
{
if (cellCalcX < 10)
{
cellPosX += mapPosXChange;
cellCalcX ++;
}
else if (cellCalcY < 2)
{
cellPosY++;
cellCalcY++;
}
else
{
cellCalcX = 0;
cellCalcY = 0;
mapPosXChange = -mapPosXChange;
}
A_Cells[i].pos = new Vector2(cellPosX, cellPosY);
}
}
public void DrawMap()
{
for (int i = 0; i < A_Cells.Length; i++)
{
A_Cells[i].DrawCell();
}
}
}
#endregion
struct Player
{
public E_PlayerType playerType;
public int nowWhere;
public bool isPause;
public Player(E_PlayerType player, int where)
{
playerType = player;
nowWhere = where;
}
//不用写PlayerType和在哪里的原因是调用这个方法必然会调用结构所在方法
public void DrawPlayer(Map cellInfo)
{
//将光标移动到所在格
Cell cell;
cell = cellInfo.A_Cells[nowWhere];
Console.SetCursorPosition(cell.pos.x, cell.pos.y);
if (playerType == E_PlayerType.Player)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("✈ ");
}
if (playerType == E_PlayerType.PC)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("✈ ");
}
}
}
internal class Program
{
#region 静态地图
static void DrawMap()
{
Console.ForegroundColor = ConsoleColor.Red;
for (int row = 0; row < 31; row++)
{
Console.SetCursorPosition(0, row);
if (row == 0 || row == 30 || row == 25 || row == 20)
{
for (int column = 0; column < 52; column += 2)
{
Console.Write("■ ");
}
}
else
{
Console.Write("■");
Console.SetCursorPosition(50, row);
Console.Write("■");
}
}
Console.ForegroundColor = ConsoleColor.White;
Console.SetCursorPosition(2, 21);
Console.Write("□:普通格子");
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.SetCursorPosition(2, 22);
Console.Write("‖:暂停,一回合不动");
Console.ForegroundColor = ConsoleColor.Red;
Console.SetCursorPosition(28, 22);
Console.Write("☢:炸弹,倒退5格");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.SetCursorPosition(2, 23);
Console.Write("¤:时空隧道:随机倒退,暂停,换位置");
Console.SetCursorPosition(2, 24);
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("✈:玩家");
Console.SetCursorPosition(12, 24);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("✈:电脑");
Console.SetCursorPosition(22, 24);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("◎:电脑与玩家重合");
Console.ForegroundColor = ConsoleColor.White;
Console.SetCursorPosition(2, 26);
Console.Write("按任意键开始扔骰子");
}
#endregion
#region 画玩家
static void DrawPlayer(Player player, Player pc, Map map)
{
//重合时
if (player.nowWhere == pc.nowWhere)
{
//得到重合的位置
Cell cell = map.A_Cells[player.nowWhere];
Console.SetCursorPosition(cell.pos.x, cell.pos.y);
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write("◎");
}
//不重合的时候
else
{
player.DrawPlayer(map);
pc.DrawPlayer(map);
}
}
#endregion
#region 菜单
static E_Scene Menu(ref E_Scene inScene)
{
bool inMenu = true;
Console.Clear();
if (inScene == E_Scene.StartGame)
{
Console.SetCursorPosition(23, 10);
Console.Write("飞行棋");
Console.ForegroundColor = ConsoleColor.Red;
Console.SetCursorPosition(22, 13);
Console.Write("开始游戏");
Console.ForegroundColor = ConsoleColor.White;
}
else if (inScene == E_Scene.FinishGame)
{
Console.SetCursorPosition(22, 10);
Console.Write("游戏结束");
Console.ForegroundColor = ConsoleColor.Red;
Console.SetCursorPosition(21, 13);
Console.Write("回到主菜单");
Console.ForegroundColor = ConsoleColor.White;
}
Console.SetCursorPosition(22, 15);
Console.Write("退出游戏");
byte row = 13;
while (inMenu)
{
char input = Console.ReadKey(true).KeyChar;
switch (input)
{
#region W和S
case 'W':
case 'w':
case 'S':
case 's':
if (row == 13)
{
Console.SetCursorPosition(21, 13);
Console.Write(" ");
switch (inScene)
{
case E_Scene.StartGame:
Console.SetCursorPosition(22, 13);
Console.Write("开始游戏");
break;
case E_Scene.FinishGame:
Console.SetCursorPosition(21, 13);
Console.Write("回到主菜单");
break;
}
Console.SetCursorPosition(22, 15);
Console.Write(" ");
Console.ForegroundColor = ConsoleColor.Red;
Console.SetCursorPosition(22, 15);
Console.Write("退出游戏");
Console.ForegroundColor = ConsoleColor.White;
row = 15;
}
else if (row == 15)
{
Console.SetCursorPosition(22, 15);
Console.Write("退出游戏");
Console.SetCursorPosition(21, 13);
Console.Write(" ");
Console.ForegroundColor = ConsoleColor.Red;
switch (inScene)
{
case E_Scene.StartGame:
Console.SetCursorPosition(22, 13);
Console.Write("开始游戏");
break;
case E_Scene.FinishGame:
Console.SetCursorPosition(21, 13);
Console.Write("回到主菜单");
break;
}
Console.ForegroundColor = ConsoleColor.White;
row = 13;
}
break;
#endregion
case 'J':
case 'j':
if(row == 13 && inScene == E_Scene.StartGame)
{
inScene = E_Scene.InGame;
inMenu = false;
}
else if(row == 15)
{
Console.Clear();
inMenu = false;
Environment.Exit(0);
}
else if(inScene == E_Scene.FinishGame)
{
Console.Clear();
inScene = E_Scene.StartGame;
inMenu = false;
Menu(ref inScene);
}
break;
}
}
return inScene;
}
#endregion
#region 移动
static void Movement(ref Player player, ref Player other, Map map, int dice, ref bool gameOver)
{
Cell cell;
if (!player.isPause)
{
if (player.nowWhere < map.A_Cells.Length - 1)
{
cell.cellType = map.A_Cells[player.nowWhere += dice].cellType;
switch (cell.cellType)
{
case E_CellType.Normal:
player.nowWhere += dice;
break;
case E_CellType.Bomb:
player.nowWhere += dice - 5;
break;
case E_CellType.PauseOneTurn:
player.nowWhere += dice;
player.isPause = true;
break;
case E_CellType.TimeTunnel:
Random r = new Random();
int result = r.Next(1, 4);
switch (result)
{
case 1:
player.nowWhere += dice - 5;
break;
case 2:
player.nowWhere += dice;
player.isPause = true;
break;
case 3:
Player check = new Player();
check.nowWhere = player.nowWhere;
player.nowWhere = other.nowWhere;
other.nowWhere = check.nowWhere;
break;
}
break;
}
}
}
else
{
player.isPause = false;
}
if(player.nowWhere < 0)
{
player.nowWhere = 0;
}
if (player.nowWhere >= map.A_Cells.Length - 1)
{
player.nowWhere = map.A_Cells.Length - 1;
gameOver = true;
Console.Clear();
E_Scene inScene = E_Scene.FinishGame;
Menu(ref inScene);
}
}
#endregion
#region 总命令
static void GameMain(ref Player player ,ref Player pc, Map map, int dice, ref int turn, ref bool gameOver)
{
if (turn % 2 == 0 )
{
Movement(ref pc, ref player, map, dice, ref gameOver);
map.DrawMap();
DrawPlayer(player, pc, map);
}
else
{
Movement(ref player, ref pc, map, dice ,ref gameOver);
map.DrawMap();
DrawPlayer(player, pc, map);
}
turn++;
}
#endregion
static void Main(string[] args)
{
Console.SetWindowSize(52, 31);
Console.SetBufferSize(52, 31);
Console.CursorVisible = false;
bool inGame = true;
E_Scene inScene = E_Scene.StartGame;
while (inGame)
{
switch (inScene)
{
case E_Scene.StartGame:
Menu(ref inScene);
break;
case E_Scene.InGame:
Console.Clear();
DrawMap();
Map checBoard = new Map(100, 14, 3);
checBoard.DrawMap();
Player player = new Player(E_PlayerType.Player, 0);
Player pc = new Player(E_PlayerType.PC, 0);
DrawPlayer(player, pc, checBoard);
Random r = new Random();
int turn = 1;
bool gameover = false;
while (!gameover)
{
Console.ReadKey(true);
int dice = r.Next(1, 7);
GameMain(ref player, ref pc, checBoard, dice, ref turn ,ref gameover);
}
break;
case E_Scene.FinishGame:
Menu(ref inScene);
break;
}
}
}
}
}