4e1548abe2
Part1
548 lines
18 KiB
C#
548 lines
18 KiB
C#
using System;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Numerics;
|
|
namespace 基础实践
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
int w = 50, h = 30;
|
|
ConsoleInit(w, h);
|
|
E_SceneType nowSceneType = E_SceneType.Begin;
|
|
while (true)
|
|
{
|
|
switch (nowSceneType)
|
|
{
|
|
case E_SceneType.Begin:
|
|
Console.Clear();
|
|
//开始场景逻辑
|
|
BeginOrQuitScene(w, h, ref nowSceneType);
|
|
break;
|
|
case E_SceneType.Game:
|
|
Console.Clear();
|
|
//游戏场景逻辑
|
|
GameScene(w, h, ref nowSceneType);
|
|
break;
|
|
case E_SceneType.End:
|
|
Console.Clear();
|
|
//结束场景逻辑
|
|
BeginOrQuitScene(w, h, ref nowSceneType);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
#region 控制台初始化
|
|
static void ConsoleInit(int w, int h)
|
|
{
|
|
//光标隐藏
|
|
Console.CursorVisible = false;
|
|
//设置窗口大小
|
|
Console.SetWindowSize(w, h);
|
|
//设置缓冲区大小
|
|
Console.SetBufferSize(w, h);
|
|
}
|
|
#endregion
|
|
|
|
#region 开始场景&结算场景
|
|
static void BeginOrQuitScene(int w, int h, ref E_SceneType nowSceneType)
|
|
{
|
|
Console.SetCursorPosition(nowSceneType == E_SceneType.Begin ? w / 2 - 2 : w / 2 - 3, 8);
|
|
Console.Write(nowSceneType == E_SceneType.Begin ? "飞行棋" : "游戏结束");
|
|
bool nowSelIndex = true;
|
|
bool notQuit = true;
|
|
while (notQuit)
|
|
{
|
|
|
|
Console.SetCursorPosition( w / 2 - 3, 12);
|
|
Console.ForegroundColor = nowSelIndex ? ConsoleColor.Red : ConsoleColor.White;
|
|
Console.Write(nowSceneType == E_SceneType.Begin ? "开始游戏" : "重新开始");
|
|
Console.SetCursorPosition(w / 2 - 3, 15);
|
|
Console.ForegroundColor = !nowSelIndex ? ConsoleColor.Red : ConsoleColor.White;
|
|
Console.Write("退出游戏");
|
|
|
|
//通过readkey可以得到一个输入的枚举类型(选中key按F12,Consolekey中有对应键值
|
|
switch (Console.ReadKey(true).Key)
|
|
{
|
|
case ConsoleKey.W:
|
|
nowSelIndex = true;
|
|
break;
|
|
case ConsoleKey.S:
|
|
nowSelIndex = false;
|
|
break;
|
|
case ConsoleKey.J:
|
|
if (nowSelIndex)
|
|
{
|
|
//进入
|
|
//1.改变当前场景ID
|
|
nowSceneType = E_SceneType.Game;
|
|
//2.退出当前循环
|
|
notQuit = false;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
//退出
|
|
Environment.Exit(0);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 游戏场景
|
|
static void GameScene(int w, int h, ref E_SceneType nowSceneType)
|
|
{
|
|
DrawWalls(w, h);
|
|
Map map = new Map(14, 3, 80);
|
|
map.Draw();
|
|
//grid.Draw();
|
|
Player player = new Player(0, E_PlayerType.Player);
|
|
Player computer = new Player(0, E_PlayerType.Computer);
|
|
bool isGameover = false;
|
|
DrawPlayer(player, computer, map);
|
|
while (true)
|
|
{
|
|
//玩家:
|
|
//检测输入
|
|
Console.ReadKey(true);
|
|
//扔色子
|
|
isGameover = RandomMove(w, h, ref player, ref computer, map);
|
|
|
|
//绘制地图
|
|
map.Draw();
|
|
//绘制玩家
|
|
DrawPlayer(player, computer, map);
|
|
//判断是否结束
|
|
if (isGameover)
|
|
{
|
|
nowSceneType = E_SceneType.End;
|
|
break;
|
|
}
|
|
|
|
|
|
//电脑:
|
|
//检测输入
|
|
Console.ReadKey(true);
|
|
//扔色子
|
|
isGameover = RandomMove(w, h, ref computer, ref player, map);
|
|
|
|
//绘制地图
|
|
map.Draw();
|
|
//绘制玩家
|
|
DrawPlayer(player, computer, map);
|
|
//判断是否结束
|
|
if (isGameover)
|
|
{
|
|
nowSceneType = E_SceneType.End;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 画红墙&提示信息
|
|
static void DrawWalls(int w, int h)
|
|
{
|
|
#region 墙
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
for (int i = 0; i < w; i += 2)
|
|
{
|
|
for (int j = 0; j < h; j++)
|
|
{
|
|
if (i == 0 || j == 0 || i == w - 2 || j == h - 1 || j == h - 6 || j == h - 11)
|
|
{
|
|
Console.SetCursorPosition(i, j);
|
|
Console.Write('■');
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
#region 提示信息
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
Console.SetCursorPosition(2, h - 10);
|
|
Console.Write("□:普通格子");
|
|
Console.ForegroundColor = ConsoleColor.Blue;
|
|
Console.SetCursorPosition(2, h - 9);
|
|
Console.Write("Ⅱ:暂停一回合");
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.SetCursorPosition(30, h - 9);
|
|
Console.Write("●:炸弹(倒退5格");
|
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
|
Console.SetCursorPosition(2, h - 8);
|
|
Console.Write("¤:混乱(随机倒退,暂停,交换玩家位置");
|
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
|
Console.SetCursorPosition(2, h - 7);
|
|
Console.Write("★:玩家");
|
|
Console.ForegroundColor = ConsoleColor.Magenta;
|
|
Console.SetCursorPosition(16, h - 7);
|
|
Console.Write("▲:电脑");
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
Console.SetCursorPosition(30, h - 7);
|
|
Console.Write("◎:重合");
|
|
#endregion
|
|
}
|
|
#endregion
|
|
|
|
#region 绘制玩家
|
|
static void DrawPlayer(Player player, Player computer, Map map)
|
|
{
|
|
if (player.nowIndex == computer.nowIndex)
|
|
{
|
|
Grid grid = map.grid[player.nowIndex];
|
|
Console.SetCursorPosition(grid.pos.x, grid.pos.y);
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
Console.Write('◎');
|
|
}
|
|
else
|
|
{
|
|
player.Draw(map);
|
|
computer.Draw(map);
|
|
}
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region 擦提示
|
|
static void ClearInfo(int h)
|
|
{
|
|
//擦之前的提示信息
|
|
Console.SetCursorPosition(2, h - 5);
|
|
Console.Write(" ");
|
|
Console.SetCursorPosition(2, h - 4);
|
|
Console.Write(" ");
|
|
Console.SetCursorPosition(2, h - 3);
|
|
Console.Write(" ");
|
|
Console.SetCursorPosition(2, h - 2);
|
|
Console.Write(" ");
|
|
}
|
|
#endregion
|
|
|
|
#region 扔骰子
|
|
/// <summary>
|
|
/// 扔骰子函数
|
|
/// </summary>
|
|
/// <param name="w">窗口宽</param>
|
|
/// <param name="h">窗口高</param>
|
|
/// <param name="p">控制的对象</param>
|
|
/// <param name="map">地图信息</param>
|
|
/// <returns></returns>
|
|
static bool RandomMove(int w, int h, ref Player p, ref Player otherp, Map map)
|
|
{
|
|
//擦除提示信息
|
|
ClearInfo(h);
|
|
//根据类型 决定信息颜色
|
|
Console.ForegroundColor = p.type == E_PlayerType.Player ? ConsoleColor.Cyan : ConsoleColor.Magenta;
|
|
|
|
//扔骰子之前判断是否被暂停
|
|
if (p.isPause)
|
|
{
|
|
Console.SetCursorPosition(2, h - 5);
|
|
Console.Write("处于暂停,{0}需要暂停一回合",p.type == E_PlayerType.Player ? "你" : "电脑");
|
|
p.isPause = false;//停止暂停
|
|
return false;
|
|
}
|
|
Random r = new Random();
|
|
int randomNum = r.Next(1, 7);
|
|
p.nowIndex += randomNum;
|
|
|
|
//打印扔的点数
|
|
Console.SetCursorPosition(2, h - 5);
|
|
Console.Write("{0}扔出的点数为{1}", p.type == E_PlayerType.Player ? "你" : "电脑", randomNum);
|
|
|
|
if (p.nowIndex >= map.grid.Length - 1)//判断结束
|
|
{
|
|
p.nowIndex = map.grid.Length - 1;//移动大于了地图直接等于最后一格
|
|
Console.SetCursorPosition(2, h - 4);
|
|
if (p.type == E_PlayerType.Player)
|
|
{
|
|
Console.Write("恭喜你率先到达了终点");
|
|
}
|
|
else
|
|
{
|
|
Console.Write("很遗憾电脑率先到达了终点");
|
|
}
|
|
Console.SetCursorPosition(2, h - 3);
|
|
Console.Write("请按任意键结束");
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
//没到地图 就判断当前到了 什么格子
|
|
Grid grid = map.grid[p.nowIndex];
|
|
switch (grid.type)
|
|
{
|
|
case E_GridType.Normal:
|
|
//不处理
|
|
Console.SetCursorPosition(2, h - 4);
|
|
Console.Write("{0}无事发生", p.type == E_PlayerType.Player ? "你" : "电脑");
|
|
Console.SetCursorPosition(2, h - 3);
|
|
Console.Write("请{0}按任意键继续", p.type != E_PlayerType.Player ? "你" : "电脑");
|
|
break;
|
|
case E_GridType.Boomb:
|
|
//炸弹退格
|
|
p.nowIndex -= 5;
|
|
if (p.nowIndex < 0)//防止BUG小于起点
|
|
{
|
|
p.nowIndex = 0;
|
|
}
|
|
Console.SetCursorPosition(2, h - 4);
|
|
Console.Write("{0}踩到炸弹,退后5格", p.type == E_PlayerType.Player ? "你" : "电脑");
|
|
Console.SetCursorPosition(2, h - 3);
|
|
Console.Write("请{0}按任意键继续", p.type == E_PlayerType.Player ? "你" : "电脑");
|
|
break;
|
|
case E_GridType.Pause:
|
|
//暂停一回合
|
|
//加一个暂停标示
|
|
p.isPause = true;
|
|
break;
|
|
case E_GridType.Tunnel:
|
|
//随机
|
|
Console.SetCursorPosition(2, h - 4);
|
|
Console.Write("{0}踩到时空隧道,将触发随机效果", p.type == E_PlayerType.Player ? "你" : "电脑");
|
|
randomNum = r.Next(1, 91);
|
|
if (randomNum < 30)
|
|
{
|
|
p.nowIndex -= 5;
|
|
if (p.nowIndex < 0)//防止出界
|
|
{
|
|
p.nowIndex = 0;
|
|
}
|
|
Console.SetCursorPosition(2, h - 4);
|
|
Console.Write("{0}触发了倒退,退后5格", p.type == E_PlayerType.Player ? "你" : "电脑");
|
|
} else if (randomNum <= 60)
|
|
{
|
|
p.isPause = true;
|
|
Console.SetCursorPosition(2, h - 3);
|
|
Console.Write("{0}被随机到了暂停", p.type == E_PlayerType.Player ? "你" : "电脑");
|
|
}
|
|
else
|
|
{
|
|
int temp = p.nowIndex;
|
|
p.nowIndex = otherp.nowIndex;
|
|
otherp.nowIndex = temp;
|
|
Console.SetCursorPosition(2, h - 3);
|
|
Console.Write("{0}被随机到了位置互换", p.type == E_PlayerType.Player ? "你" : "电脑");
|
|
}
|
|
Console.SetCursorPosition(2, h - 2);
|
|
Console.Write("{0}请按键扔骰子", p.type == E_PlayerType.Player ? "你" : "电脑");
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
|
|
#region 场景枚举
|
|
enum E_SceneType
|
|
{
|
|
/// <summary>
|
|
/// 开始场景
|
|
/// </summary>
|
|
Begin,
|
|
/// <summary>
|
|
/// 游戏场景
|
|
/// </summary>
|
|
Game,
|
|
/// <summary>
|
|
/// 结束场景
|
|
/// </summary>
|
|
End,
|
|
}
|
|
#endregion
|
|
|
|
#region 格子枚举
|
|
enum E_GridType
|
|
{
|
|
/// <summary>
|
|
/// 普通格子
|
|
/// </summary>
|
|
Normal,
|
|
/// <summary>
|
|
/// 炸弹 会倒退
|
|
/// </summary>
|
|
Boomb,
|
|
/// <summary>
|
|
/// 暂停
|
|
/// </summary>
|
|
Pause,
|
|
/// <summary>
|
|
/// 隧道
|
|
/// </summary>
|
|
Tunnel
|
|
}
|
|
#endregion
|
|
|
|
#region 格子结构体
|
|
struct Grid
|
|
{
|
|
public E_GridType type;
|
|
public Vector2 pos;
|
|
|
|
public Grid(int x, int y, E_GridType type)
|
|
{
|
|
pos.x = x;
|
|
pos.y = y;
|
|
this.type = type;
|
|
}
|
|
|
|
public void Draw()
|
|
{
|
|
Console.SetCursorPosition(pos.x, pos.y);
|
|
switch (type)
|
|
{
|
|
case E_GridType.Normal:
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
Console.Write("□");
|
|
break;
|
|
case E_GridType.Boomb:
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.Write("●");
|
|
break;
|
|
case E_GridType.Pause:
|
|
Console.ForegroundColor = ConsoleColor.Blue;
|
|
Console.Write("Ⅱ");
|
|
break;
|
|
case E_GridType.Tunnel:
|
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
|
Console.Write("¤");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 位置信息结构体
|
|
struct Vector2
|
|
{
|
|
public int x;
|
|
public int y;
|
|
|
|
public Vector2(int x, int y)
|
|
{
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 地图结构体
|
|
struct Map
|
|
{
|
|
public Grid[] grid;
|
|
public Map(int x, int y, int num)
|
|
{
|
|
grid = new Grid[num];
|
|
Random r = new Random();
|
|
int randomNum;
|
|
int indexX = 0, indexY = 0;//用于位置改变的计数
|
|
int stepNumber = 2;
|
|
for (int i = 0; i < num; i++)
|
|
{
|
|
//应该初始化格子类型
|
|
randomNum = r.Next(1, 101);
|
|
//设置类型 普通格子
|
|
if (randomNum <= 85 || i == 0 || i == num - 1)
|
|
{
|
|
grid[i].type = E_GridType.Normal;
|
|
}
|
|
else if (randomNum > 85 && randomNum <= 90)
|
|
{
|
|
grid[i].type = E_GridType.Boomb;
|
|
}
|
|
else if (randomNum > 90 && randomNum <= 95)
|
|
{
|
|
grid[i].type = E_GridType.Pause;
|
|
}
|
|
else
|
|
{
|
|
grid[i].type = E_GridType.Tunnel;
|
|
}
|
|
//设置位置
|
|
grid[i].pos = new Vector2(x, y);
|
|
if (indexX == 10)
|
|
{
|
|
y += 1;
|
|
++indexY;
|
|
if (indexY == 2)
|
|
{
|
|
indexX = 0;
|
|
indexY = 0;
|
|
stepNumber = -stepNumber;
|
|
}
|
|
}else
|
|
{
|
|
x += stepNumber;
|
|
++indexX;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Draw()
|
|
{
|
|
for (int i = 0; i < grid.Length; i++)
|
|
{
|
|
grid[i].Draw();
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 玩家枚举
|
|
enum E_PlayerType
|
|
{
|
|
Player,
|
|
Computer
|
|
}
|
|
#endregion
|
|
|
|
#region 玩家结构体
|
|
struct Player
|
|
{
|
|
//玩家类型
|
|
public E_PlayerType type;
|
|
//当前所在地图的哪一个格子的索引
|
|
public int nowIndex;
|
|
public bool isPause;
|
|
|
|
public Player(int index, E_PlayerType type)
|
|
{
|
|
this.type = type;
|
|
nowIndex = index;
|
|
isPause = false;
|
|
}
|
|
|
|
public void Draw(Map mapInfo)
|
|
{
|
|
//设置位置(从传入的地图中得到格子信息
|
|
Grid grid = mapInfo.grid[nowIndex];
|
|
Console.SetCursorPosition(grid.pos.x, grid.pos.y);
|
|
//绘画
|
|
switch (type)
|
|
{
|
|
case E_PlayerType.Player:
|
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
|
Console.Write("★");
|
|
break;
|
|
case E_PlayerType.Computer:
|
|
Console.ForegroundColor = ConsoleColor.Magenta;
|
|
Console.Write("▲");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
} |