Java源码实现《植物大战僵尸》

前言

学Java的朋友们,福利来了,今天小编给大家带来了一款 植物大战僵尸源码,看图:

视频演示

环境JDK1.8

类继承UML图

源码实现

我们先从main函数看起,继承了javafx.application.Application。JavaFx是Java图形化界面技术AWT、Swing技术的新替代品。

通过main()执行Application的launch(String str)方法,当然launch(String str)方法不传入任何值也是可以执行的.launch(String str)方法会默认执行本类下的init()、start()、stop()方法。执行下面的main()方法后显示顺序为:

这是初始化方法➡这是start()方法➡这是stop()方法➡这是main()方法。

复制代码
public class Main extends Application {
    // 完整源码: gitee店康姆/hadluo/java_code.git
    public static MediaPlayer mediaPlayer;
    private static Database currentd;

    @Override
    public void start(Stage primaryStage) throws Exception{
        // 程序启动执行
        deserialize();
        addMusic();
        Parent mainPage=FXMLLoader.load(getClass().getResource("MainPage.fxml"));
        Scene scene = new Scene(mainPage,1024,600);
        primaryStage.setTitle("Plants VS Zombies");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void addMusic() {
        Media sound = new Media(getClass().getResource("/assets/background.wav").toString());
        mediaPlayer = new MediaPlayer(sound);
        mediaPlayer.setAutoPlay(true);
        mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
        mediaPlayer.setStartTime(Duration.seconds(0));
        mediaPlayer.setStopTime(Duration.seconds(50));
        mediaPlayer.play();
    }

    public static Database getDatabase(){
        return currentd;
    }
    public static void serialize() throws IOException {
        ObjectOutputStream out=null;
        try {
            out = new ObjectOutputStream (new FileOutputStream("database.txt"));
            out.writeObject(currentd);
        }
        finally {
            out.close();
            //System.out.println("Saved!");
            System.exit(0);
        }
    }
    public static void deserialize() throws ClassNotFoundException, FileNotFoundException, IOException{
        ObjectInputStream in = null;
        try {
            in=new ObjectInputStream (new FileInputStream("database.txt"));
            currentd=(Database) in.readObject();
            in.close();
        }
        catch (FileNotFoundException e){
            currentd=new Database();
        }
        catch (NullPointerException e) {
            currentd=new Database();
            //System.out.println("This user does not exist in the database");
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
// 完整源码: gitee店康姆/hadluo/java_code.git

结尾语

我是分享好物+教程+源码的老罗,欢迎关注!

相关推荐
sharemylove5 小时前
Java集合框架全面详解
java·开发语言·经验分享·笔记·学习
FYKJ_20105 小时前
springboot众创项目团队管理系统94666-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·spark·课程设计
摇滚侠7 小时前
《On Java 中文版 基础卷》阅读笔记 内部类 20
java·笔记·python
郝学胜-神的一滴9 小时前
Effective Python 条款43:自定义容器类
开发语言·windows·python·pandas·scikit-learn·pyqt
FYKJ_20109 小时前
springboot贫困生认定管理系统59009-计算机毕业设计、课程设计
java·javascript·vue.js·spring boot·后端·django·课程设计
考虑考虑10 小时前
JDK27中的结构化并发
java·后端·java ee
好焊孙辉10 小时前
一条产线省下 30 个电机位:Z 轴和 R 轴,该合成一个了
开发语言·r语言
布莱克60510 小时前
Swift 中的逃逸闭包与非逃逸闭包:@escaping 到底在做什么
开发语言·ios·swift·闭包·逃逸
郝学胜-神的一滴10 小时前
Effective STL 条款 14:用扩容函数避免重新分配
linux·开发语言·网络·数据结构·c++·单片机
吃我一窝jio11 小时前
Java IO流核心分类与应用
java·开发语言·笔记