Josephs Final Project

<< Ian's Final Project | OldProjectsTrailIndex | Matthew's Final Project >>


public class Ball{
	private int xPos,yPos;
	public int dx = 5, dy = -5;

	public Ball(){
		setPos(250, 140);
	}

	public void setPos(int x, int y){
		this.xPos = x;
		this.yPos = y;
	}

	public int getX(){
		return xPos;
	}

	public int getY(){
		return yPos;
	}
	public void move(){
		setPos(this.getX() + dx, this.getY() + dy);
	}	
	public void reset(){
		setPos(250,140);
		dx = 5;
		dy = -5;
	}
}

	public class PaddleLeft{
		private int yPos = 0;
		final int XPOS = 30;

		public PaddleLeft(){
			setPos(120);
		}

		public void setPos(int pos){
			this.yPos = pos;
			if(yPos > 230){
				setPos(230);
			}
			else if(yPos < 0){
				setPos(0);
			}
		}

		public int getPos(){
			return yPos;
		}
	}

public class PaddleRight{

	private int yPos = 0, score;
	final int XPOS = 460;

	public PaddleRight(int ballPos){
		setPos(ballPos);
		setScore(0);
	}

	public void setPos(int pos){
		this.yPos = pos;
		if(yPos > 230){
			setPos(230);
		}
		else if(yPos < 0){
			setPos(0);
		}
	}

	public int getPos(){
		return yPos;
	}
	public void setScore(int score){
		this.score = score;
	}

	public int getScore(){
		return this.score;
	}

}


import java.applet.*; 
import java.awt.event.*; 
import java.awt.*;
import javax.swing.Timer;


public class pongMain extends Applet implements MouseMotionListener, ActionListener 
{ 
     Ball ball;
	 PaddleLeft pLeft; 
	 PaddleRight pRight;
	 Font newFont = new Font("sansserif", Font.BOLD, 20);
	 Graphics bufferGraphics; 
     Image offscreen; 
     final int WIDTH = 500, HEIGHT = 300; 
     long currentTime;

     public void init()  
     { 
          setSize(500, 300);
          ball = new Ball();
    	  pLeft = new PaddleLeft();
    	  pRight = new PaddleRight(ball.getY() - 35);

    	  addMouseMotionListener(this);
    	  setBackground(Color.green); 
          offscreen = createImage(WIDTH,HEIGHT); 
          bufferGraphics = offscreen.getGraphics(); 
     }

     public void start(){
    	 currentTime = System.currentTimeMillis();


    	 Timer time = new Timer(15, this);     

    	 time.start(); 
    	 while(pRight.getScore() < 10){

         }
    	 time.stop();
    	 currentTime = System.currentTimeMillis() - currentTime;
    	 repaint();
     }

     public void stop(){

     }

      public void paint(Graphics g)  
     { 
    	  bufferGraphics.clearRect(0,0,WIDTH,HEIGHT); 

          bufferGraphics.setColor(Color.blue);

          bufferGraphics.fillRect(pLeft.XPOS,pLeft.getPos(),10,70);
          bufferGraphics.fillRect(pRight.XPOS, pRight.getPos(), 10, 70);

          bufferGraphics.setColor(Color.white);
          bufferGraphics.setFont(newFont);
          bufferGraphics.drawString("Futile", 150, 15);
          bufferGraphics.drawString(""+ pRight.getScore(),300,15);
          bufferGraphics.fillRect(240,0,20,300);

          if(pRight.getScore() == 10){
        	  bufferGraphics.drawString("You Lasted: " + (currentTime/ 1000) + "sec.", 40, 150);
          }

          bufferGraphics.setColor(Color.red);
          bufferGraphics.fillRect(ball.getX(),ball.getY(),10,10);
          g.drawImage(offscreen,0,0,this);
          Toolkit.getDefaultToolkit().sync();
     }

     public void update(Graphics g)
     { 
          paint(g); 
     } 


     public void mouseMoved(MouseEvent evt)  
     { 
          pLeft.setPos(evt.getY()- 35); 
     } 

     public void mouseDragged(MouseEvent evt)  
     { 
     }

     public void checkCollision(){
    	if(ball.getY() == 0 || ball.getY() == 290){
 			ball.dy = (ball.dy * -1);
 		}

    	if((ball.getX() == 40) && hitPaddle()){
 			ball.dx = (ball.dx * -1);
 		}

 		if(ball.getX() == 460){
 			ball.dx = (ball.dx * -1);
 		}

 		if(ball.getX() == 0){
 			pRight.setScore(pRight.getScore() + 1);
 			ball.reset();
 		}
 	 }

     public boolean hitPaddle(){
    	 boolean didHit = false;
    	 if((pLeft.getPos() - 10) <= ball.getY() && (pLeft.getPos() + 70) > ball.getY()){
    		 didHit = true;
    	 }
    	 return didHit;
     }

	@Override
	public void actionPerformed(ActionEvent arg0) {
		 ball.move();
		 pRight.setPos(ball.getY() - 35);
		 checkCollision();
		 repaint();
	}

 }