avatarLincoln W Daniel

Summary

The website content outlines the development of a Java-based graphical user interface (GUI) fighting game, which builds upon concepts such as abstract classes, interfaces, and the model-view-controller pattern, and serves as a practical application of the knowledge acquired throughout a Java programming course.

Abstract

The provided content celebrates the reader's progress in learning Java and introduces the final project of creating a GUI-based fighting game, akin to the "Conquer" MMORPG. This game utilizes character classes previously discussed, including features such as names, skills, pets, health, attack and defense powers, and speech abilities. The game's structure is supported by polymorphism, detailed source code documentation, and adherence to the model-view-controller pattern, facilitating easy navigation. The game's architecture comprises an interface, an abstract class, and several concrete classes that implement various character types and behaviors. The gameplay involves characters engaging in battles, with the last character standing declared the winner. The game also includes a GUI powered by JavaFX, which displays real-time information and allows user interaction. The content concludes by encouraging readers to explore the code, rebuild the project, and consider future enhancements or new game development as a means to solidify their Java programming skills.

Opinions

  • The author expresses confidence in the reader's abilities, emphasizing the significance of the reader's accomplishments in reaching this stage of the Java learning journey.
  • The use of detailed documentation, including javadocs and comments, is highly recommended for maintaining clarity and ease of understanding in the project's codebase.
  • The author believes in the importance of hands-on experience, suggesting that readers should reconstruct the game from scratch to reinforce their learning.
  • The game's design, which includes abstract classes and interfaces, is presented as an effective way to manage the diversity of character types and their unique behaviors, particularly in terms of attacking and training.
  • The author values the final project as a practical example of applying Java programming concepts in a real-world context, suggesting that it equips readers with the necessary skills to develop their own Java applications.
  • The inclusion of a fixed-length array to store game history is acknowledged as a design choice for illustrative purposes, with the suggestion that it could be replaced with a LinkedList for more flexible application usage.
  • The author promotes JavaFX as a powerful tool for creating user-friendly GUIs, indicating its effectiveness in enhancing the user experience for desktop, mobile, and web applications.
  • The content concludes with an optimistic view of the reader's potential, encouraging further exploration and creativity in Java programming through the development of additional projects and engagement with the job market.

Congratulations on making it this far. Really, you have accomplish a great feat. The final test of how far you have come is for you to build your first graphical user interface game in Java. I have developed the entire game for you, and in this chapter, I will touch on the purpose of each class. With that knowledge, you will be able to reconstruct the entire project from the ground up.

Building a Fighting Game with a GUI

If you remember the game we discussed in the Abstract Classes & Interface chapter, you will be glad to know we are using the character classes we created then in our game. We are going to mimic the “Conquer” MMORPG game. In Conquer, each character has a name, some set of special skills, a pet, health, attack power, defense power, and the ability to speak. Also, each user can select the type of character they want to be. However, there are two types of characters and users can only choose to be a Human character. A Human character can then have a Pet character. Each character battles until there’s a winner. Our game will emulate all of this.

Because we make use of polymorphism, the model-view-controller programming pattern, and interfaces, the application is quite easy to navigate. I have documented the source code of this project in great detail by commenting virtually every line of code and providing javadocs for each method, class, and interface. With that said, I will not spend much time explaining the intricacies of the project’s code base in this chapter.

The package for this chapter’s supporting code is called “buildingAGame”. Please have it open as we walk through it to at a high level to understand what’s going on. Lets take a look at the fourteen classes and one interface of our game and walk through each class to see how we apply all of the knowledge we’ve accumulated in this book.

Characters

The most important part of our game are the characters because there wouldn’t be much action without them. Every player in the game is a character, so we have a Character interface. Any class that intends to create a template for constructing characters that can play in our game must implement our Character interface. To make that implementation easier, we have an abstract AbstractCharacter class which implements the most general methods of the Character interface.

The AbstarctCharacter class must be extended by any class that intends to create a template for constructing characters that can play in our game. The AbstractCharacter class implements all but two of the methods from the Character interface. That means that any class that extends our AbstractCharacter class only has to implement the attack() and train() instance methods from the Character interface. We have left those two methods for subclasses to implement because it is intuitive that different types of characters attack and train in distinct ways: an Archer attacks with arrows, a Warrior attacks with a sword, and a Taoist attacks with magic. While each character has different defensive powers, they all defend the same way — perhaps by holding up their weapon to block an oncoming attack. Furthermore, there are two base types of characters and they are vastly different. For that reason, we have two more abstract classes that extend the AbstractCharacter class.

