Esempio n. 1
0
        public MovementResult Move(int fieldWidth, int fieldHeight, UIApple apple)
        {
            UISegment oldHead = this.body.Last();
            UISegment newHead = new UISegment(
                oldHead.X + this.deltas[this.direction].X * oldHead.Width,
                oldHead.Y + this.deltas[this.direction].Y * oldHead.Height,
                oldHead.Width,
                oldHead.Height,
                oldHead.Pen);

            if (newHead.X >= fieldWidth || newHead.X < 0 ||
                newHead.Y >= fieldHeight || newHead.Y < 0)
            {
                return MovementResult.Wall;
            }

            if (this.IsOn(newHead))
            {
                return MovementResult.SelfBite;
            }

            // the following two operations create the effect of motion:
            // 1. add the new head in front of the old one
            this.body.Enqueue(newHead);

            // if the snake is on the apple
            if (this.IsOn(apple))
            {
                // increase the length by 1, i.e. don't remove the last segment
                this.removedSegment = null;
                return MovementResult.OnApple;
            }
            else
            {
                // 2. remove the last segment
                this.removedSegment = this.body.Dequeue();
            }

            return MovementResult.OK;
        }
Esempio n. 2
0
        public MovementResult Move(int fieldWidth, int fieldHeight, UIApple apple)
        {
            UISegment oldHead = this.body.Last();
            UISegment newHead = new UISegment(
                oldHead.X + this.deltas[this.direction].X * oldHead.Width,
                oldHead.Y + this.deltas[this.direction].Y * oldHead.Height,
                oldHead.Width,
                oldHead.Height,
                oldHead.Pen);

            if (newHead.X >= fieldWidth || newHead.X < 0 ||
                newHead.Y >= fieldHeight || newHead.Y < 0)
            {
                return(MovementResult.Wall);
            }

            if (this.IsOn(newHead))
            {
                return(MovementResult.SelfBite);
            }

            // the following two operations create the effect of motion:
            // 1. add the new head in front of the old one
            this.body.Enqueue(newHead);

            // if the snake is on the apple
            if (this.IsOn(apple))
            {
                // increase the length by 1, i.e. don't remove the last segment
                this.removedSegment = null;
                return(MovementResult.OnApple);
            }
            else
            {
                // 2. remove the last segment
                this.removedSegment = this.body.Dequeue();
            }

            return(MovementResult.OK);
        }