Vinodh Mechery

Computer Science 1

Professor Jiang

5/14/08

 

Pong

 

 

 

 

 

 

 

Purpose: To create a 2-player Pong game

 

            Pong was the first commercially successful game created by Atari decades ago. It has been created in Java and other languages that are capable of graphics. Until this project I had never worked on a graphics interface before. I had studied Java a little bit in school and I had always wanted to create Pong. As simple as the game seems, it was quite difficult to create because there were a lot of variables to keep track off. I used the Draw and DrawListener classes to create the graphics. Originally I was going to take the Object Oriented Programming approach and have three classes. I was going to have a Paddle Class, Ball Class, and Pong class. The paddle class would have a constructor to create the Paddle object which would take four arguments(x coordinate, y coordinate, length, and width). The class would also have a collision detect method for ball with the paddle object. In the Ball class the Ball object was to be created and it would have collision detect with the walls. The Pong class would have brought it all together implementing both classes through objects and creating a board and interface.

This was the original plan but unfortunately it did not work. Instead I took a procedural approach and combined everything into the Pong class. In this class there were 6 methods: Draw_paddle1, Draw_paddle2, run, pscore1,  pscore2, keytyped, and setVelocity. In addition to this there was a constructor and main method. The Draw_paddle1 and Draw_paddle2 methods took initialized data before the constructor and used this to draw the paddles using the Draw class.

Draw_paddle1:

public void Draw_paddle1()// draw paddle 1

         {

                 

            d.line(x1,y1,x1, y1+l1);

            d.line(x1, y1, x1 + w1,y1);

            d.line(x1 + w1,y1, x1 + w1, y1 + l1 );

            d.line(x1,y1 +l1, x1 + w1 ,y1 + l1);

           

    }

 The keyTyped method came from the DrawListener method and took input from the keyboard necessary to move the paddles up and down. There were if statements to make sure the paddles stayed inbounds. The setVelocity method took to arguments to change the speed of the ball. The run method took one argument which was to how many points to play too.

Run method:

public void run(int maxpoints)// main animation loop

    {

     

      

      while(p1score != maxpoints && p2score != maxpoints)

      {

                  // reset all values

                 

            rx = 1;// x of ball

                   ry = .5;// y of ball

                   rd = .01;// radius of ball

                   x1 = 0;// paddle 1

                   y1 = .5;

                   l1 = .15;

                   w1 = .05;

                   x2 = 1.9;// paddle 2

                   y2 = .5;

                  while(true)

            {

                 

            d.clear();// clear screen

            d.line(1,0,1,1);

            d.text(.5,0,"p1");

            d.text(1.5,0,"p2");

            d.text(.5,.5,Integer.toString(p1score));

                  d.text(1.5,.5,Integer.toString(p2score));

                 

            Draw_paddle1();

            Draw_paddle2();

            //bounce off walls

 

            if ((rx + vx) > 2.0 - rd || (rx + vx)< 0 + rd )

            {

                  if(rx > 1)

                  {p1score++;

                  break;}

                  if(rx < 1)

                  {

                  p2score++;

                  break;

                  }

            }

                  if (Math.abs(ry + vy) > 1.0 - rd || (ry + vy) < 0 + rd)

                  vy = -vy;

           

            //  paddle 1 collision

            if(((rx + vx) <  x1 + w1 + rd ) && (ry + vy) > y1 && (ry + vy) < y1 + l1)

                  vx = -vx;

                 

           

            // paddle 2 collision

           

            if(((rx + vx) > x2 - rd) && ((ry + vy) > y2 && (ry + vy) <y2 + l1))

           

                  vx = -vx;

           

                       

 

            // set values of ball

            rx += vx;

            ry += vy;

        

            // draw ball

            d.setPenColor(Color.blue);

            d.filledCircle(rx, ry,rd );

            // display and pause for 1 ms

            d.show(1);     

 

            }

                  try

              {

                  sleep(1500);// pause for 1.5 seconds inbetween points

              }

              catch(InterruptedException e){

                  throw new RuntimeException(e);

              }  

           

      }

   

    }

 

There was a while loop within a while loop. The first while loop was for the overall game and the second was for each individual point. Before entering the second while loop the values of the paddle and ball were all reset. In the second while loop the paddles were drawn and there was detect for the ball with the paddles and the walls. Finally the ball was redrawn and everything was refreshed every 1 ms. A sleep method was placed so the game would pause every 1.5 seconds inbetween points. The main method created input boxes to choose the maximum amount of points and velocity. After the game was over a winner display is shown.

Ultimately the project was a success. I would have changed the paddle function from keyTyped to keyPressed in order to make it easier for the user. I plan on continuing this project through the summer. I hope to make it using Swing instead of the Draw class. More importantly I intend to use this project as the start for learning AI code using the chasing example you showed in class. I hope to show you my work after the summer is done.

 

Finished Product!!!!