applet射击小游戏开源

类别:Java 点击:0 评论:0 推荐:

首先请准备好图像素材,例如把以下图像分别命名为bird.gif shark.gif 和weed.gif放到源文件同一目录下,
                             
bird.gif            shark.gif       weed.gif
大家也可以自己随便指定图像,看代码就知道了
空格开始或暂停 方向键控制方向 f和g为开火两种不同的子弹 z和x为增加横向和纵向的移动速度 player的子弹可以抵消敌人的子弹 更多内容请自己添加修改 
start:
Watergame.java

//小李开源之javagame by difeijin 2004 10 8
//如有疑问请联系我
//一个横版射击游戏,需要素材为bird.gif, shark.gif, weed.gif
//bird.gif 为player的图案 shark.gif为enemy的图案 weed为背景草的图案 游戏可以很方便的扩充
//例如添加敌人增强火力增加吃奖励提升战斗能力等
import java.awt.*;
import java.applet.Applet;
import java.awt.*;
import java.applet.*;
import java.lang.*;
import java.util.*;
import java.net.*;

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
class shark_fire implements Runnable    //同一高度有三颗子弹
{
    int  startx;
    int  starty;
    int  xoffset;
    int  firespeed;
    int  firesize;
    int  rot;
    int  countofexplode;
    Thread myThread;
    static int count = 0;
    boolean state, explode;
    public shark_fire()
    {
        state = false;
        count++;
        countofexplode = 0;
        xoffset = 0;

        myThread = null;
    }
    public shark_fire(int x, int y, int z, int size)
    {
        count++;
        startx = x;
        starty = y;
        firespeed = z;
        firesize = size;
        myThread = null;
        explode = false;
        state = true;
        countofexplode = 0;
        xoffset = 0;
        this.start();
    }
    public void set(int x, int y, int z, int size)
    {                           //count++;
        startx = x;
        starty = y;
        firespeed = z;
        firesize = size;
        explode = false;
        state = true;
        countofexplode = 0;
        xoffset = 0;
        // myThread=null;
        this.start();
    }
    public void setexplode()
    {                           //count++;
        explode = true;
        countofexplode = 8;
        //System.out.println("explode is true now ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
        //this.paint
    }

    public void start()
    {                           //state=true;

        if (myThread == null)
        {
            System.out.println("sharkfire_shot Starting!");
            myThread = new Thread(this);
            myThread.start();

        }
        else
        {
            myThread.stop();
            myThread = new Thread(this);
            myThread.start();
        }
    }

    public void stop()
    {
        state = false;
        if (myThread != null)
        {
            myThread.stop();
            myThread = null;
            System.out.println(" sharkfire_shot thread stopped   ");
        }
    }

    public void destroy()
    {
        System.gc();
    }

    public void run()
    {
        //Do stuff
        //System.out.println("run");
        myThread.setPriority(Thread.MIN_PRIORITY);

        while (myThread != null)
        {
            try
            {
                Thread.sleep(100);
            }
            catch(InterruptedException e)
            {
            }
            //           repaint();

            if (starty < 0)     //from buttom to top
            {
                System.out.println("sharkfire_shot stop" + starty + "cont is " + count);

                myThread.stop();
            }

        }
        myThread = null;
    }

    public void move()          //tells shotH to move all the shots
    {
        starty -= firespeed;
        xoffset += firespeed;   //the fire will be move to the position with x cordinate startx startx+xoffset and startx-xoffset

        //shark_fire
        //      System.out.println("moving   ");
    }

