My First Game --- C++

My First Game --- C++

Post by Muskelmann098 on Thu Dec 10, 2009 11:52 am
([msg=31226]see My First Game --- C++[/msg])

Hey,

Here is a game I wrote in C++ as a project to teach myself the syntax once and for all. It is a small game, kinda arcade style, but still "fun." Some of you might want to try it :D Anyway, while my syntax is correct, and everything works, there are some things I am not to happy about. You might notice I'm using a lot of "goto"s, and that is because I just didn't know how else to do it. If anyone could tell me a better way to do it, I would be very grateful. Also, if you find anything else you would have done different (probably a lot) then please tell me, and I will do my best to fix it.

Thanks, code to follow:
Code: Select all
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;
    //ATTACK VARIABLES-----------------------------------
    int kill_count = 0;
    int playerhp = 100;
    int playeratk;
    int enemyvalue = 280;
    int enemyhp = 100;
    int enemyatk;
    char fightorflee;
    int playwp_atk;
    int playwp_def;
    //Money Variables------------------------------------
    int money = 850;
    int playerxp = 0;
    int buywhich;
    //Weapon Variables-----------------------------------
    string weaponID1 = "Sword";
    int weaponID1_atk = 5;
    int weaponID1value = 600;
    string weaponID2 = "Axe";
    int weaponID2_atk = 10;
    int weaponID2value = 1000;
    string weaponID3 = "Shield";
    int weaponID3_def = 3;
    int weaponID3value = 300;
    //Menu Variables-------------------------------------
    int menu_desicion;
    string mainmenu = "Welcome to 'Fight Till You Die' V.1.2.0.  -  Copyright 2009||Muskelmann098 :)\n\n[MAIN MENU]\nFrom here you can decide whether to fight or to buy a new weapon.\n\n";
   
    //START MAIN FUNCTUION-------------------------------
   
