飛機(jī)大戰(zhàn)(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="經(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ī)

*/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容