    public void exploding(Graphics g)   //tells shotH to move all the shots
    {                           //starty+=firespeed;
        System.out.println(" sharkfire_shot exploding  ");

        g.fillOval(startx, starty, (int)((firesize + (10 - countofexplode) * 3) * Math.random()),
                   (int)((firesize + (10 - countofexplode) * 3) * Math.random()));

        g.fillRoundRect(startx, starty, (10 - countofexplode) * 3, (10 - countofexplode) * 3,
                        (int)((firesize + (10 - countofexplode) * 3) * Math.random()),
                        (int)((firesize + (10 - countofexplode) * 3) * Math.random()));
        g.fillOval(startx - xoffset, starty, (int)((firesize + (10 - countofexplode) * 3) * Math.random()),
                   (int)((firesize + (10 - countofexplode) * 3) * Math.random()));

        g.fillRoundRect(startx + xoffset, starty, (10 - countofexplode) * 3, (10 - countofexplode) * 3,
                        (int)((firesize + (10 - countofexplode) * 3) * Math.random()),
                        (int)((firesize + (10 - countofexplode) * 3) * Math.random()));
        g.fillOval(startx - xoffset, starty, (int)((firesize + (10 - countofexplode) * 3) * Math.random()),
                   (int)((firesize + (10 - countofexplode) * 3) * Math.random()));

        g.fillRoundRect(startx + xoffset, starty, (10 - countofexplode) * 3, (10 - countofexplode) * 3,
                        (int)((firesize + (10 - countofexplode) * 3) * Math.random()),
                        (int)((firesize + (10 - countofexplode) * 3) * Math.random()));
        Color temp = g.getColor();
        switch (countofexplode % 8)
        {
           case 0:
           case 1:
           case 2:
               g.setColor(new Color(255, 255, 206));
               break;
           case 3:
               g.setColor(new Color(49, 48, 49));
               break;
           case 4:
           case 5:
               g.setColor(new Color(255, 251, 247));
               break;
           case 6:
               g.setColor(new Color(255, 69, 255));
               break;
           case 7:
               //case 8:
               //default:g.setColor(new Color(255,243,231));
               break;
        }
        if (countofexplode % 3 == 0)
        {
            g.fillOval(startx, starty, (int)((firesize + (10 - countofexplode) * 300) * Math.random()),
                       (int)(2 * (firesize) * Math.random()));

        }
        else
        {
            g.setColor(new Color(66, 65, 66));
            for(int k = 0; k <= 12; k += 3)
            {
                g.drawOval(startx - xoffset + k * 20, starty, 10 * k, 16 - k);
                g.drawRect(startx - xoffset + k * 20 - 10, starty + 5, 5 * k, 16 - k);
            }
        }
        g.setColor(temp);

        // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //

    }
    public boolean checkexplode(Graphics g)
    {
        if (explode)
        {
            exploding(g);
            countofexplode--;
            if (countofexplode == 0)
            {
                stop();

                return true;
            }
        }
        return false;
    }

    public void check()         //tells shotH to check the shots' movement
    {
        if (starty < 0)
        {
            stop();
        }

    }
    public void paint(Graphics g)
    {                           //this is the shots paint method... draws a line from its last position to its current position, basically
        rot++;                  //advances color rotation
        if (rot == 16)
        {
            rot = 0;
        }                       //keeps rotation below 17
        Color col1 = new Color(255 - (rot * 8), rot * 8, 127 + rot * 8);        //makes the new color
        if (rot % 2 == 0)
        {
            g.setColor(col1);   //sets the current color to the rotation color
        }

        g.fillOval((int)startx, (int)starty, firesize, firesize);       //draws a line from the current coords to the previous coords
        g.fillOval((int)startx - xoffset, (int)starty, firesize, firesize);     //draws a line from the current coords to the previous coords
        g.fillOval((int)startx + xoffset, (int)starty, firesize, firesize);     //draws a line from the current coords to the previous coords

    }
    //}
};

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
class fire implements Runnable
{
    int  startx;
    int  starty;
    int  firespeed;
    int  firesize;
    int  rot;
    int  countofexplode;
    int  type;
    Thread myThread;
    static int count = 0;
    boolean state, explode, sharkfirehitted;
    public fire()
    {
        state = false;
        count++;
        countofexplode = 0;
        type = 0;

        myThread = null;
    }
    public fire(int x, int y, int speed, int size)
    {
        count++;
        startx = x;
        starty = y;
        firespeed = speed;
        firesize = size;
        myThread = null;
        explode = false;
        sharkfirehitted = false;
        state = true;
        countofexplode = 0;
        type = 0;
        this.start();
    }
    public fire(int x, int y, int speed, int size, int atype)
    {
        set(x, y, speed, size, atype);
    }

