潜艇大作战

游戏测试类

package Submarine03;

import javax.crypto.SealedObject;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class StartGame extends JPanel {
    public static final int READAY = 0;
    public static final int GAMEOVER = 1;
    private int state = READAY;
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;
    BattleShip bts = new BattleShip();
    SaeSubmarine[] ssb = {};
    Mine[] mines = {};
    Bomb[] bombs = {};


    //=======================================================================================================================
    //水雷碰战舰 --结束
    private void mmBbts() {
        for (int i = 0; i < mines.length; i++) {
            if (mines[i].isLive() && bts.isLive() && bts.collideAction(mines[i])) {
                mines[i].colliDend();
                bts.minusLife();
                if (bts.getLife() == 0) {//-------------------判断生命值,改变状态,结束
                    state = GAMEOVER;
                }
            }
        }
    }
    //碰撞--@1消失+@2死亡+@3得分
    int score = 0;
    private void bobBssb() {
        for (int j = 0; j < bombs.length; j++) {
            for (int i = 0; i < ssb.length; i++) {
                if (bombs[j].isLive() && ssb[i].isLive() && ssb[i].collideAction(bombs[j])) {
                    bombs[j].colliDend();
                    ssb[i].colliDend();
                    if (ssb[i] instanceof InputLive) {//----------------------------获得生命值
                        InputLive msb = (InputLive) ssb[i];
                        bts.addLife(msb.inputLive());
                    } else if (ssb[i] instanceof InputScore) {//-----------------获得分数
                        InputScore msb2 = (InputScore) ssb[i];
                        score += msb2.inputScore();
                    }
                }
            }
        }
    }

    //数据清除
    private void outBound() {
        for (int i = 0; i < ssb.length; i++) {
            if (ssb[i].outOfBounds() || ssb[i].isLDend()) {
                ssb[i] = ssb[ssb.length - 1];
                ssb = Arrays.copyOf(ssb, ssb.length - 1);
            }
        }
        for (int i = 0; i < mines.length; i++) {
            if (mines[i].outOfBounds() || mines[i].isLDend()) {
                mines[i] = mines[mines.length - 1];
                mines = Arrays.copyOf(mines, mines.length - 1);
            }
        }
        for (int i = 0; i < bombs.length; i++) {
            if (bombs[i].outOfBounds() || bombs[i].isLDend()) {
                bombs[i] = bombs[bombs.length - 1];
                bombs = Arrays.copyOf(bombs, bombs.length - 1);
            }
        }
    }

    //移动
    private void omtMove() {
        for (int i = 0; i < ssb.length; i++) {
            ssb[i].move();
        }
        for (int i = 0; i < mines.length; i++) {
            mines[i].move();
        }
        for (int i = 0; i < bombs.length; i++) {
            bombs[i].move();
        }
    }
    //生成数据-定时生成 1-潜艇+水雷
    //潜艇--->@1控制种类生成概率 +@2生成频率-------------------------------------
    int temp = 0;
    private void nextSSb() {
        temp++;
        if (temp % 40 == 0) {
            SaeSubmarine ssb1 = otm();
            ssb = Arrays.copyOf(ssb, ssb.length + 1);
            ssb[ssb.length - 1] = ssb1;
        }
    }
    //@1控制种类生成概率
    public SaeSubmarine otm() {
        Random random = new Random();
        int rd = random.nextInt(20);
        if (rd < 10) {
            return new ObserverSubmarine();
        } else if (rd < 15) {
            return new TorpedoSubmarine();
        } else {
            return new MineSubmarine();
        }
    }
    //生成水雷,@1生成水雷方法+@2控制生成频率----------------------------------------------
    int temp2 = 0;
    private void nextMines() {
        temp2++;
        if (temp2 % 100 == 0) {
            for (int i = 0; i < ssb.length; i++) {//水雷是根据水雷潜艇出生
                if (ssb[i] instanceof MineSubmarine) {
                    Mine msm = ((MineSubmarine) ssb[i]).NextMine();//调用生成方法
                    mines = Arrays.copyOf(mines, mines.length + 1);
                    mines[mines.length - 1] = msm;
                }
            }
        }
    }

    //启动游戏,@1定时器+@2事件监听器
    private void subAction() {
        //@2事件监听器
        KeyAdapter k = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_SPACE) {//按下键盘生成炸弹,生成数据-事件生成 2-炸弹
                    Bomb bb = bts.nextBome();
                    bombs = Arrays.copyOf(bombs, bombs.length + 1);//数组扩容
                    bombs[bombs.length - 1] = bb;
                } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                    bts.liftMove();
                } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    bts.rightMove();
                }
            }
        };
        this.addKeyListener(k);
        
        //@1定时器
        Timer timer = new Timer();
        int timi = 10;//定个10毫秒一次
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                nextSSb();//生成潜艇----->潜艇是多种,控制每种潜艇的出生率,+生成潜艇
                nextMines();//生成水雷
                //移动+事件战舰炸弹移动
                omtMove();
                outBound();//溢出数据清除
                bobBssb();//碰撞消失得分得命
                mmBbts();//战舰碰水雷
            }
        }, timi, timi);
    }

    //重写画画,显示游戏
    public void paint(Graphics g) {
        switch (state) {
            case READAY:
                images.sea.paintIcon(null, g, 0, 0);
                bts.PantImage(g);
                //画潜艇
                for (int i = 0; i < ssb.length; i++) {
                    ssb[i].PantImage(g);
                }
                //画水雷
                for (int i = 0; i < mines.length; i++) {
                    mines[i].PantImage(g);
                }
                //画炸弹
                for (int i = 0; i < bombs.length; i++) {
                    bombs[i].PantImage(g);
                }
                g.drawString("SCORE" + score, 200, 50);
                g.drawString("LIVE" + bts.getLife(), 400, 50);
                repaint();
                break;
            case GAMEOVER:
                images.Gameover.paintIcon(null, g, 0, 0);
                break;
        }
    }

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        StartGame start = new StartGame();
        start.setFocusable(true);
        jFrame.add(start);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setSize(WIDTH + 16, HEIGHT + 39);
        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);//,显示视图,2调用paint画画

        start.subAction();//-------------启动
    }
}

