返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
Production

[Open Source] Releasing a JavaFX-based Mini Game: CrazyAlpha

Features Designed a game engine XEngine based on JavaFX Implemented complete game functionality using the self-developed game engine XEngine Game resource management: font, image, and audio management Game map management, multi-map switching MVVM layered design, decoupled code, highly cohesive modules Model system design, modular design Message system and event handling implemented using the publish-subscribe pattern Built with Gradle Download and Run First, click to open: CrazyAlpha Download Page Download CrazyAlpha

曦远 (DealiAxy)约 22 分钟
目录13 个章节

Features

  1. Designed a game engine XEngine based on JavaFX
  2. Implemented complete game functionality using the self-developed game engine XEngine
  3. Game resource management: font, image, and audio management
  4. Game map management, multi-map switching
  5. MVVM layered design, decoupled code, highly cohesive modules
  6. Model system design, modular design
  7. Message system and event handling implemented using the publish-subscribe pattern
  8. Built with Gradle

Download and Run

First, click to open: CrazyAlpha Download Page

Download the latest version of CrazyAlpha; for example, the one in the screenshot is 1.0.1, and the filename is: crazyalpha.jar

PS: Since the game is written in Java, friends who don't have Java installed please download Java first. Windows Java runtime download: Click to download

After installing the Java environment, you can double-click the jar to run it, or right-click and choose to run it with Java Runtime Of course, you can also run it from the command line

java -jar crazyalpha.jar

Open Source

Github project address: https://github.com/Deali-Axy/CrazyAlpha

PS: Due to copyright reasons, the Github code does not include resource files such as images, fonts, and sounds used by the game, so those who want to run the game can replace the resource files themselves, or contact me to obtain the resource files.

Architecture

  1. Game.Engine: self-implemented game engine package
    1. Annotation: engine annotation package, containing annotations defined by the engine
      1. GameEventHandler: event handler annotation
      2. GameManager: annotation for various managers in the game
      3. GameModel: model annotation
      4. GameObject: game object annotation
      5. GameView: view annotation, used to implement MVVM layered design
    2. Base: base class package, containing various abstract base classes defined by the engine
      1. BaseModel: base model
      2. ImageModel: base image model
      3. ShapeModel: base shape model
      4. TextModel: base text model
    3. Enum: definitions of various enums (omitted)
    4. Utils: other utilities (omitted)
    5. GameObject: game object base class; all objects derive from this class
    6. GameView: screens other than the main game interface are all derived from the view base class
  2. Game.EventHandler: contains all event handlers
  3. Game.Manager: contains various managers, including map, resource, data, model, event, and other managers
  4. Game.Object: contains definitions of all game objects
    1. Model: contains definitions of all game models
    2. GameMap: game map object
    3. Generator: game event generator
  5. Game.View: view package, containing definitions of all views
  6. Game: global game object, created using the singleton pattern
  7. Main: game main class
  8. Render: core renderer, responsible for the game's rendering

GUI Implementation

To develop this game, I first developed a JavaFX-based game engine. The engine implements a series of features from game resource management and model definition to screen rendering, designed following the design approach of existing commercial game engines on the market. “Though the sparrow is small, it has all the vital organs”—although it cannot rival other mature game engines, in terms of functionality it is fully equipped and can meet the development needs of most 2D mini games.

Screenshots

Game startup screen:

The upper left is the game logo, the lower left shows the high score record, and below that are the author information and version. The lower right has two buttons: a start-game button and a quit button.

Game main screen:

The upper left shows the player avatar, current score, and health; directly above is the current level; In the center of the screen is a large pinwheel. The pinwheel shoots out letters following the music's rhythm, and the player needs to press the corresponding letter before it flies off the screen in order to score.

Game pause screen

Game over screen

The lower left shows the score for this round, and whether the high score record was broken. The lower right shows a button to return to the main screen.

Development Notes

This is my first time using Java for game development. Previously, my understanding of the Java language was limited to JavaEE web development. Recently I learned JavaFX, and after using it I discovered that JavaFX is actually so nice to use. It really is a framework designed specifically for GUIs, just that relatively few people use it. I ran into many problems during development and could barely find any Chinese resources. Luckily there's the almighty StackOverFlow, and Oracle's official documentation is also quite detailed. Thanks~

For someone like me who's used to writing dynamic languages like Python, the feeling of writing a lot of Java code is that it's tedious, but worry-free. You don't need to think much; you don't have to wrestle with what type a parameter is or whether you need forced type checking. Everything is clearly arranged by the IDE and the JVM, so I don't need to worry about types anymore. Also, because Java syntax is relatively rigid and doesn't have that many flexible ways of writing things, this is both a disadvantage and an advantage. The disadvantage is that for someone like me who likes to show off, writing Java code doesn't bring much pleasure. But the advantage is that for the same functionality, the implementation code is basically similar, and different people implementing it won't differ much, which ensures code maintainability. Maybe if you write a large project in Python now, the code is all free-flowing and unconstrained, which feels great while writing, but when it comes time to refactor later, you're done for—you can't understand the previous code. Even though there are comments, it's impossible for every line of code to have comments, right? Flexible writing styles increase the difficulty of understanding. Therefore, Java's absolute dominance in large projects is not unfounded.

This game development also deepened my understanding of the Java language, for example reflection, for example events. At first, when working on the event-handling part, what puzzled me a bit was that Java has neither delegates nor a true concept of “events”; everything is implemented with interfaces and classes. Implementing interface functionality is generally done by passing in an anonymous class that implements some event interface (of course in Java 8 you can use lambda expressions). But this isn't a big problem; these issues can all be solved in other ways, it's just more tedious to write. Maybe because I was used to writing C#, I always involuntarily compare when writing Java code, but actually these two languages aren't very comparable, because they were born in different eras. C# is indeed much more sophisticated than Java in language design. In recent years, new versions of Java have been more or less imitating features that C# had years ago, for example Java 8's lambda expressions, Java 11's var for defining variables, and so on.

Additionally, developing this mini game gave me this impression of Java: the syntax is very simple, not as numerous and complex as C# and C++, so it's especially beginner-friendly, because you can pick it up right away. Combined with the best IDE: IDEA, you can easily write all kinds of projects. There's a saying that because Java itself has too few features, various design patterns emerged to make up for the language's shortcomings.

About

Learn more on my WeChat Official Account:DealiAxy

Every post was in my blog:blog.deali.cn

Welcome to Connect

For questions, please leave a message on the WeChat official account backend; I'll reply to every message~

_EOF_

[Open Source] Releasing a JavaFX-based Mini Game: CrazyAlpha

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…