Jump to content
  • Advertisement

Assassin7257

Member
  • Content Count

    69
  • Joined

  • Last visited

Community Reputation

105 Neutral

About Assassin7257

  • Rank
    Member
  1. Assassin7257

    Loading Different Events in SDL

    SDL_Rect *pointer = (choice == -1 ? NULL : &clips[choice]) this part confused me, whats with the question mark ?
  2. Assassin7257

    Loading Different Events in SDL

    enum Gamestate { Gender, Main, Exit, }; int main(int argc, char* args[]) { //if init failed if(init() == false) { return 1; } PokemonChoice myPokemon; Gamestate current = Gender; while(current != Exit) { while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) { current = Exit; } switch(current) { case Gender: //handle if(Gender) { current = Main; } break; case Main: myPokemon.PokemonEvents(); break; } } SDL_Flip(screen); } clean_up(); return 0; } This is my code, and all it shows a blank screen, and if I declare an Gender instance then it'll show an error saying expected ; before the instance
  3. Assassin7257

    Loading Different Events in SDL

    switch(current) //Now for each Gamestate you can handle events seperately { case Gender: //Handle quitting. if(Gender) { current = Main; } break; case Main: Pokemon.HandleEvents(); break; } } switch(current) { case Gender: myGender.Draw(); break; case Main: myPokemon.Draw(); break; } I don't understand the switch statement part, I know how to use switch statements but this is confusing :S The second switch statement in specific
  4. Assassin7257

    Loading Different Events in SDL

    Yea, how it works
  5. Assassin7257

    Loading Different Events in SDL

    Can you explain what a transverse is ? How I can implement it in other code as well ?
  6. #include"SDL/SDL.h" #include"SDL/SDL_image.h" #include<string> using namespace std; //screen attributes const int screenWidth = 640; const int screenHeight = 480; const int screenBPP = 32; //surfaces SDL_Surface* screen = NULL; //event SDL_Event event; //portion being clipped SDL_Rect clip [ 2 ]; //pokemonclip SDL_Rect pokemonClip[3]; SDL_Surface* load_image ( string filename ) { //temporarily store image SDL_Surface* loadedImage = NULL; //store optimizedimage SDL_Surface* optimizedImage = NULL; //load the image loadedImage = IMG_Load( filename.c_str() ); //if no errors if(loadedImage != NULL) { //optimize Image now optimizedImage = SDL_DisplayFormat( loadedImage ); //free up old surface SDL_FreeSurface(loadedImage); //if no errors loading optimizedImage if(optimizedImage != NULL) { SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB (optimizedImage->format, 0, 0xFF, 0xFF)); } } //if no errrors return optimizedImage; } void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL) { //create rectangle to represent screen SDL_Rect offset; //set offset values offset.x = x; offset.y = y; //blit surface SDL_BlitSurface(source, clip, destination, &offset); } bool init() { //load everything if(SDL_Init(SDL_INIT_EVERYTHING) == -1) { return false; } //set sscreen screen = SDL_SetVideoMode(screenWidth, screenHeight, screenBPP, SDL_SWSURFACE); //if there was an error if(screen == NULL) { return false; } //windows caption SDL_WM_SetCaption("Choose your DECISION", NULL); //if no errors return true; } void clean_up() { //quit SDL_Quit(); } //gender class class Gender { private: SDL_Surface* genderChoice; //genderChoice SDL_Rect* choice; public: bool GenderEvents(); //handles the gender events choice void showGenderChoice(); //display what gender they choose void clipGender(); Gender(); ~Gender(); }; Gender::Gender() { clipGender(); genderChoice = load_image("Characters.png"); choice = NULL; } Gender::~Gender() { SDL_FreeSurface(genderChoice); } void Gender::clipGender() { clip[0].x = 83; clip[0].y = 43; clip[0].w = 197; clip[0].h = 385; clip[1].x = 320; clip[1].y = 15; clip[1].w = 410; clip[1].h = 460; } bool Gender::GenderEvents() { //get mouse position int x = 0, y = 0; apply_surface(0, 0, genderChoice, screen); if(event.type == SDL_MOUSEBUTTONDOWN) { if(event.button.button == SDL_BUTTON_LEFT) { //get mouse offsets x = event.motion.x; y = event.motion.y; if( (x > clip[0].x) && (x < clip[0].x + clip[0].w) && (y > clip[0].y) && (y < clip[0].y + clip[0].h) ) { choice = &clip[0]; return true; } if( (x > clip[1].x ) && (x < clip[1].x + clip[1].w) && (y > clip[1].y) && (y < clip[1].y + clip[1].h) ) { choice = &clip[1]; return true; } } } } void Gender::showGenderChoice() { apply_surface(0, 0, genderChoice, screen, choice); } class PokemonChoice { private: SDL_Surface* Pokemon; SDL_Rect* PokeChoice; //the choice they make public: void setPokemonClip(); void PokemonEvents(); void showPokemon(); PokemonChoice(); ~PokemonChoice(); }; PokemonChoice::PokemonChoice() { Pokemon = load_image("pokemon.png"); setPokemonClip(); PokeChoice = NULL; } PokemonChoice::~PokemonChoice() { SDL_FreeSurface(Pokemon); } void PokemonChoice::setPokemonClip() { pokemonClip[0].x = 25; pokemonClip[0].y = 140; pokemonClip[0].w = 145; pokemonClip[0].h = 185; pokemonClip[1].x = 275; pokemonClip[1].y = 140; pokemonClip[1].w = 145; pokemonClip[1].h = 185; pokemonClip[2].x = 475; pokemonClip[2].y = 150; pokemonClip[2].w = 145; pokemonClip[2].h = 185; } void PokemonChoice::PokemonEvents() { //mouse offsets int x = 0, y = 0; apply_surface(0, 0, Pokemon, screen); if(event.type == SDL_MOUSEBUTTONDOWN) { if(event.button.button == SDL_BUTTON_LEFT) { x = event.motion.x; y = event.motion.y; if( (x > pokemonClip[0].x) && (x < pokemonClip[0].x + pokemonClip[0].w) && (y > pokemonClip[0].y) && (y < pokemonClip[0].y + pokemonClip[0].w)) { PokeChoice = &pokemonClip[0]; } if( (x > pokemonClip[1].x) && (x < pokemonClip[1].x + pokemonClip[1].w) && (y > pokemonClip[1].y) && (y < pokemonClip[1].y + pokemonClip[1].w)) { PokeChoice = &pokemonClip[1]; } if( (x > pokemonClip[2].x) && (x < pokemonClip[2].x + pokemonClip[2].w) && (y > pokemonClip[2].y) && (y < pokemonClip[2].y + pokemonClip[2].w)) { PokeChoice = &pokemonClip[2]; } } } showPokemon(); } void PokemonChoice::showPokemon() { apply_surface(0, 0, Pokemon, screen, PokeChoice); } void runGame(PokemonChoice& aPokemon, Gender& aGender) { bool Gender = aGender.GenderEvents(); if(Gender == true) { SDL_Delay(1000); SDL_Flip(screen); aPokemon.PokemonEvents(); } } int main(int argc, char* args[]) { //quit flag bool quit = false; //if init failed if(init() == false) { return 1; } PokemonChoice myPokemon; Gender myGender; myPokemon.PokemonEvents(); while(quit == false) { while(SDL_PollEvent(&event)) { runGame(myPokemon, myGender); if(event.type == SDL_QUIT) { quit = true; } } if(SDL_Flip(screen) == -1) { return 1; } } clean_up(); return 0; } What I'm trying to do is sort of recreate the begginning of a pokemon game, but I don't seem to know how to load the different events. Does anyone have any idea ? They both load at the same time btw, and not just one at a time.
  7. Assassin7257

    Pointer to object

    Say I'm creating a game that has two classes. The player class and enemy class. The player makes a choice, and the enemy has to make a choice as based on the players. For example, in Pokemon you choose your starting pokemon, if you pick a Cyndaquil or Charmander, your rival will automatically pick a Squirtable or Totodile. How would I do that ?
  8. Assassin7257

    Vector array of Objects

    Now say I wanted to output the array, how would I do that ?
  9. Assassin7257

    Vector array of Objects

    Is it possible to create a vector array with objects. Could I use that vector functions as well ? vector<Object> myObject; myObject.push_back(Object); Could I use a base class as well, and let it store derived classes. vector<baseClass> myVector; myVector.push_back(DerivedClass);
  10. Assassin7257

    Tic Tac Toe Graphics

    #include"SDL/SDL.h" #include"SDL/SDL_image.h" #include<string> using namespace std; //video attribuets const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int SCREEN_BPP = 32; //surface SDL_Surface* screen = NULL; SDL_Surface* background = NULL; SDL_Surface* All = NULL; //events SDL_Event event; //rectangle SDL_Rect board[9]; SDL_Surface* load_image(string filename) { //temporarily store image SDL_Surface* loadedImage = NULL; //optimize Image SDL_Surface* optimizedImage = NULL; //load image loadedImage = IMG_Load(filename.c_str()); if(loadedImage != NULL) { //optimize Image optimizedImage = SDL_DisplayFormat(loadedImage); //free old surface SDL_FreeSurface(loadedImage); if(optimizedImage != NULL) { SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) ); } } //return image return optimizedImage; } void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) { //create rectangel SDL_Rect offset; offset.x = x; offset.y = y; //blit screen SDL_BlitSurface(source, NULL, destination, &offset); } bool init() { //initlize everyting if(SDL_Init(SDL_INIT_EVERYTHING) == -1) { return false; } screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE); //if screen didn't work if(screen == NULL) { return false; } //if no errors return true; } bool load_files() { background = load_image("board.png"); All = load_image("Picture.png"); if(background == NULL) { return false; } if(All == NULL) { return false; } //if no errors return true; } void clean_up() { //quit SDL_Quit(); } void set_clips() { board[ 0 ].x = 0; board[ 0 ].y = 0; board[ 0 ].w = 175; board[ 0 ].h = 155; board[ 1 ].x = 182; board[ 1 ].y = 0; board[ 1 ].w = 175; board[ 1 ].h = 155; board[ 2 ].x = 367; board[ 2 ].y = 0; board[ 2 ].w = 175; board[ 2 ].h = 155; board[ 3 ].x = 0; board[ 3 ].y = 160; board[ 3 ].w = 175; board[ 3 ].h = 155; board[ 4 ].x = 182; board[ 4 ].y = 160; board[ 4 ].w = 175; board[ 4 ].h = 155; board[ 5 ].x = 367; board[ 5 ].y = 160; board[ 5 ].w = 175; board[ 5 ].h = 155; board[ 6 ].x = 0; board[ 6 ].y = 325; board[ 6 ].w = 175; board[ 6 ].h = 155; board[ 7 ].x = 182; board[ 7 ].y = 325; board[ 7 ].w = 175; board[ 7 ].h = 155; board[ 8 ].x = 367; board[ 8 ].y = 325; board[ 8 ].w = 175; board[ 8 ].h = 155; } class PictureX { private: SDL_Surface* m_pImage; //pointer to the image SDL_Rect* pSurface[9]; public: void testImage(int x, int y, SDL_Surface* destination); void handle_events(); bool checkWinner(); PictureX(); }; PictureX::PictureX() { m_pImage = load_image("X.png"); } bool PictureX::checkWinner() { if( (pSurface[0] == &board[0]) && (pSurface[1] == &board[1]) && (pSurface[2] == &board[2]) ) { apply_surface(325, 320, All, screen); return true; } if( (pSurface[3] == &board[3]) && (pSurface[4] == &board[4]) && (pSurface[5] == &board[5])) { apply_surface(325, 320, All, screen); return true; } if( (pSurface[6] == &board[6]) && (pSurface[7] == &board[7]) && (pSurface[8] == &board[8]) ) { apply_surface(325, 320, All, screen); return true; } if( (pSurface[0] == &board[0]) && (pSurface[3] == &board[3]) && (pSurface[6] == &board[6]) ) { apply_surface(325, 320, All, screen); return true; } if( (pSurface[1] == &board[1]) && (pSurface[4] == &board[4]) && (pSurface[7] == &board[7]) ) { apply_surface(325, 320, All, screen); return true; } if( (pSurface[2] == &board[2]) && (pSurface[5] == &board[5]) && (pSurface[8] == &board[8]) ) { apply_surface(325, 320, All, screen); return true; } if( (pSurface[2] == &board[2]) && (pSurface[4] == &board[4]) && (pSurface[6] == &board[6]) ) { apply_surface(325, 320, All, screen); return true; } if( (pSurface[0] == &board[0]) && (pSurface[4] == &board[4]) && (pSurface[8] == &board[8]) ) { apply_surface(325, 320, All, screen); return true; } } void PictureX::handle_events() { //mouse offset int x = 0, y = 0; if(event.type == SDL_MOUSEBUTTONDOWN) { if(event.button.button == SDL_BUTTON_LEFT) { //get mouse offsets x = event.motion.x; y = event.motion.y; if( (x > board[0].x) && (x < board[0].x + board[0].w) && (y > board[0].y) && (y < board[0].y + board[0].h) ) { pSurface[0] = &board[0]; testImage(board[0].x, board[0].y, screen); } if( (x > board[1].x) && (x < board[1].x + board[1].w) && (y > board[1].y) && (y < board[1].y + board[1].h) ) { pSurface[1] = &board[1]; testImage(board[1].x, board[1].y, screen); } if( (x > board[2].x) && (x < board[2].x + board[2].w) && (y > board[2].y) && (y < board[2].y + board[2].h) ) { pSurface[2] = &board[2]; testImage(board[2].x, board[2].y, screen); } if( (x > board[3].x) && (x < board[3].x + board[3].w) && (y > board[3].y) && (y < board[3].y + board[3].h) ) { pSurface[3] = &board[3]; testImage(board[3].x, board[3].y, screen); } if( (x > board[4].x) && (x < board[4].x + board[4].w) && (y > board[4].y) && (y < board[4].y + board[4].h) ) { pSurface[4] = &board[4]; testImage(board[4].x, board[4].y, screen); } if( (x > board[5].x) && (x < board[5].x + board[5].w) && (y > board[5].y) && (y < board[5].y + board[5].h) ) { pSurface[5] = &board[5]; testImage(board[5].x, board[5].y, screen); } if( (x > board[6].x) && (x < board[6].x + board[6].w) && (y > board[6].y) && (y < board[6].y + board[6].h) ) { pSurface[6] = &board[6]; testImage(board[6].x, board[6].y, screen); } if( (x > board[7].x) && (x < board[7].x + board[7].w) && (y > board[7].y) && (y < board[7].y + board[7].h) ) { pSurface[7] = &board[7]; testImage(board[7].x, board[7].y, screen); } if( (x > board[8].x) && (x < board[8].x + board[8].w) && (y > board[8].y) && (y < board[8].y + board[8].h) ) { pSurface[8] = &board[8]; testImage(board[8].x, board[8].y, screen); } } } } void PictureX::testImage(int x, int y, SDL_Surface* destination) { //create rect offset SDL_Rect offset; offset.x = x; offset.y = y; //blit SDL_BlitSurface(m_pImage, NULL, screen, &offset); } class PictureO : public PictureX { private: SDL_Surface* m_pImage; SDL_Rect* pSurface[9]; public: void testImage(int x, int y, SDL_Surface* destination); void handle_events(); bool checkWinner(); PictureO(); }; PictureO::PictureO() { m_pImage = load_image("O.png"); } void PictureO::testImage(int x, int y, SDL_Surface* destination) { //create recet SDL_Rect offset; offset.x = x; offset.y = y; SDL_BlitSurface(m_pImage, NULL, destination, &offset); } bool PictureO::checkWinner() { if( (pSurface[0] == &board[0]) && (pSurface[1] == &board[1]) && (pSurface[2] == &board[2]) ) { apply_surface(325, 320, All, screen); return true; } if( (pSurface[3] == &board[3]) && (pSurface[4] == &board[4]) && (pSurface[5] == &board[5])) { apply_surface(325, 320, All, screen); return true; } if( (pSurface[6] == &board[6]) && (pSurface[7] == &board[7]) && (pSurface[8] == &board[8]) ) { apply_surface(325, 320, All, screen); return true; } if( (pSurface[0] == &board[0]) && (pSurface[3] == &board[3]) && (pSurface[6] == &board[6]) ) { apply_surface(325, 320, All, screen); return true; } if( (pSurface[1] == &board[1]) && (pSurface[4] == &board[4]) && (pSurface[7] == &board[7]) ) { apply_surface(325, 320, All, screen); return true; } if( (pSurface[2] == &board[2]) && (pSurface[5] == &board[5]) && (pSurface[8] == &board[8]) ) { apply_surface(325, 320, All, screen); return true; } if( (pSurface[2] == &board[2]) && (pSurface[4] == &board[4]) && (pSurface[6] == &board[6]) ) { apply_surface(325, 320, All, screen); return true; } if( (pSurface[0] == &board[0]) && (pSurface[4] == &board[4]) && (pSurface[8] == &board[8]) ) { apply_surface(325, 320, All, screen); return true; } } void PictureO::handle_events() { //mouse offset int x = 0, y = 0; if(event.type == SDL_MOUSEBUTTONDOWN) { if(event.button.button == SDL_BUTTON_LEFT) { //get mouse offsets x = event.motion.x; y = event.motion.y; if( (x > board[0].x) && (x < board[0].x + board[0].w) && (y > board[0].y) && (y < board[0].y + board[0].h) ) { pSurface[0] = &board[0]; testImage(board[0].x, board[0].y, screen); } if( (x > board[1].x) && (x < board[1].x + board[1].w) && (y > board[1].y) && (y < board[1].y + board[1].h) ) { pSurface[1] = &board[1]; testImage(board[1].x, board[1].y, screen); } if( (x > board[2].x) && (x < board[2].x + board[2].w) && (y > board[2].y) && (y < board[2].y + board[2].h) ) { pSurface[2] = &board[2]; testImage(board[2].x, board[2].y, screen); } if( (x > board[3].x) && (x < board[3].x + board[3].w) && (y > board[3].y) && (y < board[3].y + board[3].h) ) { pSurface[3] = &board[3]; testImage(board[3].x, board[3].y, screen); } if( (x > board[4].x) && (x < board[4].x + board[4].w) && (y > board[4].y) && (y < board[4].y + board[4].h) ) { pSurface[4] = &board[4]; testImage(board[4].x, board[4].y, screen); } if( (x > board[5].x) && (x < board[5].x + board[5].w) && (y > board[5].y) && (y < board[5].y + board[5].h) ) { pSurface[5] = &board[5]; testImage(board[5].x, board[5].y, screen); } if( (x > board[6].x) && (x < board[6].x + board[6].w) && (y > board[6].y) && (y < board[6].y + board[6].h) ) { pSurface[6] = &board[6]; testImage(board[6].x, board[6].y, screen); } if( (x > board[7].x) && (x < board[7].x + board[7].w) && (y > board[7].y) && (y < board[7].y + board[7].h) ) { pSurface[7] = &board[7]; testImage(board[7].x, board[7].y, screen); } if( (x > board[8].x) && (x < board[8].x + board[8].w) && (y > board[8].y) && (y < board[8].y + board[8].h) ) { pSurface[8] = &board[8]; testImage(board[8].x, board[8].y, screen); } } } } class TicTacBoard { private: PictureO boardO; PictureX boardX; int playerTurn; public: void playGame(); void checkWinner(); TicTacBoard(int turn = 1); }; TicTacBoard::TicTacBoard(int turn): playerTurn(turn) {} void TicTacBoard::playGame() { if(playerTurn == 1) { boardO.handle_events(); playerTurn += 1; } else if(playerTurn == 2) { boardX.handle_events(); playerTurn -= 1; } } int main(int argc, char* args[]) { bool quit = false; if(init() == false) { return 1; } if(load_files() == false) { return 1; } set_clips(); TicTacBoard myBoard; apply_surface(0, 0, background, screen); //mouse offsets int x = 0, y = 0; while(quit == false) { //while there's evetns to handle while(SDL_PollEvent(&event)) { myBoard.playGame(); if(event.type == SDL_QUIT) { quit = true; } } if(SDL_Flip(screen) == -1) { return 1; } } clean_up(); return 0; } So I'm trying to make a tic tac toe game using sdl, but I'm having troubles changing which player turn it is. It changes players turn but it doesn't do it properly. Like X will go 2 times then O goes once. Any idea how I could ? Also if there's way to make this code better let me know
  11. #include<iostream> #include<string> #include<ctime> #include<cstdlib> using namespace std; class Player { protected: string* m_name; //players name int m_health; //the players health public: virtual int getHealth(); //returns the players health virtual int attack(); string getName(); //returns the users name Player(const string& name = " ", int health = 100); //constructor sets users name Player(const Player& aPlayer); //copy constructor ~Player(); }; Player::Player(const string& name, int health) { m_name = new string(name); m_health = health; } Player::~Player() { delete m_name; } Player::Player(const Player& aPlayer) { m_name = new string(*(aPlayer.m_name)); m_health = aPlayer.m_health; } int Player::getHealth() { return m_health; } string Player::getName() { return *m_name; } int Player::attack() { cout << "NOthing " << endl; int attack = 15; return attack; } class Warrior : public Player { public: int getHealth(); //returns warriors health int attack(); //the user attacks Warrior(string name = " ", int health = 100); }; Warrior::Warrior(string name, int health): Player(name, health) {} int Warrior::getHealth() { return m_health; } int Warrior::attack() { srand(time(0)); int choice; cout << "Pick an attack: " << endl; cout << "1 - Lunge" << endl; cout << "2 - Slash" << endl; cout << "3 - Stab" << endl; cin >> choice; switch(choice) { case 1: { int attack = ( (rand() % 20) + 1); return attack; } case 2: { int attack = ( (rand() % 25) + 1); return attack; } case 3: { int attack = ( (rand() % 15) + 1); return attack; } default: cout << "You choose an illegal move." << endl; } } class Mage : public Player { public: int attack(); //different attack for a mage Mage(string name = " ", int health = 100); }; Mage::Mage(string name, int health): Player(name, health) {} int Mage::attack() { srand(time(0)); int choice; cout << "Select an attack: " << endl; cout << "1 - Fire Blast" << endl; cout << "2 - Ice Strike" << endl; cout << "3 - Ground Strike" << endl; cin >> choice; switch(choice) { case 1: { int attack = ( (rand () % 30) + 1); return attack; } case 2: { int attack = ( (rand() % 25) + 1); return attack; } case 3: { int attack = ( (rand() % 35) + 1); return attack; } } } class Archer : public Player { public: int potion(); //add potion if user request int attack(); //attacks Archer(string name = " ", int health = 100); }; Archer::Archer(string name, int health): Player(name, health) {} int Archer::attack() { srand(time(0)); int choice; cout << "Select an attack: " << endl; cout << "1 - Short Range" << endl; cout << "2 - Long Range" << endl; cout << "3 - Potion" << endl; cin >> choice; switch(choice) { case 1: { int attack = ( (rand() % 20) + 1); return attack; } case 2: { int attack = ( (rand() % 25) + 1); return attack; } case 3: { int getPotion = potion(); return getPotion; } } } int Archer::potion() { int potion = 15; return potion; } class enemy { private: enemy* pNext; //gets the memory address of the next enemy string m_name; //enemies name int m_health; //the enemies health public: enemy* getNext() const; //returns the next pointer void setNext(enemy* next); //sets the next enemy on the list string getName() const; //returns enemies name int getHealth(); //returns health int attack(); //the computer attacks enemy(string name = " ", int health = 100); }; enemy::enemy(string name, int health): m_name(name), m_health(health), pNext(0) {} void enemy::setNext(enemy* next) { pNext = next; } enemy* enemy::getNext() const { return pNext; } string enemy::getName() const { return m_name; } int enemy::getHealth() { m_health += 100; return m_health; } int enemy::attack() { srand(time(0)); int attack = ( (rand() % 25) + 1); return attack; } class fightingGround { friend ostream& operator<<(ostream& os, const fightingGround& aFightingGround); private: enemy* m_pHead; //head of the chain public: void AddEnemies(); //adds enemies void DeleteEnemies(); //deletes enemies when done fighting void Clear(); //clears enemies int Attack(); //attacks the player the top user enemy* getHead(); //returns top pointer fightingGround(); ~fightingGround(); }; fightingGround::fightingGround(): m_pHead(0) {} fightingGround::~fightingGround() { Clear(); } enemy* fightingGround::getHead() { return m_pHead; } int fightingGround::Attack() { enemy* fightEnemy = m_pHead; srand(time(0)); int attack = ( (rand() % 25) + 1); return attack; } void fightingGround::AddEnemies() { cout << "Type in the name of an enemy: " << endl; string name; cin >> name; enemy* pNewEnemy = new enemy(name); if(m_pHead == 0) { m_pHead = pNewEnemy; } else { enemy* pIter = m_pHead; while(pIter->getNext() != 0) { pIter = pIter->getNext(); } pIter->setNext(pNewEnemy); } } void fightingGround::DeleteEnemies() { if(m_pHead == 0) { cout << "NO MORE ENEMIES" << endl; } else { enemy* pTemp = m_pHead; m_pHead = m_pHead->getNext(); delete pTemp; } } void fightingGround::Clear() { while(m_pHead != 0) { DeleteEnemies(); } } ostream& operator<<(ostream& os, const fightingGround& afightingGround) { enemy* pIter = afightingGround.m_pHead; os << "\nHere is a list of the enemies :\n"; if(pIter == 0) { os << "There are no more enemies.\n"; } else { while(pIter != 0) { os << pIter->getName() << endl; pIter = pIter->getNext(); } } return os; } void fight(Player* aPlayer, fightingGround aFightingGround) { cout << "You've decided to fight, well this task isn't easy." << endl; enemy* enemyFight = aFightingGround.getHead(); int playerHealth = aPlayer->getHealth(); int computerHealth = enemyFight->getHealth(); bool loop = true; while(loop) { int attack = aPlayer->attack(); cout << "You hit a " << attack << "\n"; int computerHealth = computerHealth - attack; cout << "The computer has " << computerHealth << "\n"; int computerAttack = enemyFight->attack(); cout << "The computer hit " << computerAttack << "\n"; playerHealth = playerHealth - computerAttack; cout << "You have " << playerHealth << "\n"; if(playerHealth <= 0) { cout << "The computer has defeated you.." << endl; loop = false; } if(computerHealth <= 0) { cout << "You've beaten the computer !" << endl; aFightingGround.DeleteEnemies(); loop = false; } } delete enemyFight; enemyFight = 0; delete aPlayer; aPlayer = 0; } int main() { int classChoice; Player* pPlayerClass; fightingGround theFightingGround; theFightingGround.AddEnemies(); cout << "Select a Class: " << endl; cout << "1 - Warrior" << endl; cout << "2 - Mage" << endl; cout << "3 - Archer" << endl; cin >> classChoice; switch(classChoice) { case 1: { cout << "You choose a Warrior." << endl; cout << "What is his name: " << endl; string name; cin >> name; pPlayerClass = new Warrior(name); break; } case 2: { cout << "You choose a Mage." << endl; cout << "What is his name: " << endl; string name; cin >> name; pPlayerClass = new Mage(name); break; } case 3: { cout << "You choose a Warrior." << endl; cout << "What is his name: " << endl; string name; cin >> name; pPlayerClass = new Archer(name); break; } } fight(pPlayerClass, theFightingGround); cout << "Welcome BACK !" << endl; return 0; } So I'm learning to use polymorphism and inheritance, so this is a game I tried to make but everytime at the end of it, it keeps crashing. I'm guessing there's a problem with the deconstructor. Any Ideas ?
  12. Assassin7257

    Best Graphics to use C++ ?

    Soon, I will start trying to use graphics but first I was wondering which is the best to use and the most versetile. OpenGL or DirectX ? Which one of the two is better to learn ?
  13. Assassin7257

    Program keeps crashing

    I running it in codeblocks and it crashes
  14. Assassin7257

    Program keeps crashing

    For every number divisible by 5, so 10 equal 2 enemies.
  15. Assassin7257

    Program keeps crashing

    #include<iostream> #include<string> using namespace std; class enemy { private: int m_health; //the health of each enemy string m_name; //name of enemy jumping you enemy* pNext; //points to next object on the list public: void taunt(); //taunts the user void setNext(enemy* next); //gives next enemy on the list enemy* getNext() const; //returns next enemy on the battlefield string getName(); //returns the enemies name enemy(string name = " ", int health = 100); }; enemy::enemy(string name, int health): m_health(health), m_name(name), pNext(0) {} void enemy::setNext(enemy* next) { pNext = next; } enemy* enemy::getNext() const { return pNext; } void enemy::taunt() { cout << "Ha Ha You suck, you dumb loser !" << endl; } string enemy::getName() { return m_name; } class battlefield { private: enemy* m_pHead; // the top of the chain public: void AddEnemy(); void DeleteEnemy(); void EnemyTaunt(); void Clear(); void GetEnemiesName(); //list through all the names of the enemies made void searchName(string* enemyName); void killEnemy(); //decides which enemy to kill battlefield(); ~battlefield(); }; battlefield::battlefield(): m_pHead(0) {} battlefield::~battlefield() { cout << "Clearing the BATTLEFIELD !" << endl; Clear(); } void battlefield::Clear() { while(m_pHead != 0) { DeleteEnemy(); } } void battlefield::AddEnemy() { cout << "What type of enemy is this: "; string enemyType; cin >> enemyType; enemy* pNewEnemy = new enemy(enemyType); if(m_pHead == 0) { m_pHead = pNewEnemy; } else { enemy* pIter = m_pHead; while(pIter->getNext() != 0) { pIter = pIter->getNext(); } pIter->setNext(pNewEnemy); } } void battlefield::DeleteEnemy() { if(m_pHead == 0) { cout << "The garage is empty !" << endl; } else { enemy* pTemp = m_pHead; m_pHead = m_pHead->getNext(); delete pTemp; } } void battlefield::GetEnemiesName() { enemy* pFirstEnemy = m_pHead; cout << pFirstEnemy->getName() << endl; while(pFirstEnemy->getNext() != 0) { pFirstEnemy = pFirstEnemy->getNext(); // gets the next object cout << pFirstEnemy->getName() << endl; // returns name } } void battlefield::searchName(string* enemyName) { enemy* searchEnemy = m_pHead; string p_EnemyName; if( searchEnemy->getName() == *enemyName) { cout << "We've found the enemy, He's FIRST !" << endl; } //iterating through list of objects while(searchEnemy->getNext() != 0) { searchEnemy = searchEnemy->getNext(); //gets next pointer p_EnemyName = searchEnemy->getName(); // gets the name if( *enemyName == p_EnemyName ) { cout << "We found the enemy !" << endl; } } } void battlefield::killEnemy() { cout << "Type in the name of the enemy you would like to kill: "; string enemyName; cin >> enemyName; enemy* p_EnemyKill = m_pHead; enemy* pEnemyName; while(p_EnemyKill->getNext() != 0) { p_EnemyKill = p_EnemyKill->getNext(); *pEnemyName = p_EnemyKill->getName(); } delete pEnemyName; pEnemyName = 0; } void battlefield::EnemyTaunt() { enemy* tauntEnemy = m_pHead; // the head enemy will taunt. cout << "The enemy is taunting you.\n"; tauntEnemy->taunt(); } int main() { battlefield theBattleField; cout << "Enemies are spawning !" << endl; int number; cout << "Please enter a number: "; cin >> number; for(int i = 0; i < number; i += 5) { theBattleField.AddEnemy(); } theBattleField.EnemyTaunt(); theBattleField.GetEnemiesName(); cout << "Type in the name of the enemy you would like to search: "; string enemySearch; cin >> enemySearch; theBattleField.searchName(&enemySearch); theBattleField.killEnemy(); theBattleField.GetEnemiesName(); return 0; } I made this game, where the user enters a number and makes a bunch of enemies based on that number. What I wanted to try was calling a function that asks a user to type in the enemy name, and will kill that enemy. everytime i run the game crashes. Any Ideas on why ?
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!