示例#1
0
    protected void Updateball(System.Object sender, ElapsedEventArgs evt)
    {
        real_coord_x = real_coord_x + ball_horizontal_delta;
        real_coord_y = real_coord_y - vertical_delta;
        init_coord_x = (int)System.Math.Round(real_coord_x);
        init_coord_y = (int)System.Math.Round(real_coord_y);

        if (init_coord_x <= (0))              // if ball hits left "goal post"
        {
            System.Console.WriteLine("Right Player gets a point!");
            contact          = true;
            rightplayerscore = rightplayerscore + 1;             // if Left Player can't hit ball, then Right Player gets a point
        }

        // player hits right goal post
        else if (init_coord_x >= formwidth)
        {
            System.Console.WriteLine("Left Player gets a point!");
            leftplayerscore = leftplayerscore + 1;
            contact         = true;
        }

        // ball has either hit top or gone above "top". NOTE THAT VALUE IS LOW
        else if (init_coord_y <= titlebottom)
        {
            vertical_delta = -vertical_delta;
        }

        // ball has either hit bottom or gone past "bottom".
        else if (init_coord_y + 2 * ball_radius >= ground)
        {
            vertical_delta = -vertical_delta;
        }


        // ball has hit left paddle:
        else if (init_coord_x >= 0 && init_coord_x <= (left_paddle_x + paddlelength) &&
                 init_coord_y >= left_paddle_y && init_coord_y <= (left_paddle_y + paddlewidth))
        {
            ball_horizontal_delta = -ball_horizontal_delta;
        }

        // ball has hit right paddle:
        else if (init_coord_x >= right_paddle_x && init_coord_x <= (right_paddle_x + paddlelength) &&
                 init_coord_y >= right_paddle_y && init_coord_y <= (right_paddle_y + paddlewidth))
        {
            ball_horizontal_delta = -ball_horizontal_delta;
        }


        // If Ball hits the the goal post and there are still some remaining balls left..Continue.
        if (contact == true && (leftplayerscore + rightplayerscore) < 5)
        {
            contact = false;
            System.Console.WriteLine("Generating new random posiion...Done.");

            // new random direction for generated ball
            algorithms         = new algorithmlogic();
            ball_angle_radians = algorithms.get_random_direction();
            degrees            = ball_angle_radians * 180.0 / System.Math.PI;

            while ((degrees > 80 && degrees < 100) || (degrees > 260 && degrees < 280) || (degrees < -80 && degrees > -100))
            {
                algorithms         = new algorithmlogic();
                ball_angle_radians = algorithms.get_random_direction();
                degrees            = ball_angle_radians * 180.0 / System.Math.PI;
            }

            ball_horizontal_delta = distance_moved_per_refresh * System.Math.Cos(ball_angle_radians);
            vertical_delta        = distance_moved_per_refresh * System.Math.Sin(ball_angle_radians);

            // new coordinates ( ball goes back to center )
            init_coord_y = (ground - titlebottom) / 2;
            init_coord_x = formwidth / 2;
            real_coord_y = (ground - titlebottom) / 2;
            real_coord_x = formwidth / 2;
            distance_moved_per_refresh         = distance_moved_per_refresh + 1.1;
            control_clock.Enabled              = true;
            graphic_area_refresh_clock.Enabled = true;
        }

        // GAME OVER. Ran out of balls
        if (leftplayerscore + rightplayerscore == 5)
        {
            control_clock.Enabled = false;
            System.Console.WriteLine("The game has ended.");
            if (leftplayerscore > rightplayerscore)
            {
                System.Console.WriteLine("Left player has won!");
            }
            else
            {
                System.Console.WriteLine("Right player has won!");
            }
            System.Console.WriteLine("You can start a new game if you'd like, but that would erase your previous score" + "\n" + "If you wish to continue, press the New Game button");
        }
    }     //End of method Updateball
示例#2
0
    public Paddleframe()         // Class Constructor
    // Set the form
    {
        Text = "Pong Game";
        System.Console.WriteLine("formwidth = {0}. formheight = {1}.", formwidth, formheight);
        Size      = new Size(formwidth, formheight);
        BackColor = background_color;

        // GENERATES RANDOM LOCATION FOR BALL
        init_coord_x = (int)(real_coord_x);
        init_coord_y = (int)(real_coord_y);

        // Random Direction


        ball_angle_radians = algorithms.get_random_direction();

        degrees = ball_angle_radians * 180.0 / System.Math.PI;

        // if degrees > 80 && degrees < 90 && degrees > 260 && degrees < 280 ) get a new a direction.
        while (degrees > 80 && degrees < 100 || degrees > 260 && degrees < 280 || (degrees < -80 && degrees > -100))
        {
            algorithms         = new algorithmlogic();
            ball_angle_radians = algorithms.get_random_direction();
            degrees            = ball_angle_radians * 180.0 / System.Math.PI;
        }

        ball_horizontal_delta = distance_moved_per_refresh * System.Math.Cos(ball_angle_radians);
        vertical_delta        = distance_moved_per_refresh * System.Math.Sin(ball_angle_radians);
        System.Console.WriteLine("Direction of ball: {0} Degrees, {1} Radians",
                                 Round(degrees, 2), Round(ball_angle_radians, 2));


        // Set clocks
        graphic_area_refresh_clock.Enabled  = false;
        graphic_area_refresh_clock.Elapsed += new ElapsedEventHandler(Updatedisplay);

        control_clock.Enabled  = false;        //Initially the clock controlling the ball is stopped.
        control_clock.Elapsed += new ElapsedEventHandler(Updateball);

        Startgraphicclock(graphicrefreshrate);
        //refreshrate is how many times per second the display area is re-painted.
        Startclock(update_rate);



        //Make buttons visible
        Controls.Add(top_bar);
        Controls.Add(newgame);
        Controls.Add(gamestate);
        Controls.Add(exit);

        Controls.Add(leftscore_text);
        Controls.Add(leftscore_number);
        Controls.Add(speed_text);
        Controls.Add(speed_number);
        Controls.Add(rightscore_number);
        Controls.Add(rightscore_text);

        //Initialize buttons
        newgame.Click   += new EventHandler(startgame);
        gamestate.Click += new EventHandler(gamestatus);
        exit.Click      += new EventHandler(terminate);


        //Initialize paddle (near) the center of the form.
        left_paddle_x = paddlelength / 2;
        left_paddle_y = (ground - titlebottom) / 2;

        right_paddle_x = formwidth - (2 * paddlelength);
        right_paddle_y = (ground - titlebottom) / 2;

        KeyPreview = true;
        KeyUp     += new KeyEventHandler(OnKeyUp);

        DoubleBuffered = true;
    }    //End of constructor