Exemple #1
0
        public static void Main(string[] args)
        {
            // size of map
            int MaxMapWidth = 80;
            int MaxMapHeight = 25;

            Console.SetBufferSize(MaxMapWidth, MaxMapHeight);

            // drow frame
            Walls walls = new Walls(MaxMapWidth, MaxMapHeight);
            walls.Draw();

            // drow point draw snake

            Point point = new Point(5, 10, '*');
            Snake snake = new Snake (point, 5, Direction.RIGHT);
            snake.Draw ();

            // draw food

            FoodCreater foodCreator = new FoodCreater (MaxMapWidth, MaxMapHeight, '$');
            Point food = foodCreator.CreateFood ();
            //Console.ForegroundColor = ConsoleColor.Cyan;
            food.Draw ();

            while (true) {
                // data de perete
                if (walls.IsHit (snake) || snake.IsHitTail()) break;

                // daca maninca

                if(snake.Eat(food)){
                    food = foodCreator.CreateFood ();
                    food.Draw ();
                }
                else
                    snake.Move ();

                Thread.Sleep (200);

                if (Console.KeyAvailable) {
                    ConsoleKeyInfo key = Console.ReadKey ();
                    snake.Hadlekey (key.Key);

                    if (key.Key == ConsoleKey.Escape)
                        break;
                }

            }
            // END

            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(MaxMapWidth/3 , 10);
            Console.WriteLine ("--=***=--  E N D  --=***=--");
            Console.SetCursorPosition(MaxMapWidth/3 , 12);
            //Thread.Sleep (300);
            //Console.ReadLine ();
        }
