My First game. c++

Questions about programming languages and debugging
Post Reply
p99
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 291
Joined: 14 Oct 2006, 16:00
17
Location: Some hippy's van
Contact:

My First game. c++

Post by p99 »

Well I started to learn C++. Here is my very first game ever in any language.

Code: Select all

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>

using namespace std;

float percent = 0.1;
double lvl = 1;
double exp =  0;
double maxexp = 10;
double strength = 1;
double monst_strength = 1;
double monst_power = monst_strength + (lvl/2);
double power = strength + (lvl/2);
double health = 10;
double total_health = 10;
double monsterhealth = 5;
double monst_max_hp = 5;
double monstgive = 1;



int level(){
    cout << "\nWoot you leveled.\n" << flush;
    // lvl function
    lvl += 1;
    exp = 0;
    maxexp = (maxexp*lvl)/3;
    strength += 2;
    monst_strength += 2;
    total_health += 2;
    health = total_health;
    monst_max_hp += 2;
    monsterhealth = monst_max_hp;
    percent += 0.01;
    monstgive +=1;
    cout << "Your current stats are:\nLvl: " << lvl << "\nExperience: " << exp << "/" << maxexp << "\nHealth: " << "/" << total_health << "\nStrength: " << strength << "\nDefense: " << percent << "\n" << flush;
    //end lvl function
}


int Attack(){
         double dmg;
         int x;
         srand(time(NULL));
         x = rand() % 5;
         if (x == 0){//if 2
            dmg = power;
         }else if (x == 1){
               dmg = power -1;
         }else if (x == 2){
               dmg = power + 1;
         }else if (x == 3){
               dmg = power*2;
         }else if (x == 4){
               dmg = 0;
               cout << "The monster blocked your blow.\n"  << endl;
               if (lvl > 1){//if 3
                  exp -= monstgive;
               }else{
               exp--;
               }// end if 3
         }//end if 2
                   
         monsterhealth -= dmg;
         cout << "\n You attack:\nYou deal " << dmg << " damage " << flush;
         if (monsterhealth <=0){//if 4
            cout << "\nGreat you killed the monster, but a new one has appeared." << endl;
            monsterhealth = monst_max_hp;
            cin.get();
         }else{
               cout << "leaving the monster with " << monsterhealth << " health.\n\nNow the monster will attack.\n" << flush;
               double monst_dmg;
               int y;
               srand(time(NULL));
               y = rand() % 5;
               if (y == 0){//if 5
                  monst_dmg = monst_power;
               }else if (y == 1){
                     monst_dmg = monst_power - 1;
               }else if (y == 2){
                     monst_dmg = monst_power + 1;
               }else if (y == 3){
                     monst_dmg = monst_power*2;
               }else if (y == 4){
                     monst_dmg = percent;
                     cout << "Miss\n" << endl;
               }// end if 5
                         
               health = health - monst_dmg + percent;
               if (health <= 0){//if 6
                  string hmm;
                  cout << "The monster attacks you for " << monst_dmg - percent << " damage.\n" <<  "Well you just got pwned. Now start over ;-]" << endl;
                  cin >> hmm;
                  exit(1);
               }else{
                     cout << "The monster attacks you for " << monst_dmg - percent << " damage  leaving you with " << health << " health.\n" << flush;
               }// end if 6
         }//end if 4
                   
         exp += monstgive;
         cout << "\nYou now have " << exp << "/" << maxexp << " experience." << endl;
         if (exp >= maxexp){//if 7
            level();
         }// end if 7 
}

