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="經(jīng)典小游戲" 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="飛機(jī)大戰(zhàn)" 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>
利用面向?qū)ο蟮乃枷?,?lái)封裝具體的實(shí)體,因?yàn)樗械膶?shí)體都具有通用的屬性和行為,則抽為基類定義。
/**
* 飛行物基類
*/
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;//狀態(tài)
? public Image image;
? public int x;
? public int y;
? //英雄機(jī)或子彈構(gòu)造
? public FlyingObject(Image image, int x, int y) {
? ? this.image = image;
? ? this.x = x;
? ? this.y = y;
? }
? //敵機(jī)構(gòu)造
? 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();
}
再通過(guò)繼承方式,再定義自己獨(dú)特的行為及屬性
/**
* 英雄機(jī)
*/