飞机大战(2)

fxml 布局文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>

<?import javafx.scene.*?>

<?import javafx.scene.control.*?>

<?import javafx.scene.effect.*?>

<?import javafx.scene.layout.*?>

<?import javafx.scene.text.*?>

<BorderPane xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1">

<top>

  <Label style="-fx-font-family: '等线 Light';" text="经典小游戏" textAlignment="CENTER" textFill="#0800e3" BorderPane.alignment="CENTER">

    <BorderPane.margin>

      <Insets top="100.0" />

    </BorderPane.margin>

    <font>

      <Font size="49.0" />

    </font>

    <opaqueInsets>

      <Insets />

    </opaqueInsets>

    <effect>

      <Glow />

    </effect>

  </Label>

</top>

<center>

  <Button fx:id="plane" defaultButton="true" graphicTextGap="0.0" mnemonicParsing="false" text="飞机大战" textAlignment="CENTER" textFill="#1a10d7" BorderPane.alignment="TOP_CENTER">

    <BorderPane.margin>

      <Insets top="150.0" />

    </BorderPane.margin>

    <font>

      <Font name="System Bold" size="33.0" />

    </font>

    <cursor>

      <Cursor fx:constant="HAND" />

    </cursor>

  </Button>

</center>

<bottom>

  <Button defaultButton="true" graphicTextGap="0.0" mnemonicParsing="false" text="FlyBird" textAlignment="CENTER" textFill="#1a10d7" BorderPane.alignment="TOP_CENTER">

    <BorderPane.margin>

      <Insets bottom="150" />

    </BorderPane.margin>

    <font>

      <Font name="System Bold" size="35.0" />

    </font>

    <cursor>

      <Cursor fx:constant="HAND" />

    </cursor>

  </Button>

</bottom>

</BorderPane>

利用面向对象的思想,来封装具体的实体,因为所有的实体都具有通用的属性和行为,则抽为基类定义。

/**

* 飞行物基类

*/

public abstract class FlyingObject {

  public static final int ALIVE = 0;//活着

  public static final int DEAD = 1;//死亡

  public static final int DELETABLE = 2;//可删除

  public int state = ALIVE;//状态

  public Image image;

  public int x;

  public int y;

  //英雄机或子弹构造

  public FlyingObject(Image image, int x, int y) {

    this.image = image;

    this.x = x;

    this.y = y;

  }

  //敌机构造

  public FlyingObject(Image image) {

    this.image = image;

    int w1 = ShootGame.WIDTH;//屏幕宽

    int w2 = (int) this.image.getWidth();

    int h2 = (int) this.image.getHeight();

    Random random = new Random();

    this.x = random.nextInt(w1 - w2);

    this.y = h2 / 2;

  }

  public int getState() {

    return state;

  }

  public void setState(int state) {

    this.state = state;

  }

  public Image getImage() {

    return image;

  }

  public void setImage(Image image) {

    this.image = image;

  }

  public int getX() {

    return x;

  }

  public void setX(int x) {

    this.x = x;

  }

  public int getY() {

    return y;

  }

  public void setY(int y) {

    this.y = y;

  }

  int index = 1;

  public void setImage(Image[] images) {

    if (state == ALIVE) {

      image = images[0];

    } else if (state == DEAD) {

      image = images[index];

      index++;

      if (index == images.length) {

        state = DELETABLE;

      }

    } else {

      image = null;

    }

  }

  public abstract void setImage();

}

再通过继承方式,再定义自己独特的行为及属性

/**

* 英雄机

*/

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容