超类

package Submarine03;

import Submarine04.SubmarineObject;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public abstract class SaeSubmarine {
    public static final int LIVE = 0;
    public static final int DEND = 1;
    public int state = LIVE;
    int width;
    int height;
    int x;
    int y;
    int speed;

    SaeSubmarine(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;
    }

    SaeSubmarine(int width, int height) {
        Random random = new Random();
        this.width = width;
        this.height = height;
        x = -width;
        y = random.nextInt(479 - height - 150 + 1) + 150;
        speed = random.nextInt(3) + 1;
    }

    public abstract void move();
    //判断状态方法
    public boolean isLive() {
        return state == LIVE;
    }
    public boolean isLDend() {
        return state == DEND;
    }
    //取图的方法 第一步
    public abstract ImageIcon getImage();
    //第二步、将取到的图片画到窗口  这个对象  的  getImage(取图片)图片  的 画到 painImage
    public void PantImage(Graphics g) {
        if (isLive()) {
            this.getImage().paintIcon(null, g, this.x, this.y);
        }
    }
    //数据越界的
    public boolean outOfBounds() {
        return this.x >= SubmarineObject.Test.WIDTH;
    }
    //碰撞+   this 潜艇,ssb炸弹
    public boolean collideAction(SaeSubmarine ssb){
            int x1=this.x-ssb.width;
            int x2=this.x+this.width;
            int y1=this.y-ssb.height;
            int y2=this.y+this.height;
            int x=ssb.x;
            int y=ssb.y;
        return  x>=x1 &&x<=x2 && y>=y1 &&y<=y2;
    }
    //+消失的
    public int  colliDend(){
        return state=DEND;
    }
}

俩接口+图片类

public interface InputLive {
    public int inputLive();
}
---------------------------------------接口1
public interface InputScore {
    public int inputScore();
}
--------------------------------------接口2
public class images {
    public static ImageIcon battleship;
    public static ImageIcon minesubm;
    public static ImageIcon obsersubm;
    public static ImageIcon torpesubm;
    public static ImageIcon Gameover;
    public static ImageIcon sea;
    public static ImageIcon mine;
    public static ImageIcon bomb;

    static {
        battleship = new ImageIcon("src/img/battleship.png");
        minesubm = new ImageIcon("src/img/minesubm.png");
        obsersubm = new ImageIcon("src/img/obsersubm.png");
        torpesubm = new ImageIcon("src/img/torpesubm.png");
        Gameover = new ImageIcon("src/img/Gameover.png");
        sea = new ImageIcon("src/img/sea.png");
        mine = new ImageIcon("src/img/mine.png");
        bomb = new ImageIcon("src/img/bomb.png");
    }
}