int main(int argc, char *argv[], int attack_or_defend )
{
    main:
    int enemy_hp_org;
    enemy_hp_org = enemyhp;
    cout << mainmenu << "KILL STREAK:\nYou have killed " << kill_count << " enemies.\n\n(1)Weapons Store || (2)Play\n";
    cin >> menu_desicion;
    //STORE MENU------------------------------------------
   if(menu_desicion == 1){
                    system("CLS");
                cout << "Here you can buy weapons!\n\nNOTE:\nATTACK VALUES ONLY COUNT WHEN YOU CHOOSE 'ATTACK' DURING BATTLE.\nDEFENSE VALUES ONLY COUNT WHEN CHOOSING 'DEFEND'\n\nThe weapons that are availiable are:\n\n";
cout << "(You have " << money << " coins)\n"<< "\n(1) Sword  - Attack: +5  Defense +0  - Cost: 600\n(2) Axe    - Attack: +10 Defense +0  - Cost: 1000\n(3) Shield - Attack: +0  Defense +3  - Cost: 300\n";
                cout << "\n\nOr press (4) to go back to the main menu\n";
                cin >> buywhich;
                switch(buywhich){
                                case 1:
                                     if(money >= weaponID1value){
                                     money -= weaponID1value;
                                     playwp_atk = weaponID1_atk;
                                     cout << "You have bought a " << weaponID1 << "!\nYou have " << money << " coins left.\n\n\n\n\n";
                                     system("PAUSE");
                                     system("CLS");
                                     goto main;
                                     }
                                     else{
                                             cout << "\n\nYou do not have enough money to buy this weapon.\n";
                                             system("PAUSE");
                                             system("CLS");
                                             goto main;
                                             }
                                     break;
                                case 2:
                                     if(money >= weaponID2value){
                                     money -= weaponID2value;
                                     playwp_atk = weaponID2_atk;
                                     cout << "You have bought a " << weaponID2 << "!\nYou have " << money << " coins left.\n\n\n\n\n";
                                     system("PAUSE");
                                     system("CLS");
                                     goto main;
                                     }
                                     else{
                                             cout << "\n\nYou do not have enough money to buy this weapon.\n";
                                             system("PAUSE");
                                             system("CLS");
                                             goto main;
                                             }
                                     break;
                                case 3:
                                     if(money >= weaponID3value){
                                     money -= weaponID3value;
                                     playwp_def = weaponID3_def;
                                     cout << "You have bought a " << weaponID3 << "!\nYou have " << money << " coins left.\n\n\n\n\n";
                                     system("PAUSE");
                                     system("CLS");
                                     goto main;
                                     }
                                     else{
                                             cout << "\n\nYou do not have enough money to buy this weapon.\n";
                                             system("PAUSE");
                                             system("CLS");
                                             goto main;
                                             }
                                     break;
                                default:
                                        cout << "\n\n\n\n";
                                        system("CLS");
                                        goto main;
                                        }
                                       
   }
   //END STORE MENU ------------------------------------------------------
   else{
    cout << "You are under attack! You must fight!\n" << "Your Hitpoints:" << playerhp << "\nEnemy Hitpoints:" << enemyhp;
    //START ATTACK LOOP --------------------------------------------------
while (enemyhp >= 1 && playerhp >= 1){
      cout << "\n\nAttack (1) or Defend (2)\n";
      srand(time(NULL));
      cin >> attack_or_defend;
      system("CLS");
      //ATTACK/DEFEND SWITCH----------------------------------------------
      switch(attack_or_defend){
      case 1:
           if(enemyhp <= 0 || playerhp <= 0){
                      goto dead;}
                      else{
      playeratk = (rand() % 22 + playwp_atk);
      enemyatk = (rand() % 23 + 4);
      enemyhp = enemyhp - playeratk;
      cout << "\n\nYou did " << playeratk << " damage.\nEnemy hitpoints left: " << enemyhp;
      playerhp = playerhp - enemyatk;
      cout << "\n\nYou lost " << enemyatk << " damage. \nYour hitpoints: "<< playerhp << "  \n\n";
      }
      break;
     
      case 2:
           if(enemyhp <= 0 || playerhp <= 0){
                      goto dead;}
                      else{
      playeratk = (rand() % 3 + 1);
      enemyatk = (rand() % (6 - playwp_def) + (3 - playwp_def));
      enemyhp = enemyhp - playeratk;
      cout << "\n\nYou did " << playeratk << " damage.\nEnemy hitpoints left: " << enemyhp;
      playerhp = playerhp - enemyatk;
      cout << "\n\nYou lost " << enemyatk << " damage. \nYour hitpoints: "<< playerhp << "  \n\n";
      }
      break;
     
      default:
      cout << "Press (1) to attack and (2) to defend.";
      }
}
//END ATTACK LOOP -------------- START AFTER FIGHT CALCULATION -----------------------------------
if(playerhp > 0){
            playerxp = playerxp += 25;
            int loot = (enemyvalue + playerxp);                //IF PLAYER WINS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            money = money + loot;
            kill_count++;
           cout << "\n\nYOU WIN!\nCoins Collected from Enemy: " << loot << "\nNew Balance: " << money << " coins\n\n";
           cout << "Your hitpoints have been restored\nYour next enemy will be harder!\n\n\n";
           system("PAUSE");
           }
           else{
                dead:
                playerxp = playerxp -= 10;                     //IF PLAYER LOSES!!!!!!!!!!!!!!!!!!!!!!!!!!!
                kill_count = 0;
                cout << "\n\nYou are dead...\n\nYour health has been restored.\nHopefully, your next enemy will be easier!\n\n\n";
                system("PAUSE");
                }
}
//END AFTER FIGHT CALCULATIONS ----------- START CLEANUP PHASE --------------------------------
enemyhp = enemy_hp_org + playerhp;
playerhp = 100;
system("CLS");
goto main;
//END PROGRAM --------------------------------------------------------------------------------------------
}
Muskelmann098
Experienced User
Experienced User
 
Posts: 78
Joined: Mon Feb 02, 2009 9:39 am
Blog: View Blog (0)


Re: My First Game --- C++

Post by Goatboy on Thu Dec 10, 2009 1:04 pm
([msg=31229]see Re: My First Game --- C++[/msg])

Without reading too far into it, I see you use a lot of system("PAUSE") calls. There is a much better way to do this (and a lot prettier) and that is to use cin.get();

Code: Select all
#include <iostream>

using namespace std;

int main()
{
  cout << "Hello, world!";
  cin.get(); // Used instead of system("PAUSE")
  cout << endl << "How are you?";

  return 0;
}

This will pause the program without saying "Press any key to continue..." and it makes it more portable, for example from Windows to Linux.

As for all of those gotos, they were made for a reason and you can use them, but functions work much better. Read up on that.
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2782
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: My First Game --- C++

Post by Muskelmann098 on Thu Dec 10, 2009 2:17 pm
([msg=31231]see Re: My First Game --- C++[/msg])

Thanks for that, Goatboy. I'll implement the cin.get() and read up on functions ;) I considered trying to use them, but decided against it since I wasn't sure about how to do it.
Muskelmann098
Experienced User
Experienced User
 
