Пример #1
0
        /// <summary>
        /// 判断是否失败
        /// </summary>
        /// <returns>是否失败</returns>
        private bool IsLose()
        {
            ControlHost host = Native.Host;
            SnakeBody   body = m_bodys[0];

            //蛇头是否撞墙
            if (body.Left < 0 || body.Right > Width || body.Top < 0 || body.Bottom > Width)
            {
                return(true);
            }
            //蛇关节间是否碰撞
            int bodySize = m_bodys.Count;

            for (int i = 0; i < bodySize; i++)
            {
                for (int j = i + 1; j < bodySize - i - 1; j++)
                {
                    RECT tempRect = new RECT();
                    RECT iRect    = m_bodys[i].Bounds;
                    RECT jRect    = m_bodys[j].Bounds;
                    if (host.GetIntersectRect(ref tempRect, ref iRect, ref jRect) > 0)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Пример #2
0
        /// <summary>
        /// 添加食物
        /// </summary>
        private void AddFood()
        {
            Food food = new Food();

            food.Size = new SIZE(SNAKE_SIZE, SNAKE_SIZE);
            ControlHost host = Native.Host;

            while (true)
            {
                //获取随机数
                int num  = m_random.Next(0, Width / SNAKE_SIZE - 1);
                int num2 = m_random.Next(0, Height / SNAKE_SIZE - 1);
                //设置位置
                food.Location = new POINT(num * SNAKE_SIZE, num2 * SNAKE_SIZE);
                bool reset    = false;
                RECT fRect    = food.Bounds;
                int  bodySize = m_bodys.Count;
                for (int j = 0; j < bodySize; j++)
                {
                    //如果与关节重合,需要重新设置位置
                    RECT tempRect = new RECT();
                    RECT bRect    = m_bodys[j].Bounds;
                    if (host.GetIntersectRect(ref tempRect, ref bRect, ref fRect) > 0)
                    {
                        reset = true;
                    }
                }
                int foodSize = m_foods.Count;
                for (int j = 0; j < foodSize; j++)
                {
                    RECT fiRect   = m_foods[j].Bounds;
                    RECT tempRect = new RECT();
                    //如果与食物重合,需要重新设置位置
                    if (host.GetIntersectRect(ref tempRect, ref fiRect, ref fRect) > 0)
                    {
                        reset = true;
                    }
                }
                if (!reset)
                {
                    break;
                }
            }
            AddControl(food);
            m_foods.Add(food);
            food.BringToFront();
        }
Пример #3
0
        /// <summary>
        /// 拖动中方法
        /// </summary>
        public override void OnDragging()
        {
            base.OnDragging();
            ControlA        parent       = Parent;
            ControlHost     host         = Native.Host;
            RECT            tempRect     = new RECT();
            RECT            bounds       = Bounds;
            List <ControlA> controls     = parent.m_controls;
            int             controlsSize = controls.Count;
            int             thisIndex    = -1;

            for (int i = 0; i < controlsSize; i++)
            {
                ControlA iControl = controls[i];
                if (iControl == this)
                {
                    thisIndex = i;
                    break;
                }
            }
            int mx = bounds.left + (bounds.right - bounds.left) / 2;
            int my = bounds.top + (bounds.bottom - bounds.top) / 2;

            for (int i = 0; i < controlsSize; i++)
            {
                ControlA iCell = controls[i];
                if (iCell != this && !(iCell is ScrollBarA))
                {
                    RECT iBounds = iCell.Bounds;
                    if (host.GetIntersectRect(ref tempRect, ref bounds, ref iBounds) > 0)
                    {
                        if (mx >= iBounds.left && mx <= iBounds.right && my >= iBounds.top && my <= iBounds.bottom)
                        {
                            controls[thisIndex] = iCell;
                            controls[i]         = this;
                            RECT oldBounds = iCell.Bounds;
                            iCell.Bounds = m_paintRect;
                            m_paintRect  = oldBounds;
                            parent.Invalidate();
                            break;
                        }
                    }
                }
            }
        }
Пример #4
0
        /// <summary>
        /// 单元格拖动方法
        /// </summary>
        /// <param name="cell">单元格</param>
        public void OnCellDragging(UserSecurityCellT2 cell)
        {
            ControlHost host      = Native.Host;
            RECT        tempRect  = new RECT();
            RECT        bounds    = cell.Bounds;
            int         cellsSize = m_cells.Count;
            int         thisIndex = -1;

            for (int i = 0; i < cellsSize; i++)
            {
                UserSecurityCellT2 iCell = m_cells[i];
                if (iCell == cell)
                {
                    thisIndex = i;
                    break;
                }
            }
            int mx = bounds.left + (bounds.right - bounds.left) / 2;
            int my = bounds.top + (bounds.bottom - bounds.top) / 2;

            for (int i = 0; i < cellsSize; i++)
            {
                UserSecurityCellT2 iCell = m_cells[i];
                if (iCell != cell)
                {
                    RECT iBounds = iCell.PaintRect;
                    if (host.GetIntersectRect(ref tempRect, ref bounds, ref iBounds) > 0)
                    {
                        if (mx >= iBounds.left && mx <= iBounds.right && my >= iBounds.top && my <= iBounds.bottom)
                        {
                            m_cells[thisIndex] = iCell;
                            m_cells[i]         = cell;
                            Update();
                            break;
                        }
                    }
                }
            }
        }
Пример #5
0
        /// <summary>
        /// 移动蛇
        /// </summary>
        private void MoveSnake()
        {
            ControlHost host     = Native.Host;
            int         bodySize = m_bodys.Count;
            SnakeBody   lastBody = m_bodys[bodySize - 1];
            int         left     = lastBody.Left;
            int         top      = lastBody.Top;

            //改变蛇身每个关节的位置
            for (int i = 0; i < bodySize; i++)
            {
                SnakeBody body = m_bodys[i];
                switch (body.Direction)
                {
                //向下
                case SnakeDirection.Down:
                    body.Top += body.Height;
                    break;

                //向左
                case SnakeDirection.Left:
                    body.Left -= body.Width;
                    break;

                //向右
                case SnakeDirection.Right:
                    body.Left += body.Width;
                    break;

                //向上
                case SnakeDirection.Up:
                    body.Top -= body.Height;
                    break;
                }
            }
            //判断游戏失败
            if (IsLose())
            {
                m_play.GameState = GameState.Lose;
                m_play.BringToFront();
            }
            else
            {
                bool newFood = false;
                //判断吃食物
                int foodSize = m_foods.Count;
                for (int i = 0; i < foodSize; i++)
                {
                    Food food     = m_foods[i];
                    RECT tempRect = new RECT();
                    RECT bRect    = m_bodys[0].Bounds;
                    RECT fRect    = food.Bounds;
                    if (host.GetIntersectRect(ref tempRect, ref bRect, ref fRect) > 0)
                    {
                        m_eatCount++;
                        //添加关节
                        RECT      rc   = new RECT(left, top, left + SNAKE_SIZE, top + SNAKE_SIZE);
                        SnakeBody body = new SnakeBody(rc);
                        m_bodys.Add(body);
                        AddControl(body);
                        body.BringToFront();
                        //移除食物
                        RemoveControl(food);
                        m_foods.Remove(food);
                        newFood = true;
                        break;
                    }
                }
                //改变移动方向
                bodySize = m_bodys.Count;
                for (int i = bodySize - 1; i > 0; i--)
                {
                    m_bodys[i].Direction = m_bodys[i - 1].Direction;
                }
                //设置新的食物
                if (newFood)
                {
                    AddFood();
                }
            }
            Invalidate();
        }