    public void set(int x, int y, int speed, int size, int atype)
    {
        startx = x;
        starty = y;
        firespeed = speed;
        firesize = size;
        explode = false;
        sharkfirehitted = false;
        state = true;
        countofexplode = 0;
        type = atype;
        this.start();
    }
    public void set(int x, int y, int speed, int size)
    {                           //count++;
        startx = x;
        starty = y;
        firespeed = speed;
        firesize = size;
        explode = false;
        sharkfirehitted = false;
        state = true;
        countofexplode = 0;
        type = 0;
        // myThread=null;
        this.start();
    }
    public void setexplode()
    {                           //count++;
        explode = true;
        countofexplode = 8;
        //System.out.println("explode is true now ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
        //this.paint
    }
    public void setsharkfirehitted()
    {                           //count++;
        sharkfirehitted = true;
        countofexplode = 4;
        //System.out.println("explode is true now ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
        //this.paint
    }

    public void start()
    {                           //state=true;

        if (myThread == null)
        {
            System.out.println("Starting!");
            myThread = new Thread(this);
            myThread.start();

        }
        else
        {
            myThread.stop();
            myThread = new Thread(this);
            myThread.start();
        }
    }

    public void stop()
    {
        state = false;
        if (myThread != null)
        {
            myThread.stop();
            myThread = null;
            System.out.println("thread stopped   ");
        }
    }

    public void destroy()
    {
        System.gc();
    }

    public void run()
    {
        //Do stuff
        //System.out.println("run");
        myThread.setPriority(Thread.MIN_PRIORITY);

        while (myThread != null)
        {
            try
            {
                Thread.sleep(100);
            }
            catch(InterruptedException e)
            {
            }
            //           repaint();

            if (starty > 200)
            {
                System.out.println("stop" + starty + "cont is " + count);

                myThread.stop();
            }

        }
        myThread = null;
    }

    public void move()          //tells shotH to move all the shots
    {
        starty += firespeed;
        //      System.out.println("moving   ");
    }

    public void exploding(Graphics g)   //tells shotH to move all the shots
    {                           //starty+=firespeed;
        System.out.println("exploding  ");
        //g.fillOval((int)startx, (int)starty, firesize, firesize); //draws a line from the current coords to the previous coords

        // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
        System.out.println(" " + (int)(Math.random() * 250) + " " + (int)(Math.random() * 250) + " " +
                           (int)(Math.random() * 250));
        g.setColor(new Color((int)(Math.random() * 250), (int)(Math.random() * 250), (int)(Math.random() * 250)));
        int  MaxRocketNumber = 60;
        int  arr[] = { -1, 0, 1, 2, -2, -3, -4, 3, 4, 5, -5, 6, -6, 7, -7, 8, -8, 9, -9 };

        g.drawOval(startx, starty, firesize + (10 - countofexplode) * 3, firesize + (10 - countofexplode) * 3);
        g.fillOval(startx, starty, (int)((firesize + (10 - countofexplode) * 3) * Math.random()),
                   (int)((firesize + (10 - countofexplode) * 3) * Math.random()));
        g.fillRoundRect(startx, starty, (10 - countofexplode) * 3, (10 - countofexplode) * 3,
                        (int)((firesize + (10 - countofexplode) * 3) * Math.random()),
                        (int)((firesize + (10 - countofexplode) * 3) * Math.random()));

    }
    public void dispsharkfirehitted(Graphics g)
    {
        g.drawOval(startx, starty, firesize * 6 - countofexplode * 3, firesize * 6 - countofexplode * 6);
    }
    public boolean checkexplode(Graphics g)
    {
        if (explode)
        {
            exploding(g);
            countofexplode--;
            if (countofexplode == 0)
            {
                stop();

                return true;
            }
        }
        return false;
    }
    public boolean checksharkfirehitted(Graphics g)
    {
        if (sharkfirehitted)
        {
            dispsharkfirehitted(g);
            countofexplode--;
            if (countofexplode == 0)
            {
                stop();

                return true;
            }

        }
        return false;
    }

