コード例 #1
0
ファイル: Form1.cs プロジェクト: aitorperezzz/pinball-game
        /* Función constructora de la form. */
        public Form1()
        {
            InitializeComponent();

            /* Añadimos el timer y lo asociamos a la función OnTimedEvent. */
            System.Timers.Timer aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 1;
            aTimer.Enabled  = true;

            /* Funciones de callback para eventos. */
            Paint   += new PaintEventHandler(Form1_Paint);
            KeyDown += new KeyEventHandler(Form1_KeyDown);
            //KeyUp += new KeyEventHandler(Form1_KeyUp);

            KeyPreview     = true;
            DoubleBuffered = true;

            /* Iniciamos algunas variables para crear los flippers. */
            this.flippers = new List <Flipper>(2);
            Vector flipper0position = new Vector(this.canvasWidth / 4, this.canvasHeight / 5);
            Vector flipper1position = new Vector(3 * this.canvasWidth / 4, this.canvasHeight / 5);

            this.flipperData = new FlipperData(this.flipperLength, this.flipperHeight, this.interval);

            this.flippers.Add(new Flipper("left", flipper0position, -Math.PI / 5, flipperData));
            this.flippers.Add(new Flipper("right", flipper1position, Math.PI + Math.PI / 5, flipperData));

            /* Creamos los bumpers. */
            Vector[] bumperPositions = new Vector[3]
            {
                new Vector(this.canvasWidth / 4, 3 * this.canvasHeight / 4),
                new Vector(this.canvasWidth / 2, 3 * this.canvasHeight / 4),
                new Vector(3 * this.canvasWidth / 4, 3 * this.canvasHeight / 4)
            };
            this.bumpers = new List <Bumper>(3);
            for (int i = 0; i < 3; i++)
            {
                this.bumpers.Add(new Bumper(bumperPositions[i], this.bumperRad));
            }

            /* Creamos la pelota. */
            Vector ballPosition = new Vector(this.canvasWidth / 2, this.canvasHeight - 100);
            Vector ballVelocity = new Vector(rand.Next(1, 250), rand.Next(1, 250));

            this.ball = new Ball(ballPosition, ballVelocity, this.radius);

            /* Creamos las paredes de abajo. */
            this.walls = new List <Polygon>(2);
            double cuty, cutx;
            /* Pared de la izquierda. */
            Vector       flipper0direction = new Vector(Math.Cos(-Math.PI / 5), Math.Sin(-Math.PI / 5));
            StraightLine baseWall0         = new StraightLine(flipper0position, flipper0direction);

            cuty = baseWall0.GetYAt(0);
            cutx = baseWall0.GetXAt(0);
            List <Vector> verticesWall0 = new List <Vector>(3);

            verticesWall0.Add(new Vector(0, cuty));
            verticesWall0.Add(new Vector(cutx, 0));
            verticesWall0.Add(new Vector(0, 0));
            /* Pared de la derecha. */
            Vector       flipper1direction = new Vector(Math.Cos(Math.PI + Math.PI / 5), Math.Sin(Math.PI + Math.PI / 5));
            StraightLine baseWall1         = new StraightLine(flipper1position, flipper1direction);

            cuty = baseWall1.GetYAt(canvasWidth);
            cutx = baseWall1.GetXAt(0);
            List <Vector> verticesWall1 = new List <Vector>(3);

            verticesWall1.Add(new Vector(cutx, 0));
            verticesWall1.Add(new Vector(canvasWidth, cuty));
            verticesWall1.Add(new Vector(canvasWidth, 0));

            this.walls.Add(new Polygon(verticesWall0));
            this.walls.Add(new Polygon(verticesWall1));
        }
コード例 #2
0
        /* Recibe una pelota y tiene que comprobar si hay colisión
         * con este flipper por el método de ray casting. */
        public bool HandleCollisionRayCasting(Ball ball)
        {
            /* Si el flipper no se está moviendo, no comprobamos. */
            if (!this.riseTimer.IsRunning() && !this.fallTimer.IsRunning())
            {
                return(false);
            }

            /* Marco los límites superior e inferior para la colisión. */
            double top    = this.position.Y + Math.Sin(this.maxAngle) * this.length;
            double bottom = this.position.Y + Math.Sin(this.minAngle) * this.length;

            if (ball.Position.Y > top || ball.Position.Y < bottom)
            {
                /* Ni siquiera comprobamos en este caso. */
                return(false);
            }

            /* Apunto a un vector en la superficie del flipper. */
            Vector flipperVector;

            if (this.situation == "left")
            {
                flipperVector = Vector.Subtract(this.polygon.Vertices[2], this.polygon.Vertices[1]);
            }
            else
            {
                flipperVector = Vector.Subtract(this.polygon.Vertices[3], this.polygon.Vertices[0]);
            }

            /* Vector desde la pelota antigua hasta la actual. */
            Vector ballVector = Vector.Subtract(ball.Position, ball.LastPosition);

            /* Creo las rectas para los dos vectores. */
            StraightLine ballLine    = new StraightLine(ball.Position, ballVector);
            StraightLine flipperLine = new StraightLine(this.position, flipperVector);

            /* Calculo el punto de intersección de ambas líneas. */
            Vector intersection = StraightLine.CalculateIntersection(ballLine, flipperLine);

            /* Marco los límites derecho e izquierdo según la situación del flipper. */
            double left, right;

            if (this.situation == "left")
            {
                left  = this.position.X;
                right = this.position.X + this.length;
            }
            else
            {
                left  = this.position.X - this.length;
                right = this.position.X;
            }

            /* Comprobamos que el punto de intersección está en el área de colisión. */
            if (intersection.Y < top && intersection.Y > bottom &&
                intersection.X > left && intersection.X < right)
            {
                /* Hay colisión. */
                ball.Position = ball.LastPosition;

                /* Reflejo la velocidad sobre la perpendicular al lado del flipper. */
                ball.Deflect(flipperVector.NewPositiveRotation(Math.PI / 2).NewWithLength(1));

                ball.Velocity.X = ball.Velocity.X * 3.5;
                ball.Velocity.Y = ball.Velocity.Y * 3.5;
                if (ball.Velocity.Y < 0)
                {
                    ball.Velocity.Y = -ball.Velocity.Y;
                }
                if (this.situation == "left" && ball.Velocity.X < 0)
                {
                    ball.Velocity.X = -ball.Velocity.X;
                }
                else if (this.situation == "right" && ball.Velocity.X > 0)
                {
                    ball.Velocity.X = -ball.Velocity.X;
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }