Sign in to follow this  

items for rpg

This topic is 3403 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey i'm using dev c++ to make a text base rgp and i was wondering what would be the best way to store items like gold and potions using an array ect.. and also how can i use it in combat situations ?

Share this post


Link to post
Share on other sites
The optimal way would be to use a heterogeneous list of inventory:


// Main game class that handles the main game mechanics and such.
class game
{
/* ... */
};

// Game object
class entity
{
protected:
game &m_game; // This object's local instance of game (in case there are multiple instances of "game")

public:
entity( game &g ) : m_game( g ) { }
virtual ~entity() { }

virtual bool step( double dt ) = 0; // Advance this object's logic
virtual void draw( const camera &cam ) = 0; // Draw this object using "cam"
};

// This class contains the common properties between the player and monsters
class player_base : public entity
{
protected:
int hp, mp, sp; // etc...

public:
player_base( game &g, /* ... */ ) : entity( g ) { }
virtual ~player_base() { }

int &get_hp() { return hp; }
};

// The player, who moves around accomplishing goals
class player : public player_base { /* ... */ };

// Rats you occasionally battle in some grass
class npc_giant_rat : public player_base { /* ... */ };

// This class contains the common properties between all times of items
class item : public entity
{
protected:
player_base *m_owner; // Who's carrying this item, 0 if it's laying out in the world

public:
item( game &g, player_base *owner = 0 ) : entity( g ), m_owner( owner ) { /* ... */ }
virtual ~item() { /* ... */ }

virtual const char *get_name() const = 0; // Returns the specific name of the item
virtual const char *get_description() const = 0; // Returns the description of what this item does

// If any of these three functions return true, the
virtual bool consume( player_base *target ) const { return false; } // Uses the item, target specifies who to use it on
virtual bool equip( player_base *target ) const { return false; } // Modifies the target's stats
virtual bool unequip( player_base *target ) const { return false; } // Unmodifies the changes to the target's stats

virtual bool is_equipable() const = 0; // Ask if this item can be equipped, such as a weapon
virtual bool is_consumable() const = 0; // Ask if this item can be consumed, such as a potion
};

// Sample item that recovers a target's hp
class item_potion : public item
{
protected:

public:
item_potion( game &g, player_base *parent = 0 ) : item( g, parent ) { }
~item_potion() { }

const char *get_name() const { return "Potion"; }
const char *get_description() const { return "Recovers +12 HP"; }

bool consume( player_base *target )
{
if ( m_parent )
{
m_parent->get_hp() += 12;
}

return false; // destroy item
}

void draw( const camera &cam ) {}
bool step( const double dt ) { return false; }

bool is_equipable() { return false; }
bool is_consumable() { return true; }
};




Oops, sorry got carried away; I've got to run now, sorry for any mistakes, but now you can:


std::list<item*> m_items;
// OR, if you the list to take ownership of the objects:
// boost::ptr_list<item> m_items;

m_items.push_back( new item_potion( /* ... */ ) );
m_items.push_back( new item_sword( /* ... */ ) );







EDIT:
Cleaned up the code a bit.
And documented it.
And fixed a small pointer issue.


