潜艇游戏总结

1.创建SeaObject超类,设计6个类继承超类

  • 这样子类里就不需要定义共有的属性了,只需要在SeaObject中定义一份,子类去继承即可获得属性
  • 三种潜艇的宽和高不同,其他值在超类的构造方法中定义,不同的潜艇子类去调用构造方法,只需要传入宽和高即可;而战舰和水雷和炸弹则需要传入宽、高、x、y和速度
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+1)+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;
    }
//战舰
public class Battleship extends SeaObject{
    int life;
    Battleship(){
        super(66,26,270,124,2);
        life=5;
    }
    void move(){
        System.out.println("战舰在移动");
    }
}

//水雷
public class Mine extends SeaObject{
    Mine(int x,int y){
        super(11,11,x,y,1);
    }
    void move(){
        System.out.println("水雷y向上移动");
    }
}

//炸弹
public class Bomb extends SeaObject{
    Bomb(int x,int y){//需传入x和y确定坐标值
        super(9,12,x,y,3);
    }
    void move(){
        System.out.println("炸弹y向下移动");
    }
}

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

2.向上造型和画窗口

//向上造型
 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<submarines.length;i++){
     SeaObject s = submarines[i];
     System.out.println(s.x+","+s.y);
     s.move();
 }

//画窗口
public class World extends JPanel {
    public static void main(String[] args) {
       JFrame frame=new JFrame();
       World world=new World();
       frame.setFocusable(true);
       frame.add(world);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setSize(641+16,479+39);
       frame.setLocationRelativeTo(null);
       frame.setVisible(true);
    }
}

3.给类成员添加访问修饰符

属性用protected修饰,子类可以使用

public class SeaObject {
    protected int width;
    protected int speed;
    public SeaObject(int width,int height){
	XXXX	
	}

4.设计Images图片类

public class Images {
    static ImageIcon battleship;
    static ImageIcon sea;
    static ImageIcon gameover;
    static {
        battleship=new ImageIcon("img/battleship.png");
        sea=new ImageIcon("img/sea.png");
        gameover=new ImageIcon("img/gameover.png");
    }
	//main是自测使用
    public static void main(String[] args) {
        System.out.println(battleship.getImageLoadStatus());
        System.out.println(sea.getImageLoadStatus());
        System.out.println(gameover.getImageLoadStatus());
    }
}

5.使用抽象方法获取图片,在超类中定义画图的方法

//在SeaObject类中定义
public abstract ImageIcon getImage();
public void paintImage(Graphics g){
    this.getImage().paintIcon(null,g,x,y);
}

6.判断游戏内对象的生死

在SeaObject类中定义

public static final int LIVE=0;
public static final int DEAD=1;
protected int state=LIVE;
public boolean isLive(){
	return state==LIVE;
}
public boolean isDead(){
	return state==DEAD;
}

你可能感兴趣的:(项目总结,java)