示例#1
0
 Point pointForBdPart(BodyPart bdPart)
 {
     return(bdPart.picBox.Location);
 }
        void ContinuedRun(BodyPart snakeHead, List <ContinuousSnake> snakes, List <Obstacle> obstacles)
        {
            // get x and y comps of velocity
            double thisX  = picBox.Location.X + (double)picBox.Width / (double)2;
            double thisY  = picBox.Location.Y + (double)picBox.Height / (double)2;
            double snakeX = snakeHead.picBox.Location.X + (double)snakeHead.picBox.Width / (double)2;
            double snakeY = snakeHead.picBox.Location.Y + (double)snakeHead.picBox.Height / (double)2;
            // get the comps
            double deltaX = snakeX - thisX;
            double deltaY = snakeY - thisY;
            // scale them according to target velocity magnitude
            double triMag = Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2));

            deltaX = deltaX * velocityMag / triMag;
            deltaY = deltaY * velocityMag / triMag;

            // run checks here for obstacles
            foreach (Obstacle obstacle in obstacles)
            {
                // get the rectangle that represents collision area with an obstacle
                Rectangle collision = Rectangle.Intersect(obstacle.picBox.Bounds, picBox.Bounds);
                if (collision != Rectangle.Empty) // only run if collision is existent
                {
                    if (collision.Width > collision.Height)
                    {
                        deltaY = 0;
                        // push the enemy out by the height
                        if (collision.Y > picBox.Location.Y)
                        {
                            picBox.Location = new Point(picBox.Location.X, picBox.Location.Y - collision.Height - 1);
                        }
                        else if (collision.Y < picBox.Location.Y)
                        {
                            picBox.Location = new Point(picBox.Location.X, picBox.Location.Y + collision.Height + 1);
                        }
                    }
                    else if (collision.Height > collision.Width)
                    {
                        deltaX = 0;
                        if (collision.X > picBox.Location.X)
                        {
                            picBox.Location = new Point(picBox.Location.X - collision.Width - 1, picBox.Location.Y);
                        }
                        else if (collision.Y < picBox.Location.Y)
                        {
                            picBox.Location = new Point(picBox.Location.X + collision.Width + 1, picBox.Location.Y);
                        }
                    }
                    else
                    {
                        //deltaX = 0;
                        //deltaY = 0;
                    }
                }
            }
            // run checks here for snakes
            foreach (ContinuousSnake snake in snakes)
            {
                // get body segment that the enemy collided with
                BodySegment segment = snake.CollidedWith(picBox);
                // end the current loop if no segment returned
                if (segment == null)
                {
                    continue;
                }
                // get the rectangle that represents collision area with an obstacle
                Rectangle collision = Rectangle.Intersect(segment.picBox.Bounds, picBox.Bounds);
                if (collision != Rectangle.Empty) // only run if collision is existent
                {
                    if (collision.Width > collision.Height)
                    {
                        deltaY = 0;
                    }
                    else if (collision.Height > collision.Width)
                    {
                        deltaX = 0;
                    }
                    else
                    {
                        //deltaX = 0;
                        //deltaY = 0;
                    }
                }
            }

            // move the picturebox
            picBox.Location = new System.Drawing.Point((int)(picBox.Location.X + deltaX), (int)(picBox.Location.Y + deltaY));
        }