6个子类

//战艇类
public class BattleShip extends SaeSubmarine {
    int life;
    //构造方法初始化数据
    public BattleShip() {
        super(66, 26, 270, 124, 20);
        this.life=5;
    }

    @Override
    public void move() {
        System.out.println(width+"\t"+height+"\t"+x+"\t"+y+"\t"+speed+"\t"+"发射炸弹!");
    }

    @Override
    public ImageIcon getImage() {
        return images.battleship;
    }
    public Bomb nextBome(){
        return new Bomb(this.x,this.y);
    }
    public void liftMove(){
        x-=speed;
    }
    public void rightMove(){
        x+=speed;
    }
    //获取life,显示用
    public int getLife(){
        return life;
    }
    //添加生命
    public int addLife(int num){
        return life+=num;
    }
    public int minusLife(){
        return life--;
    }
}
---------------------------------------------------------------
public class Bomb extends SaeSubmarine {
    Bomb(int x, int y) {
        super(9, 12, x, y, 3);
    }
    @Override
    public void move() {
        y+=speed;
        System.out.println(width+"\t"+height+"\t"+x+"\t"+y+"\t"+speed+"\t"+"发射炸弹!");
    }
    @Override
    public ImageIcon getImage() {
        return images.bomb;
    }
    //数据越界的
    public boolean outOfBounds() {
        return this.y >= SubmarineObject.Test.HEIGNT;
    }
}
-----------------------------------------------------------------
public class Mine extends SaeSubmarine {
    Mine(int x, int y) {
        super(9, 12, x, y, 3);
    }
    @Override
    public void move() {
        y-=speed;
        System.out.println(width+"\t"+height+"\t"+x+"\t"+y+"\t"+speed+"\t"+"发射炸弹!");
    }
    @Override
    public ImageIcon getImage() {
        return images.mine;
    }
    //数据越界的
    public boolean outOfBounds() {
        return this.y <= 150-this.height;
    }
}
-----------------------------------------------------------------
public class ObserverSubmarine extends  SaeSubmarine implements InputScore {
    public ObserverSubmarine() {
        super(63, 19);
    }
    @Override
    public void move() {
        x+=speed;
        System.out.println(width+"\t"+height+"\t"+x+"\t"+y+"\t"+speed+"\t"+"发射炸弹!");
    }
      /*
        侦察潜艇: 宽63高19,x设置初始 为-x, y位置是随机的,速度
     */
    @Override
    public ImageIcon getImage() {
        return images.obsersubm;
    }
    @Override
    public int inputScore() {
        return 10;
    }
}
-----------------------------------------------------------------
public class TorpedoSubmarine extends SaeSubmarine implements InputScore {
    public TorpedoSubmarine() {
        super(64, 20);
    }
    @Override
    public void move() {
        x+=speed;
        System.out.println(width+"\t"+height+"\t"+x+"\t"+y+"\t"+speed+"\t"+"发射炸弹!");
    }
    /*
        鱼雷潜艇: 宽63高19,x设置初始 为-x, y位置是随机的,速度
     */
    @Override
    public ImageIcon getImage() {
        return images.torpesubm;
    }
    @Override
    public int inputScore() {
        return 40;
    }
}
----------------------------------------------------------------
public class MineSubmarine extends SaeSubmarine implements InputLive {
    /*
        水雷潜艇: 宽63高19,x设置初始 为-x, y位置是随机的,速度
     */
    public MineSubmarine() {
        super(63, 19);
    }
    @Override
    public void move() {
        x+=speed;
        System.out.println(width+"\t"+height+"\t"+x+"\t"+y+"\t"+speed+"\t"+"发射炸弹!");
    }
    @Override
    public ImageIcon getImage() {
        return images.minesubm;
    }

    public Mine NextMine(){
        int x = this.x+this.width; //x:水雷潜艇的x+水雷潜艇的宽
        int y = this.y-5;          //y:水雷潜艇的y-固定的5
        return new Mine(x,y); //返回水雷对象
    }
    @Override
    public int inputLive() {
        return 1;
    }
}

学自王克晶老师:
https://blog.csdn.net/jason13579?type=blog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值