    public void check(int width, int height)    //tells shotH to check the shots' movement
    {
        if (starty > height)
        {
            stop();
        }

    }
    public void paint(Graphics g)
    {                           //this is the shots paint method... draws a line from its last position to its current position, basically
        switch (type)
        {
           case 0:
               rot++;           //advances color rotation
               if (rot == 15)
               {
                   rot = 0;
               }                //keeps rotation below 15
               Color col1 = new Color(255 - (rot * 15), rot * 10, 127 + rot * 8);       //makes the new color
               g.setColor(col1);        //sets the current color to the rotation color
               g.fillOval((int)startx, (int)starty, firesize, firesize);        //draws a line from the current coords to the previous coords
               break;
           case 1:
               //g.setColor(new Color(255,255,156));
               g.fillRect((int)startx, (int)starty, firesize / 3, firesize / 2);        //draws a line from the current coords to the previous coords
               g.setColor(new Color((int)(Math.random() * 250), (int)(Math.random() * 60), (int)(Math.random() * 156)));
               g.draw3DRect((int)startx - firesize / 4, (int)starty, firesize / 2, firesize / 4, false);
               g.setColor(new
                          Color((int)(Math.random() * 156), 200 - (int)(Math.random() * 160),
                                255 - (int)(Math.random() * 160)));
               g.fillOval(startx - (int)(firesize / 3), starty + (int)(firesize / 3), (int)(firesize / 1.5),
                          firesize / 2);
               g.drawLine(startx - firesize / 2, starty + firesize / 2, startx, starty + firesize);
               g.drawLine(startx + firesize / 2, starty + firesize / 2, startx, starty + firesize);
               break;

        }
    }
    //}
};

public class Watergame extends java.applet.Applet implements Runnable
{
    Thread killme = null;
    ImageCanvas board;

    public void init()
    {
        // setLayout() is a java.awt.Container member.  It sets the LayoutManager
        // for the container.  Without it, the ImagePanels would get different
        // relative positions when the viewer is resized.
        setLayout(new BorderLayout());

        // Place the ImageHelp at the top of the applet.
        ImageHelp imHelp = new ImageHelp();
             add("North", imHelp);

        // Place the ImagePanel at the center of the applet.
        ImagePanel ip = new ImagePanel(this, imHelp.getLabel());
             add("Center", ip);
             board = ip.getBoard();
             System.out.println("initial finish   ");

    }

    public void run()
    {
        while (killme != null)
        {
            try
            {
                Thread.sleep(100);
            }
            catch(InterruptedException e)
            {
            }

            board.tick();
        }
        killme = null;
    }

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

    public void stop()
    {
        killme = null;
    }

}

class ImageHelp extends Panel
{
    Label l;
    public ImageHelp()
    {
        setLayout(new GridLayout(2, 1));
        // Create a new label, centered in the panel
        add(new Label("Move the player using the arrow keys and fire with f and g,and speed up using x and z ", Label.CENTER));
        l = new Label("Score: ");
        add(l);
        System.out.println("imagehelp finished   ");
    }

    public Label getLabel()
    {
        return l;
    }
}

class ImagePanel extends Panel
{
    ImageCanvas board;

    public ImagePanel(Applet app, Label l)
    {
        // Set the layout so that it is one column full size of the panel
        //
        setLayout(new GridLayout(0, 1));

        board = new ImageCanvas(app, l);
        add(board);
        System.out.println("imagepanel initial finished   ");
    }

    public ImageCanvas getBoard()
    {
        return board;
    }
}

class ImageCanvas extends Canvas        /*implements ImageObserver */
{
    int  banks[][] = new int[2][12];
    int  xpos = 50, ypos = 50;
    int  yOff = 0;
    int  ySpeed = 1;
    int  ymoveoffset = 5;               //player's move offset
    int  xmoveoffset = 5;
    fire fireshot[] = new fire[20];     //player's shoot
    shark_fire sharkfire_shot[] = new shark_fire[20]; //shark's shoot
    Image im;
    Graphics gOff;

