import chess.Chess;
import chess.RunnableApplet;
import chess.Screen;
import chess.Sprite;
import java.applet.Applet;
import java.awt.Dimension;
import javax.swing.JFrame;

public class DuckGame extends RunnableApplet {
    
    Screen myScreen;
    
    public void init() {
        myScreen= Chess.newScreen(this);
        
    }
    
    public void run() {
        
        int score= 0;
        
        boolean gameOver= false;
        while ( !gameOver ) {
            int y= Chess.getRandom(400);
            int x=1000;
            int spinTimer=0;
            
            int duckNumber= Chess.getRandom(5) + 1;
            Sprite duck= myScreen.newSprite("e"+duckNumber+".gif",x,y);
            
            while ( x>0 ) {
                x= x-5;
                duck.setPosition( x, y );
                if ( myScreen.mouseWasClicked() ) {
                    Sprite hit= myScreen.getSpriteAt( myScreen.getMouseX(), myScreen.getMouseY() );
                    if ( hit==duck ) {
                        spinTimer= 20;
                    }
                }
                if ( spinTimer>0 ) {
                    duck.setOrientation( duck.getOrientation()+15 );
                    spinTimer= spinTimer-1;
                    if (spinTimer==0 ) {
                        myScreen.removeSprite(duck);
                        score= score+1;
                        myScreen.report("Score: "+score);
                        break;
                    }
                }
                Chess.waitForNextFrame(60);
            } // while(x>0)
            myScreen.removeSprite(duck);
        } // while(!gameOver)
    }
    
    /**
     *  For testing within NetBeans, we define a main method.  This is
     *  ignored when a web page uses the applet.  Here is the code you should
     *  have in your web page:
     *    <applet archive="CHESS.jar"
     *            code="CoinDrag" width="800" height="800" >
     *    </applet>
     *  The file CHESS.jar is found in the folder "dist", and should be
     *  put next to the html file.
     */
    public static void main( String[] args ) {
        JFrame frame= new JFrame("DuckGame");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        Applet applet= new DuckGame();
        Dimension appletDimension= new Dimension(800,800);
        applet.setMinimumSize( appletDimension );
        applet.setPreferredSize( appletDimension );
        frame.setContentPane(applet);
        frame.pack();
        frame.setVisible(true);
        applet.init();
        applet.start();
    } //main
}
