Docstoc

duck

Document Sample
duck Powered By Docstoc
					import java.applet.*;
import java.awt.*;
import java.awt.image.*;

public class test extends Applet implements Runnable{
           Image       offImage,
                       BackGround,
                       BirdImgOne,
                       BirdImgTwo,
                       Shot,
                       MainLogo,
                       WaitState,
                       IntroText;
           Graphics    offGrfx;
           Thread            animate;
           MediaTracker      tracker;
           Sprite            birdOneSprite,
                       birdTwoSprite;
           SpriteVector      sv;
           boolean           birdOneIsDead = false,
                       birdTwoIsDead = false,
                       initialized = false,
                       EndOfGame = false;

           int        delay = 50,
                      x = 550,
                      y = 0,
                      pause = 0,
                      Score = 0, //Sets the Score eq to Zero
                      Lives = 3, //Gives user three lives in beginning
                      Shots = 3, //Allows user to have 3 shots in
begining
                      Round = 1; //Starts user at Round 1


           public void init(){
                 //Load and track the images
                 tracker = new MediaTracker(this);

                 BackGround = getImage(getDocumentBase(),
"pix/BackGround.jpg");
                 BirdImgOne = getImage(getDocumentBase(),
"pix/canadago.gif");
                 BirdImgTwo = getImage(getDocumentBase(),
"pix/canadago.gif");
                 Shot = getImage(getDocumentBase(), "pix/canadago.gif");
                 MainLogo = getImage(getDocumentBase(),
"pix/CrazyDuck.jpg");
                 IntroText =
getImage(getDocumentBase(),"pix/Presenting.gif");
                 WaitState =
getImage(getDocumentBase(),"pix/Loading.gif");

                 tracker.addImage(BackGround, 0);
                  tracker.addImage(BirdImgOne, 0);
                  tracker.addImage(BirdImgTwo, 0);
                  tracker.addImage(Shot, 0);
                  tracker.addImage(MainLogo, 0);
                  tracker.addImage(IntroText, 0);
                  tracker.addImage(WaitState, 0);

                  birdOneSprite = new Sprite(this, BirdImgOne, new
Point(500,270),
                             new Point(-10,-5), 1);
                  birdTwoSprite = new Sprite(this, BirdImgTwo, new
Point(450,200),
                             new Point(-10,-5), 1);
                  try{
                              tracker.waitForID(0);
                  }
                  catch(InterruptedException e){
                        return;
                  }

                  //Create and add the sprites
                  sv = new SpriteVector(this, BackGround);
                  sv.add(birdOneSprite);
                  sv.add(birdTwoSprite);

           }

           public void start(){
                 if(animate == null){
                       animate = new Thread(this);
                       animate.start();
                 }
           }

           public void stop(){
                 if(animate != null){
                       animate.stop();
                       animate = null;
                 }
           }

           public void run(){

                  sv.setBackground(BackGround);
                  //update everything
                  long t = System.currentTimeMillis();
                  while(Thread.currentThread() == animate){
                        sv.update();
                        if((birdOneSprite.getPositionRect().x >=   0)   &&
                              (birdOneSprite.getPositionRect().y   >=   0) ||
                              (birdTwoSprite.getPositionRect().x   >=   0) &&
                              (birdTwoSprite.getPositionRect().y   >=   0)){
                              repaint();
                        }
                       try{
                             t += delay;
                             Thread.sleep(Math.max(0, t -
System.currentTimeMillis()));
                       }
                       catch(InterruptedException e){
                             break;
                       }
                 }
           }

           public void update(Graphics g){
                 if(initialized == true){

                       //Create the offscreen graphics context
                       Dimension dim = size();
                       if(offGrfx == null){
                             offImage = createImage(dim.width,
dim.height);
                              offGrfx = offImage.getGraphics();
                       }

                       //Draw the sprites
                       sv.draw(offGrfx);

                       //Draw the image onto the screen
                       g.drawImage(offImage, 0, 0, null);
                 }
           }

           public void paint(Graphics g){
                 if(initialized == false)
                       Intro(g);
                 if((tracker.statusID(0, true) & MediaTracker.ERRORED) !=
0){
                       //Draw the error rectangle
                       g.setColor(Color.red);
                       g.fillRect(0, 0, size().width, size().height);
                       return;
                 }
                 if((tracker.statusID(0, true) & MediaTracker.COMPLETE)
!= 0){
                       //Draw the offscreen image
                       g.drawImage(offImage, 0, 0, this);
                 }
           }

      public boolean mouseDown(Event evt, int x, int y){
            Sprite killZone = new Sprite(this, Shot, new Point(x, y),
                             new Point(-5,-1), 1);
            if(birdOneSprite.testCollision(killZone)){
                  System.out.println("mouseDown");
                  return true;
            }
          else if(birdTwoSprite.testCollision(killZone)){
                System.out.println("mouseDown");
                return true;
          }
          return true;

     }

     public void Intro(Graphics g){
           initialized = true;
           while(x >= 0)
                 {
                 g.drawImage(MainLogo, x, y, this);
                 while(pause != 1000)
                       pause++;
                 x -= 5;
                 }
           //---------------Slide In Text-------------------------------
--
          x=550;
          while(x >= 250)
                {
                g.drawImage(IntroText, x, y, this);
                while(pause != 1000)
                      pause++;
                x -= 5;
                }
          //---------------Let them know pix are loading---------------
--
                g.drawImage(WaitState, 370, 130, this);
          Score(g);
          Lives(g);
          Shots(g);
          Round(g);
     }

     public void Score(Graphics g){

          g.setColor(new Color(255, 255, 255));
          g.setFont(new Font("Helvetica", Font.BOLD, 20));
          g.drawString(Integer.toString(Score), 572,144);
          Score+=10;
     }

     public void Lives(Graphics g){

          //g.setFont(new Font("Helvetica", Font.BOLD, 20));
          g.drawString(Integer.toString(Lives), 582,199);
          Lives--;
          if (Lives == 0) {
                EndOfGame = true;
          }
     }
     public void Shots(Graphics g){

           //g.setFont(new Font("Helvetica", Font.BOLD, 20));
           g.drawString(Integer.toString(Shots), 582,253);
           Shots--;
     }

     public void Round(Graphics g){

           //g.setFont(new Font("Helvetica", Font.BOLD, 20));
           g.drawString(Integer.toString(Round), 582,306);
           Round++;
           if (Round == 11) {
                 EndOfGame = true;
           }

     }


} //end test class

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:3
posted:6/26/2012
language:Latin
pages:5