    Image image_bird;
    Image image_shark;
    int  xshark[] = new int[6];
    int  yshark[] = new int[6];
    int  xSpeedshark[] = new int[6];
    int  nShoreTick = 0;
    int  nScore = 0;
    Label labelScore;
    boolean fPaused = true;
    boolean fDead = false;
    Image image_weed;
    Dimension r;
    int  xweed[] = new int[4];
    int  yweed[] = new int[4];

    public ImageCanvas(Applet app, Label l)
    {
        labelScore = l;

        image_bird = app.getImage(app.getCodeBase(), "bird.gif");
        image_shark = app.getImage(app.getCodeBase(), "shark.gif");
        image_weed = app.getImage(app.getCodeBase(), "weed.gif");
        r = app.getSize();

        initBoard();

        try
        {                       //layout();
            System.out.println(r);
            im = app.createImage(r.width, r.height);
            System.out.println(im);
            gOff = im.getGraphics();

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        System.out.println(" finish image canvas creat  ");
    }

    void initBoard()
    {
        // Initialize the banks
        for(int i = 0; i < 12; i++)
        {
            for(int j = 0; j < 2; j++)
            {
                banks[j][i] = (int)(Math.random() * 30);
            }
        }

        // Initialize the weed
        for(int i = 0; i < 4; i++)
            xweed[i] = -999;

        // Initialize the sharks
        for(int i = 0; i < 6; i++)
            xshark[i] = -999;

        xpos = 100;
        ypos = 50;
        yOff = 0;
        ymoveoffset = 5;
        xmoveoffset = 5;
        ySpeed = 1;
        nShoreTick = 0;
        nScore = 0;
        fDead = false;
    }

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

    public void paint(Graphics g)
    {
        Rectangle r = bounds(); //
        paintApplet(gOff);
        g.drawImage(im, 0, 0, this);
        //  System.out.println("paint   ");  //error
    }

    public void paintApplet(Graphics g)
    {                           //System.out.println(" start to paint applet" );
        Rectangle r = bounds();
        g.setColor(Color.lightGray);
        g.clearRect(0, 0, r.width, r.height);

        // Draw the banks
        Polygon p1 = new Polygon();
        p1.addPoint(0, 0);
        Polygon p2 = new Polygon();
        p2.addPoint(r.width, 0);
        for(int i = 0; i < 12; i++)
        {
            p1.addPoint(banks[0][i], i * r.height / 10 - yOff);
            p2.addPoint(r.width - banks[1][i], i * r.height / 10 - yOff);
        }
        p1.addPoint(0, r.height);
        p1.addPoint(0, 0);
        p2.addPoint(r.width, r.height);
        p2.addPoint(r.width, 0);
        g.setColor(Color.yellow);
        g.fillPolygon(p1);
        g.fillPolygon(p2);

        // Draw the weed
        for(int i = 0; i < 4; i++)
            if (xweed[i] != -999)
            {
                // System.out.println("Drawing a weed");
                g.drawImage(image_weed, xweed[i], yweed[i], this);
            }

        // Draw the sharks
        for(int i = 0; i < 6; i++)
            if (xshark[i] != -999)
                g.drawImage(image_shark, xshark[i], yshark[i], this);

        // Draw the player
        g.drawImage(image_bird, xpos, ypos, this);

        for(int i = 0; i < 20; i++)
        {
            if (fireshot[i] != null && fireshot[i].state)
            {
                if (!fireshot[i].checkexplode(g) && !fireshot[i].checksharkfirehitted(g));      // if not explode and if not hit the fire of shark
                fireshot[i].paint(g);
                //fireshot[i].check(100,200);
            }
        }

        for(int i = 0; i < 20; i++)
        {
            if (sharkfire_shot[i] != null && sharkfire_shot[i].state)
            {
                if (!sharkfire_shot[i].checkexplode(g));        // if not explode
                sharkfire_shot[i].paint(g);
                //fireshot[i].check(100,200);
            }
        }

        // Display any status messages
        g.setColor(Color.gray);
        if (fDead)
            g.drawString("haha ^_^ ,you are hitted ,Press space bar for new game", 50, 50);
        else if (fPaused)
            g.drawString("Press space bar to tube", 50, 50);
    }

    int  leftbank(int y)
    {
        Rectangle r = bounds();
        int  segment = (y + yOff) * 10 / r.height;
        return Math.max(banks[0][segment], banks[0][segment + 1]);
    }

    int  rightbank(int y)
    {
        Rectangle r = bounds();
        int  segment = (y + yOff) * 10 / r.height;
        return r.width - Math.min(banks[1][segment], banks[1][segment + 1]);
    }

    public synchronized boolean handleEvent(Event e)
    {
        switch (e.id)
        {
           case Event.KEY_ACTION:
           case Event.KEY_PRESS:
               switch (e.key)
               {
                  case Event.UP:
                  case 'k':
                      if (ypos - ymoveoffset > 0 && !fPaused)
                          ypos -= ymoveoffset;
                      repaint();
                      return true;
                  case Event.DOWN:
                  case 'j':
                      if (ypos + ymoveoffset < bounds().height - 32 && !fPaused)
                          // {//if (ySpeed>5)
                          // {
                          // }
                          ypos += ySpeed + ymoveoffset;
                      repaint();
                      return true;
                  case Event.RIGHT:
                  case 'l':
                      if (xpos + xmoveoffset < rightbank(ypos) - 32 && !fPaused)
                          xpos += xmoveoffset;
                      repaint();
                      return true;
                  case Event.LEFT:
                  case 'h':
                      if (xpos - xmoveoffset > leftbank(ypos) && !fPaused)
                          xpos -= xmoveoffset;
                      repaint();
                      return true;

                  case 'f':
                      startafire(xpos, ypos, 20, 10, 0);  //fire type 0 with speed of 20
                      //System.out.println("fire   ");
                      //fire a=new fire(20,20,30);
                      break;
                  case 'g':
                      startafire(xpos, ypos, 35, 20, 1);   //fire type 1 with speed of 35
          break;
                      //repaint();
                      //return true;
                  case 'z':                                //accecerate the player's x speed
                      //               if (ySpeed > 0)
                      //                   ySpeed--;
                      xmoveoffset++;
                      return true;
                  case 'x':                                //accecerate the player's y speed
                      //                if (ySpeed < 10)
                      //                 ySpeed++;
                      ymoveoffset++;
                      return true;
                  case ' ':                                //it is the blank press
                      if (fDead)
                      {
                          initBoard();
                          fPaused = false;
                          labelScore.setText("Score: " + String.valueOf(nScore));
                      }
                      else
                          fPaused = !fPaused;
                      return true;
                  default:
                      return false;
               }

           default:
               return false;
        }
    }

    // tick() is because I couldn't figure out if I made this class Runnable what
    // whould I do about start()&stop()
    public void tick()
    {
        if (fPaused)
            return;

        // Scroll the shore
        yOff += ySpeed;

        // Scroll the player
        if (ypos - ySpeed > 0)
        {
            ypos -= ySpeed;
        }

        if (xpos < leftbank(ypos))
            xpos = leftbank(ypos);
        else if (xpos > rightbank(ypos) - 32)
            xpos = rightbank(ypos) - 32;

        // Scroll the weed
        for(int i = 0; i < 4; i++)
            if (xweed[i] != -999)
            {
                yweed[i] -= ySpeed;
                if (yweed[i] < 0)
                {
                    //               System.out.println("Killing a cactus");
                    xweed[i] = -999;
                }
            }
        // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% below start to check collision//
        Rectangle rectPlayer = new Rectangle(xpos + 4, ypos + 4, 32 - 4, 32 - 4);
        Rectangle rectShark = new Rectangle();
        Rectangle rectshot = new Rectangle();
        Rectangle rectsharkshot1 = new Rectangle();
        Rectangle rectsharkshot2 = new Rectangle();
        Rectangle rectsharkshot3 = new Rectangle();

        // Scroll the sharks
        for(int i = 0; i < 6; i++)
        {
            yshark[i] -= Math.random() * ySpeed;
            if (xshark[i] != -999 && (yshark[i] < 0 || xshark[i] < leftbank(yshark[i])))
            {
                // Shark is off-screen.  Destroy.
                //            System.out.println("Destroyed a shark");
                xshark[i] = -999;
            }
            if (xshark[i] != -999)
            {
                xshark[i] -= xSpeedshark[i] * ySpeed;

                // check if the shark hit the player?
                rectShark.reshape(xshark[i] + 8, yshark[i] + 8, 32 - 8, 32 - 8);
                if (rectPlayer.intersects(rectShark))
                {
                    fDead = true;
                    fPaused = true;
                }
            }
            //check if fire shot  hit the shark
            for(int j = 0; j < 20; j++)
            {
                if (fireshot[j] != null && fireshot[j].state)
                {
                    rectshot.reshape(fireshot[j].startx - fireshot[j].firesize / 2,
                                     fireshot[j].starty - fireshot[j].firesize / 2, fireshot[j].firesize,
                                     fireshot[j].firesize);
                    if (rectshot.intersects(rectShark))
                    {
                       
                        xshark[i] = -999;       //// Shark   Destroy.
                        // fireshot[j].explode();
                        nScore += 200;  //score added as you hit the shark
                        fireshot[j].setexplode();
                    }
                }
            }

        }

        //check if  fire shot of shark  hit the player
        for(int j = 0; j < 20; j++)
        {
            if (sharkfire_shot[j] != null && sharkfire_shot[j].state)
            {
                rectsharkshot1.reshape(sharkfire_shot[j].startx - sharkfire_shot[j].firesize / 2,
                                       sharkfire_shot[j].starty - sharkfire_shot[j].firesize / 2,
                                       sharkfire_shot[j].firesize, sharkfire_shot[j].firesize);
                rectsharkshot2.reshape(sharkfire_shot[j].startx - sharkfire_shot[j].firesize / 2 -
                                       sharkfire_shot[j].xoffset,
                                       sharkfire_shot[j].starty - sharkfire_shot[j].firesize / 2,
                                       sharkfire_shot[j].firesize, sharkfire_shot[j].firesize);
                rectsharkshot3.reshape(sharkfire_shot[j].startx - sharkfire_shot[j].firesize / 2 +
                                       sharkfire_shot[j].xoffset,
                                       sharkfire_shot[j].starty - sharkfire_shot[j].firesize / 2,
                                       sharkfire_shot[j].firesize, sharkfire_shot[j].firesize);

                if (rectsharkshot1.intersects(rectPlayer) || rectsharkshot2.intersects(rectPlayer) ||
                    rectsharkshot3.intersects(rectPlayer))
                {

                    sharkfire_shot[j].setexplode();
                    fDead = true;
                    fPaused = true;     //player is hitted
                }
    for(int k = 0; k < 20; k++)
                {
                    if (fireshot[k] != null && fireshot[k].state)
                    {
                        rectshot.reshape(fireshot[k].startx - fireshot[k].firesize / 2,
                                         fireshot[k].starty - fireshot[k].firesize / 2, fireshot[k].firesize,
                                         fireshot[k].firesize);
                         ////check shark fire shot if hit the player fire shot 
                         if (rectsharkshot1.intersects(rectshot) || rectsharkshot2.intersects(rectshot) ||
                            rectsharkshot3.intersects(rectshot))
                        {
                            fireshot[k].setsharkfirehitted();      //fire of player hit with fire of shark
                            sharkfire_shot[j].stop();
                            nScore += 30;
                        }
                    }
                }
            }
        }
       

        // Update score, if on the bottom half of screen
        Rectangle r = bounds();
        if (ypos + yOff > r.height / 2)
        {
            nScore += ySpeed;
            labelScore.setText("Score: " + String.valueOf(nScore));
        }

        // Do we need to generate another shore coordinate?
        if (yOff >= r.height / 10)
        {
            yOff -= r.height / 10;

            for(int i = 1; i < 12; i++)
            {
                banks[0][i - 1] = banks[0][i];
                banks[1][i - 1] = banks[1][i];
            }
            //         banks[0][11] = banks[0][10] + 10;
            //         banks[1][11] = banks[0][10] + 10;
            banks[0][11] = banks[0][10] + (int)(Math.random() * 20) - 7;
            banks[1][11] = banks[1][10] + (int)(Math.random() * 20) - 7;
            if (banks[0][11] < 0)
                banks[0][11] = 0;
            if (r.width - banks[1][11] < 100)
                banks[1][11] -= 64;
            if (banks[0][11] >= r.width)
                banks[0][11] = r.width - 1;
            if (banks[1][11] >= r.width)
                banks[1][11] = r.width - 1;
            if (banks[0][11] > banks[1][11])
            {
                int  i = banks[0][11];
                banks[0][11] = banks[1][11];
                banks[1][11] = i;
            }
            if (banks[0][11] + 64 > r.width - banks[1][11])
            {
                banks[0][11] = r.width - banks[1][11] - 40;
                banks[1][11] -= 22;
            }

            // If necessary, generate a new shark
            for(int i = 0; i < 6; i++)
                if (xshark[i] == -999 && Math.random() < 0.2)
                {
                    xshark[i] = r.width - banks[1][11] - 32;
                    yshark[i] = 9 * r.height / 10;
                    xSpeedshark[i] = (int)(Math.random() * 3) + 1;
                    //               System.out.println("Created a shark");
                    break;
                }
            // start a shark fire;
            int  ran = (int)(Math.random() * 6);
            startasharkfire(xshark[ran], yshark[ran], 5, 10);   //start shark fire with speed 5 and size 10

            nShoreTick++;
            if (nShoreTick > 3)
            {
                //            System.out.println("Finished level " + String.valueOf(ySpeed));
                nShoreTick = 0;
                banks[0][11] = 0;
                banks[1][11] = 0;
                if (ySpeed < 10)
                    ySpeed++;
            }
        }

        // Add any needed weed
        for(int i = 0; i < 4; i++)
            if (xweed[i] == -999 && Math.random() < .02)
            {
                //            System.out.println("Making a weed");
                yweed[i] = r.height;
                if (Math.random() < 0.5)
                    xweed[i] = (int)(Math.random() * banks[0][11]);
                else
                    xweed[i] = r.width - (int)(Math.random() * banks[1][11]);
            }

        for(int i = 0; i < 20; i++)
        {
            if (fireshot[i] != null && fireshot[i].state)
            {
                fireshot[i].move();
                fireshot[i].check(100, 500);
            }
        }

        for(int i = 0; i < 20; i++)
        {
            if (sharkfire_shot[i] != null && sharkfire_shot[i].state)
            {
                sharkfire_shot[i].move();
                sharkfire_shot[i].check();
            }
        }

        repaint();
    }
    public void startafire(int x, int y, int speed, int size, int type)
    {                           //System.out.println("start to find fire   ");
        for(int i = 0; i < 20; i++)
        {
            if (fireshot[i] == null)
            {
                fireshot[i] = new fire(x, y, speed, size, type);
                System.out.println(" creat new  " + i);
                return;
            }
            else if (fireshot[i].state == false)
            {
                fireshot[i].set(x, y, speed, size, type);
                System.out.println(" state false reset " + i);
                return;
            }
            //System.out.println(fireshot[i].state + " " + i);
            // System.out.println(fireshot[i].state+" "+fireshot[i].myThread+" "+i);
        }

    }

    public void startasharkfire(int x, int y, int z, int size)
    {                           //System.out.println("start to find fire   ");
        for(int i = 0; i < 20; i++)
        {
            if (sharkfire_shot[i] == null)
            {
                sharkfire_shot[i] = new shark_fire(x, y, z, size);
                System.out.println("shark fire creat new  " + i);
                return;
            }
            else if (sharkfire_shot[i].state == false)
            {
                sharkfire_shot[i].set(x, y, z, size);
                System.out.println("shark fire state false reset " + i);
                return;
            }
            //System.out.println(sharkfire_shot[i].state + " " + i);
            // System.out.println(fireshot[i].state+" "+fireshot[i].myThread+" "+i);
        }

    }
}

/*

   <applet  code=Watergame.class width=500 height=500>
   </applet>
 */

本文地址:http://com.8s8s.com/it/it14857.htm