int Start(){
    cout << "Starting a new game..." << endl;
    string char_name;
    cout << "What will you name your character?\nName: " <<flush;
    cin >> char_name;
    cin.ignore();
    
    ofstream chars;
    chars.open("Charactesr.9mm", ofstream::app);
    chars << char_name << endl;
    chars.close();
    string task;
    int hold = 1;
    cout << "Your options are as follows:\n1. Attack\n2. Shop - Not implemented\n3. Sleep - Raises health by 3. Potions??\n4. Stats\n5. Rules\n6. Quit\n" << flush;
    while (hold == 1){//while 1
          cout << "-$ " << flush;
          cin >> task;
          if (task == "4"){//if 1
             cout << "Your current stats are:\nLvl: " << lvl << "\nExperience: " << exp << "/" << maxexp << "\nHealth: " << "/" << total_health << "\nStrength: " << strength << "\nDefense: " << percent << "\n" << flush;
             }else if ( task == "1"){
                   Attack();
             }else if (task == "2"){
                   cout << "I did say \"Not Implemented\"!\n" << flush;
             }else if (task == "3"){
                   //sleep function
                   cout << "\nNighty night ;-]\n" << flush;
                   health += 3;
                   if ( health >= total_health){
                        health = total_health;
                        cout << "\nNice try but your at max HP.\n" << flush;
                   }
                   cout << "Health +3\nYou now have " << health << " health." << endl;
                   // end sleep function
             }else if (task == "6"){
                   string ans;
                   int x = 1;
                   while ( x == 1){
                         cout << "You sure? y/n: " << flush;
                         cin >> ans;
                         if ( ans == "y"){
                              cout << "bye\n" << endl;
                              x = 2;
                              hold = 2;
                              return 0;
                         }else if ( ans == "n"){
                               cout << "Ok just a bit longer...\n" << flush;
                               x = 2;
                         }else{
                               cout << "WTF enter the right answer!\n" << flush;
                         }
                   }
             }else if (task == "5"){
                   cout << "Well being that this game is rather simple i'll spare you the technical junk. You really only need to know them if you plan on developing the 'game' further. So...\n\nRules of fighting:\n-------------------\nWhen you choose to attack a monster it will counter your attack with it's own. Keep in mind that a critical hit means double damage and can occur regularely.\n\nSleeping:\n---------\nSleeping raises your health by 3. Simple enough right? Well this will suck at high level but who's gonna play that long? Maybe i'll add in potions. But that will be much later.\n\nOther:\n------\nJust so you know your stats do increase upon leveling (maybe i'll add the ability to buy stats). Then monsters get stringer as do you. That is all, numbers change... Hopefully i'll make an option to save the game (which is proving to be difficult). But whatever I make the rules :-)\n\nJust enjoy!" << endl;
             }else if (task == "8"){
                   cout << "Your options are as follows:\n1. Attack\n2. Shop - Not implemented\n3. Sleep - Raises health by 3. Potions??\n4. Stats\n5. Rules\n6. Quit\n" << flush;
             }else{
                   cout << "Invalid command! Press 8 for commands" << endl;
             

}}}
             
                               
int main(){
    cout << "Welcom to \"A-Game\"! (alpha)\nCreated by p99\nYour options are as follows:\n1. Start\n2. Load\n3. Info\n4. exit\n" << endl;
    string starting_choice;
    begin:
          cout << "-$ " << flush;
          cin >> starting_choice;
          cin.ignore();
          
          if (starting_choice == "1"){
                              Start();
          }else if (starting_choice == "2"){
                //Load();
          }else if (starting_choice == "3"){
                //Info();
          }else if (starting_choice == "4"){
                return 0;
          }else{
                cout << "Wtf, That wasn't valid!" << endl;
                goto begin;
          }
}
Enjoy. Took about a month on account of little computer use and still learning c++.

User avatar
Nerdz
The Architect
The Architect
Posts: 1127
Joined: 15 Jun 2005, 16:00
18
Location: #db_error in: select usr.location from sucko_member where usr.id=63;
Contact:

Post by Nerdz »

That look nice. You said it was your first game ever? It seems really interesting!

You may want to search a bit for
switch : http://tinyurl.com/275uv7
classes : http://www.cplusplus.com/doc/tutorial/classes.html

And, try to not use global variable... you'll find a way to get ride of them with constant and Oriented Object Programming.
Give a man a fish, you feed him for one day.
Learn a man to fish, you feed him for life.

p99
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 291
Joined: 14 Oct 2006, 16:00
17
Location: Some hippy's van
Contact:

Post by p99 »

Yeah it's my first attempt at a game. YEah I need to learn to use switches but classes are hard for me to create and implement. I know I could create classes for the attacking part but I hate the return value thing.

Post Reply