# Starting my project (again)...

This topic is 4214 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, this time, I'm REALLY going to work hard on my Turn-based RPG in Java. I started earlier, but it was VERY poorly coded, and largely procedural. I wasn't taking advantage of Java, so I got frustrated and just gave up. I'm starting a clean slate this time. I've only read the basics of C/C++ so I don't think that's enough to start this RPG game. This will be a small project I'll work on with my free time... First, I'm doing this in a simple GUI interface, with JFrames, JLabels, and all that. Just a simple interface- I don't want to delve too deep in 3D yet, but I don't think a text-based game would interest anyone, including me much. My plan is to create a universal JFrame, and within it, three JPanels. One JPanel for the main display (maps, villages, battle screen), the second for the stats, and the third for the movement compass JButtons. It worked pretty well last time, so I think I'll take this route again. Whenever I switch to a battle, I'll just call a method to remake the main JPanel from the map to a battle screen. Is this a good plan or is there a more efficient way to do this? Second, I really need to start using more OOP. I'm thinking to create a player object, and have all his stats as attributes. I previously called those attributes with "player.strength", but I saw that this method kind of defeated the purpose of OOP. Would I have to create a method to return the value for EACH stat? (We call them accessor methods at my high school) Also, is there an easy way to do this with enemy stats as well? Third, I keep the player's stats in a ".dat" file. Whenever I launch the game, I call a method that reads from the file (with Scanner) and stores the values into the application. I was thinking I could do the opposite- taking the variables and overwriting it to the .dat file, to save the game. This makes sense, but I just feel this is a poor way to code this... Note- I don't have much experience in programming yet. I've just finished my first year of Computer Science in high school, so I only know the basics. I don't want to learn something too advanced yet, because chances are I'll just be using examples, and NOT knowing what I'm actually coding. This is a project that I'm coding to show MY knowledge in programming, and it will grow as I learn more. Right now, it will be pretty basic before I add more stuff to it. I'm waiting for a few tips before I start coding this...

##### Share on other sites
Quote:
 Original post by XG08ZeroSecond, I really need to start using more OOP. I'm thinking to create a player object, and have all his stats as attributes. I previously called those attributes with "player.strength", but I saw that this method kind of defeated the purpose of OOP. Would I have to create a method to return the value for EACH stat? (We call them accessor methods at my high school) Also, is there an easy way to do this with enemy stats as well?

What's the difference between the player and an enemy? If they have the same attributes, you just create two instances of the object, naming one 'player' and one 'enemy'. If they are slightly different, create an object with the same attributes and inherit your player and enemy class from this super-class. That's what OOP is for.
Regarding the accessor-methods: yes, you should encapsulate those fields you like to access from outside of the object. If you are using an IDE (like Eclipse, Netbeans, or something similar) you can let those accessor-methods create automatically.

Quote:
 Original post by XG08ZeroThird, I keep the player's stats in a ".dat" file. Whenever I launch the game, I call a method that reads from the file (with Scanner) and stores the values into the application. I was thinking I could do the opposite- taking the variables and overwriting it to the .dat file, to save the game. This makes sense, but I just feel this is a poor way to code this...

Why should this be a poor way to code? Saving a game works like this... Maybe you should not overwrite the same .dat file everytime. Instead give the player the opportunity to have more than one save-game.
Good luck.

##### Share on other sites
You really should read a book about Java to get you started thinking about object-oriented programming or you will be confused nevertheless (leaving aside issues of the merits and demerits of OOP since Java is object oriented). I recommend this one, which is also what I used to learn Java. The prose is engaging so you will not fall asleep reading it.

Acessor methods in Java - you should create them, but only for those that need to be exposed. A setHealth() method may be ok, but (a simplistic example) what about a takeDamage() method that modifies health in some way instead? The good news is that it's easy to create these getters and setters - in Eclipse, for instance, you can right click on a variable and select (source->generate getters and setters)

##### Share on other sites
Quote:
 Original post by JockelWhat's the difference between the player and an enemy? If they have the same attributes, you just create two instances of the object, naming one 'player' and one 'enemy'. If they are slightly different, create an object with the same attributes and inherit your player and enemy class from this super-class. That's what OOP is for.Regarding the accessor-methods: yes, you should encapsulate those fields you like to access from outside of the object. If you are using an IDE (like Eclipse, Netbeans, or something similar) you can let those accessor-methods create automatically.

See- I NEVER thought of that! XD You are right. I still have alot to learn...
So when I need to start a battle, should I create the enemy object then? I was originally thinking that I would create a LARGE .dat file that would keep the stats of all kinds of monsters. I would then read from the file and create a monster based on that. So, what I'm saying is that I'm thinking of having a large .dat file, or many small ones, to store the monster's stats. Man, I have alot to learn...

Quote:
 Original post by JockelWhy should this be a poor way to code? Saving a game works like this... Maybe you should not overwrite the same .dat file everytime. Instead give the player the opportunity to have more than one save-game.Good luck.

Ok, that makes sense. I just felt it was poor because I was overwriting it, which could lead to problems. I'm excited about this now. Thanks a ton. I'll post back when I need more help! :D

