栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA小项目潜艇大战

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA小项目潜艇大战

一、第一天

1、创建战舰、侦察潜艇、鱼雷潜艇、水雷潜艇、水雷、深水炸弹类

完整代码:

package day01;
//战舰
public class Battleship {
    int width;
    int height;
    int x;
    int y;
    int speed;
    int life;

    void move(){
        System.out.println("战舰移动");
    }
}
package day01;
//侦察潜艇
public class ObserveSubmarine {
    int width;
    int height;
    int x;
    int y;
    int speed;
    void move(){
        System.out.println("侦察潜艇x向右移动");
    }
}
package day01;
//鱼雷潜艇
public class TorpedoSubmarine {
    int width;
    int height;
    int x;
    int y;
    int speed;
    void move(){
        System.out.println("鱼雷潜艇x向右移动");
    }
}
package day01;
//水雷潜艇
public class MineSubmarine {
    int width;
    int height;
    int x;
    int y;
    int speed;
    void move(){
        System.out.println("水雷潜艇x向右移动");
    }
}
package day01;
//水雷
public class Mine {
    int width;
    int height;
    int x;
    int y;
    int speed;
    void move(){
        System.out.println("水雷y向上移动");
    }
}
package day01;
//深水炸弹
public class Bomb {
    int width;
    int height;
    int x;
    int y;
    int speed;
    void move(){
        System.out.println("深水炸弹y向下移动");
    }
}

2、创建World类并测试

完整代码:

package day01;

public class World {
    public static void main(String[] args) {
        Battleship s = new Battleship();
        s.width = 50;
        s.height = 30;
        s.x = 100;
        s.y = 200;
        s.speed = 20;
        s.life = 5;
        System.out.println(s.width+","+s.height+","+s.x+","+s.y+","+s.speed+","+s.life);
        s.move();

        ObserveSubmarine os1 = new ObserveSubmarine();
        os1.width = 30;
        os1.height = 40;
        os1.x = 200;
        os1.y = 400;
        os1.speed = 3;
        System.out.println(os1.width+","+os1.height+","+os1.x+","+os1.y+","+os1.speed);
        os1.move();

        ObserveSubmarine os2 = new ObserveSubmarine();
        os2.width = 30;
        os2.height = 40;
        os2.x = 100;
        os2.y = 450;
        os2.speed = 3;
        System.out.println(os2.width+","+os2.height+","+os2.x+","+os2.y+","+os2.speed);
        os2.move();

        TorpedoSubmarine ts1 = new TorpedoSubmarine();
        TorpedoSubmarine ts2 = new TorpedoSubmarine();
        MineSubmarine ms1 = new MineSubmarine();
        MineSubmarine ms2 = new MineSubmarine();
        Torpedo t1 = new Torpedo();
        Torpedo t2 = new Torpedo();
        Mine m1 = new Mine();
        Mine m2 = new Mine();
        Bomb b1 = new Bomb();
        Bomb b2 = new Bomb();
    }
}
二、第二天

给6个类添加构造方法,并测试

完整代码:

package day02;
//战舰
public class Battleship {
    int width;
    int height;
    int x;
    int y;
    int speed;
    int life;
    Battleship(){
        width = 66;
        height = 26;
        x = 270;
        y = 124;
        speed = 2;
        life = 5;
    }

    void move(){
        System.out.println("战舰移动");
    }
}

package day02;
import java.util.Random;
//侦察潜艇
public class ObserveSubmarine {
    int width;
    int height;
    int x;
    int y;
    int speed;
    ObserveSubmarine(){
        width = 63;
        height = 19;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(479-height-150)+150;
        speed = rand.nextInt(3)+1;
    }
    void move(){
        System.out.println("侦察潜艇x向右移动");
    }
}

package day02;
import java.util.Random;
//鱼雷潜艇
public class TorpedoSubmarine {
    int width;
    int height;
    int x;
    int y;
    int speed;
    TorpedoSubmarine(){
        width = 64;
        height = 20;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(479-height-150)+150;
        speed = rand.nextInt(3)+1;
    }
    void move(){
        System.out.println("鱼雷潜艇x向右移动");
    }
}

package day02;
import java.util.Random;
//水雷潜艇
public class MineSubmarine {
    int width;
    int height;
    int x;
    int y;
    int speed;
    MineSubmarine(){
        width = 63;
        height = 19;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(479-height-150)+150;
        speed = rand.nextInt(3)+1;
    }
    void move(){
        System.out.println("水雷潜艇x向右移动");
    }
}

package day02;
//水雷
public class Mine {
    int width;
    int height;
    int x;
    int y;
    int speed;
    Mine(int x,int y){
        width = 11;
        height = 11;
        this.x = x;
        this.y = y;
        speed = 1;
    }
    void move(){
        System.out.println("水雷y向上移动");
    }
}

package day02;
//深水炸弹
public class Bomb {
    int width;
    int height;
    int x;
    int y;
    int speed;
    Bomb(int x,int y){
        width = 9;
        height = 12;
        this.x = x;
        this.y = y;
        speed = 3;
    }

    void move(){
        System.out.println("深水炸弹y向下移动");
    }
}

package day02;

