//James Riley
//Final Project
//Due date: 4/29/98
//A duck hunting game

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

public class DuckHunt extends Applet implements  MouseListener, MouseMotionListener{

        private AudioClip duckso, rifleso;
        private Image duck[], dduck[], rduck[];
        private int duckImages = 4,deadImages= 2, rduckImages=4;
        private int currentImage = 0;
        private int sleepTime[]={500, 400, 300, 200, 100, 50};
        private int locx=100;
        private int locy=100;
        private int x=0, y=0, sign=0, j=0;
        private int dead=-1, side=0, time=0;
        private int cx=-50, cy=-50;
        private int hit=0, missed=0, sound=0;


        public void init()
                {
                

                duckso= getAudioClip(getDocumentBase(), "duck.au");
                rifleso= getAudioClip(getDocumentBase(), "rifle_shot.au");
                duck = new Image[duckImages];

                duck[0]=getImage(getDocumentBase(), "duck1.gif");
                duck[1]=getImage(getDocumentBase(), "duck3.gif");
                duck[2]=getImage(getDocumentBase(), "duck2.gif");
                duck[3]=getImage(getDocumentBase(), "duck3.gif");

                rduck = new Image[rduckImages];
                rduck[0]=getImage(getDocumentBase(), "rduck1.gif");
                rduck[1]=getImage(getDocumentBase(), "rduck3.gif");
                rduck[2]=getImage(getDocumentBase(), "rduck2.gif");
                rduck[3]=getImage(getDocumentBase(), "rduck3.gif");
                
                dduck = new Image[deadImages];
                for(int i=0; i4)
                                        sound=0;
                                
                                if(sign==0)
                                        locy = locy  + (int)(Math.random() *20);
                                if(sign==1)
                                        locy = locy  - (int)(Math.random() *20);
                                if(locx ==-100)
                                        {
                                        side = (int)(Math.random() *2);
                                        locy  = 100;
                                        if (side ==0)
                                        locx =-100;
                                        else if(side ==1)
                                        locx =500;
                                        time = (int)(Math.random() *6);
                                        }

                                repaint();
                                }
                        if(side ==0)
                                {
                                if(sound==4)
                                        duckso.play();
                                g.drawImage(rduck[currentImage], locx , locy ,this);
                                currentImage = (currentImage + 1) % rduckImages;
                                try {
                                Thread.sleep(sleepTime[time ]);
                                }
                                catch (InterruptedException e) {
                                showStatus(e.toString());
                                }
                                locx  = locx  +10;
                                sound++;
                                if(sound>4)
                                        sound=0;

                                sign = (int)(Math.random() *2);
                                if(sign==0)
                                        locy = locy  + (int)(Math.random() *20);
                                if(sign==1)
                                        locy = locy  - (int)(Math.random() *20);
                                if(locx ==500)
                                        {
                                        side = (int)(Math.random() *2);
                                        locy  = 100;
                                        if (side ==0)
                                        locx =-100;
                                        else if(side ==1)
                                        locx =500;
                                        time =(int)(Math.random() *6);
                                        }
                                if(locy >500)
                                        locy =locy  -20;
                                if(locy <0)
                                        locy =locy  + 10;
                                        
                                repaint();
                                }
                                }

               if(dead==1)
                        {

                        g.drawImage(dduck[currentImage], locx , locy , this);
                        currentImage = (currentImage + 1) % deadImages;
                        
                        try {
                        Thread.sleep(100);
                        }
                        catch (InterruptedException e) {
                        showStatus(e.toString());
                        }
                        locy =locy  + 20;
                        if(locy >=500)
                                {
                                dead=0;
                                locy =50 +(int)(Math.random()*300);
                                side=(int)(Math.random() * 2);
                                if (side==0)
                                        locx=-100;
                                else if(side==1)
                                        locx=500;
                                time= (int)(Math.random() *6);
                                }
                        repaint();
                        }
                else if (dead==2)
                {
                setBackground(Color.white);
                }
        }
        public void hitormiss(int x, int y)
                {
                if ((locx < x) && (x < locx +100) && (locy < y)&& (y < locy + 50))
                        {
                        dead=1;
                        currentImage=0;
                        if(missed+hit<20)
                                hit=hit+1;
                        if(hit + missed>=20)
                                {
                                dead=2;
                                if(hit>missed)
                                        showStatus("Game Over!  You hit: " + hit + " and missed: " + missed + " Congratulations! You hit more than you missed!");
                                else if(hit==missed)
                                        showStatus("Game Over!  You hit: " + hit + " and missed: " + missed + " Congratulations! You hit the same amount you missed!");
                                else if(missed>hit)
                                        showStatus("Game Over!  You hit: " + hit + " and missed: " + missed + " Congratulations! You suck!");
                                repaint();
                                }
                        else
                                showStatus("Hit!   "+ "Hit:" + hit + " Missed:" + missed);

                        repaint();
                        }
                else
                        {
                        if(missed+hit<20)
                                missed=missed+1;
                        if(hit + missed>=20)
                                {
                                dead=2;
                                if(hit>missed)
                                        showStatus("Game Over!  You hit: " + hit + " and missed: " + missed + " Congratulations! You hit more than you missed!");
                                else if(hit==missed)
                                        showStatus("Game Over!  You hit: " + hit + " and missed: " + missed + " Congratulations! You hit the same amount you missed!");
                                else if(missed>hit)
                                        showStatus("Game Over!  You hit: " + hit + " and missed: " + missed + " Congratulations! You suck!");
                                        
                                repaint();
                                }
                        else
                                showStatus("Missed!   " + "Hit:" + hit + " Missed:" + missed);
                        repaint();
                        }
                }

        public void mouseClicked(MouseEvent e)
                {
                if(dead==-1){
                showStatus("How to Play: Hit the ducks with the gun! You only have 20 bullets!  You don't win anything!");
                dead=0;
                repaint();
                }
                else {x=e.getX();
                y=e.getY();
                hitormiss(x, y);
                rifleso.play();
                }
                }
        public void mousePressed(MouseEvent e)
                {
                }
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e)
                {
                cx=e.getX();
                cy=e.getY();
                }
        public void mouseExited(MouseEvent e)
                {
                cx=-50;
                cy=-50;
                }
        public void mouseDragged(MouseEvent e){}
        public void mouseMoved(MouseEvent e)
                {
                

                cx=e.getX();
                cy=e.getY();
                }
   }