Thread: Little Game

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    46

    Little Game

    Hello Everyone, I'm trying to write a simple and funny fighting game. I want to write the first class of the game called teacher and i'm not sure if i'm going about it right. I have the moves Punch, Kick, Deafening Whistle, and Textbook Takedown..haha.. don't ask! I also have methods reduceHealth() which controls nmber of health points. and an accessor method called getHealth(). Right now i'm working on the first class of the game. Later i'm going to write 4 more classes..aka levels to fight.(antagonists)..haha. here is my code for the first class called teacher:

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    class teacher{
    private:
      int health;
      string name;
    public:
      void showHealth();
      void reduceHealth(int damage);
      int block(int damage);
      int punch();
      int kick();
      int deafeningWhistle();
      int textbookTakedown();
      int getHealth();
      teacher(int _health, string _name);
      ~teacher();
    };
    
    teacher::teacher(){
    
      health = 200;
      name = "Professor Spartan";
    }
    
    ~teacher(){
    }
    
    int teacher::block(int damage){
    
      health = health - damage;
    
    }
    
    int teacher::punch(){
    
      health = health - 10;
    }
    
    int teacher::kick(){
    
      health = health - 15;
    }
    
    int teacher::defeningWhistle(){
    
      health = health - 25;
    }
    
    int teacher::textbookTakedown(){
    
      health = health - 50;
    }
    Am I using these right for the game? How do I go about using the Health Methods. I'd apreciate any help. Thanks! I'm so rusty...with C++ ughh..

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Here's a suggestion:

    Code:
    const unsigned int MAX_FIGHTER_HEALTH = 200;
    
    class Fighter
    {
      public:
        Fighter(std::string name_, const unsigned int health_ = MAX_FIGHTER_HEALTH)
          : name(name_), health(health_) 
        {
          InitializeFighterMoves( );
        }
    
        virtual void InitializeFighterMoves( ) = 0;
    
        bool Attack(Fighter* target, std::string move_type)
        {
            if(target != 0 && !(((rand() &#37; 100) + 1) % 10))
            {
              target->health -= move_list[move_type];
              return true;
            }
            return false;
        }
    
        const unsigned int GetHealth( ) const { return health; }
    
      public:
        std::string name;
    
      protected:
        unsigned int health;
        std::map<std::string, unsigned int> move_type;
    };

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Oh well since the OP didn't answer, I'd still like to get some feedback on that code. I think it's pretty neat but maybe I'm wrong.

    Edit: Just noticed the user could enter a stupid health value ( <= 0 ) without the program noticing. That could be ameliorated.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    @Desolation: What are you trying to do here?
    Code:
    !(((rand() &#37; 100) + 1) % 10)
    It basically generates a number from 0 to 9, inclusive. If you were trying to shift the distribution of the random numbers, it didn't work, because all you've done is eliminate 0 and add 100, both of which evaluate to 0 when fed through modulus 10.

    If all you wanted was something that happened one time in ten, consider
    Code:
    !(rand() % 10)
    or, perhaps more readably,
    Code:
    rand()% 10 == 0
    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.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    Wow, I must have been really tired when I coded that. I wanted to have a 10&#37; chance to block hits and I literally didn't 10 / 100 instead of 1 / 10

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    46

    Question

    Wow pretty cool code man. Thanks!! I'm trying to do a basic outline of my game and here is what I have so far.. some pretty funny attacks i know...

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    class teacher{
    protected:
      int health;
      string name;
    public:
      void showHealth();
      void reduceHealth(int damage);  
      int block(int damage);
      int punch();
      int kick();
      int deafeningWhistle();
      int textbookTakedown();
      int getHealth();
      teacher(int _health, string _name);
      ~teacher();
    };
    
    teacher::teacher(){
    
      health = 200;
      name = "Professor Spartan";
    }
    
    ~teacher(){
    }
    
    int teacher::block(int damage){
    
      health = health - damage;
    
    }
    
    int teacher::punch(){
    
      health = health - 10;
    }
    
    int teacher::kick(){
    
      health = health - 15;
    }
    
    int teacher::defeningWhistle(){
    
      health = health - 25;
    }
    
    int teacher::textbookTakedown(){
    
      health = health - 50;
    }
    
    class freshman::public teacher{
    
    public:
      int sissyslap();
    
    }
    
    freshman::freshman(){
    
      health = 50;
      name = "Freshman";
    
    }
    
    ~freshman(){
    }
    
    int freshman::block(int damage){
    
      health = health - damage;
    
    }
    
    int freshman::sissyslap(){
    
      health = health - 5;
    }
    
    
    class sophomore::public teacher{
    
    public:
      int sissykick();
    
    }
    
    sophomore::sophomore(){
    
      health = 75;
      name = "Sophomore";
    
    }
    
    ~sophomore(){
    }
    
    int sophomore::block(int damage){
    
      health = health - damage;
    
    }
    
    int sophomore::sissykick(){
    
      health = health - 5;
    }
    
    int sophomore::punch(){
    
      health = health - 10;
    }
    
    class junior::public teacher{
    
    public:
    
      int headbutt();
    
    }
    
    junior::junior(){
    
      health = 85;
      name = "Junior";
    }
    
    ~junior(){
    }
    
    int junior::block(int damage){
    
      health = health - damage;
    
    }
    
    int junior::headbutt(){
    
      health = health - 20;
    }
    
    int junior::punch(){
    
      health = health - 10;
    }
    
    int junior::kick(){
    
      health = health - 15;
    }
    
    class senior::public teacher(){
    
     public:
      int headbutt();
      int beercanblitz();
    }
    
    senior::senior(){
    
      health = 100;
      name = "SuperSeniors!";
    }
    
    ~senior(){
    
    }
    
    int senior::block(int damage){
    
      health = health - damage;
    
    }
    
    int senior::beercanblitz(){
    
      health = health - 5;
    }
    
    int senior::punch(){
    
      health = health - 10;
    }
    
    int senior::headbutt(){
    
      health = health - 20;
    }
    
    int senior::kick(){
    
      health = health - 15;
    }
    Now I'm having problems trying to make sure I have the Inheritance from the Public teacher class right. Basically I'm trying to figure out how I can create methods to keep track of my health and the four other classes. I also need to figure out how I'm going to block (which reduces the attack by 1/2) I want to start out with figuring out how to call them in my MAIN function to see if they work first..then get to the harder stuff. Thanks again for the help when I get to the actual fighting I'll be sure to look into your code! Any more help would be cool. ..P.S. I know my game is cheesy.. but its simple to learn with ( :

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I also need to figure out how I'm going to block (which reduces the attack by 1/2)
    If you wanted an attack to be blocked 50&#37; of the time, you could use something like this.
    Code:
    void take_damage(int damage) {
        if(rand() % 2 == 0) damage /= 2;
    
        health -= damage;
    }
    x -= y is the same as x = x - y, BTW.

    Code:
    class senior::public teacher(){
    I think it would work better if you used
    Code:
    class senior::public teacher {
    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.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    One thing: you have very many functions for different attacks but they all do the same thing: reduce some health. What you might do instead, for example, is to create a map that maps attack name together with the damage it does. This way you won't need separate fighter classes: instead a single class would behave differently by having a name/id ("Teacher", "Freshman" etc) and a different map of attack styles.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    46

    Question

    Alright I have one more question. How do I implement this game to go through the four levels in my INT MAIN? I mean I want to use a do while with a menu to play but I'm not sure how the classes work with a menu and such in INT MAIN...

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Alright I have one more question. How do I implement this game to go through the four levels in my INT MAIN? I mean I want to use a do while with a menu to play but I'm not sure how the classes work with a menu and such in INT MAIN...
    Well, a start would be something prompting each player for which type of person they want to use. Then you could create two (assuming a two-player game) instances of classes. Then you could prompt each player in turn for what they want to do. Then you could actually perform that action.

    This would all be a lot easier if you unified the attacks somehow as anon suggested. A map is probably the best way to do it. A map is just like an array, except the indicies into the data structure can be of any type instead of just ints. This means that you can create a map with the damages dealt by each type of attack, and have the name of the attack index the array. Then you just go
    Code:
    health -= attack[name];
    where name is "textbook takedown" or whatever. You could have the user type "textbook takedown" directly, and that would take care of a lot of your job.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 10:44 AM
  3. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  4. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM