View Full Version: My first game! :)

C++ Learning Community > C++ Works in Progress > My first game! :)


Title: My first game! :)
Description: Well...


Pie.taste() - November 25, 2005 04:38 PM (GMT)
Look at this game i'm working on! :)

Its like, a sort of, fun game thing. (WIP).

Its got two integers to know your position (console app). And stuff.

SO lookie! :)

CODE


#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

void position(int charX, int charY); //displays users position
int move(int charX, int charY, char moveDir, bool XY); //makes player move, cin function, etc.
bool checkCoOrd(int charX, int charY); //checks to see if any events are at that Co-Ordinate
int encounter(int charX, int charY); //decides what event is at the current co-ordinate, if any.
string ENCOUNTER(int encNum); //decides some more what event is at the current co-ordinate, if any.

int main()
{
   
   int charX, charY, encNum;
   bool XY = false;
   bool run = true;
   bool check;
   char moveDir;
   string player;
   string encounterVar;
   
   cout << "Hello, welcome to this game.\n\n";
   cout << "\tPlease enter your name: ";
   cin >> player;
   cout << "\n\n";
   
   charX = 0;
   charY = 0;
   
   while(run != false)
   {
   cout << "\n\tPlease enter a direction (W,A,S,D), enter 'Q' to quit: ";
   cin >> moveDir;
   cout << "\n\n";
   
   if(moveDir == 'Q') {
              run = false;
              return 0;
   }
   
   charX = move(charX, charY, moveDir, XY);
   
   XY = true;
   
   charY = move(charX, charY, moveDir, XY);
   
   XY = false;
   
   position(charX, charY);
   
   check = checkCoOrd(charX, charY);
   encNum = encounter(charX, charY);
   encounterVar = ENCOUNTER(encNum);
   
   if(check == true) {
            cout << "You have encountered " << encounterVar << endl;
   }
   
   }
   
   system("PAUSE");
   return 0;
}

void position(int charX, int charY)
{
    cout << "Your position is: " << charX << ", " << charY << ".\n\n";
}

int move(int charX, int charY, char moveDir, bool XY)
{
   if(XY == false) {
         switch(moveDir)
         {
                        case 'A' : charX = (charX - 1); break;
                        case 'D' : charX = (charX + 1); break;
         }
   return charX;
   }
   else
   {
   switch(moveDir)
         {
                        case 'W' : charY = (charY + 1); break;
                        case 'S' : charY = (charY - 1); break;
         }
   return charY;
   }
}

bool checkCoOrd(int charX, int charY)
{
    if((charX == 3 && charY == 4) || (charX == 2 && charY == 8)) {
              return true;
    }
}

int encounter(int charX, int charY)
{
   int num;
   if((charX == 3 && charY == 4) || (charX == 2 && charY == 8)) {
             if(charX == 3 && charY == 4) {
                      num = 1;
             }
             else if(charX == 2 && charY == 8) {
                  num = 2;
             }
   }
   return num;      
}

string ENCOUNTER(int encNum)
{
      string str;
     
      if(encNum == 1 || encNum == 2) {
                if(encNum == 1) {
                          str = "Goblin";
                          return str;
                }
                else if(encNum ==2) {
                     str = "Dragon";
                     return str;
                }
      }
}





Keep in mind that i am pretty new to the world of C++. :)

YAY

*is happy with code :) *

ALSO: To see it in PRETTY READBLE COLOURFUL FORMAT, goto:


http://www.rafb.net/paste/results/ncgFFA96.html

:)

Pie.taste() - November 26, 2005 11:42 AM (GMT)
Update! :)

.cpp file code:

CODE

#include <cstdlib>
#include <string>
#include "RPGfuncts.hpp"
#include <iostream>
using namespace std;

