示例#1
0
        private void timerSnakeGame_Tick(object sender, EventArgs e)
        {
            MovementResult movementResult = this.snake.Move(
                this.pictureBoxSnakeGame.Width,
                this.pictureBoxSnakeGame.Height,
                this.apple);

            if (movementResult == MovementResult.Wall ||
                movementResult == MovementResult.SelfBite)
            {
                timerSnakeGame.Stop();

                // Game is over
                MessageBox.Show(string.Format("Game over!!! Your points: {0}", snake.Length));
                this.Close();
            }

            if (movementResult == MovementResult.OnApple)
            {
                do
                {
                    this.apple = this.appleGenerator.CreateApple();
                }while (this.snake.IsOn(apple));
            }

            this.Invalidate(true);
        }
示例#2
0
        private void timerSnakeGame_Tick(object sender, EventArgs e)
        {
            MovementResult movementResult = this.snake.Move(
                this.pictureBoxSnakeGame.Width,
                this.pictureBoxSnakeGame.Height,
                this.apple);

            if (movementResult == MovementResult.Wall ||
                movementResult == MovementResult.SelfBite)
            {
                timerSnakeGame.Stop();

                // Game is over
                MessageBox.Show(string.Format("Game over!!! Your points: {0}", snake.Length));
                this.Close();
            }

            if (movementResult == MovementResult.OnApple)
            {
                do
                {
                    this.apple = this.appleGenerator.CreateApple();
                }
                while (this.snake.IsOn(apple));
            }

            this.Invalidate(true);
        }
        public UIApple CreateApple()
        {
            UIApple apple = new UIApple(
                randomGenerator.Next(1, this.fieldWidth / this.appleWidth) * this.appleWidth,
                randomGenerator.Next(1, this.fieldHeight / this.appleHeight) * this.appleHeight,
                this.appleWidth,
                this.appleHeight,
                Pens.Red);

            return(apple);
        }
示例#4
0
        public FormSnakeGame()
        {
            InitializeComponent();

            this.snake = new UISnake(snakeLength, segmentSize, segmentSize, Direction.Right, Pens.Green);

            this.appleGenerator = new UIAppleGenerator(
                this.pictureBoxSnakeGame.Width,
                this.pictureBoxSnakeGame.Height,
                this.appleSize,
                this.appleSize);

            do
            {
                this.apple = this.appleGenerator.CreateApple();
            }while (this.snake.IsOn(apple));

            this.timerSnakeGame.Start();
        }
示例#5
0
        public FormSnakeGame()
        {
            InitializeComponent();

            this.snake = new UISnake(snakeLength, segmentSize, segmentSize, Direction.Right, Pens.Green);

            this.appleGenerator = new UIAppleGenerator(
                this.pictureBoxSnakeGame.Width,
                this.pictureBoxSnakeGame.Height,
                this.appleSize,
                this.appleSize);

            do
            {
                this.apple = this.appleGenerator.CreateApple();
            }
            while (this.snake.IsOn(apple));

            this.timerSnakeGame.Start();
        }
示例#6
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;
        }
示例#7
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);
        }