C # 创 建 第 一 个 代 孕 小 程 序

了解一下C#的基本用法,C#看起来语法和java差不多,还没有仔细研究,这是跟着视频做的第一个代孕DEMO

using System;

namespace myfristgame
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //游戏启动时候初始化值
            //常量只能给常量,能给变量或者表达式 否则会报错 
            //const int MapViewX = Convert.ToInt32 ("20");
            const int MapViewX = 16;
            const int MapViewY = 46;
            bool isStart = true;
            const int EmptyLine = 0;
            const int TBLine = 1;
            const int LRLine = 2;
            const int Player = 3;
            int playerPosX = 1;
            int playerPosY = 1;
            int playerOldPosX = 1;
            int playerOldPosY = 1;
            //地图数据
            int[,]mapData = new int[MapViewX,MapViewY];
            //初化地图数据
            for(int i = 0;i<MapViewX;i++){
                for(int j = 0;j<MapViewY;j++){
                    if (i == 0 || i == MapViewX - 1) {
                        mapData [i,j] = TBLine;
                    } else if (j == 0 || j == MapViewY - 1) {
                        mapData [i,j] = LRLine;
                    } else if(playerPosX==i&&j==playerPosY){
                        mapData [i,j] = Player;
                    } else{
                        mapData [i,j] = EmptyLine;
                    }
                }
            }
            //根据数据初始化地图
            String[,]map = new String[MapViewX,MapViewY];
            //根据地图数据初始化地图
            for(int i=0;i<MapViewX;i++){
                String str = "";
                for(int j=0;j<MapViewY;j++){
                    int data =     mapData[i,j];
                    switch (data) {
                    case EmptyLine:
                        str += " ";
                        break;
                    case TBLine:
                        str += "-";
                        break;
                    case LRLine:
                        str += "|";
                        break;
                    case Player:
                        str += "P";
                        break;
                    }
                }
                Console.WriteLine (str);
            }
            //游戏启动进行时操作
            String[,]mapVies = new string[MapViewX,MapViewY];
            while(isStart){
                Console.WriteLine ("Game is start!");
                Console.ReadLine();
                String inputKey = Console.ReadKey().KeyChar.ToString();
                if (inputKey.Equals ("q")|| inputKey.Equals ("Q")) {
                    isStart = false;
                    break;
                } else {
                    playerOldPosX = playerPosX;
                    playerOldPosY = playerPosY;
                    if("w".Equals(inputKey)||"W".Equals(inputKey)){
                        if(playerPosX>1){
                            playerPosX--;
                        }
                    }
                    if("s".Equals(inputKey)||"S".Equals(inputKey)){
                        if(playerPosX<MapViewX-2){
                            playerPosX++;
                        }
                    }

                    if("d".Equals(inputKey)||"D".Equals(inputKey)){
                        if(playerPosY<MapViewY-2){
                            playerPosY++;
                        }
                    }
                    if("a".Equals(inputKey)||"A".Equals(inputKey)){
                        if(playerPosY>1){
                            playerPosY--;
                        }
                    }
                    mapData [playerOldPosX,playerOldPosY] = EmptyLine;
                    mapData [playerPosX,playerPosY] = Player;
                    //根据地图数据初始化地图
                    for(int i=0;i<MapViewX;i++){
                        String str = "";
                        for(int j=0;j<MapViewY;j++){
                            int data =     mapData[i,j];
                            switch (data) {
                            case EmptyLine:
                                str += " ";
                                break;
                            case TBLine:
                                str += "-";
                                break;
                            case LRLine:
                                str += "|";
                                break;
                            case Player:
                                str += "P";
                                break;
                            }
                        }
                        Console.WriteLine (str);
                    }
                }    
            }
            //游戏停止时的操作
            Console.WriteLine ("Game is Over! Come agin");
        }
    }
}


运行结果如下:
还是挺好玩的。。。

猜你喜欢

转载自www.cnblogs.com/aiqh/p/9910384.html