Exemplo n.º 1
0
 public SnakeBody(int _x, int _y, Color _col, bool _ex, SnakeProperty _p)
 {
     x     = _x;
     y     = _y;
     color = _col;
     exist = _ex;
     proty = _p;
 }
Exemplo n.º 2
0
        /// <summary>
        /// 蛇身前进一步
        /// </summary>
        /// <param name="dir">前进方向</param>
        /// <returns>蛇生命值</returns>
        private SnakeLife SnakeMoveStep(SnakeDirection dir)
        {
            SnakeBody tail = new SnakeBody(0, 0, _SColor, true, SnakeProperty.Body);

            tail.x = _SnakeList[_SnakeList.Count - 1].x;//保存蛇尾坐标
            tail.y = _SnakeList[_SnakeList.Count - 1].y;


            SnakeBody body_t = new SnakeBody(0, 0, _SColor, true, SnakeProperty.Body);

            for (int i = (_SnakeList.Count - 1); i > 0; i--)
            {//更新蛇身坐标
                body_t.x      = _SnakeList[i - 1].x;
                body_t.y      = _SnakeList[i - 1].y;
                _SnakeList[i] = body_t;
            }

            int dx = 0, dy = 0;

            _GetVariation(dir, out dx, out dy);                                        //根据移动方向获取移动xy轴上的增量
            SnakeBody body_h = new SnakeBody(0, 0, _SColor, true, SnakeProperty.Head); //更新蛇头坐标

            body_h.x = _SnakeList[0].x + dx;
            body_h.y = _SnakeList[0].y + dy;

            //穿墙处理
            int _maxX = _Windows.Size.Width / _BodySize;
            int _maxY = _Windows.Size.Height / _BodySize;

            if (_CrossWall == false)
            {
                if ((body_h.x >= _maxX) || (body_h.x < 0) ||
                    (body_h.y >= _maxY) || (body_h.y < 0))
                {
                    _Life = SnakeLife.Die;
                }
            }

            if (body_h.x >= _maxX)
            {
                body_h.x = 0;
            }
            if (body_h.x < 0)
            {
                body_h.x = _maxX - 1;
            }
            if (body_h.y >= _maxY)
            {
                body_h.y = 0;
            }
            if (body_h.y < 0)
            {
                body_h.y = _maxY - 1;
            }

            _SnakeList[0] = body_h;//更新蛇头坐标

            //判断是否吃到食物或咬到自己(在未更新坐标之前预判)
            int           foodx = 0, foody = 0;
            SnakeProperty p = _SnakeGetPointProperty(_SnakeList[0].x, _SnakeList[0].y);

            switch (p)
            {
            case SnakeProperty.Food:
                Console.WriteLine("SnakeProperty: " + p.ToString() + " SnakeLife: " + _Life.ToString());
                //吃到食物后产生一个食物,蛇身长度增加
                if (_SnakeGetRandomXY(out foodx, out foody))    //创建食物
                {
                    _Food.x = foodx;
                    _Food.y = foody;
                    _SnakeList.Add(tail); //增加蛇身长度
                    _Score++;             //得分增加
                    _Life = SnakeLife.Live;
                }
                else
                {
                    _Life = SnakeLife.Win;
                    //return _Life;
                }
                break;

            case SnakeProperty.Body:
            case SnakeProperty.Wall:
                Console.WriteLine("SnakeProperty: " + p.ToString() + " SnakeLife: " + _Life.ToString());
                _Life = SnakeLife.Die;    //死亡
                //return _Life;
                break;

            case SnakeProperty.Head:
            case SnakeProperty.Null:
            default:
                _Life = SnakeLife.Live;
                break;
            }

            _SnakeClearPoint(tail); //清除蛇尾
            _SnakeReDrawGame();     //更新显示蛇身

            return(_Life);
        }