public class World {
    public static void main(String[] args) {
        Battleship s = new Battleship();
        ObserveSubmarine os1 = new ObserveSubmarine();
        ObserveSubmarine os2 = new ObserveSubmarine();
        TorpedoSubmarine ts1 = new TorpedoSubmarine();
        TorpedoSubmarine ts2 = new TorpedoSubmarine();
        MineSubmarine ms1 = new MineSubmarine();
        MineSubmarine ms2 = new MineSubmarine();
        Mine m1 = new Mine(123,345);
        Mine m2 = new Mine(345,234);
        Bomb b1 = new Bomb(200,300);
        Bomb b2 = new Bomb(300,400);
        //最少输出4个对象的数据(一定要包括2个侦察潜艇的数据)
        System.out.println(s.width+","+s.height+","+s.x+","+s.y+","+s.speed+","+s.life);
        System.out.println(os1.width+","+os1.height+","+os1.x+","+os1.y+","+os1.speed);
        System.out.println(os2.width+","+os2.height+","+os2.x+","+os2.y+","+os2.speed);

        Bomb[] bs = new Bomb[3]; //创建Bomb数组对象
        bs[0] = new Bomb(100,200); //创建Bomb对象
        bs[1] = new Bomb(123,345);
        bs[2] = new Bomb(200,300);
        bs[0].x = 111; //给第1个炸弹的x修改为111
        System.out.println(bs[1].width); //输出第2个炸弹的宽
        bs[2].move(); //第3个炸弹移动
    }
}
三、第三天

1、创建侦察艇数组、鱼雷艇数组、水雷艇数组、水雷数组、深水炸弹数组,并测试

完整代码:

package day03;