Exemple #2
0
        public static void Main()
        {
            List <Animal> animals = new List <Animal>();
            string        command = Console.ReadLine();

            while (command != "End")
            {
                string[] token = command.Split();

                Animal animal = AnimalsCreater.CreateAnimal(token);
                animals.Add(animal);
                Console.WriteLine(animal.ProduceSound());

                string[] foodInfo = Console.ReadLine().Split();
                Food     food     = FoodCreater.CreateFood(foodInfo);

                try
                {
                    animal.EatFood(food);
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine(e.Message);
                }

                command = Console.ReadLine();
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
        private void DecodeMessage(string message)
        {
            this.textBoxLogger.Invoke(new TextBoxReceive(Logger), "<RECEIVE>  " + message);
            // 解析发送过来的消息
            string[] msgArray = ((string)message).Trim().Split(',');

            switch (Convert.ToInt32(msgArray[0]))
            {
            case MessageCode.LOGIN:
                // snakeBodyList 添加 同时转发消息
                PlayerList.Add(new SnakeBody(msgArray[1]));
                GameServerSocket.BroadcastMessage(GeneraterOnlineList());
                if (PlayerList.Count == PlayerNum)
                {
                    // 在线用户数目达到一定的数目的时候向所有在线用户发送游戏开始信号
                    Food.CreateFood();
                    broadMessage = MessageCode.GAME_START.ToString() + "," +
                                   Food.FoodPosition.X.ToString() + "," +
                                   Food.FoodPosition.Y.ToString() + "," +
                                   ChooseFoodColorType(Food.FoodColor);
                }
                break;

            case MessageCode.DEAD:
                // snakeBodyList 移除 同时转发消息
                PlayerList.Remove(FindSnakeBodyByID(msgArray[1], PlayerList));
                broadMessage = message;
                break;

            case MessageCode.EAT_FOOD:
                // eatfood 之后要再生成一个食物
                Food.CreateFood();
                broadMessage += message + "," +
                                Food.FoodPosition.X.ToString() + "," +
                                Food.FoodPosition.Y.ToString() + "," +
                                ChooseFoodColorType(Food.FoodColor);
                break;

            default:     // 其他消息直接广播
                broadMessage = message;
                break;
            }
        }
        /// <summary>
        /// 游戏开始
        /// </summary>
        /// <param name="winWidth">界面宽度</param>
        /// <param name="winHeight">界面高度</param>
        /// <param name="snakeGrap">画布</param>
        public void GameStart(int winWidth, int winHeight, BufferedGraphics bufferGrap)
        {
            // 游戏开始时候设置 winWidth, winHeight, snakeGrap
            // 初始化winwidth, winheight, snakegrap
            this.m_bufferGrap = bufferGrap;
            this.m_gameGrap   = bufferGrap.Graphics;
            this.m_winHeight  = winHeight;
            this.m_winWidth   = winWidth;

            // 计算snake初始点
            m_snakeBeginPos = new Point(winWidth / 2 - (winWidth / 2) % 10,
                                        winHeight / 2 - (winHeight / 2) % 10);

            m_snake.CreateSnakeBody(m_snakeBeginPos, this.m_gameGrap);
            m_snake.SnakeBodyMoveSpeed = ClientGameControl.m_snakeMoveSpeed;

            if (this.PlayerGameMode == GameMode.OFFLINE)
            {
                // 单机模式
                this.IsGameStart = true;
                this.IsGamePause = false;
                // 实例化食物类
                m_food = new FoodCreater(winWidth, winHeight,
                                         this.m_gameGrap, new Size(10, 10));
                m_food.CreateFood();   // 生成食物
            }
            else if (this.PlayerGameMode == GameMode.ONLINE)
            {
                // 联机模式
                // 这里不能设置IsGameStart为true, 只有收到GAME_START信号的时候才能开始游戏
                // 实例化本地SnakeBody

                // 实例化食物类
                m_food = new FoodCreater(winWidth, winHeight,
                                         this.m_gameGrap, new Size(10, 10));
            }
        }
        public void DecodeMessage(string message)
        {
            Console.WriteLine("<CLIENT_RECEIVE> {0}", (string)message);

            string[] msgArray = ((string)message).Trim().Split(',');

            switch (Convert.ToInt32(msgArray[0]))
            {
            case MessageCode.LOGIN:
                // 识别到登录码
                for (int i = 1; i < msgArray.Length; i++)
                {
                    if (msgArray[i] != this.Snake.SnakeBodyID && !IsSnakeBodyExist(msgArray[i], m_playerList))
                    {
                        SnakeBody newSnake = new SnakeBody(msgArray[i]);
                        newSnake.CreateSnakeBody(this.m_snakeBeginPos, this.m_gameGrap);
                        m_playerList.Add(newSnake);
                    }
                }
                break;

            case MessageCode.DEAD:
                // 识别到退出码
                if (msgArray[1] != this.Snake.SnakeBodyID)
                {
                    FindSnakeBodyByID(msgArray[1], m_playerList).IsAlive = false;
                }
                break;

            case MessageCode.GAME_START:
                // GAME_START,X,Y,COLOR
                // 识别到游戏开始码
                this.IsGameStart = true;
                m_food.CreateFood(new Point(Convert.ToInt32(msgArray[1]), Convert.ToInt32(msgArray[2])),
                                  ChooseColor(Convert.ToInt32(msgArray[3])));       // 生成食物
                break;

            case MessageCode.GAME_OVER:
                break;

            case MessageCode.EAT_FOOD:
                // 吃掉食物码 判断哪一个SnakeBody 吃掉食物 addItem 同时生成
                if (msgArray[1] != this.Snake.SnakeBodyID)
                {
                    FindSnakeBodyByID(msgArray[1], m_playerList).AddSnakeItem();
                }
                m_food.CreateFood(new Point(Convert.ToInt32(msgArray[2]), Convert.ToInt32(msgArray[3])),
                                  ChooseColor(Convert.ToInt32(msgArray[4])));      // 生成食物
                Console.WriteLine("#Create food");
                break;

            case MessageCode.CHANGE_DIREC:
                if (msgArray[1] == this.Snake.SnakeBodyID)
                {
                    this.Snake.SnakeBodyDirec = (SnakeBody.Direction)(Convert.ToInt32(msgArray[2]));
                }
                else
                {
                    FindSnakeBodyByID(msgArray[1], m_playerList).SnakeBodyDirec = (SnakeBody.Direction)(Convert.ToInt32(msgArray[2]));
                }
                break;

            case MessageCode.REACH_BORDER:
            case MessageCode.EAT_SELF:
                // 如果是自身发送的消息返回的则不用处理
                if (msgArray[1] == this.Snake.SnakeBodyID)
                {
                    return;
                }
                FindSnakeBodyByID(msgArray[1], m_playerList).IsAlive = false;
                break;

            case MessageCode.MOVE:
                SnakeMove();
                break;

            default:
                break;
            }

            GameUpdate();
        }