int main()
{
   
   
   int charX, charY, encNum, fight;
   
   
   bool XY = false, run = true, check2 = false, alive = true, check;
   
   
   char moveDir, action, talkAct;
   
   
   string player;
   string encVar;
   
   
   cout << "Hello, welcome to this game.\n\n";
   cout << "\tPlease enter your name: ";
   cin >> player;
   cout << "\n\n";
   
   
   charX = 0;
   charY = 0;
   
   
   while(run != false)
   {
   cout << "\n\tPlease enter a direction (W,A,S,D), enter 'Q' to quit: ";
   cin >> moveDir;
   cout << "\n\n";
   
   
   if(moveDir == 'Q') {
              run = false;
              return 0;
   }
   
   
   charX = move(charX, charY, moveDir, XY);
   
   
   XY = true;
   
   
   charY = move(charX, charY, moveDir, XY);
   
   
   XY = false;
   
   
   position(charX, charY);
   
   
   check = checkCoOrd(charX, charY);
   encNum = encounter(charX, charY);
   encVar = ENCOUNTER(encNum);
   
   
   if(check == true) {
            cout << "You have encountered a " << encVar << ".\n\n";
            action = event(encNum);
   
   
            switch(action)
            {
                          case 'A' : Attack(encVar, player); break;
                          case 'T' : fight = Talk(encVar, player, talkAct, charX, charY, check2); break;
                          case 'R' : charX = Run(encVar, charX, charY, check2); check2 = true; charY = Run(encVar, charX, charY, check2); cout << "\nYou have run away!\n\n"; position(charX, charY); check2 = false; break;
            }
   }
   
   if(fight == 2) {
            cout << "\n\t\tPrepare to FIGHT!\n\n\n";
            alive = Attack(encVar, player);
   }
   
   if(alive == false) {
            cout << player << " is dead.\n\n";
            cout << "\t\t!GAME OVER!\n\n\n";
   }
}
   
   system("PAUSE");
   return 0;
}




the .hpp file code:

CODE


#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;


// ----------------------------------------------------------------------------------------------
/* ********************* DECLARATIONS DECLARATIONS DECLARATIONS DECLARATIONS ***************** */
// ----------------------------------------------------------------------------------------------


void position(int charX, int charY); //displays users position

char event(int encNum); //performs actions on encounters, fighting, etc.

int move(int charX, int charY, char moveDir, bool XY); //makes player move, cin function, etc.

bool checkCoOrd(int charX, int charY); //checks to see if any events are at that Co-Ordinate

int encounter(int charX, int charY); //decides what event is at the current co-ordinate, if any.

string ENCOUNTER(int encNum); //decides some more what event is at the current co-ordinate, if any.

bool Attack(string encVar, string player); //attack function (fight target)

int Talk(string encVar, string player, char talkAct, int charX, int charY, bool check2); //talk function (speak to target)

int Run(string encVar, int charX, int charY, bool check2); //run function (run away, duh)



// ---------------------------------------------------------------------------------------
/* **********************FUNCTIONS FUNCTIONS FUNCTIONS FUNCTIONS*********************** */
// ---------------------------------------------------------------------------------------



void position(int charX, int charY)
{
    cout << "Your position is: " << charX << ", " << charY << ".\n\n";
}

int move(int charX, int charY, char moveDir, bool XY)
{
   if(XY == false) {
         switch(moveDir)
         {
                        case 'A' : charX = (charX - 1); break;
                        case 'D' : charX = (charX + 1); break;
         }
   return charX;
   }
   else
   {
   switch(moveDir)
         {
                        case 'W' : charY = (charY + 1); break;
                        case 'S' : charY = (charY - 1); break;
         }
   return charY;
   }
}



bool checkCoOrd(int charX, int charY)
{
    if((charX == 3 && charY == 4) || (charX == 2 && charY == 8)) {
              return true;
    }
}



int encounter(int charX, int charY)
{
   int num;
   if((charX == 3 && charY == 4) || (charX == 2 && charY == 8)) {
             if(charX == 3 && charY == 4) {
                      num = 1;
             }
             else if(charX == 2 && charY == 8) {
                  num = 2;
             }
   }
   return num;      
}



string ENCOUNTER(int encNum)
{
      string str;
     
      if(encNum == 1 || encNum == 2) {
                if(encNum == 1) {
                          str = "Goblin";
                          return str;
                }
                else if(encNum ==2) {
                     str = "Dragon";
                     return str;
                }
      }
}


char event(int encNum)
{
    char action;
   
    string creature;
   
    if(encNum == 1) {
              creature = "Goblin";
    }
    else if(encNum == 2) {
         creature = "Dragon";
    }
    cout << "\tA - Attack " << creature << ".\n";
    cout << "\tT - Talk to " << creature << ".\n";
    cout << "\tR - Run away from " << creature << ".\n\n";
    cout << "Choose an action: ";
    cin >> action;
    switch(action)
    {
                  case 'A' : cout << "\n\n"; return action; break;
                  case 'T' : cout << "\n\n"; return action; break;
                  case 'R' : cout << "\n\n"; return action; break;
                  default : cout << "\n\nYou did not enter a valid action.\n\n"; break;
    }
}