[Edited by - _fastcall on August 15, 2008 2:01:18 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by idono
yeah its no compiling for me i must be doing something wrong?


I hope you didn't copy and pasted that code... I have no idea how your code works or its coding style, how am I supposed to know if my code will compile for your project? [smile] That's why I posted in pseudo-code; its not supposed to work -- you have to fill in the /* ... */ yourself. Sorry if I failed to mention that.

But, the general idea is there: Use inheritance so you can store a heterogeneous list -- a list containing multiple-types -- of items. Create a base class to generalize the common properties of every item. (Such as the item's name, its description, can it be equipped, and etc.)

Share this post


Link to post
Share on other sites
i was actually using it as an example, but i did not fully understand the logic of the code you wrote i spent some time today trying to understand them

Share this post


Link to post
Share on other sites
Well I was thinking about using a structure instead.

// Potion structure
struct _Potion
{
string name; // Potion name
int effect; // How much HP it heals or what not
// Extra Things
/* ....... */
};

Share this post


Link to post
Share on other sites
The potion structure type might work, but then you need a list of potions, a list of weapons, a list of ___. That might be what you need, although for a good, dynamic, single object type list the inheritance model would be preferred.

I don't know what else I can add that the others haven't covered (and believe me I was looking!). If you are trying to understand it, don't look at the methods right away, look at the classes and how they're inherited so you have an understanding of their layout, then look at only a few methods and how the inheritance comes into play, etc... Just break it down piece by piece starting at the highest level and drilling down.

Share this post


Link to post
Share on other sites
Hi,

_fastcall: Don't you think that your entity class in too dependent of your game framework ?

BioX : You should try out your idea if you don't understand _fastcall concept.

Share this post


Link to post
Share on other sites
Quote:
Original post by _fastcall
But, the general idea is there: Use inheritance so you can store a heterogeneous list -- a list containing multiple-types -- of items. Create a base class to generalize the common properties of every item. (Such as the item's name, its description, can it be equipped, and etc.)


I agree completely with the general idea; listen to _fastcall.

However, in my RPG engine, my hierarchy only goes up to class item; there is no generic class entity. The completely generic hierarchy, where everything is an entity, _could_ be too generic, depending on how your system works.

For instance, in my engine, there's no need to step() or draw() a potion, which just sits in inventory until used, whereas that's certainly important for a character. If my hierarchy included a generic "entity", I would either have some useless step() code, or some useless code to call step() all over the place.

Of course, there are drawbacks; if I ever decide that a character should be able to place another character in it's inventory, I'll have some tricky special case code to write!

Anyways, I definitely agree with the concept _fastcall is putting forward, but do carefully consider your game design when setting up your class hierarchy.

Thanks,
Geoff

Share this post


Link to post
Share on other sites
Quote:
Original post by gdunbar
For instance, in my engine, there's no need to step() or draw() a potion, which just sits in inventory until used,


I considered the case that an item might be laying out in the world, without an owner.

Quote:
Original post by lollan
_fastcall: Don't you think that your entity class in too dependent of your game framework ?


What do you mean? I don't see why it shouldn't be... If there was the existence of more than one game class (such as save states), then we wouldn't want an entity spawning its children across a different game state, would we? [smile] In my point of view, I'd rather have a local reference to the entity's creator rather than a GetCurrentGameState() function or a global game* pointer. If you have an alternative solution, I'd love to hear it.

Quote:
Original post by idono
i did not fully understand the logic of the code you wrote i spent some time today trying to understand them


You don't have to use the code, if you don't want to / don't understand it. There's always more than one solution. I just prefer the object-oriented-programming approach.

OOP is difficult to learn at first, but once you learn it; you'll never program without it. We'll be glad to help you out -- tell us which parts you do and don't understand about the code.

Share this post


Link to post
Share on other sites
this store would be a funtion that the player would return to in the text based game but there are many issues with it is there a better way that i could do this?



#include<iostream>
using namespace std;
int main()
{
int health,num,gold,potions;
char buy;

cout<<"gold then potions you have now for demo sake"<<endl<<endl<<endl;

cin >>gold;//obtained gold and potion in other part of the game
cin >>potions;

cout<<endl;

cout<<"you now have "<<gold<< " gold to play with"<<endl;
cout<<"you have "<< potions <<" potions"<<endl;//tells the player what he has in invintory

cout<<"welcom to me strore"<<endl;

cout<<"Would you like to buy a health potion?"<<endl;

//choice of thing he wants to buy in the store right now just a potion for (demo)nstration
cout<<"to buy a health potion for 5 gold pres B"<<endl;

//simple yes no question
cin >>buy;

//if the player has no gold to spend
if(gold < 5)
{
cout<<"sorry you dont have the gold to buy me potions..."<<endl;
cout<<"Get Out"<<endl;
system("pause");
return 0;//would in a real game would return somewhere else.
}

else if(buy == 'b' ||buy == 'B')
{
int potion,gold;

cout<<"you bought a health potion"<<endl;
gold - 5;
potion + 1;


cout<<"you now have "<<gold<< " gold to play with"<<endl;
cout<<"you now have "<<potions<<" potions"<<endl;
system("pause");
return 0;
///option to leave the store blah blah
}

//if the player is just in the store
else cout<<"then go away ye scarrvy dog!!"<<endl;
system("pause");
return 0;

}

Share this post


Link to post
Share on other sites
#include<iostream>
using namespace std;
int main()
{
int health,num,gold,potions;
char buy;

cout<<"gold then potions you have now for demo sake"<<endl<<endl<<endl;

cin >>gold;//obtained gold and potion in other part of the game
cin >>potions;

cout<<endl;

cout<<"you now have "<<gold<< " gold to play with"<<endl;
cout<<"you have "<< potions <<" potions"<<endl;//tells the player what he has in invintory

cout<<"welcom to me strore"<<endl;

cout<<"Would you like to buy a health potion?"<<endl;

//choice of thing he wants to buy in the store right now just a potion for (demo)nstration
cout<<"to buy a health potion for 5 gold pres B"<<endl;

//simple yes no question
cin >>buy;

//if the player has no gold to spend
if(gold < 5)
{
cout<<"sorry you dont have the gold to buy me potions..."<<endl;
cout<<"Get Out"<<endl;
system("pause");
return 0;//would in a real game would return somewhere else.
}

else if(buy == 'b' ||buy == 'B')
{
int potion,gold;

cout<<"you bought a health potion"<<endl;
gold - 5;
potion + 1;


cout<<"you now have "<<gold<< " gold to play with"<<endl;
cout<<"you now have "<<potions<<" potions"<<endl;
system("pause");
return 0;
///option to leave the store blah blah
}

//if the player is just in the store
else cout<<"then go away ye scarrvy dog!!"<<endl;
system("pause");
return 0;

}



Share this post


Link to post
Share on other sites
Well, first of all, you can read up on object-oriented-programming and/or C++ Then creating the classes "player", and "store" make programming fun and easier to manage!

(This code sample actually compiles and runs!)

#include <iostream>
using namespace std;

// Ok so here we go.


// We're going to have an object to represent the player and his/her attributes:
class player
{
public:
// The player will have potions and gold variables
int potions;
int gold;

// You can also have variables to describe the player's race, class, and name
// But for now, we'll keep it simple

public:
// Here is what's called a "constructor," this is called when the player is created
// We want the progammer (aka you) to specify the starting amounts of the player's
// gold and potion, thus the "int p" and "int g" parameters
player( int p, int g )
{
// Set potions and gold
potions = p;
gold = g;

// Notify the console:
cout << "Player was created with " << potions << " potions and " << gold << " gold." << endl;
}

// Here is what's called the "destructor," this is called when the player class is destroyed.
~player()
{
cout << "Player was destroyed :.(" << endl;
}

// Here we have a utility function to display the player's current gold and potions
void display_status()
{
cout << "You have " << gold << " gold and " << potions << " potions" << endl;
}
};

// Now we're going to create a store class that handles the store operations and other such etc
class store
{
private:
// The members of a store object
// Note, this is listed under "private," which means that only the functions below can access this variable
int potions_in_stock;

public:
// The constructor
store( int num_potions )
{
// Stock up on potions!
potions_in_stock = num_potions;

cout << "A store was created with " << potions_in_stock << " potions in stock." << endl;
}

// The destructor
~store()
{
cout << "The store was destroyed :.(" << endl;
}

// The player parameter is to specify which player is entering this store
// The '&' stands for "reference to" which means that we'll directly be
// modifying the player, and not a copy of it.
// by changing it to "player the_player" we're going to be working with
// a copy of the player that is passed, which isn't what we want, we want
// to modify the object itself.
void player_enters( player &the_player )
{
// Welcome message
cout << endl;
cout << "Welcome to my store!" << endl;

// Check to see if we have some potions:
if ( potions_in_stock == 0 )
{
// We don't
cout << "Unfortunately, I'm out of potions!" << endl;
}
else if ( the_player.gold < 5 ) // (We have potions and then) Check to see if the player has money
{
// He/she doesn't have money
cout << "Unfortunately, you requires more moneys" << endl;
}
else // (The player has money and we have potions)
{
// This is the main loop, which will run forever until this boolean turn false (or the player buys all the potions)
bool player_is_still_buying = true;
while ( player_is_still_buying && potions_in_stock > 0 ) // Reads as while the player is still buying and we still have potions
{
// Display player's status:
cout << endl;
the_player.display_status();

if ( the_player.gold < 5 ) // The player is unable to buy any more potions
{
cout << "Looks like you're out of money lol" << endl;
player_is_still_buying = false;
break; // Force out of the "while" loop
}

// Display the menu
cout << endl;
cout << "What would you like to buy?" << endl;
cout << "1) Potion (" << potions_in_stock << ")" << endl;
cout << "2) Leave" << endl;
cout << endl;
cout << "Enter choice: ";

// Get a choice from the user
int choice;
cin >> choice;

if ( choice == 2 ) // Player decides to leave
player_is_still_buying = false; // When this is set to false, this "while" loop will stop running
else
{
// Player decides not to leave

if ( choice == 1 ) // Player decides to buy a potion
{
the_player.gold -= 5; // Subtract the money out of the player
the_player.potions += 1; // Add a potion to the player
potions_in_stock -= 1; // Subtract a potion out of the store's stock

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "You just bought my last potion! D:" << endl;
player_is_still_buying = false; // Nothing left to buy!
}
else
cout << "Thank you for your business" << endl;
}
}
// (This is the end of the while loop, if all those '}' throw you off)
}
}

// Player is all done here.
cout << "Goodbye now!" << endl;
}
};


int main( int, char ** )
{
// Temporary variables to get input from user
int g, p, ps;

// Self-explanatory:
cout << "Enter starting gold amount:" << endl;
cin >> g;

cout << "Enter starting potions amount:" << endl;
cin >> p;

cout << "Enter number of potions currently in stock (at the store)" << endl;
cin >> ps;

// This code block here is for demonstration of the constructors and destructors, and isn't necessary
{
player hero( p, g ); // Create a player object named "hero" with "p" potions and "g" gold
store the_store( ps ); // Create a store object named "the_store" with "ps" potions in stock

// Main game loop
bool is_running = true;
while ( is_running ) // will loop forever until "is_running" is false
{
// Display separator, so it is easier to read:
cout << endl << endl;

// Display current player status:
hero.display_status();

// Display available choices:
cout << "There's a store nearby, what must you do?" << endl;
cout << "1) Enter store" << endl;
cout << "2) Make gold" << endl;
cout << "3) Exit game" << endl;
cout << endl;
cout << "Enter choice: " << endl;

// Receive a choice from the user:
int choice;
cin >> choice;

// Act on it:
if ( choice == 1 ) // Player enters the store
the_store.player_enters( hero );
else if ( choice == 2 ) // Player creates gold out of thin air
{
cout << "How much gold will you make: " << endl;

int moar_gold;
cin >> moar_gold;

// Of course moar_gold could be a negative number...
// Oh well, this is for debugging purposes anyway :)
hero.gold += moar_gold;
}
else if ( choice == 3 ) // Player decides he's had enough :)
is_running = false;
}

// And finally...
cout << "Thank you for playing!" << endl;

} // Here, at this brace, the destructors of "hero" and "the_store" are automatically called

// Pause here so you can view the results
cin.ignore();
cin.get();

return 0;
}



This should give you several ideas on how to get started, and will advance your knowledge of C++ when you practice it. [smile]

If you still don't understand, try going step by step through the sample code using the debugger. If you are using visual studio, here's how you do it:

Press F10 to start the debugger.
Press F10 on statements using cout or cin, and press F11 on the others. (If you accidentally get into some unfamiliar looking code, press shift+F11 to step out of that function)

Also, you can use
[ source lang="cpp" ]
[ /source ]


to display your code, and you can use the "edit" button on the top right hand corner of your post to modify or delete your posts.

EDIT:
Here's a sample run of the code:

Enter starting gold amount:
14
Enter starting potions amount:
0
Enter number of potions currently in stock (at the store)
3
Player was created with 0 potions and 14 gold.
A store was created with 3 potions in stock.


You have 14 gold and 0 potions
There's a store nearby, what must you do?
1) Enter store
2) Make gold
3) Exit game

