Level Up Problem
Feb 29, 2012 at 8:53pm UTC
Hi, I'm new to programming and have been following some tutorials on youtube. After doing the first few, I thought I'd put my knowledge to the test to see if I'd been learning what I was watching. I have decided to put together a little crafting game using very...very little knowledge. (Mainly just loops and functions, so bear that in mind when answering)
I have the harvesting and refining of the wood done, along with a sleep function so that the player can't harvest wood as fast as they can click. (I sped it up for testing purposes)
I have also got it adding to my total woodcutting xp counter and my "xp to level" counter. However, I can't seem to make it level me up once the "xp to level" counter reaches/dips below zero. I've tried a few things already, although I just can't get it working. Any help would be much appreciated, along with any improvements. (I know I could use cases instead of "if loops" for the menu's, but I like the look of "if loops"...There is also a small bug if you try to refine 0 wood, although I can fix that myself.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
#include <iostream>
#include <cmath>
#include <windows.h>
using namespace std;
void inventoryCheck();
void gatherWood();
void refineWood(int );
void craftMenu();
void skillMenu();
//Skills
int woodcuttingLevel = 1;
int woodcuttingEXPtoLevel = 40 * woodcuttingLevel;
int woodcuttingEXPgained = 0;
int woodcuttingEXPtotal = 0;
int logXP = 10;
//Wood Resources
int totalWood = 0;
int totalRefinedWood = 0;
int gatherWoodNumber = 5;
int main()
{
int menuItem;
int quitGame = 0;
while (quitGame == 0)
{
cout << endl << "[1] Check Inventory" << endl;
cout << "[2] Craft Menu" << endl;
cout << "[3] Skill Menu" << endl;
cout << "[4] Quit Game" << endl;
cin >> menuItem;
if (menuItem == 1)
{
inventoryCheck();
}
if (menuItem == 2)
{
craftMenu();
}
if (menuItem == 3)
{
skillMenu();
}
if (menuItem == 4)
{
quitGame = 1;
}
}
}
void skillMenu()
{
cout << endl;
cout << "Your level in Woodcutting is: " << woodcuttingLevel <<endl;
cout << "You have: " << woodcuttingEXPtotal << " xp." << endl;
cout << "You need: " << woodcuttingEXPtoLevel - woodcuttingEXPgained << " xp to level up." << endl;
}
void craftMenu()
{
int craftMenuItem;
int craftMenuToMainMenu = 0;
while (craftMenuToMainMenu == 0)
{
cout << "[1] Gather Wood" << endl;
cout << "[2] Refine Wood" << endl;
cout << "[3] Back to Main Menu" << endl;
cin >> craftMenuItem;
if (craftMenuItem == 1)
{
gatherWood();
}
if (craftMenuItem == 2)
{
refineWood(totalWood);
}
if (craftMenuItem == 3)
{
craftMenuToMainMenu = 1;
}
}
}
void inventoryCheck()
{
cout << endl;
if (totalWood > 0)
{
cout << "You have: " << totalWood << " Wood." << endl;
}
if (totalRefinedWood > 0)
{
cout << "You have: " << totalRefinedWood << " Refined wood." << endl;
}
if (totalWood == 0 && totalRefinedWood == 0)
{
cout << "You have nothing in your inventory." << endl;
}
}
void gatherWood()
{
int backToMenu = 0;
int choiceGatherWood;
while (backToMenu == 0)
{
cout << endl << "Gathering Wood...3" ;
Sleep(100);
cout << endl << "Gathering Wood...2" ;
Sleep(100);
cout << endl << "Gathering Wood...1" ;
Sleep(100);
cout << endl << "You gathered: " << gatherWoodNumber << " wood!" << endl;
cout << endl << "You gained " << logXP << " woodcutting xp!" << endl;
woodcuttingEXPtotal += logXP;
woodcuttingEXPgained += logXP;
totalWood += gatherWoodNumber;
cout << endl << "Would you like to gather more wood?" << endl;
cout << "[1] Yes \n[2] No \n" ;
cin >> choiceGatherWood;
if (choiceGatherWood == 1)
{
backToMenu = 0;
}
else
{
backToMenu = 1;
}
}
}
void refineWood(int x)
{
int refineAmount;
int refineMoreWood = 1;
while (refineMoreWood == 1)
{
cout << endl << "You have: " << totalWood << " wood." << endl;
cout << "How much wood would you like to refine?" << endl;
cin >> refineAmount;
int refineTime = refineAmount * 100;
if (refineAmount <= totalWood)
{
totalRefinedWood += refineAmount;
totalWood -= refineAmount;
cout << "Refining Wood..." << endl;
Sleep(refineTime);
refineMoreWood = 0;
}
else
{
cout << "You do not have enough wood, please enter a lower number." << endl;
}
}
}
Feb 29, 2012 at 9:34pm UTC
1 2 3 4 5 6 7 8 9 10 11 12
void gainEXP(int amount)
{
woodcuttingEXPtotal += amount ;
woodcuttingEXPgained += amount ;
if ( woodcuttingEXPgained > woodcuttingEXPtoLevel )
{
++woodcuttingLevel ;
woodcuttingEXPgained = woodcuttingEXPgained - woodcuttingEXPtoLevel;
woodcuttingEXPtoLevel = 40* woodcuttingLevel ;
}
}
Then, in places where you gain experience, such as in line 119 - 120 you replace those lines with a call to gainEXP(logXP).
Feb 29, 2012 at 10:05pm UTC
Thanks cire. Easily solved my problem without over complicating things :)
Topic archived. No new replies allowed.