Exemple #1
0
        private readonly string situation; /* Derecha o izquierda. */

        /* Función constructora de un flipper. */
        public Flipper(string situation, Vector position, double angle, FlipperData data)
        {
            /* Guardo algunas variables. */
            this.length = data.length;
            this.height = data.height;

            this.situation = String.Copy(situation);
            this.position  = position.Copy();
            this.angle     = angle;

            /* Inicializo los timers. */
            riseTimer = new AnimationTimer(25, "easeout");
            fallTimer = new AnimationTimer(175, "easein");

            /* Establezco ángulos mínimo y máximo. */
            this.minAngle = -Math.PI / 5;
            this.maxAngle = Math.PI / 5;

            /* Creo los vértices distinguiendo derecha e izquierda. */
            List <Vector> vertices  = new List <Vector>();
            Vector        direction = new Vector(Math.Cos(this.angle), Math.Sin(this.angle));

            direction.SetLength(this.length);
            Vector perp;

            if (this.situation == "left")
            {
                perp = direction.NewPositiveRotation(Math.PI / 2);
                perp.SetLength(this.height);
                Vector begin = Vector.Subtract(this.position, perp.NewWithLength(this.height / 2));
                vertices.Add(begin);
                vertices.Add(Vector.Sum(vertices[0], perp));
                vertices.Add(Vector.Sum(vertices[1], direction));
                vertices.Add(Vector.Subtract(vertices[2], perp));
            }
            else
            {
                perp = direction.NewNegativeRotation(Math.PI / 2);
                perp.SetLength(this.height);
                Vector begin = Vector.Sum(this.position, perp.NewWithLength(this.height / 2));
                vertices.Add(begin);
                vertices.Add(Vector.Subtract(vertices[0], perp));
                vertices.Add(Vector.Sum(vertices[1], direction));
                vertices.Add(Vector.Sum(vertices[2], perp));
            }
            this.polygon = new Polygon(vertices);
        }
Exemple #2
0
        /* 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));
        }