Enter choice:
1

Welcome to my store!

You have 14 gold and 0 potions

What would you like to buy?
1) Potion (3)
2) Leave

Enter choice: 1
Thank you for your business

You have 9 gold and 1 potions

What would you like to buy?
1) Potion (2)
2) Leave

Enter choice: 1
Thank you for your business

You have 4 gold and 2 potions
Looks like you're out of money lol
Goodbye now!


You have 4 gold and 2 potions
There's a store nearby, what must you do?
1) Enter store
2) Make gold
3) Exit game

Enter choice:
1

Welcome to my store!
Unfortunately, you requires more moneys
Goodbye now!


You have 4 gold and 2 potions
There's a store nearby, what must you do?
1) Enter store
2) Make gold
3) Exit game

Enter choice:
2
How much gold will you make:
999


You have 1003 gold and 2 potions
There's a store nearby, what must you do?
1) Enter store
2) Make gold
3) Exit game

Enter choice:
1

Welcome to my store!

You have 1003 gold and 2 potions

What would you like to buy?
1) Potion (1)
2) Leave

Enter choice: 1
You just bought my last potion! D:
Goodbye now!


You have 998 gold and 3 potions
There's a store nearby, what must you do?
1) Enter store
2) Make gold
3) Exit game

Enter choice:
1

