DELETED

Recommended Videos

Bad Jim

New member
Nov 1, 2010
1,763
0
0
Java is object oriented. What you want to do is create a class for each of the various things in your game and create methods within those classes that do stuff. For example a Player class might look a little like this:

class Player{
int health,mana,gold

public void rest(){
if (gold >= 30) {
health = maxHealth;
mana = maxMana;
gold = gold - 30;
JOptionPane.showMessageDialog(null, "You rested!\nHealth: " + health + "/" + maxHealth + "\nMana: " + mana + "/" + maxMana + "\nGold: " + gold + "(-30)");
}
else {
JOptionPane.showMessageDialog(null, "You don't have enough gold, " + name + "! \n30 gold needed.");
}
}
}

You now just have to declare a player object like this
Player myplayer;
and you can make it rest by invoking the 'rest method
myplayer.rest();

If you want to get a bit more advanced with object orientation you'll want to explore inheritance. This allows you to declare generic classes like say Monster and derive specific classes like Dragon, HellHound, RabidWombat etc that inherit the data and methods of Monster. You can then make a list of monsters that can contain dragons, hell hounds and rabid wombats and invoke methods that are declared in Monster but defined in Dragon, HellHound and RabidWombat, getting different results from the same method call.
 

triggrhappy94

New member
Apr 24, 2010
3,376
0
0
TheNamlessGuy said:
gold = gold - 30;
It's been forever since I've done anything with Java and I didn't even know that much, but isn't there a shorter way to write that.

I'm sure there's also some way to get around some of your problems with variables and calls, but you'll end up with some additional clutter.

I know there's some good tutorials on Youtube.
 

Esotera

New member
May 5, 2011
3,400
0
0
The way I'd do it (for a very small text-based game) is something like the following python code:

Code:
class Player:

    def __init__(self):
        self.n_health = 100

    def set_health(self, n):
        if n >= 0 and n <= 100:
            self.
        else:
            return -1
    
    def get_health(self):
        return self.n_health

def main():
    jimmy = Player()
    jimmy.get_health() # returns health of 100
    jimmy.set_health(50)
    jimmy.get_health() # returns health of 50
The benefit of this approach is that you can store the class in another file and import it to wherever you want. And also instead of writing the same code out again and again for different players, you could also initialise a new object to store the health for all the NPCs or multiple players.

If you want to get into OOP then I'd really suggest reading up on the philosophy of how to organise information, as that's the most important bit - not the syntax of classes and all.
 

Esotera

New member
May 5, 2011
3,400
0
0
TheNamlessGuy said:
Esotera said:
Now, I don't know a dang diddily thing about Python, but if I understand correctly, it's basically what Jim wrote up there?
[small]And there aren't really any NPCs... as I said, veeeeeeeeeery basic[/small]
Yep, it's basically the same. But I wrote it in a different language and for a different scenario as the best way to improve your program design is to think about it yourself and plan it out very carefully. If your game suddenly got way more complex to the level of assassin's creed or something, my approach of handling player data would suddenly get absolutely stupid, as Ezio must have hundreds of thousands of variables at any one time, and it'd be impossible to define hundreds of thousands of functions getting & setting variables from one class. The take home message is think through data structures carefully, as otherwise you'll end up doing a lot more work than is needed..
 

Bad Jim

New member
Nov 1, 2010
1,763
0
0
TheNamlessGuy said:
Bad Jim said:
Tried this, however, I keep getting a "The local variable may not have been initialized" when I try to use it. Even if I use it outside any and all whiles and ifs and whatnots.
Sorry. I haven't programmed for ages, never used Java much and did a bit of lazy cut and pasting for the example.

I think I forgot to declare a few variables in that class, specifically maxHealth and maxMana.

triggrhappy94 said:
TheNamlessGuy said:
gold = gold - 30;
It's been forever since I've done anything with Java and I didn't even know that much, but isn't there a shorter way to write that.
gold -= 30;
 

triggrhappy94

New member
Apr 24, 2010
3,376
0
0
TheNamlessGuy said:
Do you know of any? Not to be rude, but I don't feel like wading through 500 different tutorials on the YouTubes just to understand less.
This was the series I used.
You'll want a different video because this is just the first one, but this will hopefully help some.
http://www.youtube.com/watch?v=Hl-zzrqQoSE
 

Bad Jim

New member
Nov 1, 2010
1,763
0
0
TheNamlessGuy said:
It doesn't complain when I write the whole "Rest nRest;" thing, but when I write "nRest.heal();".
I'm an idiot. You have to declare a player object like this
Player myplayer = new Player();

myplayer is really just a reference to a Player object and you have to create a Player object with new
 

SteelPanther

New member
Jul 25, 2011
9
0
0
You could pass them as arguments for the Player class or for the rest method.

For example, your Player class could read:

Code:
public Player(int gold, int maxHealth, int maxMana)
{
   ...
}
so when you created a new player, you put in the starting variables
Code:
 jim = new Player(0, 40, 40)

Or you could use it directly in the rest method:
Code:
public void rest(int gold)
{
   ...
}
then whenever you called the rest method, you would call:
Code:
jim.rest( *whatever int holds the current gold* )
and it could use that variable in it's calculations.


EDIT: For your new problem, I believe the issues is you are using the 'or' in the while loop. If any one of those is true, the while will loop through again. With '&&' instead, it should break when any one of those is true.
 

Arcobalen

New member
Aug 17, 2011
67
0
0
Expanded upon pretty easily.

Code:
public class Player {
  private int gold;
  private int health;
  private int mana;
  private String name;
private int maxHealth;
private int maxMana;

  public Player(String name, int gold, int health, int mana) {
    this.gold = gold;
this.health = health;
this.mana = mana;
this.name = name;
  }

  public void rest() {
    if (gold >= 30) {
      health = maxHealth;
      mana = maxMana;
      gold -= 30;
      JOptionPane.showMessageDialog(null, "You rested!\nHealth: " + health + "/" + maxHealth +             "\nMana: " + mana + "/" + maxMana + "\nGold: " + gold + "(-30)");
    } else {
       JOptionPane.showMessageDialog(null, "You don't have enough gold, " + name + "! \n30 gold needed.");
    }
  }
}