| 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."); } } } |
| Player myplayer; |
| myplayer.rest(); |
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.TheNamlessGuy said:gold = gold - 30;
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
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..TheNamlessGuy 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?Esotera said:-snip-
[small]And there aren't really any NPCs... as I said, veeeeeeeeeery basic[/small]
Sorry. I haven't programmed for ages, never used Java much and did a bit of lazy cut and pasting for the example.TheNamlessGuy 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.Bad Jim said:-snip-
gold -= 30;triggrhappy94 said: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.TheNamlessGuy said:gold = gold - 30;
This was the series I used.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.
I'm an idiot. You have to declare a player object like thisTheNamlessGuy said:It doesn't complain when I write the whole "Rest nRest;" thing, but when I write "nRest.heal();".
| Player myplayer = new Player(); |
public Player(int gold, int maxHealth, int maxMana)
{
...
}
jim = new Player(0, 40, 40)
public void rest(int gold)
{
...
}
jim.rest( *whatever int holds the current gold* )
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.");
}
}
}