bool Attack(string encVar, string player)
{
    int playLife = 100, playAttack = 30; //set player stats
    int enemLife, enemAttack;
    char AtkDef;
    bool enemLive = true, charLive = true;
   
    cout << "\tFight between " << player << " and " << encVar << "!!!!\n\n";
   
    if(encVar == "Goblin") { //set goblin stats
                             enemLife = 100;
                             enemAttack = 20;
                   }
                   else if(encVar == "Dragon") { //set dragon stats
                        enemLife = 150;
                        enemAttack = 25;
                   }
                   
    while(charLive != false && enemLive != false)
    {
                   
                   

    cout << "\tA - Attack\n";
    cout << "\tD - Defend\n\n";
    cout << "Attack or Defend? ";
    cin >> AtkDef;
   
    if(AtkDef == 'A') {
              cout << "\n\n" << player << " attacks " << encVar << " and causes " << playAttack << " damage.\n\n";
              enemLife = (enemLife - playAttack);
              if(enemLife > 0) {
                          cout << "\tThe " << encVar << " is now at " << enemLife << " health!\n\n";
              }
              else if(enemLife < 1) {
                   cout << "\tThe " << encVar << " is dead!\n\n";
                   cout << "\t***YOU WIN!***\n\n\n";
              }
             
              if(enemLife > 0) {
                          cout << encVar << " counter attacks, causing " << enemAttack << " damage.\n\n";
                          playLife = (playLife - enemAttack);
                          cout << "\tYour health is now at " << playLife << "!!\n\n";
              }
    }
    else if(AtkDef == 'D') {
         cout << player << " enters a defensive stance!\n\n";
         cout << encVar << " attacks " << player << "!!\n\n";
         cout << encVar << " does half damage because player is in defence -\n\n";
         cout << encVar << " does " << (enemAttack / 2) << " damage!\n\n";
         playLife = (playLife - (enemAttack / 2));
         
         cout << player << "'s health is increased by 10 health points because of defence mode.\n\n";
         playLife = (playLife + 10);
    }
   
    if(playLife < 1) {
                charLive = false;
    }
                   
    if(enemLife < 1) {
                enemLive = false;
    }
   
   
    if(charLive == true && enemLive == false) {
                return true;
    }
    else if(charLive == false && enemLive == true) {
         return false;
    }
    }
}

int Talk(string encVar, string player, char talkAct, int charX, int charY, bool check2)
{
   int act;
   
   cout << encVar << ": Hello...\n\n";
   cout << "\tH - 'Hello!'\n";
   cout << "\tF - 'FIGHT ME!'\n";
   cout << "\tK - *Kick " << encVar << " in crotch and run*\n";
   cout << "\tR - *RUN!*\n\n";
   
   cout << "Choose an action: ";
   cin >> talkAct;
   
   switch(talkAct)
   {
                  case 'H' : act = 1; break;
                  case 'F' : act = 2; break;
                  case 'K' : act = 3; break;
                  case 'R' : act = 4; charX = Run(encVar, charX, charY, check2); check2 = true; charY = Run(encVar, charX, charY, check2); cout << "\nYou have run away!\n\n"; position(charX, charY); check2 = false; break;
   }
   
   if(act == 4) {
              return 0;
   }
   
   switch(act)
   {
              case 1 : cout << "\n" << player << ": Hello there.\n\n"; break;
              case 3 : cout << "\n\t" << player << " kicks the " << encVar << " in the crotch and runs away!\n"; charX = Run(encVar, charX, charY, check2); check2 = true; charY = Run(encVar, charX, charY, check2); cout << "\nYou have run away!\n\n"; position(charX, charY); check2 = false; break;
   }
   
   if(act == 2) {
          return 2;
   }
}

int Run(string encVar, int charX, int charY, bool check2)
{
   switch(check2)
   {
                 case false : charX = (charX - 2); return charX; break;
                 case true : charY = (charY - 4); return charY; break;
   }
}





:) Somebody's gotta reply some day! :D

Hippocrass - January 10, 2006 01:16 AM (GMT)
I found a bug. (Yay for me!!)
Essetially, the program is not changing your coordinates as you move.( see attached image)

I am quite new to the c++ thing so I cannot begin to figure out exactly what the problem is, but finding the problem is, at least, the first step in solving it.

DeAs91 - January 10, 2006 03:57 PM (GMT)
that's because you're typing with lower-cases and the program's switch is with upper-cases :)

You can use
CODE
var = tolower(var);




Hosted for free by InvisionFree