Posts: 78
Joined: Mon Feb 02, 2009 9:39 am
Blog: View Blog (0)


Re: My First Game --- C++

Post by The Riviera Kid on Fri Dec 11, 2009 2:47 pm
([msg=31262]see Re: My First Game --- C++[/msg])

Hi Muskelmann,
I've took a look at your code a little, and printed below a re-write of it, just a few fixes, for example
Code: Select all
Weapon weapon[countWeapons] =
   {
      Weapon("Sword", 5, 0, 600),
      Weapon("Axe", 10, 0, 1000),
      Weapon("Shield", 0, 3, 300),
   };

that was the main modification to the code, just look through the rest of the code, this array makes it easier to handle adding new weapons, and printing information about them.
I am by no means a professional programmer and the new code isn't great but this is just one aspect in which it can be better (I trimmed quite a few lines out of the shop section)
I cut the 'if' out of the 'switch(attack_or_defend)' section and tried to give the code some structure.
I suggest you read up on some coding conventions, I don't really know what convention I use (there are quite a few) but I have adopted a convention based on everything i've picked up on the internet and from university.

If you want to improve the code further I would suggest:
- Creating a loop to replace the 'goto main;' statements
- Storing the character data in some struct or class
- The weapon class I created should really have private sections and access functions
- Make the code more modular (create functions to handle the shop and battle states of the program)

Code: Select all
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <string>

using namespace std;

class Weapon
{
public:
   string name;
   int attack;
   int defence;
   int value;
   Weapon(string newName, int newAtk, int newDef, int newValue)
   {
      name = newName;
      attack = newAtk;
      defence = newDef;
      value = newValue;
   }
};
   //ATTACK VARIABLES-----------------------------------
   int kill_count = 0;
   int playerhp = 100;
   int playeratk;
   int enemyvalue = 280;
   int enemyhp = 100;
   int enemyatk;
   char fightorflee;
   int playwp_atk;
   int playwp_def;
   //Money Variables------------------------------------
   int money = 850;
   int playerxp = 0;
   int buywhich;
   //Weapon Variables-----------------------------------
   const int countWeapons = 3;
   Weapon weapon[countWeapons] =
   {
      Weapon("Sword", 5, 0, 600),
      Weapon("Axe", 10, 0, 1000),
      Weapon("Shield", 0, 3, 300),
   };
   
   string weaponID1 = "Sword";
   int weaponID1_atk = 5;
   int weaponID1value = 600;
   string weaponID2 = "Axe";
   int weaponID2_atk = 10;
   int weaponID2value = 1000;
   string weaponID3 = "Shield";
   int weaponID3_def = 3;
   int weaponID3value = 300;

   //Menu Variables-------------------------------------
   int menu_desicion;
   string mainmenu = "Welcome to 'Fight Till You Die' V.1.2.0.  -  Copyright 2009||Muskelmann098 :)\n\n[MAIN MENU]\nFrom here you can decide whether to fight or to buy a new weapon.\n\n";
   
   //START MAIN FUNCTUION-------------------------------
   