##### Share on other sites
As lighbringer mentioned it, maybe it would be best for you to get a book and go through the examples. Even though it might be pretty boring but it's just frustrating if you have an idea in mind and you cannot realize it because you wanted to skip the basic stuff.
So take a book, work through it and then things will be much clearer (and easier).

##### Share on other sites
Quote:
 Original post by XG08ZeroI don't want to delve too deep in 3D yet, but I don't think a text-based game would interest anyone, including me much.

Just as a quick side note - Theres a pretty large community of text based gamers out there, actually

##### Share on other sites
A general principle of OOP: methods should *do something*.

// BadMonster x, y;y.hp -= x.strength;// Much worse!Monster x, y;y.setHp(y.getHp() - x.getStrength());// Don't write those functions. In the real world, they basically never end up// doing anything beyond "raw" access or mutation of the variable, unless you// specifically designed for it from the beginning.// Properclass Monster {  // other stuff  public void injure(Monster other) { other.takeDamage(strength); }  public void takeDamage(int amount) { hp -= amount; }}Monster x, y;x.injure(y);

##### Share on other sites
Ok, I know that the first example is bad- bc that's what I did before...

I was planning to code my game like the second example. I never knew that was a bad way. Now that I think about it, I understand why. So THIS is OOP. My teachers taught me to code like the second example, and said that was the majority of OOP. Now that I think about it, it kind of is the same thing as the first example, just one more step. I understand this now. So the player/monster object should have it's OWN functions for calculating damage and such. I only used the object's attributes, and used global functions. I understand this now. Thanks a ton!

##### Share on other sites
Though the second example *is* appropriate - in some cases. Such as a physics engine might need to let you change about the friction of an object. The advantage of the second example over the first is that if the object needs to change something when a value is changed, it can. So, when the friction is changed, the object may also need to recalculate a couple other values based off of that.

The trick is to pick what is appropriate to use and when.

##### Share on other sites
Yeah, I agree with most of what has been said. You DEFINATELY want to do the monster.injure method, and definately want to have the player and the monster be made or inherited from the same class. Make sure you do everything very professionally, if you catch yourself starting to say "bah, I don't want to have to do all of that! I'll just stick this code in the main method here..." Stop yourself right away, and remind youself of what happened last time.. You don't want to scrap this and start over agian, do you?

I'm currently making an RTS in java, lots of the same concepts apply. Its easy to do surprisingly complicated things with your objects once you've set up a good class system to make things less complicated. If you have x.injure(y), you don't even have to think about it, you just know it will work, since you thought about how it will work once, and wrote the injure function.

I suppose I'll give one tiny example. I made a unit class, with soldiers, clerics, archers, workers derived from it. I had a move function for each unit - call it once, and on each update of the unit, it would automatically move towards the destination. Because I had such a nice function & it was very easy to use, I was able to move battalions of soldiers at a time, all into a formation, without much difficulty. This would have been impossible for me without OOP.

Good luck with your project, don't get discouraged, and definately don't fall into sloppy coding habits!

Good luck,

[Edited by - Adams555 on July 4, 2006 8:51:58 PM]

##### Share on other sites
Thanks a ton! This has REALLY helped me and I have a better understanding on how this should be coded. I really should be thinking about the structure before I begin coding anything. I shouldn't dive headfirst and start programming my game, which is what I did earlier...

I'll post back when I have more specific problems. I can't wait to REALLY get started... :D

##### Share on other sites
I posted again because I figured you wouldn't look back to previous posts.

Definately do not dive headfirst into your game. You WILL regret it later if you don't have a class structure all set up before you start.

Just come up with a bunch of classes and methods you will need - here is a small list to get you started. This, of course, is all up to you, don't feel obligated to use any of it. Its there to get you started, give you ideas, if you need it.

A monster class, with the ability to draw itself on the map and in battle (depending if you have a seperate battle screen) along with the normal max hp, hp, max mana, mana, strength, defense, armor (Its nice to have defense to divide damage, and then armor to absorb damage directly. Damage calculations could be done like this..

Say a monster was attacked by someone that would normally do 27 damage.
The monster has 2 absorbtion and 50 defense. Assume defense can be on a scale from 0 to 99, which is the usual cap. We can do defense logarithmically, I like it that way. It reduces the effect of additional defense points by a little bit, so you can't have insane damage reduction due to defense.

The damage will be.. (27-2)/((log base 2 of 50)+1)

The -2 is from absorbtion
The +1 is so that the damage can only be reduced, not increased (the denominator is always at least 1)
log base 2 of 50 is roughly 5.65

So.. damage is (27-2)/6.65, rounded up, which is 4 damage.

I've found that to work very well. Change to log base 3 or log base 4 if you want your defense to be less powerful.

ok.. A few more classes.

You'll need a map class, and a mapManager class. The mapManager class should have a map of your entire world, with each entry being a filename. The mapManager should be able to load a map from a file, creating a Map object. When you switch maps, jsut switch map objects. The map needs to be able to draw itself, and also have a function to say if the character / AI is able to move onto a specific tile, to allow for obstructions.

You will, of course, need more than this, but those should get you started :)

I was planning on writing more, but I need to do some other stuff right now.

##### Share on other sites

This topic is 4214 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.