Justin And Xaviers Tennis Game

<< Michael's Checkers Game | OldProjectsTrailIndex | Eni's and Erik's Hockey Game >>

Tennis


Notes from Fr Chris:

I love the green! The look is wonderful.

Still needs to update the score when the ball goes past a player, and reset for the next volley by moving the ball to the correct player's side

It's coming along very nicely!

Fr Chris' Hints for the Tennis Class

The passing of time is handled in the ActionListener.

public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source == timer) 
        // the passage of time, so we move our ball
        // and if it was hit or missed by p1 or p2
        {
            if (timer.isRunning()){
                ball.move();
                if (ball.getX()+50>maxX) // ball on the right side of the court
                {

                    if (ball.getY()>=p2.getY() &&
                        ball.getY()<=p2.getY()+40)// see if p2 is near ball
                    {
                        //p2 hits the ball
                        ball.changeDirection();
                    }else{// p2 misses
                        //score a point for player one";
                        timer.stop();//stop the ball
                        //upate the scoring and move ball to be
                        //served by player 1

                    }
                }
                if (ball.getX()-30<0) //ball on the left side of court
                {
                   if (ball.getY()>=p1.getY() &&
                        ball.getY()<=p1.getY()+40)  
                   {
                       // p1 is nearby
                       ball.changeDirection();
                    } else { //p1 misses the ball
                        //score a point for player two

                        timer.stop();//stop the ball
                        //upate the scoring and move ball to be
                        //served by player 2
                    }
                }
            }
        }

TENNIS

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

/**

 * Class Tennis - a Small Demonstration of the Key Listener combined with 
 * an instance of a Timer object that generates ActionListener events.
 * 
 * The user input is generated from key presses,
 * and once the puck it hit, it is animated by
 * starting the timer which causes Action events
 * at regular time intervals.  The ActionListener
 * can respond to them by drawing the puck in the new
 * location.
 *
 * @author Xavier Soto & Justin Velasco
 * @version May 14-19 2009
 */

public class Tennis extends JApplet implements KeyListener,ActionListener {

    private int keyCode;
    char c;
    int maxX,maxY;
    private char  p1up, p1down, p2up, p2down, hit;
    String action="none";
    Timer timer;
    Image virtualMem;
    Graphics gBuffer;
    Player p1,p2;
    int p1score=0;
    int p2score=0;
    Ball ball;

    int appletWidth;
    int appletHeight;
    public void  init()
    {
        this.addKeyListener(this);
        p1up='a';
        p1down='z';
        p2up='\'';
        p2down=Character.toChars(47)[0];
        hit=Character.toChars(32)[0];
        maxX=getSize().width;
        maxY=getSize().height;
        p1=new Player(Color.white, 20, 150, maxX, maxY);
        p2=new Player(Color.white, maxX-40, 150, maxX, maxY);
        ball=new Ball(40, 150, maxX, maxY);
        appletWidth = getWidth();
        appletHeight = getHeight(); 
        virtualMem = createImage(appletWidth,appletHeight);
        gBuffer = virtualMem.getGraphics();
        gBuffer = virtualMem.getGraphics();
        gBuffer.setColor(Color.white);
        gBuffer.fillRect(0,0,appletWidth,appletHeight);
        timer = new Timer(10, this);
        timer.stop();
    }

    public void paint(Graphics g)
    {
        // simple text displayed on applet
        Color green=new Color(0,75,0);
        g.setColor(green);
        g.fillRect(0, 0, 500, 500);
        g.setColor(Color.black);
        g.drawString("Tennis - KeyListener / ActionListener Demo - Press a Key", 20, 20);
        g.setColor(Color.blue);
        g.setColor(Color.black);
        g.fillRect(245,0,6,700);
        g.fillOval(238,1,20,20);
        g.fillOval(238,480,20,20);
        g.setColor(Color.white);
        g.fillRect(0,60,700,4);
        g.fillRect(0,440,700,4);
        g.fillRect(492,250,8,4);
        g.fillRect(0,250,7,4);
        g.fillRect(371,60,4,380);
        g.fillRect(125,60,4,380);
        g.fillRect(125,250,246,4);
        g.setColor(Color.white);
        g.drawString("\""+p1score+"\"", 90, 480);
        g.drawString("Player One", 20, 480);
        g.drawString("\""+p2score+"\"", 430, 480);
        g.drawString("Player Two", 360, 480);

        g.setFont(new Font("Arial", Font.PLAIN, 32));
        g.setColor(Color.white);
        g.drawString("action is "+action, 20, 60);
        g.setFont(new Font("Arial", Font.PLAIN, 32));
        g.setColor(Color.white);
        g.drawString("\""+c+"\"", 50, 100);
        p1.draw(g);
        p2.draw(g);
        ball.draw(g);

        ball.draw(gBuffer);

    }