int main(int argc, char *argv[], int attack_or_defend )
{
   main:
   int enemy_hp_org;
   enemy_hp_org = enemyhp;
   cout << mainmenu << "KILL STREAK:\nYou have killed " << kill_count << " enemies.\n\n(1)Weapons Store || (2)Play\n";
   cin >> menu_desicion;
   //STORE MENU------------------------------------------
   if(menu_desicion == 1)
   {
      system("CLS");
      cout << "Here you can buy weapons!\n\nNOTE:\nATTACK VALUES ONLY COUNT WHEN YOU CHOOSE 'ATTACK' DURING BATTLE.\nDEFENSE VALUES ONLY COUNT WHEN CHOOSING 'DEFEND'\n\nThe weapons that are availiable are:\n\n";
      cout << "(You have " << money << " coins)\n"<< "\n";
      for (int i = 0; i < countWeapons; i++)
         cout<<"("<<i<<") "<<weapon[i].name<<"  - Attack: +"<<weapon[i].attack<<"  Defense +"<<weapon[i].defence<<"  - Cost: "<<weapon[i].value<<"\n";
      cout << "\n\nOr press ("<<countWeapons<<") to go back to the main menu\n";
      cin >> buywhich;
      if (2 == buywhich || 1 == buywhich || 0 == buywhich) // This could be better, please suggest fixes
      {
         if (money >= weapon[buywhich].value)
         {
            money -= weapon[buywhich].value;
            playwp_atk = weapon[buywhich].attack;
            cout<<"You have bought a "<<weapon[buywhich].name<<"!\nYou have "<<money<<" coins left.\n\n\n\n\n";
            system("PAUSE");
            system("CLS");
            goto main;
         }
         else
         {
            cout<<"\n\nYou do not have enough money to buy this weapon.\n";
            system("PAUSE");
            system("CLS");
            goto main;
         }
      }
      else
      {
         cout << "\n\n\n\n";
         system("CLS");
         goto main;
      }
   }
//END STORE MENU ------------------------------------------------------
   else
   {
      cout << "You are under attack! You must fight!\n" << "Your Hitpoints:" << playerhp << "\nEnemy Hitpoints:" << enemyhp;
//START ATTACK LOOP --------------------------------------------------
      while (enemyhp >= 1 && playerhp >= 1)
      {
         cout << "\n\nAttack (1) or Defend (2)\n";
         srand(time(NULL));
         cin >> attack_or_defend;
         system("CLS");
//ATTACK/DEFEND SWITCH----------------------------------------------
         switch(attack_or_defend)
         {
         case 1:
            playeratk = (rand() % 22 + playwp_atk);
            enemyatk = (rand() % 23 + 4);
            enemyhp = enemyhp - playeratk;
            cout << "\n\nYou did " << playeratk << " damage.\nEnemy hitpoints left: " << enemyhp;
            playerhp = playerhp - enemyatk;
            cout << "\n\nYou lost " << enemyatk << " damage. \nYour hitpoints: "<< playerhp << "  \n\n";
            break;
         case 2:
            playeratk = (rand() % 3 + 1);
            enemyatk = (rand() % (6 - playwp_def) + (3 - playwp_def));
            enemyhp = enemyhp - playeratk;
            cout << "\n\nYou did " << playeratk << " damage.\nEnemy hitpoints left: " << enemyhp;
            playerhp = playerhp - enemyatk;
            cout << "\n\nYou lost " << enemyatk << " damage. \nYour hitpoints: "<< playerhp << "  \n\n";
            break;
         default:
            cout << "Press (1) to attack and (2) to defend.";
         }
      }
//END ATTACK LOOP -------------- START AFTER FIGHT CALCULATION -----------------------------------
      if(playerhp > 0)
      {
         playerxp = playerxp += 25;
         int loot = (enemyvalue + playerxp);            //IF PLAYER WINS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
         money = money + loot;
         kill_count++;
         cout << "\n\nYOU WIN!\nCoins Collected from Enemy: " << loot << "\nNew Balance: " << money << " coins\n\n";
         cout << "Your hitpoints have been restored\nYour next enemy will be harder!\n\n\n";
         system("PAUSE");
      }
      else
      {
         dead:
         playerxp = playerxp -= 10;                //IF PLAYER LOSES!!!!!!!!!!!!!!!!!!!!!!!!!!!
         kill_count = 0;
         cout << "\n\nYou are dead...\n\nYour health has been restored.\nHopefully, your next enemy will be easier!\n\n\n";
         system("PAUSE");
      }
   }
//END AFTER FIGHT CALCULATIONS ----------- START CLEANUP PHASE --------------------------------
   enemyhp = enemy_hp_org + playerhp;
   playerhp = 100;
   system("CLS");
   goto main;
//END PROGRAM --------------------------------------------------------------------------------------------
}
"You can't even find a typecast error!"
User avatar
The Riviera Kid
New User
New User
 
Posts: 24
Joined: Sat Jun 20, 2009 9:36 pm
Location: Ellesmere Port, North West England
Blog: View Blog (0)


Re: My First Game --- C++

Post by Muskelmann098 on Sun Dec 13, 2009 6:09 am
([msg=31316]see Re: My First Game --- C++[/msg])

Thanks a lot for that, Riviera Kid, that really helps. As you have probably guessed, I'm 100% self-taught, so I have never been taught any formal conventions or the like. I will definitely read up on it online, though.

Again, thanks a lot. I will be sure to implement your tips.
Muskelmann098
Experienced User
Experienced User
 
Posts: 78
Joined: Mon Feb 02, 2009 9:39 am
Blog: View Blog (0)


Re: My First Game --- C++

Post by thedotmaster on Sun Dec 13, 2009 6:52 am
([msg=31317]see Re: My First Game --- C++[/msg])

You use a lot of system commands, that isn't good. It makes the code entirely unportable when really, it should be very portable.
Image
User avatar
thedotmaster
Contributor
Contributor
 
Posts: 984
Joined: Sun May 04, 2008 4:39 pm
Location: North West UK
Blog: View Blog (1)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests