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
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 --------------------------------------------------------------------------------------------
}





