蓝鸥Unity开发基础—— 实践课程源代码二

wKioL1e1LFuQrlpKAAX1wx1Cz4M783.png-wh_50


using System;

namespace MyFirstGame
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //游戏即将启动时做一些操作
            const int mapW=46;//地图宽度
            const int mapH = 16;//地图高度

            //定义常量
            const int emptyTiledMap=0;//空砖块
            const int TBTiledMap=1;//顶部和底部砖块
            const int LRTiledMap=2;//左右两边的砖块
            const int Player=3;//玩家
        

            //游戏初始化变量
            bool isOver = false;//游戏是否结束
            int[,] mapData=new int[mapH,mapW] ;//地图数据
            string[,] mapView= new string[mapH,mapW];//地图视图

            int posW = 1;//玩家在W的位置
            int posH = 1;//玩家在H的位置

            //初始化地图数据
            for (int i = 0; i < mapH; i++) {
                for (int j= 0; j < mapW;j++) {
                    //j将地图初始化为空砖块
                    mapData[i,j]=emptyTiledMap;
                    //如果砖块处于最上方侧或最下方,就初始化为TBTiledMap
                    if(i==0 || i==mapH-1){
        
                        mapData[i,j]=TBTiledMap;
                        
                    }else if(j==0 || j==mapW-1){
                        //如果砖块处于最右侧或最左侧,就初始化为LRTiledMap
                        mapData[i,j] = LRTiledMap;

                    }else if(i==posW &&j==posH){
                        //当前位置是玩家
                        mapData [i, j] = Player;
                        
                    }
                }
            }
            //游戏主循环
            while(!isOver){
                //游戏启动后,主循环张做一些操作
                //计算机计算数据->将数据转成视图->把视图显示出来->获取玩家的事件更新数据
                //刷新地图
                for (int i = 0; i < mapH; i++) {
                    for (int j = 0; j < mapW; j++) {
                        //从地图数据中获取数据,决定砖块如何显示
                        //取出砖块数据
                        int tiledData =mapData[i,j];
                        //砖块视图
                        string tiledView="";
                        //确定砖块的显示
                        switch(tiledData){
                        case emptyTiledMap:
                            tiledView=" ";
                            break;
                        case TBTiledMap:
                            tiledView="-";
                            break;
                        case LRTiledMap:
                            tiledView="|";
                            break;
                        case Player:
                            tiledView="P";
                            break;
                        }
                        //将砖块视图存放到地图视图中
                        mapView[i,j]=tiledView;
                        //将每行的每个字符拼接到字符串中
                        //str +=mapView[i,j];
                    }
                }
                //显示地图
                for (int i = 0; i < mapH; i++) {
                    //创建一个用于接收一行信息的字符串
                    string str="";
                    for (int j = 0; j < mapW; j++) {
                        //将每行中的所有字符串进行拼接
                        str+=mapView[i,j];

                    }
                    //打印一行的内容
                    Console.WriteLine (str);
                }

                //保存玩家当前位置
                int oldPosW=posW;
                int oldPosH = posH;


                //等待用户输入
                //获取用户输入的按键字符串

                string key =    Console.ReadKey().KeyChar.ToString();
                if(key=="W"||key=="w"){

                    //让玩家向上移动
                    if(posH>1){
                        posH--;
                    }

                }else if(key=="S"||key=="s")
                {
                    //让玩家向下移动
                    if(posH<mapH-2){
                        posH++;
                    }
                }else  if(key=="A"||key=="a"){
                    //让玩家向下左移动
                    if(posW>1){
                        posW--;
                    }

                }else if(key=="D"||key=="d"){
                    //让玩家向下右移动
                    if(posW<mapW-2){
                        posW++;
                    }
                }else if(key=="Q"||key=="q"){
                    //如果按下Q退出游戏
                    isOver = true;
                    continue;
                }

                    
                //更新地图数据
                mapData[oldPosH,oldPosW]=emptyTiledMap;
                mapData [posH, posW] = Player;


            }
    
            /*有些即将结束时做一些操作 */
            Console.WriteLine ("客观,下次再来哦");

        }
    }
}