Thread: Health...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    132

    Health...

    to make a mini fighting game, how do u store health and change it?
    just like, it shows say,

    Hugo - Health 100%
    he hits kick
    and the Hugo - Health changes to 90% but it doesnt re print this text. where it says Hugo - Health - 100%, it stays there but the 100 turns to 90.

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Well it all depends on what you're using for graphics, OpenGL, basic GDI, etc. But either way I'd recommend storing it in a variable and then updating the screen so it will show the variable's new content after it's changed. Just a suggestion, don't get ahead of yourself.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You'll want to position the cursor one the screen, or (ugh) clear the whole screen and repaint the whole thing.

    See the FAQ, or try gotoxy() in <conio.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    thanks for the fast replys.

    ok, well whats the easiest graphics thing to learn? basicly all i want to do it have writing thats explaining what you doin, like hugo punched david etc.. and then a picture to appear. basic JPEG image from a folder.

    yeh i thought about making a variable to hold the health in but im not too sure on how to do it... any chance you could show me a small example of a code that would work?

    also how do i refresh the whole screen?

    also once i sussed out the basic part id like to add buttons with the move name on it and when i click it, it does what it would if i typed !punch or ! kick or /punch, \kick etc...

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    How's your object orientation concepts? The best thing to do
    would be make yourself a very basic console app first - design
    a fighter class and have methods for different attacks and
    store health, then decrement the health for your oponent.
    Here's some pseudo-code to point you in the right direction:

    Code:
    class Fighter
    {
    private:
    	int health;
    	string name;
    	int kickstrength;
    	int punchstrength;
            
    public:
    	//cxtrs here
    
    	bool kickoponent (Fighter &oponent);
    	bool punchoponent (Fighter &oponent);
    	int GetHealth ();
    	//and so on for more detailed attacks
    };
    
    int main (void)
    {
        //Create instances of your fighters
        //menu stuff for turn select and attack choice
        //below is what player 1 kicking player 2 might look like
        
        if (Player1.kickoponent(Player2))
        {
    		cout << Player2.GetHealth ();
        }
    	
        else
        {
    		cout << "Player 1 wins" << endl;
        }
    
    	//and similarly for player 2 attacking player 1
    
    	return 0;
    }
    Get your console going first on its own, then try out loading it into
    a window and displaying basic graphics. Then all the fancy stuff
    with buttons can come along.

    As far as graphics librarys go, there is some choice, but I've seen
    people do amazing looking stuff with openGL in a very short
    period of time, so thats the only one I could recommend.
    Last edited by Richie T; 05-18-2006 at 04:04 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    wow thanks alot mate!

    ok ive copied it down so when i get there ill use it.

    what do u mean get the consol going first?

    ok ill try n learn OpenGL , can you learn this advanced on the internet in tutorials and stuff?

    the game i wanted to try would have like... 100's of different moves. like grab opponent and when u do that, lets say a list of 10 moves comes up and u can choose which one ud like to do, once you have chosen, it goes to ure opponents choice to choose to try to reverse it or let it hit.. etc... lol sorry its its long winded.

  7. #7
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    What i meant by console was exactly what you are talking about
    in your other post - the "DOS box". You should code your
    program so that it appears in the console - simple text menu
    indicating what options are available to the player. The user
    uses the keyboard to select an option and the program prints
    the effect of that option. This is all very basic stuff.

    In your other post, you mention that you've studied all the
    tutorials on this site. Even if you have studied them in good
    detail, very little of that will help you in relation to using the
    buttons and the mouse - the Windows API is a very strange
    beast. Keep your program simple, like the structure I've laid out,
    then build on it to intoduce more moves - and then branching
    them out as you wish. All of the attack functions will essentially
    be the same, only deducting different amounts from the oponents
    health.

    If you kept on building the program, you could have a character
    select menu, whereby you might code a generic base class
    Fighter, with all the functionality that a character needs, then
    your list of actual characters could inherit all the basic punches
    and kicks from the base, saving you time - because each derived
    class would have different values for attributes such as attack
    strengths.

    Hey all this sounds like fun, might make a basic game such as
    this myself after my exams!

    For openGL tutorials, one of my friends learned from this site,
    and was able to develop some very cool maps and stuff from
    it very rapidly.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Actually all the data members are private in Richie's class declaration, which meand that the variables are only accessable by the class itself, and not any other functions, like main() or other classes. That's why Richie declared getter functions. This is good because it protects your data from unauthorized change.

    These getter functions make it so that the data is made available to other parts of the program in a safe way. They look something like this:
    Code:
    // This function returns whatever Health is declared as. Say, an int.
    int Fighter::GetHealth(void) const {
        return Health;
    }
    Last edited by whiteflags; 05-18-2006 at 05:26 PM.

  9. #9
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Somehow missed that last post!

    Exactly as citizen said, the variables are private. The reason?
    Data encapsulation is one of the principles of good object
    oriented design, the idea that the object is a fully self sufficient
    entity - the only way to manipulate it being through methods
    coded by the programmer. Thats why OOP is so popular in
    industry - a class should contain carefully designed methods
    so that the code can be reused without need to tinker with the
    class's inards. Thats the main difference between classes and
    structs - in a struct, all variables are accessible (if an instance is
    in scope). In a class, anything under private can only be seen
    and changed by the class itself (or friend objects/functions, but
    thats another matter). Public means that they are visible to
    everyone, so thats why we normally put methods under public.
    Protected then has to do with inheritance - where we want
    the variables to be restricted like private, but classes
    that inherit from this class will inherit variables under protected
    as well.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little Game
    By cjwenigma in forum C++ Programming
    Replies: 9
    Last Post: 11-07-2007, 12:50 PM
  2. health...
    By psycho88 in forum Game Programming
    Replies: 6
    Last Post: 12-04-2005, 09:37 AM
  3. Help With Health in Games
    By Mustang5670 in forum Game Programming
    Replies: 8
    Last Post: 01-04-2004, 04:43 PM
  4. AOL And your health
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-09-2003, 09:51 AM
  5. Health Bar demo
    By jdinger in forum Game Programming
    Replies: 7
    Last Post: 04-12-2002, 12:27 PM