Posted by: Java_help April 22, 2011
IT -- Solutions Center[Forum]
Login in to Rate this Post:     0       ?        

package pingpong;

import java.awt.*;
import java.applet.*;
/*
 *
 * ball
 *
 */
class ball
{

 

 final int LEFT=0, RIGHT = 1, UP = 2, DOWN = 3;
 final int LEFT_WALL = 10, RIGHT_WALL = 300, TOP_WALL = 10, BOTTOM_WALL = 220;
 int x_pos,  y_pos, size;
 int dir_x, dir_y;
 int xinc = 10, yinc = 8;
 Graphics g;
 paddle p;
 Applet owner;

 void setApplet (Applet a)
 {
  owner = a;
 }
 void setPaddle(paddle pp)
 {
  p = pp;
 }

 void setGraphics(Graphics gr)
 {
  g = gr;
 }
 


 void move()
 {
  switch (dir_x)
  {
  case LEFT:
     {
      x_pos -= xinc;
      if (x_pos < LEFT_WALL)
      {
       x_pos = LEFT_WALL;
       dir_x = RIGHT;
      }
     }
     break;
  case RIGHT:
   {
    //paddle-ball intersection goes here
    x_pos += xinc;
    if (x_pos > RIGHT_WALL)
      {
     if ((y_pos > p.getBottom()) || (y_pos < p.getTop()))
     {
      //die
      owner.showStatus("You missed the ball!");
      x_pos = RIGHT_WALL;
      dir_x = LEFT;

     }
     else
     {
       x_pos = RIGHT_WALL;
       dir_x = LEFT;
       owner.showStatus("You hit it! Good job!");

 

Read Full Discussion Thread for this article