For our human characters, the ones users can choose to be, we have the abstract Human class from which all classes that intend to create a template for constructing human characters must extend. The Human class is one of two methods that directly extend the AbstractCharacter class. Human characters can have pets and human friends. Aside from that, a human character is much like any other character. For that reason, the only fields distinct to the Human class are a Pet instance field to hold an instance’s pet and a final LinkedList of Human objects for an instance to accumulate friends. The list of friends is final because we want our Human instances to always have a list of friends; it can never be set to null or replaced by accident. The two distinct functions of the Human class have to do with setting and getting friends and a pet. The Archer, FireArcher, Warrior, and Taoist are concrete classes that extend the Human class. Its also important to note that the FireArcher is a special type of archer that extends the Archer class. Its only distinction is its additional firePower instance field which adds to its attack power.

The abstract Pet class is the other class that directly extends the AbstractCharacter class. All classes that intend to create a template for constructing pet characters must extend to be a valid playing character in our game. The pet class’s only distinct field is a Human instance field for storing the owner of an instance. Its only distinct method is its cry() instance method which is an interface for all pets to make their special sound; a Dog can bark and a Lion can roar by invoking cry(). The Dog and Lion concrete classes are currently the the only classes that extend the Pet class.

Game

The game is simple. The user chooses a maximum number of battles for the players to engage in. Each time the user clicks “Next Battle”, the next battle starts between two characters — a random attacker and a random defender. The attacker tries to inflict damage on the defender and only succeeds if the defender has less defensive power than the attacker’s attack power. Whoever wins the battle gains experience. To gain experience for an action, a character must call the gainExperience() instance method and provide a string representing the action they’ve completed as an argument. The action will be used as a key to get the corresponding amount of experience to gain from the AbstractCharacter class’s actionExperiences static HashMap field. That way, all characters get the same amount of experience for completing the same actions.

The last man standing is the winner of the game. The game has an String array which holds the history of winners from each game. A user can only play a maximum of one hundred games each time he or she launches our application because our history array has a fixed length of a one elements. This is simply to illustrate a use of an array and can be changed to a LinkedList if you want to allow users to play your game as many times as they want. Lastly, the game has a graphical user interface (GUI) which it has to set up once each time our application is launched.

GameGUI

The GUI is what allows a Game instance to display real time information about the state of the game and its players to the user; our GameGUI class is used by the Game class to achieve that purpose. As noted before, each Game instance must have an instance of our GameGUI class in order to run. The GameGUI makes use of JavaFX, which allows developers to create desktop, mobile, and web applications nicer than ever before, to display information from an associated Game instance to the user in its views.

Our GameGUI is currently designed with three TextAreas for displaying information about the game and ongoing battles in the game, one TextField for capturing user input, one ListView for displaying the standings of all the characters in the game, a Button to stat the game, and two more buttons to advance through the game. Setting all of that up is a bit more involved, but you’ll be able to understand each part as you read the documentation in the code.

JavaFX Application

Our application runs on JavaFX components for its GUI and a nice abstract Application class provided by JavaFX for easily launching and starting a application program. Our GUIGameApplication class extends the JavaFX Application class and is at the root of our project. Its only duty is to hold the main method of our program. Its main method has a simple duty of calling the launch() instance method from the Application class. When that method is called, it will call the abstract start() instance method from the Application class to start our application. Our GUIGameApplication must implement that start() method which gives us access to the primaryStage Stage field of the application. We must provide the primaryStage reference to our Game, which will provide its instance of our GameGUI class to display the JavaFX GUI for our game.

Finishing Up

And just like that, we are finished. You have learned so much about Java. In fact, you know everything you need to know to start building your own Java applications. Remember that the best way to learn is by doing and having fun while doing so. Your next steps are to play around with our game, take a look at each class in the supporting code for this chapter, read the documentation, and then try to write the entire project again. After that, try to build your own game or add to what we already have here. You are on your way. In the next chapter, we will take a look at some more fun projects we can take on to continue applying your new knowledge of Java programming.

Next Chapter

Table of Contents

Programming
Java
Coding
Recommended from ReadMedium