Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Making a 2d pokemon game in javafx, need some help.  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,I've recently started working on a javafx project inspired by the 2d pokemon games. Right now, the game is very much in a prototype stage but you can battle using pokemon(no catching or exploration yet). But this is my first time creating a project of this size and as a result the codebase is slowly turning into a spaghetti mess. I'm having a hard time separating UI from battle logic. For example, the trainer class is responsible for enabling the pokemon swap menu and making sure that the player is forced to send  in a new pokemon after the current one dies.  

Of course the code base is kind of huge so there are many classes. Here's the github link:Github

The links to the problematic classes:
Trainer abstract class
pcTrainer(playable trainer concrete class)
battle controller class

Any pointers on how to refactor this would be greatly appreciated.

Also, there is one thing with javafx that is causing me problems- The inability to add my own UI classes to scenebuilder. I made several helper classes to group UI functionality. For example,there is a battleUIHolder class that holds hpbars aand text labels for showing pokemon info. In unity3d I would have made a prefab for this class and instaintiated it in the UI when necessary. In scenebuilder the concept of a prefab does not exist.
 
Sheriff
Posts: 5745
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hamif Ronik, and welcome to the Ranch!

Some of your questions are not unique to JavaFX, so I'm going to add this thread to the Java in General forum.
 
Knute Snortum
Sheriff
Posts: 5745
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also, there is one thing with javafx that is causing me problems- The inability to add my own UI classes to scenebuilder. I made several helper classes to group UI functionality. For example,there is a battleUIHolder class that holds hpbars aand text labels for showing pokemon info. In unity3d I would have made a prefab for this class and instaintiated it in the UI when necessary. In scenebuilder the concept of a prefab does not exist.


Scene Builder is an application to help you build an FXML file, so I'm guessing when you say, "add my own UI classes to scenebuilder" your saying you want to add GUI objects to a Scene that was created by an FXMLLoader?  If so, that's not true.  A Scene is a Scene, no matter how it was built or loaded.  You can add Nodes to a Scene to your heart's content.
 
Knute Snortum
Sheriff
Posts: 5745
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since some people on this forum don't want to click on links, here is the class Trainer:
You said:

For example, the trainer class is responsible for enabling the pokemon swap menu and making sure that the player is forced to send  in a new pokemon after the current one dies.


I have a hard time understanding exactly what the problem is here -- maybe it's above my pay grade -- but I can point out a few other problems with the code.

Always start a class with an uppercase letter.  Trainer is fine but pcTrainer should be PcTrainer or PCTrainer.
Use a real package name.  Do you own the domain company.com?  If not, fine some other unique package name.
Create object variables with an interface rather than a concrete class, if possible. For instance, you have protected ArrayList<Pokemon> party = new ArrayList<>();  Use protected List<Pokemon> party = new ArrayList<>(); instead.
Don't use underscores in variable names. The idiom for setting an field with a parameter is this.name = name;
Use more comments. Even when writing prototype code, every method and class should have a comment.  This clears your mind as to what the method is doing and helps you remember why you wrote it.
Always use braces, even when you don't have too. Especially with if statements, even if there is only one statement in the "then" clause, use braces for clarity.
 
Knute Snortum
Sheriff
Posts: 5745
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I wrote:Don't use underscores in variable names


Except for constants, where you use ALL_CAPS_AND_UNDERSCORES.
 
Sheriff
Posts: 21638
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you already check if such a game would be legal? The Pokemon copyrights are owned by Nintendo who have given licenses to a few development companies to produce games for them. If you create a Pokemon game you may (probably will) end up getting sued.

Of course you can work around this a bit by making a Pokemon-like game. You can't use any existing Pokemon, but catching monsters and having them battle and evolve isn't copyrighted - I've seen enough Pokemon clones already.
 
Hamif Ronik
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:Did you already check if such a game would be legal? The Pokemon copyrights are owned by Nintendo who have given licenses to a few development companies to produce games for them. If you create a Pokemon game you may (probably will) end up getting sued.

Of course you can work around this a bit by making a Pokemon-like game. You can't use any existing Pokemon, but catching monsters and having them battle and evolve isn't copyrighted - I've seen enough Pokemon clones already.



I'm making this for learning, Not for any sort of commercial purposes. In retrospect I should not have used existing pokemon images anyways. I will remove them later.

Knute Snortum wrote:

Also, there is one thing with javafx that is causing me problems- The inability to add my own UI classes to scenebuilder. I made several helper classes to group UI functionality. For example,there is a battleUIHolder class that holds hpbars aand text labels for showing pokemon info. In unity3d I would have made a prefab for this class and instaintiated it in the UI when necessary. In scenebuilder the concept of a prefab does not exist.


Scene Builder is an application to help you build an FXML file, so I'm guessing when you say, "add my own UI classes to scenebuilder" your saying you want to add GUI objects to a Scene that was created by an FXMLLoader?  If so, that's not true.  A Scene is a Scene, no matter how it was built or loaded.  You can add Nodes to a Scene to your heart's content.



Thank you, I tried to include them in the original post but I kept hitting the character limit. I am aware that I could add nodes freely in code. However, then I'd have to run the program to see how it looks. If I could import a UI class in scenebuilder than I could see it more easily.

Knute Snortum wrote:Since some people on this forum don't want to click on links, here is the class Trainer:
You said:

For example, the trainer class is responsible for enabling the pokemon swap menu and making sure that the player is forced to send  in a new pokemon after the current one dies.


I have a hard time understanding exactly what the problem is here -- maybe it's above my pay grade -- but I can point out a few other problems with the code.

Always start a class with an uppercase letter.  Trainer is fine but pcTrainer should be PcTrainer or PCTrainer.
Use a real package name.  Do you own the domain company.com?  If not, fine some other unique package name.
Create object variables with an interface rather than a concrete class, if possible. For instance, you have protected ArrayList<Pokemon> party = new ArrayList<>();  Use protected List<Pokemon> party = new ArrayList<>(); instead.
Don't use underscores in variable names. The idiom for setting an field with a parameter is this.name = name;
Use more comments. Even when writing prototype code, every method and class should have a comment.  This clears your mind as to what the method is doing and helps you remember why you wrote it.
Always use braces, even when you don't have too. Especially with if statements, even if there is only one statement in the "then" clause, use braces for clarity.



Valid points. I started working in C first so some of my naming convention carried over. Then I realized that I could make the IDE generate constructor code so I followed the style you mentioned in other classes. pcTrainer stands for player controlled trainer so the correct version looks even weirder (PCTrainer). This was more readable to me.

My question was more along the lines of how to reduce coupling between UI and the trainer class. But your points are still insightful since I'm mostly self taught and I forget about these things a lot.

 
Knute Snortum
Sheriff
Posts: 5745
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am aware that I could add nodes freely in code. However, then I'd have to run the program to see how it looks. If I could import a UI class in scenebuilder than I could see it more easily.  


It's true that you can't add a POJO to Scene Builder, but you can create custom components and add them or any FXML to the Custom Library.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hamif --

 In re (so to speak) factoring, I have found Martin Fowler's book, Refactoring:Improving the Design of Existing Code (2nd ed.) to be incredibly valuable.  The 2nd edition uses javaScript code, which is not one of my languages,  but is seriously generic, has clear descriptions and examples.  The 1st edition used Java, I believe, but was written in 1999.  I'm sure he must have made many improvements for the 2nd ed.

 -- Chris Olsen
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!