Enis And Eriks Hockey Game

<< Justin and Xavier's Tennis Game | OldProjectsTrailIndex | Suren's Animation >>

See a working demonstration here

Fr Chris Hints

Hint: the timer is either active or not. when the puck is on the right, a decision is made "goal" or "no goal"... won't that be a good time to not only set the score, but move the puck back to the left, and reset the timer?

To use the direction keys of the keyboard you need to compare keyCodes:

public void keyPressed(KeyEvent e) 
   { 
      keyCode=e.getKeyCode();
      c=e.getKeyChar();
<<stuff>>

if (c==p2up || keyCode==38) {
          action="player 2 up";
          p2.setY(p2.getY()-4);
        }
      if (c==p2down|| keyCode==40) {
          action="player 2 down";
          p2.setY(p2.getY()+4);
        }

Sounds

buzzer.wav

denied.wav

whatareyoudoing.wav


Hockey

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

/**
 * Class Hockey - 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 Eni Njoku, Erik Larson
 * @version 11 May 2009
 */
public class Hockey extends JApplet implements KeyListener,ActionListener
{

    private int keyCode;
    char c;
    int maxX,maxY;
    private char  p1up, p1down, p2up, p2down, p1hit, puckup, puckdown;
    String action="none";
    Timer timer;
    Shooter p1;
    Goalie p2;
    Puck puck;
    int p1score=0 ;
    int p2score=0;
    AudioClip scored;
    AudioClip blocked;
    public void init()
    {
        this.addKeyListener(this);
        puckup='r';
        puckdown='f';
        p1up='w';
        p1down='s';
        p2up='o';
        p2down='l';
        p1hit=Character.toChars(32)[0];
        maxX=getSize().width;
        maxY=getSize().height;
        p1=new Shooter(Color.RED, 20, 250, maxX, maxY);
        p2=new Goalie(Color.BLUE, maxX-40, 250, maxX, maxY);
        puck=new Puck(40, 250, maxX, maxY);
        timer = new Timer(10, this);
        timer.stop();
        scored = getAudioClip(getDocumentBase(), "buzzer.wav");
        blocked = getAudioClip(getDocumentBase(), "denied.wav");

    }

    public void paint(Graphics g)
    {
        // simple text displayed on applet
        g.setColor(Color.white);
        g.fillRect(0, 0, 500, 500);
        g.setColor(Color.black);
        g.drawString("controls: P1: up=w, down=s    P2: up=up arrowkey, down=down arrowkey", 10, 10);
        g.drawString("puck angle:"+puck.getDeltaY(), 10, 30);
        g.setColor(Color.red);
        g.drawOval(70,80,80,80);
        g.drawOval(310,80,80,80);
        g.drawOval(60,350,80,80);
        g.drawOval(320,350,80,80);
        g.drawOval(357,387,5,5);
        g.drawOval(97,387,5,5);
        g.drawOval(347,117,5,5);
        g.drawOval(107,117,5,5);
        g.drawLine(230,0,230,500);
        g.setColor(Color.blue);
        g.drawOval(180,200,100,100);
        g.drawLine(173,0,173,500);
        g.drawLine(174,0,174,500);
        g.drawLine(175,0,175,500);
        g.drawLine(287,0,287,500);
        g.drawLine(286,0,286,500);
        g.drawLine(285,0,285,500);


        g.drawString("\""+p2score+"\"", 400, 60);
        g.drawString("Goalie score", 375, 50);
        g.setColor(Color.red);
        g.drawString("\""+p1score+"\"", 50, 60);
        g.drawString("Shooter score", 25, 50);
        p1.draw(g);
        puck.draw(g);
        p2.draw(g);

    }


   /**
    * KeyListener Interface's Implementation
    */
   public void keyTyped(KeyEvent e) {}
   public void keyPressed(KeyEvent e) 
   { 
      keyCode=e.getKeyCode();
      c=e.getKeyChar();
      action="none";
      if (c==p1hit) {
          action="hit puck";
          timer.start();
        }
        if (c==puckup) {
          action="angle up";
          puck.increase();
        }
        if (c==puckdown) {
          action="angle down";
          puck.decrease();
        }
      if (c==p1up) {
          action="player 1 up";
          p1.setY(p1.getY()-20);
          puck.setY(puck.getY()-20);
        }
      if (c==p1down) {
          action="player 1 down";
          p1.setY(p1.getY()+20);
          puck.setY(puck.getY()+20);
        }
      if (c==p2up || keyCode==38) {
          action="player 2 up";
          p2.setY(p2.getY()-40);
        }
      if (c==p2down || keyCode==40) {
          action="player 2 down";
          p2.setY(p2.getY()+40);
        }
      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)
    {
        Graphics g;
        Object source = e.getSource();
        if (source == timer) 
        // the passage of time, so we move our puck
        // if it was hit
        {
            if (timer.isRunning()){
                puck.move();
                if (puck.getX()+30>maxX)
                {
                    timer.stop();
                    if (puck.getY()>=p2.getY()-60 &&
                        puck.getY()<=p2.getY()+10)
                    {
                        action="goalie wins";
                        p2score++;
                        p1.setY(250);
                        p2.setY(250);
                        puck.setX(p1.getX()+20);
                        if(blocked !=null) blocked.play();
                        if(puck.getY()<p1.getY())
                        puck.setY(puck.getY()+(p1.getY()-puck.getY()));
                        if(puck.getY()>p1.getY())
                        puck.setY(puck.getY()-(puck.getY()-p1.getY()));
                        puck.setDeltaY(0);
                    }else{
                        action="goal!";
                        p1score++;
                        p1.setY(250);
                        p2.setY(250);
                        if(scored !=null) scored.play();
                        puck.setX(p1.getX()+20);
                        if(puck.getY()<p1.getY())
                        puck.setY(puck.getY()+(p1.getY()-puck.getY()));
                        if(puck.getY()>p1.getY())
                        puck.setY(puck.getY()-(puck.getY()-p1.getY()));
                        puck.setDeltaY(0);

                    }
                }
            }
        }

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

Player

import java.awt.*;
/**
 *  class Player 
 * 
 * @author Eni Njoku
 * @version 11 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);
    }
}

Goalie

import java.awt.*;
/**
 *  class Goalie 
 * 
 * @author Erik Larson
 * @version 11 May 2009
 */
public class Goalie extends Player
{
    public Goalie(Color color, int x, int y, int maximumX, int maximumY)
    {
        super(color,x,y,maximumX,maximumY);
    }
    public void draw(Graphics g)
    {
      g.setColor(c);
      g.fillRect(xLoc-20,yLoc-20,30,50);
      g.fillOval(xLoc-20,yLoc-45,30,30);
      g.setColor(Color.BLACK);
      g.drawLine(xLoc-10,yLoc-10,xLoc-40,yLoc+20);
      g.fillRect(xLoc-60,yLoc+20,20,10);
    }



}

Shooter

import java.awt.*;
/**
 *  class Shooter
 * 
 * @author Eni Njoku
 * @version 11 May 2009
 */
public class Shooter extends Player
{
    public Shooter(Color color, int x, int y, int maximumX, int maximumY)
    {
        super(color,x,y,maximumX,maximumY);
    }
    public void draw(Graphics g)
    {
      g.setColor(c);
      g.fillRect(xLoc-20,yLoc-20,30,50);
      g.fillOval(xLoc-20,yLoc-45,30,30);
      g.setColor(Color.BLACK);
      g.drawLine(xLoc-10,yLoc-10,xLoc+20,yLoc+20);
      g.fillRect(xLoc+20,yLoc+20,20,10);
    }



}

Puck

import java.awt.*;
/**
 * Write a description of class Puck here.
 * 
 * @author Erik Larson
 * @version 11 May 2009
 */
public class Puck extends Player
{
   private int deltaY;
   public Puck (int x, int y, int maximumX, int maximumY)
    {
        super(Color.GREEN, x, y, maximumX, maximumY);
        deltaY=0;
    }
    public void setDeltaY(int u)
    {
        deltaY=u;
    }
    public int getDeltaY()
    {
        return deltaY;
    }
    public void increase()
    {
        deltaY--;
    }
    public void decrease()
    {
        deltaY++;
    }
    public void move()
	{
	    setX(xLoc+10);
	    setY(yLoc+deltaY);
	}
    public void draw(Graphics g)
    {
        g.setColor(c);
        g.fillOval(xLoc+2, yLoc+17, 16,12);
        g.fillRect(xLoc+3, yLoc+20, 15,8);
        g.fillOval(xLoc+2, yLoc+19, 16,12);
    }
}