Quantcast

Maximum PC

It is currently Mon Oct 20, 2014 5:34 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Java Game help...
PostPosted: Tue Jan 05, 2010 8:14 pm 
8086
8086
User avatar

Joined: Thu Feb 19, 2009 1:57 pm
Posts: 22
Hello,

I am working on a game idea that I had I am using java and know a little bit but need some help.

What I want to do is store info on different types of "Monsters" in a separate file from the main code and be able to call upon one type of monster and have the info so I can then play out what happens in the main code.

So I want to be able to store some thing like this in with other monsters
Code:
rat{
int rathp=5;
int ratdam=3;
}


And when the time comes be able to have something like this
Code:
    JOptionPane.showMessageDialog(null, "You have encountered a Rat!");
    JOptionPane.showMessageDialog(null, "You attack the Rat!");
   rathp=rathp-attack;
   JOptionPane.showMessageDialog(null, "The Rat now has "+rathp+ " remaining!");


I want to be able to call rathp from within the main code from another file. How do I do this???

Thanks


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jan 06, 2010 4:56 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
http://en.wikipedia.org/wiki/Object-ori ... rogramming
http://java.sun.com/docs/books/tutorial/java/concepts/

Without seeing the rest of your code, it looks like you are treating each monster as a struct. Why not create a monster class and have each type inherit from that one?

I don't know how exactly it would look in java, but this is basically a C# way of doing it:

Code:
public class monster
{
    private int _hp;
    private int _dmg;

    public int HP
    {
        get { return _hp; }
    }
    public int DMG
    {
        get { return _dmg; }
    }

    public monster (int hp, int dmg)
    {
        _hp = hp;
        _dmg = dmg;
    }

    public bool Attack()
    {
        // you can do all kinds of neat things with this, pass in a reference
        // to a creature to attak and have it handle the damage dealing, etc.
    }

    public bool TakeDamage(int dmg)
    {
        _hp -= dmg;

        if (_hp <= 0)
        {
            // Dead, do something
        }
    }
}


Top
  Profile  
 
 Post subject: Re: Java Game help...
PostPosted: Wed Jan 06, 2010 10:06 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24226
Location: Granite Heaven
jamesrascal wrote:
I want to be able to call rathp from within the main code from another file. How do I do this???


First, you need to fully define the Rat object. This is the definition of all Rats.

Good programming style says that you should use methods to access and modify variables. So, in addition to defining rathp, you'll also want methods such as void setRatHP(int newHP) and int getRatHP().

The first sets the rat's HP to the value of newHP and the second returns and int equal to the current value of ratHP.

Next, you need to instantiate a Rat. This is the creation of a single Rat. Call him ... say ... Mickey.

When you want to access the rat's current HP, you'll say something like
Code:
JOptionPane.showMessageDialog(null, "The Rat now has "+mickey.getRatHP()+ " remaining!");


Top
  Profile  
 
 Post subject:
PostPosted: Tue Feb 09, 2010 9:25 am 
8086
8086

Joined: Wed Sep 23, 2009 2:41 am
Posts: 12
I agree with both jipstyle and crashtech. The easiest way to accomplish what you want would be to create a monster class that contains all the attributes of every characteristic the all monsters have in common. Make the monster class very very general. Then extend the monster class to say a rat class, which only contains the characteristics specific to the rat. Something along these lines in Java

Code:

  public class Monster{

        int maxHP = 10:
        int minHP = 0;
        int currentHP;
   
  public int getHP(){
        return currentHP;
  }

}



then extend the monster class to the Rat class

Code:

  public class Rat extends Monster{

      int ratAttack = 3;
     
     public void setHP(int HP){

        super.currentHP = HP;
     }
}





Hope that helped, sorry if it didn't.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Feb 09, 2010 11:06 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24226
Location: Granite Heaven
This thread is over a month old; please don't necropost and bring threads back from the dead.


Top
  Profile  
 
 Post subject: Fuck. This. Forum.
PostPosted: Tue Feb 09, 2010 11:23 am 
8086
8086

Joined: Wed Sep 23, 2009 2:41 am
Posts: 12
*senseless rant from someone who can't stand to be informed of the rules*

Edit: removed profanity and personal attacks from post.

No action taken against the poster since he insists he won't be back.
-jipstyle


Top
  Profile  
 
 Post subject: Re: Fuck. This. Forum.
PostPosted: Tue Feb 09, 2010 11:31 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24226
Location: Granite Heaven
jnich91 wrote:
blah blah blah


Wow. Quite the emotional response. Take a deep breath, buddy, and calm down a bit.

Since the OP hasn't returned to the thread in over a month, it is safe to assume that he doesn't need any more help. By not posting in threads that are clearly dead, we keep the forum free of clutter.

It isn't a hard-and-fast rule .. there are times when it is appropriate. This just wasn't one of them, so I informed you of the policy. No twisted panties, believe me. Just doing my job.

If I was really pissed, I'd have locked the thread and or warned you. Instead, I just pointed out that you're uselessly dredging up dead threads.

In this particular folder, it doesn't matter too much ... we don't see much traffic. But in a folder that does see a lot of traffic, useless necroposts bury threads in which people are actively seeking help. Rather than have different rules for different sections, that change at different times of the year as people need different types of help .. we apply the same rules universally across the forum.


Last edited by Jipstyle on Fri Feb 12, 2010 3:46 pm, edited 1 time in total.

Top
  Profile  
 
 Post subject:
PostPosted: Tue Feb 09, 2010 7:57 pm 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Further more, you didn't say anything that wasn't already said. If the OP had come back asking for more help or clarification then it would have been another story all together.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2010 11:48 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
CrashTECH wrote:
I don't know how exactly it would look in java, but this is basically a C# way of doing it:


LOL... you don't know what it would look like in Java. Puhsleeaazzzeee, you know that C# is a huge Java ripoff!

edit: Sorry. I hadn't read all the way to the bottom before posting my comment.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2010 6:07 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
JAVA syntax is just enough different though... and the libraries are different.

so :p


Top
  Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2010 10:41 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
CrashTECH wrote:
JAVA syntax is just enough different though... and the libraries are different.

so :p

Different syntax?! Different libraries?!! That's does it... I'm diffing the source files. There is no way that they are THAT different. =)


Top
  Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2010 4:56 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
orly? Id be curious to see how different they really are. :)


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.