public class World {
    public static void main(String[] args) {
        ObserveSubmarine[] oses = new ObserveSubmarine[3];
        TorpedoSubmarine[] tses = new TorpedoSubmarine[2];
        MineSubmarine[] mses = new MineSubmarine[3]; //水雷潜艇数组
        mses[0] = new MineSubmarine();
        mses[1] = new MineSubmarine();
        mses[2] = new MineSubmarine();
        for(int i=0;i 

2、创建SeaObject超类,6个类继承超类

完整代码:

package day03;
import java.util.Random;
//海洋对象
public class SeaObject {
    int width;
    int height;
    int x;
    int y;
    int speed;
    void move(){
        System.out.println("海洋对象移动");
    }
}

package day03;
//战舰
public class Battleship extends SeaObject {
    int life;
    Battleship(){
        width = 66;
        height = 26;
        x = 270;
        y = 124;
        speed = 20;
        life = 5;
    }
}

package day03;
import java.util.Random;
//侦察潜艇
public class ObserveSubmarine extends SeaObject {
    ObserveSubmarine(){
        width = 63;
        height = 19;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(479-height-150)+150;
        speed = rand.nextInt(3)+1;
    }
}

package day03;
import java.util.Random;
//鱼雷潜艇
public class TorpedoSubmarine extends SeaObject {
    TorpedoSubmarine(){
        width = 64;
        height = 20;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(479-height-150)+150;
        speed = rand.nextInt(3)+1;
    }
}

package day03;
import java.util.Random;
//水雷潜艇
public class MineSubmarine extends SeaObject {
    MineSubmarine(){
        width = 63;
        height = 19;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(479-height-150)+150;
        speed = rand.nextInt(3)+1;
    }
}

package day03;
//水雷
public class Mine extends SeaObject {
    Mine(int x,int y){
        width = 11;
        height = 11;
        this.x = x;
        this.y = y;
        speed = 1;
    }
}


package day03;
//深水炸弹
public class Bomb extends SeaObject {
    Bomb(int x,int y){
        width = 9;
        height = 12;
        this.x = x;
        this.y = y;
        speed = 3;
    }
}

//注:其余类没有变化,此处省略

3、给SeaObject添加两个构造方法,6个类继承超类

完整代码:

package day03;
import java.util.Random;
//海洋对象
public class SeaObject {
    int width;
    int height;
    int x;
    int y;
    int speed;
    SeaObject(int width,int height){
        this.width = width;
        this.height = height;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(479-height-150)+150;
        speed = rand.nextInt(3)+1;
    }
    SeaObject(int width,int height,int x,int y,int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    void move(){
        System.out.println("海洋对象移动");
    }
}

package day03;
//侦察潜艇
public class ObserveSubmarine extends SeaObject {
    ObserveSubmarine(){
        super(63,19);
    }
}

package day03;
//鱼雷潜艇
public class TorpedoSubmarine extends SeaObject {
    TorpedoSubmarine(){
        super(64,20);
    }
}

package day03;
//水雷潜艇
public class MineSubmarine extends SeaObject {
    MineSubmarine(){
        super(63,19);
    }
}

package day03;
//战舰
public class Battleship extends SeaObject {
    int life;
    Battleship(){
        super(66,26,270,124,20);
        life = 5;
    }
}

package day03;
//水雷
public class Mine extends SeaObject {
    Mine(int x,int y){
        super(11,11,x,y,1);
    }
}

package day03;
//深水炸弹
public class Bomb extends SeaObject {
    Bomb(int x,int y){
        super(9,12,x,y,3);
    }
}

//注:其余类没有变化,此处省略

4、将侦察艇数组、鱼雷艇数组、水雷艇数组组合为SeaObject数组,并测试

package day03;
//整个游戏世界
public class World {
    public static void main(String[] args) {
        SeaObject[] submarines = new SeaObject[5]; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)
        submarines[0] = new ObserveSubmarine();
        submarines[1] = new ObserveSubmarine();
        submarines[2] = new TorpedoSubmarine();
        submarines[3] = new TorpedoSubmarine();
        submarines[4] = new MineSubmarine();
        for(int i=0;i 
四、第四天 

1、重写超类的move()移动

完整代码:

package day04;
//战舰
public class Battleship extends SeaObject {
    int life;
    Battleship(){
        super(66,26,270,124,20);
        life = 5;
    }
    void move(){
        //暂时搁置
    }
}

package day04;
//侦察潜艇
public class ObserveSubmarine extends SeaObject {
    ObserveSubmarine(){
        super(63,19);
    }
    void move(){
        x+=speed;
    }
}

package day04;
//鱼雷潜艇
public class TorpedoSubmarine extends SeaObject {
    TorpedoSubmarine(){
        super(64,20);
    }
    void move(){
        x+=speed;
    }
}

package day04;
//水雷潜艇
public class MineSubmarine extends SeaObject {
    MineSubmarine(){
        super(63,19);
    }
    void move(){
        x+=speed;
    }
}

package day04;
//水雷
public class Mine extends SeaObject {
    Mine(int x,int y){
        super(11,11,x,y,1);
    }
    void move(){
        y-=speed;
    }
}

package day04;
//深水炸弹
public class Bomb extends SeaObject {
    Bomb(int x,int y){
        super(9,12,x,y,3);
    }
    void move(){
        y+=speed;
    }
}

//注:其余类没有变化,此处省略

2、给类中成员添加访问控制修饰符

完整代码:

package day04;
import java.util.Random;
//海洋对象
public class SeaObject {
    protected int width;
    protected int height;
    protected int x;
    protected int y;
    protected int speed;
    public SeaObject(int width,int height){
        this.width = width;
        this.height = height;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(479-height-150)+150;
        speed = rand.nextInt(3)+1;
    }
    public SeaObject(int width,int height,int x,int y,int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public void move(){
        System.out.println("海洋对象移动");
    }
}

package day04;
//战舰
public class Battleship extends SeaObject {
    private int life;
    public Battleship(){
        super(66,26,270,124,20);
        life = 5;
    }
    public void move(){
        //暂时搁置
    }
}

package day04;
//侦察潜艇
public class ObserveSubmarine extends SeaObject {
    public ObserveSubmarine(){
        super(63,19);
    }
    public void move(){
        x+=speed;
    }
}

package day04;
//鱼雷潜艇
public class TorpedoSubmarine extends SeaObject {
    public TorpedoSubmarine(){
        super(64,20);
    }
    public void move(){
        x+=speed;
    }
}

package day04;
//水雷潜艇
public class MineSubmarine extends SeaObject {
    public MineSubmarine(){
        super(63,19);
    }
    public void move(){
        x+=speed;
    }
}

package day04;
//水雷
public class Mine extends SeaObject {
    public Mine(int x,int y){
        super(11,11,x,y,1);
    }
    public void move(){
        y-=speed;
    }
}

package day04;
//深水炸弹
public class Bomb extends SeaObject {
    public Bomb(int x,int y){
        super(9,12,x,y,3);
    }
    public void move(){
        y+=speed;
    }
}

//注:其余类没有变化,此处省略

3、设计Images图片类

完整代码:

package cn.tedu.submarine;
import javax.swing.ImageIcon;

public class Images {
//  公开的  静态的  图片数据类型   变量名
    public static ImageIcon  battleship; //战舰图片
    public static ImageIcon  obsersubm;  //侦察潜艇图片
    public static ImageIcon  torpesubm;  //鱼雷潜艇图片
    public static ImageIcon  minesubm;   //水雷潜艇图片
    public static ImageIcon  mine;       //水雷图片
    public static ImageIcon  bomb;       //深水炸弹图片
    public static ImageIcon  sea;        //海洋图
    public static ImageIcon  gameover;   //游戏结束图

    static{ //给静态图片赋值
        battleship = new ImageIcon("img/battleship.png");
        obsersubm = new ImageIcon("img/obsersubm.png");
        torpesubm = new ImageIcon("img/torpesubm.png");
        minesubm = new ImageIcon("img/minesubm.png");
        mine = new ImageIcon("img/mine.png");
        bomb = new ImageIcon("img/bomb.png");
        sea = new ImageIcon("img/sea.png");
        gameover = new ImageIcon("img/gameover.png");
    }

    public static void main(String[] args) {
        System.out.println(battleship.getImageLoadStatus()); //返回8表示读取成功
        System.out.println(obsersubm.getImageLoadStatus());
        System.out.println(torpesubm.getImageLoadStatus());
        System.out.println(minesubm.getImageLoadStatus());
        System.out.println(mine.getImageLoadStatus());
        System.out.println(bomb.getImageLoadStatus());
        System.out.println(sea.getImageLoadStatus());
        System.out.println(gameover.getImageLoadStatus());
    }
}
//注:其余类没有变化,此处省略
五、第五天

1、在World类中设计窗口的宽和高为常量,在SeaObject的两个参构造中将数据修改为常量

参考代码:

package day05;
//整个游戏世界
public class World {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;
    
    public static void main(String[] args) {
        SeaObject[] submarines = new SeaObject[5]; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)
        submarines[0] = new ObserveSubmarine();
        submarines[1] = new ObserveSubmarine();
        submarines[2] = new TorpedoSubmarine();
        submarines[3] = new TorpedoSubmarine();
        submarines[4] = new MineSubmarine();
        for(int i=0;i 

2、画窗口

参考代码:

package day05;
import javax.swing.Jframe;
import javax.swing.JPanel;
//整个游戏世界
public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;
    
    public static void main(String[] args) {
        Jframe frame = new Jframe();
        World world = new World();
        world.setFocusable(true);
        frame.add(world);
        frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
        frame.setSize(WIDTH+16, HEIGHT+39);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

//注:其余类没有变化,此处省略

3、画海洋图、画对象

在超类中设计抽象方法getImage(),派生类中重写在超类中设计状态常量及当前状态变量,设计isLive()、isDead()判断状态在超类中设计paintImage()画图片在main中:准备对象、重写paint()调用paintImage()方法

参考代码:

package day05;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.util.Random;
//海洋对象
public abstract class SeaObject {
    public static final int LIVE = 0;
    public static final int DEAD = 1;
    protected int state = LIVE; //当前状态

    protected int width;
    protected int height;
    protected int x;
    protected int y;
    protected int speed;
    public SeaObject(int width,int height){
        this.width = width;
        this.height = height;
        x = width;
        Random rand = new Random();
        y = rand.nextInt(World.HEIGHT-height-150)+150;
        speed = rand.nextInt(3)+1;
    }
    public SeaObject(int width,int height,int x,int y,int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public abstract void move();

    public abstract ImageIcon getImage();

    public boolean isLive(){
        return state==LIVE;
    }
    public boolean isDead(){
        return state==DEAD;
    }

    public void paintImage(Graphics g){
        if(isLive()){
            this.getImage().paintIcon(null,g,this.x,this.y);
        }
    }
}

package day05;
import javax.swing.ImageIcon;
//战舰
public class Battleship extends SeaObject {
    private int life;
    public Battleship(){
        super(66,26,270,124,20);
        life = 5;
    }

    public void move(){
        System.out.println("战舰移动");
    }

    public ImageIcon getImage(){
        return Images.battleship;
    }
}

package day05;
import javax.swing.ImageIcon;
//侦察潜艇
public class ObserveSubmarine extends SeaObject {
    public ObserveSubmarine(){
        super(63,19);
    }

    public void move(){
        System.out.println("侦察潜艇x向右移动");
    }

    public ImageIcon getImage(){
        return Images.obsersubm;
    }
}

package day05;
import javax.swing.ImageIcon;
//鱼雷潜艇
public class TorpedoSubmarine extends SeaObject {
    public TorpedoSubmarine(){
        super(64,20);
    }

    public void move(){
        System.out.println("鱼雷潜艇x向右移动");
    }

    public ImageIcon getImage(){
        return Images.torpesubm;
    }
}

package day05;
import javax.swing.ImageIcon;
//水雷潜艇
public class MineSubmarine extends SeaObject {
    public MineSubmarine(){
        super(63,19);
    }

    public void move(){
        System.out.println("水雷潜艇x向右移动");
    }

    public ImageIcon getImage(){
        return Images.minesubm;
    }
}

package day05;
import javax.swing.ImageIcon;
//水雷
public class Mine extends SeaObject {
    public Mine(int x,int y){
        super(11,11,x,y,1);
    }

    public void move(){
        System.out.println("水雷y向上移动");
    }

    public ImageIcon getImage(){
        return Images.mine;
    }
}

package day05;
import javax.swing.ImageIcon;
//深水炸弹
public class Bomb extends SeaObject {
    public Bomb(int x,int y){
        super(9,12,x,y,3);
    }

    public void move(){
        System.out.println("深水炸弹y向下移动");
    }

    public ImageIcon getImage(){
        return Images.bomb;
    }
}

package day05;
import javax.swing.Jframe;
import javax.swing.JPanel;
import java.awt.Graphics;
//整个游戏世界
public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;

    private Battleship ship = new Battleship(); //战舰
    private SeaObject[] submarines = {
            new ObserveSubmarine(),
            new TorpedoSubmarine(),
            new MineSubmarine()
    }; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)
    private Mine[] mines = {
            new Mine(260,200)
    }; //水雷
    private Bomb[] bombs = {
            new Bomb(200,190)
    }; //深水炸弹

    public void paint(Graphics g){
        Images.sea.paintIcon(null,g,0,0); //画海洋图
        ship.paintImage(g);
        for(int i=0;i 
六、第六天 

1、潜艇入场

完整代码:

package day06;
import javax.swing.Jframe;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
//整个游戏世界
public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;

    private Battleship ship = new Battleship(); //战舰
    private SeaObject[] submarines = {}; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)
    private Mine[] mines = {}; //水雷
    private Bomb[] bombs = {}; //深水炸弹


    
    public SeaObject nextSubmarine(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<10){
            return new ObserveSubmarine();
        }else if(type<15){
            return new TorpedoSubmarine();
        }else{
            return new MineSubmarine();
        }
    }

    private int subEnterIndex = 0;
    
    public void submarineEnterAction(){ //每10毫秒走一次
        subEnterIndex++;
        if(subEnterIndex%40==0){ //每40毫秒
            SeaObject obj = nextSubmarine();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = obj;
        }
    }

    
    public void action(){
        Timer timer = new Timer();
        int interval = 10;
        timer.schedule(new TimerTask() {
            public void run() {
                submarineEnterAction(); //潜艇(侦察、水雷、鱼雷)入场
                repaint();
            }
        }, interval, interval);
    }

    public void paint(Graphics g){
        Images.sea.paintIcon(null,g,0,0); //画海洋图
        ship.paintImage(g);
        for(int i=0;i 

2、水雷入场:

package day06;
import javax.swing.ImageIcon;
//水雷潜艇
public class MineSubmarine extends SeaObject {
    public MineSubmarine(){
        super(63,19);
    }

    public void move(){
        x+=speed;
    }

    public ImageIcon getImage(){
        return Images.minesubm;
    }

    
    public Mine shootMine(){
        int x = this.x+this.width; //x:潜艇的x+潜艇的宽
        int y = this.y-11;          //y:潜艇的y-11
        return new Mine(x,y);
    }
}

package day06;
import javax.swing.Jframe;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
//整个游戏世界
public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;

    private Battleship ship = new Battleship(); //战舰
    private SeaObject[] submarines = {}; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)
    private Mine[] mines = {}; //水雷
    private Bomb[] bombs = {}; //深水炸弹


    
    public SeaObject nextSubmarine(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<10){
            return new ObserveSubmarine();
        }else if(type<15){
            return new TorpedoSubmarine();
        }else{
            return new MineSubmarine();
        }
    }

    private int subEnterIndex = 0;
    
    public void submarineEnterAction(){ //每10毫秒走一次
        subEnterIndex++;
        if(subEnterIndex%40==0){ //每40毫秒
            SeaObject obj = nextSubmarine();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = obj;
        }
    }
    
    private int mineEnterIndex = 0;
    
    public void MineEnterAction(){
        mineEnterIndex++;
        if(mineEnterIndex%100==0){
            //如下代码暂时搁置-----第8天才讲到
        }
    }

    
    public void action(){
        Timer timer = new Timer();
        int interval = 10;
        timer.schedule(new TimerTask() {
            public void run() {
                submarineEnterAction(); //潜艇(侦察、水雷、鱼雷)入场
                MineEnterAction();      //水雷入场
                repaint();
            }
        }, interval, interval);
    }

    public void paint(Graphics g){
        Images.sea.paintIcon(null,g,0,0); //画海洋图
        ship.paintImage(g);
        for(int i=0;i 

3、海洋对象移动

完整代码:

package day06;
import javax.swing.Jframe;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
//整个游戏世界
public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;

    private Battleship ship = new Battleship(); //战舰
    private SeaObject[] submarines = {}; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)
    private Mine[] mines = {}; //水雷
    private Bomb[] bombs = {}; //深水炸弹


    
    public SeaObject nextSubmarine(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<10){
            return new ObserveSubmarine();
        }else if(type<15){
            return new TorpedoSubmarine();
        }else{
            return new MineSubmarine();
        }
    }

    private int subEnterIndex = 0;
    
    public void submarineEnterAction(){ //每10毫秒走一次
        subEnterIndex++;
        if(subEnterIndex%40==0){ //每40毫秒
            SeaObject obj = nextSubmarine();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = obj;
        }
    }
    
    private int mineEnterIndex = 0;
    
    public void MineEnterAction(){
        mineEnterIndex++;
        if(mineEnterIndex%100==0){
            //如下代码暂时搁置-----第8天才讲到
        }
    }
    
    
    public void moveAction(){
        for(int i=0;i 
七、第七天 

1、深水炸弹入场

完整代码:

package day07;
import javax.swing.ImageIcon;
//战舰
public class Battleship extends SeaObject {
    private int life;
    public Battleship(){
        super(66,26,270,124,20);
        life = 5;
    }

    public void move(){
        System.out.println("战舰移动");
    }

    public ImageIcon getImage(){
        return Images.battleship;
    }

    public Bomb shoot(){
        return new Bomb(this.x,this.y);
    }
}

package day06;
import javax.swing.Jframe;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//整个游戏世界
public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;

    private Battleship ship = new Battleship(); //战舰
    private SeaObject[] submarines = {}; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)
    private Mine[] mines = {}; //水雷
    private Bomb[] bombs = {}; //深水炸弹


    
    public SeaObject nextSubmarine(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<10){
            return new ObserveSubmarine();
        }else if(type<15){
            return new TorpedoSubmarine();
        }else{
            return new MineSubmarine();
        }
    }

    private int subEnterIndex = 0;
    
    public void submarineEnterAction(){ //每10毫秒走一次
        subEnterIndex++;
        if(subEnterIndex%40==0){ //每40毫秒
            SeaObject obj = nextSubmarine();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = obj;
        }
    }
    
    private int mineEnterIndex = 0;
    
    public void MineEnterAction(){
        mineEnterIndex++;
        if(mineEnterIndex%100==0){
            //如下代码暂时搁置-----第8天才讲到
        }
    }
    
    
    public void moveAction(){
        for(int i=0;i 

2、战舰移动

完整代码:

package day07;
import javax.swing.ImageIcon;
//战舰
public class Battleship extends SeaObject {
    private int life;
    public Battleship(){
        super(66,26,270,124,20);
        life = 5;
    }

    public void move(){
        System.out.println("战舰移动");
    }

    public ImageIcon getImage(){
        return Images.battleship;
    }

    public Bomb shoot(){
        return new Bomb(this.x,this.y);
    }

    public void moveLeft(){
        x-=speed;
    }
    public void moveRight(){
        x+=speed;
    }
}

package day07;
import javax.swing.Jframe;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//整个游戏世界
public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;

    private Battleship ship = new Battleship(); //战舰
    private SeaObject[] submarines = {}; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)
    private Mine[] mines = {}; //水雷
    private Bomb[] bombs = {}; //深水炸弹


    
    public SeaObject nextSubmarine(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<10){
            return new ObserveSubmarine();
        }else if(type<15){
            return new TorpedoSubmarine();
        }else{
            return new MineSubmarine();
        }
    }

    private int subEnterIndex = 0;
    
    public void submarineEnterAction(){ //每10毫秒走一次
        subEnterIndex++;
        if(subEnterIndex%40==0){ //每40毫秒
            SeaObject obj = nextSubmarine();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = obj;
        }
    }
    
    private int mineEnterIndex = 0;
    
    public void MineEnterAction(){
        mineEnterIndex++;
        if(mineEnterIndex%100==0){
            //如下代码暂时搁置-----第8天才讲到
        }
    }
    
    
    public void moveAction(){
        for(int i=0;i 

3、删除越界的海洋对象

完整代码:

package day07;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.util.Random;
//海洋对象
public abstract class SeaObject {
    public static final int LIVE = 0;
    public static final int DEAD = 1;
    protected int state = LIVE; //当前状态

    protected int width;
    protected int height;
    protected int x;
    protected int y;
    protected int speed;
    public SeaObject(int width,int height){
        this.width = width;
        this.height = height;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(World.HEIGHT-height-150)+150;
        speed = rand.nextInt(3)+1;
    }
    public SeaObject(int width,int height,int x,int y,int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public abstract void move();

    public abstract ImageIcon getImage();

    public boolean isLive(){
        return state==LIVE;
    }
    public boolean isDead(){
        return state==DEAD;
    }

    public void paintImage(Graphics g){
        if(isLive()){
            this.getImage().paintIcon(null,g,x,y);
        }
    }

    
    public boolean isOutOfBounds(){
        return x>= World.WIDTH; //x>=窗口宽,即为越界了
    }
}

package day07;
import javax.swing.ImageIcon;
//水雷
public class Mine extends SeaObject {
    public Mine(int x,int y){
        super(11,11,x,y,1);
    }

    public void move(){
        y-=speed;
    }

    public ImageIcon getImage(){
        return Images.mine;
    }

    
    public boolean isOutOfBounds(){
        return y<=150-height; //y<=150减去水雷的高,即为越界了(到水面)
    }
}

package day07;
import javax.swing.ImageIcon;
//深水炸弹
public class Bomb extends SeaObject {
    public Bomb(int x,int y){
        super(9,12,x,y,3);
    }

    public void move(){
        y+=speed;
    }

    public ImageIcon getImage(){
        return Images.bomb;
    }

    
    public boolean isOutOfBounds(){
        return y>= World.HEIGHT; //y>=窗口的高,即为越界了
    }
}

package day07;
import javax.swing.Jframe;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//整个游戏世界
public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;

    private Battleship ship = new Battleship(); //战舰
    private SeaObject[] submarines = {}; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)
    private Mine[] mines = {}; //水雷
    private Bomb[] bombs = {}; //深水炸弹


    
    public SeaObject nextSubmarine(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<10){
            return new ObserveSubmarine();
        }else if(type<15){
            return new TorpedoSubmarine();
        }else{
            return new MineSubmarine();
        }
    }

    private int subEnterIndex = 0;
    
    public void submarineEnterAction(){ //每10毫秒走一次
        subEnterIndex++;
        if(subEnterIndex%40==0){ //每40毫秒
            SeaObject obj = nextSubmarine();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = obj;
        }
    }
    
    private int mineEnterIndex = 0;
    
    public void MineEnterAction(){
        mineEnterIndex++;
        if(mineEnterIndex%100==0){
            //如下代码暂时搁置-----第8天才讲到
        }
    }
    
    
    public void moveAction(){
        for(int i=0;i 

4、设计EnemyScore接口、EnemyLife接口

完整代码:

package day07;
//得分接口
public interface EnemyScore {
    public int getScore();
}

package day07;
//奖励接口
public interface EnemyLife {
    public int getLife();
}

package day07;
import javax.swing.ImageIcon;
//侦察潜艇
public class ObserveSubmarine extends SeaObject implements EnemyScore {
    public ObserveSubmarine(){
        super(63,19);
    }

    public void move(){
        x+=speed;
    }

    public ImageIcon getImage(){
        return Images.obsersubm;
    }

    public int getScore(){
        return 10;
    }
}

package day07;
import javax.swing.ImageIcon;
//鱼雷潜艇
public class TorpedoSubmarine extends SeaObject implements EnemyScore {
    public TorpedoSubmarine(){
        super(64,20);
    }

    public void move(){
        x+=speed;
    }

    public ImageIcon getImage(){
        return Images.torpesubm;
    }

    public int getScore(){
        return 40;
    }
}

package day07;
import javax.swing.ImageIcon;
//水雷潜艇
public class MineSubmarine extends SeaObject implements EnemyLife {
    public MineSubmarine(){
        super(63,19);
    }

    public void move(){
        x+=speed;
    }

    public ImageIcon getImage(){
        return Images.minesubm;
    }

    public Mine shootMine(){
        int x = this.x+this.width; //x:潜艇的x+潜艇的宽
        int y = this.y-11;          //y:潜艇的y-11
        return new Mine(x,y);
    }

    public int getLife(){
        return 1;
    }
}

//注:其余类没有变化,此处省略
八、第八天

1、水雷入场:后半段

参考代码:

package cn.tedu.submarine;
import javax.swing.Jframe;
import javax.swing.JPanel; //1.
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
import java.util.Arrays;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


public class World extends JPanel { //2.
    public static final int WIDTH = 641;  
    public static final int HEIGHT = 479; 

    //如下这一堆为窗口中所显示的对象
    private Battleship ship = new Battleship(); //战舰
    private SeaObject[] submarines = {};        //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)数组
    private Mine[] mines = {};                  //水雷数组
    private Bomb[] bombs = {};                  //深水炸弹数组

    
    public SeaObject nextSubmarine(){
        Random rand = new Random(); //随机数对象
        int type = rand.nextInt(20); //0到19
        if(type<10){ //0到9时,返回侦察潜艇对象
            return new ObserveSubmarine();
        }else if(type<15){ //10到14,返回鱼雷潜艇对象
            return new TorpedoSubmarine();
        }else{ //15到19时,返回水雷潜艇对象
            return new MineSubmarine();
        }
    }

    private int subEnterIndex = 0; //潜艇入场计数
    
    public void submarineEnterAction(){ //每10毫秒走一次
        subEnterIndex++; //每10毫秒增1
        if(subEnterIndex%40==0) { //每400(40*10)毫秒走一次
            SeaObject obj = nextSubmarine(); //获取潜艇对象
            submarines = Arrays.copyOf(submarines,submarines.length+1); //扩容
            submarines[submarines.length-1] = obj; //将obj添加到submarines的最后一个元素上
        }
    }

    private int mineEnterIndex = 0; //水雷入场计数
    
    public void mineEnterAction(){ //每10毫秒走一次
        mineEnterIndex++; //每10毫秒增1
        if(mineEnterIndex%100==0){ //每1秒(100*10)走一次
            for(int i=0;i 

2、深水炸弹与潜艇的碰撞

参考代码:

package day08;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.util.Random;
//海洋对象
public abstract class SeaObject {
    public static final int LIVE = 0;
    public static final int DEAD = 1;
    protected int state = LIVE; //当前状态

    protected int width;
    protected int height;
    protected int x;
    protected int y;
    protected int speed;
    public SeaObject(int width,int height){
        this.width = width;
        this.height = height;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(World.HEIGHT-height-150)+150;
        speed = rand.nextInt(3)+1;
    }
    public SeaObject(int width,int height,int x,int y,int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public abstract void move();

    public abstract ImageIcon getImage();

    public boolean isLive(){
        return state==LIVE;
    }
    public boolean isDead(){
        return state==DEAD;
    }

    public void paintImage(Graphics g){
        if(isLive()){
            this.getImage().paintIcon(null,g,x,y);
        }
    }

    
    public boolean isOutOfBounds(){
        return x>= World.WIDTH; //x>=窗口宽,即为越界了
    }
    
    
    public boolean isHit(SeaObject other){
        //假设this指代潜艇,other指代炸弹
        int x1 = this.x-other.width;  //x1:潜艇的x-炸弹的宽
        int x2 = this.x+this.width;   //x2:潜艇的x+潜艇的宽
        int y1 = this.y-other.height; //y1:潜艇的y-炸弹的高
        int y2 = this.y+this.height;  //y2:潜艇的y+潜艇的高
        int x = other.x; //x:炸弹的x
        int y = other.y; //y:炸弹的y

        return x>=x1 && x<=x2
               &&
               y>=y1 && y<=y2; //x在x1与x2之间,并且,y在y1与y2之间,即为撞上了
    }

    
    public void goDead(){
        state = DEAD; //将状态修改为DEAD死了的
    }
}

package day08;
import javax.swing.ImageIcon;
//战舰
public class Battleship extends SeaObject {
    private int life;
    public Battleship(){
        super(66,26,270,124,20);
        life = 5;
    }

    public void step(){
        System.out.println("战舰移动");
    }

    public ImageIcon getImage(){
        return Images.battleship;
    }

    public Bomb shoot(){
        return new Bomb(this.x,this.y);
    }

    public void moveLeft(){
        x-=speed;
    }
    public void moveRight(){
        x+=speed;
    }
    
    public void addLife(int num){
        life += num;
    }
}

package day08;
import javax.swing.Jframe;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//整个游戏世界
public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;

    private Battleship ship = new Battleship(); //战舰
    private SeaObject[] submarines = {}; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)
    private Mine[] mines = {}; //水雷
    private Bomb[] bombs = {}; //深水炸弹


    
    public SeaObject nextSubmarine(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<10){
            return new ObserveSubmarine();
        }else if(type<15){
            return new TorpedoSubmarine();
        }else{
            return new MineSubmarine();
        }
    }

    private int subEnterIndex = 0;
    
    public void submarineEnterAction(){ //每10毫秒走一次
        subEnterIndex++;
        if(subEnterIndex%40==0){ //每40毫秒
            SeaObject obj = nextSubmarine();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = obj;
        }
    }
    
    private int mineEnterIndex = 0;
    
    public void MineEnterAction(){
        mineEnterIndex++;
        if(mineEnterIndex%100==0){
            for(int i=0;i 

3、画分和画命

参考代码:

package day08;
import javax.swing.ImageIcon;
//战舰
public class Battleship extends SeaObject {
    private int life;
    public Battleship(){
        super(66,26,270,124,20);
        life = 5;
    }

    public void step(){
        System.out.println("战舰移动");
    }

    public ImageIcon getImage(){
        return Images.battleship;
    }

    public Bomb shoot(){
        return new Bomb(this.x,this.y);
    }

    public void moveLeft(){
        x-=speed;
    }
    public void moveRight(){
        x+=speed;
    }

    public void addLife(int num){
        life += num;
    }
    
    public int getLife(){
        return life;
    }
}

package day08;
import javax.swing.Jframe;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//整个游戏世界
public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;

    private Battleship ship = new Battleship(); //战舰
    private SeaObject[] submarines = {}; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)
    private Mine[] mines = {}; //水雷
    private Bomb[] bombs = {}; //深水炸弹


    
    public SeaObject nextSubmarine(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<10){
            return new ObserveSubmarine();
        }else if(type<15){
            return new TorpedoSubmarine();
        }else{
            return new MineSubmarine();
        }
    }

    private int subEnterIndex = 0;
    
    public void submarineEnterAction(){ //每10毫秒走一次
        subEnterIndex++;
        if(subEnterIndex%40==0){ //每40毫秒
            SeaObject obj = nextSubmarine();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = obj;
        }
    }
    
    private int mineEnterIndex = 0;
    
    public void MineEnterAction(){
        mineEnterIndex++;
        if(mineEnterIndex%100==0){
            for(int i=0;i 
九、第九天 

1、水雷与战舰的碰撞

完整代码:

package day09;
import javax.swing.ImageIcon;
//战舰
public class Battleship extends SeaObject {
    private int life;
    public Battleship(){
        super(66,26,270,124,20);
        life = 5;
    }

    public void step(){
        
    }

    public ImageIcon getImage(){
        return Images.battleship;
    }

    public Bomb shoot(){
        return new Bomb(this.x,this.y);
    }

    public void moveLeft(){
        x-=speed;
    }
    public void moveRight(){
        x+=speed;
    }

    public void addLife(int num){
        life += num;
    }
    
    public int getLife(){
        return life;
    }
    
    public void subtractLife(){
        life--;
    }
}

package day09;
import javax.swing.Jframe;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//整个游戏世界
public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;

    private Battleship ship = new Battleship(); //战舰
    private SeaObject[] submarines = {}; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)
    private Mine[] mines = {}; //水雷
    private Bomb[] bombs = {}; //深水炸弹


    
    public SeaObject nextSubmarine(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<10){
            return new ObserveSubmarine();
        }else if(type<15){
            return new TorpedoSubmarine();
        }else{
            return new MineSubmarine();
        }
    }

    private int subEnterIndex = 0;
    
    public void submarineEnterAction(){ //每10毫秒走一次
        subEnterIndex++;
        if(subEnterIndex%40==0){ //每40毫秒
            SeaObject obj = nextSubmarine();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = obj;
        }
    }
    
    private int mineEnterIndex = 0;
    
    public void MineEnterAction(){
        mineEnterIndex++;
        if(mineEnterIndex%100==0){
            for(int i=0;i 

2、检测游戏结束

完整代码:

package day09;
import javax.swing.Jframe;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//整个游戏世界
public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;

    private Battleship ship = new Battleship(); //战舰
    private SeaObject[] submarines = {}; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)
    private Mine[] mines = {}; //水雷
    private Bomb[] bombs = {}; //深水炸弹


    
    public SeaObject nextSubmarine(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<10){
            return new ObserveSubmarine();
        }else if(type<15){
            return new TorpedoSubmarine();
        }else{
            return new MineSubmarine();
        }
    }

    private int subEnterIndex = 0;
    
    public void submarineEnterAction(){ //每10毫秒走一次
        subEnterIndex++;
        if(subEnterIndex%40==0){ //每40毫秒
            SeaObject obj = nextSubmarine();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = obj;
        }
    }
    
    private int mineEnterIndex = 0;
    
    public void MineEnterAction(){
        mineEnterIndex++;
        if(mineEnterIndex%100==0){
            for(int i=0;i 

3、画状态

完整代码:

package day09;
import javax.swing.ImageIcon;
//图片类
public class Images {
    public static ImageIcon battleship; //战舰
    public static ImageIcon bomb;       //深水炸弹
    public static ImageIcon obsersubm;  //侦察潜艇
    public static ImageIcon torpesubm;  //鱼雷潜艇
    public static ImageIcon minesubm;   //水雷潜艇
    public static ImageIcon torpedo;    //鱼雷
    public static ImageIcon mine;       //水雷
    public static ImageIcon sea;       //海洋图
    public static ImageIcon gameover;   //游戏结束图
    static{
        battleship = new ImageIcon("img/battleship.png");
        bomb = new ImageIcon("img/bomb.png");
        obsersubm = new ImageIcon("img/obsersubm.png");
        torpesubm = new ImageIcon("img/torpesubm.png");
        minesubm = new ImageIcon("img/minesubm.png");
        torpedo = new ImageIcon("img/torpedo.png");
        mine = new ImageIcon("img/mine.png");
        sea = new ImageIcon("img/sea.png");
        gameover = new ImageIcon("img/gameover.png");
    }

    public static void main(String[] args){
        System.out.println(battleship.getImageLoadStatus());
        System.out.println(bomb.getImageLoadStatus());
        System.out.println(obsersubm.getImageLoadStatus());
        System.out.println(torpesubm.getImageLoadStatus());
        System.out.println(minesubm.getImageLoadStatus());
        System.out.println(torpedo.getImageLoadStatus());
        System.out.println(mine.getImageLoadStatus());
        System.out.println(gameover.getImageLoadStatus());
    }
}

package day09;
import javax.swing.Jframe;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//整个游戏世界
public class World extends JPanel {
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;

    public static final int RUNNING = 0;
    public static final int GAME_OVER = 1;
    private int state = RUNNING;

    private Battleship ship = new Battleship(); //战舰
    private SeaObject[] submarines = {}; //潜艇(侦察潜艇、鱼雷潜艇、水雷潜艇)
    private Mine[] mines = {}; //水雷
    private Bomb[] bombs = {}; //深水炸弹


    public SeaObject nextSubmarine(){
        Random rand = new Random();
        int type = rand.nextInt(20);
        if(type<10){
            return new ObserveSubmarine();
        }else if(type<15){
            return new TorpedoSubmarine();
        }else{
            return new MineSubmarine();
        }
    }

    private int subEnterIndex = 0;
    public void submarineEnterAction(){ //每10毫秒走一次
        subEnterIndex++;
        if(subEnterIndex%40==0){
            SeaObject obj = nextSubmarine();
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = obj;
        }
    }

    private int mineEnterIndex = 0;
    
    public void MineEnterAction(){
        mineEnterIndex++;
        if(mineEnterIndex%100==0){
            for(int i=0;i 

4、运行界面

游戏启动

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-maqPrWwC-1642769396433)(C:UsersTEDUDesktopQQ截图20220121204822.png)]

游戏结束

      if(b.isLive() && s.isLive() && s.isHit(b)){
                b.goDead();
                s.goDead();
                if(s instanceof EnemyScore){
                    EnemyScore es = (EnemyScore)s;
                    score += es.getScore();
                }
                if(s instanceof EnemyLife){
                    EnemyLife ea = (EnemyLife)s;
                    int num = ea.getLife();
                    ship.addLife(num);
                }
            }
        }
    }
}

//水雷与战舰的碰撞
public void mineBangAction(){
    for(int i=0;i 

}

//注:其余类没有变化,此处省略

源代码在我的资源中

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/711213.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号