Пример #1
0
        internal int moveSnake(Game myGame, int unitLength)
        {
            bool hitFood = false;
            SnakeBody index = this.head;
            int lastX = index.rectangleLeft;
            int lastY = index.rectangleTop;
            index.rectangleLeft += horizonMove;
            index.rectangleTop += verticalMove;
            if(head.rectangleLeft >= myGame.borderRight || head.rectangleLeft < myGame.borderLeft || head.rectangleTop >= myGame.borderBottom || head.rectangleTop < myGame.borderTop)
            {
                MessageBox.Show("too bad");
                Environment.Exit(1);
                return 1;
            }

            if (head.rectangleLeft == myGame.foodLocX && head.rectangleTop == myGame.foodLocY)
            {
                hitFood = true;
            }
            index = index.next;
            while (index != null)
            {
                int tmpX = index.rectangleLeft;
                int tmpY = index.rectangleTop;
                index.rectangleLeft = lastX;
                index.rectangleTop = lastY;
                lastX = tmpX;
                lastY = tmpY;
                index = index.next;
            }
            if (hitFood)
            {
                SnakeBody newBody = new SnakeBody(lastX, lastY, unitLength);
                newBody.bodyBlock.Fill = myGame.foodRec.Fill;
                tail.next = newBody;
                tail = newBody;
                myGame.generateFoodLoc(unitLength);
            }
            else
            {
                myGame.obstacle.RemoveAt(0);
            }
            Tuple<int, int> newTuple = new Tuple<int, int>(head.rectangleLeft, head.rectangleTop);
            if(myGame.obstacle.Contains(newTuple))
            {
                MessageBox.Show("too bad");
                Environment.Exit(1);
            }
            else
            {
                myGame.obstacle.Add(new Tuple<int, int>(head.rectangleLeft, head.rectangleTop));
            }
            return 0;
        }