   /**
    * KeyListener Interface's Implementation
    */
   public void keyTyped(KeyEvent e) {}
   public void keyPressed(KeyEvent e) 
   { 
      keyCode=e.getKeyCode();
      c=e.getKeyChar();
      action="none";
      if (c==hit) {
          action="hit ball";
          timer.start();
        }
      if (c==p1up) {
          action="player 1 up";
          p1.setY(p1.getY()-4);

        }
      if (c==p1down) {
          action="player 1 down";
          p1.setY(p1.getY()+4);

        }
      if (c==p2up) {
          action="player 2 up";
          p2.setY(p2.getY()-4);
        }
      if (c==p2down) {
          action="player 2 down";
          p2.setY(p2.getY()+4);
        }
      repaint();
    }
   public void keyReleased(KeyEvent e) {}
 /**
 * ActionListener is an interface and
 * requires the actionPerformed() method
 * to be defined..in this case we
 * look for a timer event
 */
    public void actionPerformed(ActionEvent e)
    {
        boolean p1Hit;
        boolean p2Hit;
        Object source = e.getSource();
        if (source == timer) 
        // the passage of time, so we move our puck
        // if it was hit
        {
            if (timer.isRunning()){


                ball.move();


                if (ball.getY()<=(p2.getY()+40) &&
                    ball.getY()>=p2.getY() &&
                    ball.getX()<=p2.getX()+maxX &&
                    ball.getX()>=p2.getX())
                    {                
                        ball.changeDirection();
                        p2Hit=true;


                    }else {p2Hit=false;}
                 if (p2Hit=true)
                 {
                    if (ball.getY()<=(p1.getY()+40) &&
                     ball.getY()>=p1.getY() &&
                     ball.getX()<=p1.getX()+10 &&
                     ball.getX()>=p1.getX())
                     {
                         ball.changeDirection();
                         p1Hit=true;


                     }else {p1Hit=false;}
                    }



                if (ball.getX()+30>maxX)
                {
                    timer.stop();
                    p1score++;
                }

            }
        }

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

} 



PLAYER

import java.awt.*;
/**
 *  class Player 
 * 
 * @author Chris Thiel
 * @version 9 May 2009
 */
public class Player
{

    protected int xLoc;
    protected int yLoc;
    protected int maxX;
    protected int maxY;
    protected Color c;
    /**
     * Constructor for objects of class Player
     */
    public Player (Color color, int x, int y, int maximumX, int maximumY)
    {

        xLoc=x;
        yLoc=y;
        maxX=maximumX;
        maxY=maximumY;
        c=color;
    }

    public int getX()
    {
        return xLoc;
    }
    public int getY()
    {
        return yLoc;
    }
    public void setX(int x)
    {
        if (x>=0 && x<=maxX) xLoc=x;
    }
    public void setY(int y)
    {
        if (y>=0 && y<=maxY) yLoc=y;
    }
    public void draw(Graphics g)
    {
        g.setColor(c);
        g.fillRect(xLoc, yLoc, 10,40);
    }
}

BALL

import java.awt.*; /**

 * Write a description of class Puck here.
 * 
 * @author Chris Thiel, OFMCap
 * @version 9 May 2009
 */

public class Ball extends Player {


    private int direction=1;
   public Ball (int x, int y, int maximumX, int maximumY)
    {
        super(Color.YELLOW, x, y, maximumX, maximumY);
    }



    public void move()
    {
        setX(xLoc+direction*10);

    }
    public void changeDirection()
    {
        direction*=-1;

    }


    public void draw(Graphics g)
    {
        g.setColor(c);
        g.fillOval(xLoc, yLoc, 10,10);
    }





}