Welcome to my store!
Unfortunately, I'm out of potions!
Goodbye now!


You have 998 gold and 3 potions
There's a store nearby, what must you do?
1) Enter store
2) Make gold
3) Exit game

Enter choice:
3
Thank you for playing!
The store was destroyed :.(
Player was destroyed :.(



[Edited by - _fastcall on August 15, 2008 5:43:29 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by idono

cout<<"you bought a health potion"<<endl;
gold - 5;
potion + 1;


I saw this while skimming through so wanted to point it out, _fastcall amends it in the sample code he gave you, but to increment/decrement a variable you can't just leave it as is, your (fully written) line would need to read

gold = gold - 5;

Or the quicker way, which _fastcall uses, but does the same thing:

gold -= 5;

Both tell the system "assign the value of ((whatever value gold currently has) - 5) to the variable "gold". What you wrote is just the operation, it doesn't assign the outcome to anything.

Share this post


Link to post
Share on other sites
i have a huge gold mind of errors and i cant see to figure then out i followed the
example but it does not compile what im i doing wrong?

[code]#include <cstdlib>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<ctime>
using namespace std;

class player1
{
public:

int Potions;
int Gold;
int flint_gun = 4;
int Wood_stick = 2;
int Rocket_lancher = 35;
int Ham_granade = 40;
int Nature_magic = 21;
int Electropulse = 15;
int Crule_Mace = 13;
int Ice_mace = 22;
int Deadly_wind = 25;

public:

player1(int P,int G,int F,int W,int R,int H, int N,int E,int C,int I,int D)
{
Potions = P;
Gold = G;
flint_gun = F;
Wood_stick = W;
Rocket_lancher = R;
Ham_granade = H;
Nature_magic = N;
Electropulse = E;
Crule_Mace = C;
Ice_mace = I;
Deadly_wind = D;
}

void display_status()
{
cout << "You have " << gold << " gold. " << potions << " potions." << endl;
cout << "You have " << flint_gun << " flint gun Ammo. " << Rocket_lancher << " Ammo." << endl;
cout << "You have " << Ham_granade << " Ham granades. " << Nature_magic << " spell." << endl;
cout << "You have " << Electropulse << " EMP's. " << Deadly_wind << " spell." << endl;
cout << "You have " << Crule_Mace << ". " << Ice_mace << "." << endl;
}

class store
{
private:

int potions_avalible;
int flint_gun_Ammo_avalible;
int Rocket_lancher_Ammo_avalible;
int Ham_granade_Ammo_avalible;
int Nature_magic_spell_avalible;
int Electropulse_Ammo_avalible;
int Crule_Mace_avalible;
int Ice_mace_avalible;
int Deadly_wind_spell_avalible;

public:

store( int num_potions_in_store,
int num_flint_gun_Ammo,
int num_Rocket_lancher_Ammo,
int num_Ham_granade_Ammo,
int num_Nature_magic_spell,
int num_Electropulse_Ammo,
int num_Crule_Mace,
int num_Ice_mace,
int num_Deadly_wind_spell)

{
potions_avalible = num_potions_in_store;
flint_gun_Ammo_avalible = num_flint_gun_Ammo;
Rocket_lancher_Ammo_avalible = num_Rocket_lancher_Ammo;
Ham_granade_Ammo_avalible = num_Ham_granade_Ammo;
Nature_magic_spell_avalible = num_Nature_magic_spell;
Electropulse_Ammo_avalible = num_Electropulse_Ammo;
Crule_Mace_avalible = num_Crule_Mace;
Ice_mace_avalible = num_Ice_mace;
Deadly_wind_spell_avalible = num_Deadly_wind_spell;
}

void player_enters( player1 &the_player )
{

cout<<endl;
cout<< "Hello young fighter!!!" <<endl;
cout<< "Welcome to the fighters Dirty Den" <<endl;
cout<< "we have every thing you could possibly want" <<endl;

if(potions_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(flint_gun_Ammo_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(Rocket_lancher_Ammo_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(Ham_granade_Ammo_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(Nature_magic_spell_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(Electropulse_Ammo_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(Crule_Mace_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(Ice_mace_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(Deadly_wind_spell_avalible == 0)
{
cout<<" out of stock"<<endl;
}

else if ( the_player.gold < 5 ) // (We have potions and then) Check to see if the player has money
{
// He/she doesn't have money
cout << "all thats to bad ouuta money well then.. Get Out" << endl;
}

else
{
bool player_is_still_buying = true;
while ( player_is_still_buying && potions_avalible > 0 || player_is_still_buying && flint_gun_Ammo_avalible > 0||
player_is_still_buying && Rocket_lancher_Ammo_avalible > 0||player_is_still_buying && Ham_granade_Ammo_avalible > 0||
player_is_still_buying && Nature_magic_spell_avalible > 0||player_is_still_buying && Electropulse_Ammo_avalible > 0||
player_is_still_buying && Crule_Mace_avalible > 0||player_is_still_buying && Ice_mace_avalible > 0||
player_is_still_buying && Deadly_wind_spell_avalible > 0)
{

cout << endl;
the_player.display_status();

if ( the_player.gold < 0 ) // The player is unable to buy any more potions

{
cout << "Well i gesss i have all your money AH AH AH...i hate me self" << endl;
player_is_still_buying = false;
break; // Force out of the "while" loop
}

cout << endl;
cout << "What would you like to buy?" << endl;
cout << "1) Potion $3 (" << potions_avalible << ")" << endl;
cout << "2) Flint Gun Ammo $5 (" << flint_gun_Ammo_avalible << ")" << endl;
cout << "3) Rocket Lancher Ammo $40(" << Rocket_lancher_Ammo_avalible << ")" << endl;
cout << "4) Ham Granades $65(" << Ham_granade_Ammo_avalible << ")" << endl;
cout << "5) Nature Magic spell $10(" << Nature_magic_spell_avalible << ")" << endl;
cout << "6) Electropulse $12(" << Electropulse_Ammo_avalible << ")" << endl;
cout << "8) Crule Mace $70(" << Crule_Mace_avalible << ")" << endl;
cout << "8) Ice mace $56(" << Ice_mace_avalible << ")" << endl;
cout << "9) Deadly wind spell $13(" << Deadly_wind_spell_avalible << ")" << endl;
cout << "10) Leave" << endl;
cout << endl;
cout << "Enter choice: ";

int choice;
cin >> choice;

if(choice == 10)
player_is_still_buying = false;

else
{
if(choice == 1)
{
the_player.gold -= 3;
the_player.potions += 1;
potions_avalible -= 1;
}

if(choice == 2)
{
the_player.gold -= 5;
the_player.flint_gun += 1;
flint_gun_Ammo_avalible -= 1;
}

if(choice == 3)
{
the_player.gold -= 40;
the_player.Rocket_lancher += 1;
Rocket_lancher_Ammo_avalible -= 1;
}

if(choice == 4)
{
the_player.gold -= 65;
the_player.Ham_granade += 1;
Ham_granade_Ammo_avalible -= 1;
}

if(choice == 5)
{
the_player.gold -= 10;
the_player.Nature_magic += 1;
Nature_magic_spell_avalible -= 1;
}

if(choice == 6)
{
the_player.gold -= 12;
the_player.Electropulse += 1;
Electropulse_Ammo_avalible -= 1;
}

if(choice == 7)
{
the_player.gold -= 70;
the_player.Crule_Mace += 1;
Crule_Mace_avalible -= 1;
}


if(choice == 8)
{
the_player.gold -= 56;
the_player.Ice_mace += 1;
Ice_mace_avalible -= 1;
}


if(choice == 9)
{
the_player.gold -= 13;
the_player.Deadly_wind += 1;
Deadly_wind_spell_avalible -= 1;
}



if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "You Rich Basterd you took all me potions HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "more people like you come in here and i can retire HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "Hmmm all those Exploseives hmm are you some kind of Terrorist? HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "mmmm Granade Ham five star dinner right there HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "Magic thats stuff's for girls! HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "Hey you have a vacation planed in the Maxtix or something? HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "they Call it Crule, but its not as crule as me wifes cooking HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "hmm thats pretty cool.. get it coool HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "that Deadly wind is not match for my Mighty Fart! HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

else
cout<<"thanks for your time Raaaggghh im a pirate now lulz!?!?."<<endl;

}
}
}
}

cout << "C yah later HA Ha Haaa!" << endl;
system("CLS");
}
};
int main(int argc, char *argv[])
{

const int name = 30;
char player1[name];
int G = 300, P = 4, PS = 3,F = 2,R = 2,E = 3,H = 4,I = 1,D = 2,C = 1;

system("title Tale of Argonia 2: The Wizzerd and the Demon Bull King Demo ");
system("color 9");

cout<<"please enter your name Hero"<<endl;
cin >> setw (name) >> player1;

cout<<"Days later after you slain the Gorbling Commander life has slowly"<<endl;
cout<<"creeped back to normal.."<<endl;

system("pause");
system("CLS");

cout<<"hey " << player1 <<" what are you doing here?"<<endl;

cout<<"lets see if the Market is open"<<endl;

cout<<"this is a demo fuction of market"<<endl;

system("pasue");

player_enters( player1 );

system("PAUSE");
return EXIT_SUCCESS;
}
[\code]

Share this post


Link to post
Share on other sites
#include <cstdlib>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<ctime>
using namespace std;

class player1
{
public:

int Potions;
int Gold;
int flint_gun = 4;
int Wood_stick = 2;
int Rocket_lancher = 35;
int Ham_granade = 40;
int Nature_magic = 21;
int Electropulse = 15;
int Crule_Mace = 13;
int Ice_mace = 22;
int Deadly_wind = 25;

public:

player1(int P,int G,int F,int W,int R,int H, int N,int E,int C,int I,int D)
{
Potions = P;
Gold = G;
flint_gun = F;
Wood_stick = W;
Rocket_lancher = R;
Ham_granade = H;
Nature_magic = N;
Electropulse = E;
Crule_Mace = C;
Ice_mace = I;
Deadly_wind = D;
}

void display_status()
{
cout << "You have " << gold << " gold. " << potions << " potions." << endl;
cout << "You have " << flint_gun << " flint gun Ammo. " << Rocket_lancher << " Ammo." << endl;
cout << "You have " << Ham_granade << " Ham granades. " << Nature_magic << " spell." << endl;
cout << "You have " << Electropulse << " EMP's. " << Deadly_wind << " spell." << endl;
cout << "You have " << Crule_Mace << ". " << Ice_mace << "." << endl;
}

class store
{
private:

int potions_avalible;
int flint_gun_Ammo_avalible;
int Rocket_lancher_Ammo_avalible;
int Ham_granade_Ammo_avalible;
int Nature_magic_spell_avalible;
int Electropulse_Ammo_avalible;
int Crule_Mace_avalible;
int Ice_mace_avalible;
int Deadly_wind_spell_avalible;

public:

store( int num_potions_in_store,
int num_flint_gun_Ammo,
int num_Rocket_lancher_Ammo,
int num_Ham_granade_Ammo,
int num_Nature_magic_spell,
int num_Electropulse_Ammo,
int num_Crule_Mace,
int num_Ice_mace,
int num_Deadly_wind_spell)

{
potions_avalible = num_potions_in_store;
flint_gun_Ammo_avalible = num_flint_gun_Ammo;
Rocket_lancher_Ammo_avalible = num_Rocket_lancher_Ammo;
Ham_granade_Ammo_avalible = num_Ham_granade_Ammo;
Nature_magic_spell_avalible = num_Nature_magic_spell;
Electropulse_Ammo_avalible = num_Electropulse_Ammo;
Crule_Mace_avalible = num_Crule_Mace;
Ice_mace_avalible = num_Ice_mace;
Deadly_wind_spell_avalible = num_Deadly_wind_spell;
}

void player_enters( player1 &the_player )
{

cout<<endl;
cout<< "Hello young fighter!!!" <<endl;
cout<< "Welcome to the fighters Dirty Den" <<endl;
cout<< "we have every thing you could possibly want" <<endl;

if(potions_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(flint_gun_Ammo_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(Rocket_lancher_Ammo_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(Ham_granade_Ammo_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(Nature_magic_spell_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(Electropulse_Ammo_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(Crule_Mace_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(Ice_mace_avalible == 0)
{
cout<<" out of stock"<<endl;
}

if(Deadly_wind_spell_avalible == 0)
{
cout<<" out of stock"<<endl;
}

else if ( the_player.gold < 5 ) // (We have potions and then) Check to see if the player has money
{
// He/she doesn't have money
cout << "all thats to bad ouuta money well then.. Get Out" << endl;
}

else
{
bool player_is_still_buying = true;
while ( player_is_still_buying && potions_avalible > 0 || player_is_still_buying && flint_gun_Ammo_avalible > 0||
player_is_still_buying && Rocket_lancher_Ammo_avalible > 0||player_is_still_buying && Ham_granade_Ammo_avalible > 0||
player_is_still_buying && Nature_magic_spell_avalible > 0||player_is_still_buying && Electropulse_Ammo_avalible > 0||
player_is_still_buying && Crule_Mace_avalible > 0||player_is_still_buying && Ice_mace_avalible > 0||
player_is_still_buying && Deadly_wind_spell_avalible > 0)
{

cout << endl;
the_player.display_status();

if ( the_player.gold < 0 ) // The player is unable to buy any more potions

{
cout << "Well i gesss i have all your money AH AH AH...i hate me self" << endl;
player_is_still_buying = false;
break; // Force out of the "while" loop
}

cout << endl;
cout << "What would you like to buy?" << endl;
cout << "1) Potion $3 (" << potions_avalible << ")" << endl;
cout << "2) Flint Gun Ammo $5 (" << flint_gun_Ammo_avalible << ")" << endl;
cout << "3) Rocket Lancher Ammo $40(" << Rocket_lancher_Ammo_avalible << ")" << endl;
cout << "4) Ham Granades $65(" << Ham_granade_Ammo_avalible << ")" << endl;
cout << "5) Nature Magic spell $10(" << Nature_magic_spell_avalible << ")" << endl;
cout << "6) Electropulse $12(" << Electropulse_Ammo_avalible << ")" << endl;
cout << "8) Crule Mace $70(" << Crule_Mace_avalible << ")" << endl;
cout << "8) Ice mace $56(" << Ice_mace_avalible << ")" << endl;
cout << "9) Deadly wind spell $13(" << Deadly_wind_spell_avalible << ")" << endl;
cout << "10) Leave" << endl;
cout << endl;
cout << "Enter choice: ";

int choice;
cin >> choice;

if(choice == 10)
player_is_still_buying = false;

else
{
if(choice == 1)
{
the_player.gold -= 3;
the_player.potions += 1;
potions_avalible -= 1;
}

if(choice == 2)
{
the_player.gold -= 5;
the_player.flint_gun += 1;
flint_gun_Ammo_avalible -= 1;
}

if(choice == 3)
{
the_player.gold -= 40;
the_player.Rocket_lancher += 1;
Rocket_lancher_Ammo_avalible -= 1;
}

if(choice == 4)
{
the_player.gold -= 65;
the_player.Ham_granade += 1;
Ham_granade_Ammo_avalible -= 1;
}

if(choice == 5)
{
the_player.gold -= 10;
the_player.Nature_magic += 1;
Nature_magic_spell_avalible -= 1;
}

if(choice == 6)
{
the_player.gold -= 12;
the_player.Electropulse += 1;
Electropulse_Ammo_avalible -= 1;
}

if(choice == 7)
{
the_player.gold -= 70;
the_player.Crule_Mace += 1;
Crule_Mace_avalible -= 1;
}


if(choice == 8)
{
the_player.gold -= 56;
the_player.Ice_mace += 1;
Ice_mace_avalible -= 1;
}


if(choice == 9)
{
the_player.gold -= 13;
the_player.Deadly_wind += 1;
Deadly_wind_spell_avalible -= 1;
}



if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "You Rich Basterd you took all me potions HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "more people like you come in here and i can retire HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "Hmmm all those Exploseives hmm are you some kind of Terrorist? HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "mmmm Granade Ham five star dinner right there HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "Magic thats stuff's for girls! HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "Hey you have a vacation planed in the Maxtix or something? HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "they Call it Crule, but its not as crule as me wifes cooking HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "hmm thats pretty cool.. get it coool HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "that Deadly wind is not match for my Mighty Fart! HA HA HA..." << endl;
player_is_still_buying = false; // Nothing left to buy!
}

else
cout<<"thanks for your time Raaaggghh im a pirate now lulz!?!?."<<endl;

}
}
}
}

cout << "C yah later HA Ha Haaa!" << endl;
system("CLS");
}
};
int main(int argc, char *argv[])
{

const int name = 30;
char player1[name];
int G = 300, P = 4, PS = 3,F = 2,R = 2,E = 3,H = 4,I = 1,D = 2,C = 1;

system("title Tale of Argonia 2: The Wizzerd and the Demon Bull King Demo ");
system("color 9");

cout<<"please enter your name Hero"<<endl;
cin >> setw (name) >> player1;

cout<<"Days later after you slain the Gorbling Commander life has slowly"<<endl;
cout<<"creeped back to normal.."<<endl;

system("pause");
system("CLS");

cout<<"hey " << player1 <<" what are you doing here?"<<endl;

cout<<"lets see if the Market is open"<<endl;

cout<<"this is a demo fuction of market"<<endl;

system("pasue");

player_enters( player1 );

system("PAUSE");
return EXIT_SUCCESS;
}

Share this post


Link to post
Share on other sites
i finally got the code no work a few days ago, and i now better understand how the code for the player works an there relationship to items,but can this code above be adapted to combat like the code i made earlier. gain gold or items out of it? or do i need to re-work the code completely?


#include<iostream>
#include<cmath>
#include<cstdlib>
#include<iomanip>
#include<ctime>
using namespace std;
int main()
{
cout<<" What do you do!"<<endl;

char Fight;

cout<<" Press F to fight";
cout<<" or Press any key for Quit: ";
cin >>Fight;

if(Fight == 'F' || Fight == 'f')/////story my continues in here//////
{

//monster always attaks first

///////weapons & Damage///
double Flint_Gun = 5;
double Small_Dagger = 3;
double health = 70;
double Wooden_stick = 3;
double Gobling_health = 30;
double health_potion = 10;
double bonus_10 = 10;
///i ran the program no loss of data found in my program.
unsigned Gobling_Damage = time(0);
//////////////////////////



while (Gobling_health >= 1 && health >= 1)


{///master while for combat


cout<<" your health "<<health<<endl;
cout<<" the Gobling "<<Gobling_health<<endl;

srand(Gobling_Damage);

Gobling_Damage = (rand() % 10) + 1;

cout<<" The Goblin did "<<Gobling_Damage<<" Damage"<<endl;

health = health - Gobling_Damage;

///1 spaces////////////////
int space = 1;

while(space <= 1)
{
cout <<""<<endl;
space++;
}
///////////////////////////

cout<<" your health "<<health<<endl;
cout<<" the Gobling "<<Gobling_health<<endl;

system("pause");

///1 spaces////////////////
space = 1;

while(space <= 1)
{
cout <<""<<endl;
space++;
}
///////////////////////////

char Attack;

//condition that player1 dies in fight
if(Gobling_health > 0 && health < 1)
{
cout<<" Your killed lolz sorry"<<endl;

system("Title Game Over ");

system ("pause");
return 0;
}

cout<<" pick a weapon and fight or he will kill you!"<<endl;
cout<<" S:Small Dagger attack 3"<<endl;
cout<<" F:Flint Gun attack 5"<<endl;
cout<<" W:Wooden stick 2"<<endl;
cout<<" P:health bottle"<<endl;
cout<<" pick or die: ";
cin >>Attack;

///2 spaces////////////////
space = 1;

while(space <= 2)
{
cout <<""<<endl;
space++;
}
///////////////////////////

//these are the condition of fighting
if(Attack == 'S' || Attack == 's')
{
Gobling_Damage = (rand() % 10) + 1;
Gobling_health = Gobling_health - Small_Dagger;
cout<<" Small Dagger Attack dealed 3 Damage! "<<endl;
}



if(Attack == 'F' || Attack == 'f')
{
Gobling_Damage = (rand() % 20) + 1;
Gobling_health = bonus_10 + Gobling_health - Flint_Gun;
cout<<" Flint Gun attack dealed 5 Damage! "<<endl;
cout<<" you hit the monster but bullets make it Stronger!!!"<<endl;
}


if(Attack == 'W' || Attack == 'w')
{
Gobling_Damage = (rand() % 8) + 1;
Gobling_health = Gobling_health - Wooden_stick;
cout<<" Wooden stick attack dealed 3 Damage! "<<endl;
}


if(Attack == 'P' || Attack == 'p')
{
Gobling_Damage = (rand() % 5) + 1;
health = health + health_potion ;
cout<<" Monster Attacked you but"<<endl;
cout<<" health bottle gave you 5 health! "<<endl;
}

///5 spaces////////////////
space = 1;

while(space <= 5)
{
cout <<""<<endl;
space++;
}
///////////////////////////


//ondiction that player1 wins
if(Gobling_health <= 0 && health > 0)
{

system("Title The Tale of Argoonia ");
system("Color 3");

cout<<" you did it the town of Argonia is safe but for how long"<<endl;
cout<<" The End"<<endl;

///2 spaces////////////////
space = 1;

while(space <= 2)
{
cout <<""<<endl;
space++;
}
///////////////////////////

cout<<" Argonia 2:The Wizzerd and the Demon Bull King comming soon!!!"<<endl;
cout<<" Talk to idono about it!!"<<endl;



system ("pause");
return 0;

}

}///master while for combat end

}







else cout<<"You have failed do anything to save your friends or yourcity"<<endl;
system ("pause");
return 0;

}

Share this post


Link to post
Share on other sites
Quote:
Original post by idono
i now better understand how the code for the player works an there relationship to items,but can this code above be adapted to combat like the code i made earlier. gain gold or items out of it? or do i need to re-work the code completely?


It's completely up to you, if you want to rewrite it or not. However, judging by your code, I'd rewrite the whole thing; but this time, put more emphasis on representing objects and events as objects. But, before you begin coding, you might want to plan out what kind of objects you're going to need first. Get a pen and paper. Think through your game, step by step, in your head. Write down any nouns in your game that could be represented as objects. (A lot of examples of objects have been covered.) Next, write down next to those objects you've written down functionality they might need -- such as a sword might need an equip() method to modify the player's stats accordingly (and also a dagger, so why not create a base class weapon to provide equip() for both the sword and dagger?). Do you see what I'm saying? Object oriented programming is difficult at first, but as you practice it, you'll never program without it! [smile]

Here's a nice tutorial on Object-Oriented programming:
Introduction to C++ Classes and Objects

Share this post


Link to post
Share on other sites

This topic is 3403 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this