Beispiel #1
0
        private void MainLoop(object sender, EventArgs e)
        {
            /* Clears the Screen */
            CreateGraphics().FillRectangle(new SolidBrush(Color.Black), 0, 0, 800, 600);
            pongBall.Draw(this.CreateGraphics());
            right.Draw(this.CreateGraphics());
            left.Draw(CreateGraphics());
            pongBall.calculate();
            if (pongBall.getX() <= 0)
            {
                left.setPoints(left.getPoints() + 1);
                if (left.getPoints() >= numberOfPointsToWin)
                {
                    CreateGraphics().FillRectangle(new SolidBrush(Color.Black), 0, 0, 800, 600);

                    CreateGraphics().DrawString("Right WINS!", new Font("Arial", 72), new SolidBrush(Color.White), new PointF(100, 100));
                    t.Enabled = false;
                }
                right.setX(770);
                right.setY(250);
                left.setX(30);
                left.setY(250);
                pongBall.setX(400);
                pongBall.setY(300);
                pongBall.setXVelocity((int)(randomObject.Next(4, pongBall.getMaxSpeed() * 2) - pongBall.getMaxSpeed()));
                pongBall.setYVelocity((int)(randomObject.Next(4, pongBall.getMaxSpeed() * 2) - pongBall.getMaxSpeed()));
            }

            if (pongBall.getX() >= 800)
            {
                right.setPoints(right.getPoints() + 1);
                if (right.getPoints() >= numberOfPointsToWin)
                {
                    CreateGraphics().FillRectangle(new SolidBrush(Color.Black), 0, 0, 800, 600);

                    CreateGraphics().DrawString("LEFT WINS!", new Font("Arial", 72), new SolidBrush(Color.White), new PointF(90, 100));
                    t.Enabled = false;
                    return;
                }
                right.setX(770);
                right.setY(250);
                left.setX(30);
                left.setY(250);
                pongBall.setX(400);
                pongBall.setY(300);
                pongBall.setXVelocity((int)(randomObject.Next(4, pongBall.getMaxSpeed() * 2) - pongBall.getMaxSpeed()));
                pongBall.setYVelocity((int)(randomObject.Next(4, pongBall.getMaxSpeed() * 2) - pongBall.getMaxSpeed()));
            }
            if (pongBall.getY() >= 600)
            {
                pongBall.setYVelocity(-pongBall.getYVelocity());
            }
            if (pongBall.getY() <= 0)
            {
                pongBall.setYVelocity(-pongBall.getYVelocity());
            }

            if (pongBall.getX() <= left.getX() + left.getWidth() && pongBall.getX() >= left.getX())
            {
                if (pongBall.getY() <= left.getY() + left.getHeight() && pongBall.getY() >= left.getY())
                {
                    pongBall.setXVelocity(-pongBall.getXVelocity());
                }
            }
            if (pongBall.getX() <= right.getX() + right.getWidth() && pongBall.getX() >= right.getX())
            {
                if (pongBall.getY() <= right.getY() + right.getHeight() && pongBall.getY() >= right.getY())
                {
                    pongBall.setXVelocity(-pongBall.getXVelocity());
                }
            }
            CreateGraphics().DrawString(right.getPoints() + "", new Font("Arial", 72), new SolidBrush(Color.White), new PointF(100, 100));
            CreateGraphics().DrawString(left.getPoints() + "", new Font("Arial", 72), new SolidBrush(Color.White), new PointF(600, 100));
            Console.WriteLine("Pong Ball Position - X: " + pongBall.getX() + " Y: " + pongBall.getY());
            Console.WriteLine("Paddle Left Position - X: " + left.getX() + " Y: " + left.getY());
            if (right.getPoints() >= numberOfPointsToWin)
            {
                CreateGraphics().FillRectangle(new SolidBrush(Color.Black), 0, 0, 800, 600);

                CreateGraphics().DrawString("LEFT WINS!", new Font("Arial", 72), new SolidBrush(Color.White), new PointF(90, 100));
                t.Enabled = false;
                return;
            }
            if (left.getPoints() >= numberOfPointsToWin)
            {
                CreateGraphics().FillRectangle(new SolidBrush(Color.Black), 0, 0, 800, 600);

                CreateGraphics().DrawString("Right WINS!", new Font("Arial", 72), new SolidBrush(Color.White), new PointF(100, 100));
                t.Enabled = false;
                return;
            }
        }