玩家结构体和枚举

img

玩家结构体包含了玩家类型玩家位置玩家初始化构造函数还有画自己的函数方法

玩家结构体
enum E_PlayerType//玩家类型的枚举
{
    Player,
    Robot,
}
struct Player
{
    public E_PlayerType type;
    //用地图上的索引代替坐标
    public mapIndex;
    public Player(int index,E_PlayerType type)
    {
        mapIndex=index;
        this.type=type;
    }
    //用Map结构体申明一个mapInfo作为参数传入Draw
    public void Draw (Map mapInfo)
    {
        //用Grid结构体申明一个变量grid,再从mapInfo中获取索引信息
        Grid grid=mapInfo.grids[mapIndex];
        Console.SetCursorPosition(grid.pos.x,grid.pos.y);
        switch (E_PlayerType)
        {
            case E_PlayerType.Player:
            Console.Foregroundcolor=ConsoleColor.Cyan;
            Console.Write("★");
            break;
            case E_PlayerType.Robot:
            Console.Foregroundcolor=ConsoleColor.Megenta;
            Console.Write("▲");
            break;
        }
    }
}

玩家初始化
Player player = new Player(0,E_PlayerType.Player);
Player robot = new Player(0,E_PlayerType.Robot);
DrawPlayer(player,robot,map);
玩家初始化
static void DrawPlayer(Player player,Player robot,Map map)
{
    if(player.mapIndex==robot.mapIndex)
    {
        Grid grid = map.grids[player.mapIndex];
        Console.SetCursorPosition(grid.pos.x,grid.pos_y);
        Console.Foregroundcolor = ConsoleColor.DrakGreen;
        Console.Write("⚪");
    }
    else
    {
        player.Draw(map);
        robot.Draw(map);
    }
}
posted @ 2024-12-18 20:11  cannedmint  阅读(38)  评论(0)    收藏  举报