You are currently viewing Simple RPG Monster Game using C++ (With Source Code)

Simple RPG Monster Game using C++ (With Source Code)

Simple RPG Monster Game using C++ (With Source Code)

Hello, coders. Welcome to the codewithrandom blog. In this article, we will learn how to create a RPG Monster Game using C++ (With Source Code).

A RPG Monster game implemented in C++ is a console based game which simulates a simple battle scenario between the player and a monster, where each participant selects attacks and defenses randomly, and the outcome is determined based on the calculated damage and remaining health points.

Simple RPG Monster Game using C++

The game creates random qualities such as attack power, defence, and agility for both the player and the monster. The player and the monster take turns attacking and defending as the combat progresses. The traits of the attacker and defencer affect the result of each attack. The fight will continue until either the player’s or the monster’s health reaches zero.

Creating a School Fees Enquiry System using C++ (With Source Code)

Overview of the UI:

Monster start
==============
What do you want to do?
1 – Fierce Attack
2 – Lithe Attack
3 – Defensive moves

The user will choose an option to perform the operation of their choice. User will then enter a values according to the prompts that will be displayed on the console to perform that operation.

RPG Monster Game Source Code:

You can directly use this code by copying it in your IDE to understand the working and then can create it by understanding the project.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
  int choice;
  int mhp, hp, i, init, atk, def, matk, mdef, hurt, mhurt, agi, magi;
  atk = 10;
  def = 15;
  agi = 5;
  matk = 10;
  mdef = 15;
  magi = 5;
  
  srand((unsigned)time(0));
  init = rand()%2+1;
  mhp = rand()%50 + 60;
  hp = rand()%20 + 80;
  if (init == 1) {
  cout<<"You start.\n==========\n";
  while (hp > 0 || mhp > 0) {
    cout<<"What do you want to do?\n1 - Fierce Attack\n2 - Lithe Attack\n3 - Defensive moves\n";
     do{cin>>choice;}while(choice>3 || choice<1);
    switch (choice) {
      case 1:
        atk = rand()%20+10;
    def = rand()%10+10;
    agi = rand()%5;
    break;
      case 2:
        atk = rand()%5+10;
    def = rand()%10+10;
    agi = rand()%15;
        break;
      case 3:
        atk = rand()%10+10;
    def = rand()%20+10;
    agi = rand()%5;
    break;
    }
    choice = rand()%3;
    switch (choice) {
      case 1:
        matk = rand()%20+10;
    mdef = rand()%10+10;
    magi = rand()%5;
    break;
      case 2:
        matk = rand()%5+10;
    mdef = rand()%10+10;
    magi = rand()%15;
        break;
      case 3:
        matk = rand()%10+10;
    mdef = rand()%20+10;
    magi = rand()%5;
    break;
    }

//HÀr dör folk o sÄnt
    mhurt = (atk - magi) - (mdef/atk);
    if (mhurt < 0) {
      mhurt = 0;
    }
    mhp = mhp - mhurt;
    cout<<"You did "<<mhurt<<" damage to the monster!\n";
    cin.get();
//Specielt hÀr
    if (mhp < 1) {
      cout<<"You killed the beast!! You won with "<<hp<<" hp left.\n";
      cin.get();
      return 0;
      }
    cout<<"The monster now have "<<mhp<<" hp left.\n";
    hurt = (matk - agi) - (def/matk);
    if (hurt < 0) {
      hurt = 0;
    }
    hp = hp - hurt;
    cout<<"The monster hit you for "<<hurt<<" damage.\n";
//Och hÀr.
    if (hp < 1) {
      cout<<"You died. The beast still has "<<mhp<<" hp left.\n";
      cin.get();
      return 0;
      }
cout<<"You now have "<<hp<<" hp left.\n\n";
     }
     }

//Om monstret startar.
  else {
  cout<<"Monster start.\n==============\n";
    while (hp > 0 || mhp > 0) {
    choice = rand()%3;
    switch (choice) {
      case 1:
        matk = rand()%20+10;
    mdef = rand()%10+10;
    magi = rand()%5;
    break;
      case 2:
        matk = rand()%5+10;
    mdef = rand()%10+10;
    magi = rand()%15;
        break;
      case 3:
        matk = rand()%10+10;
    mdef = rand()%20+10;
    magi = rand()%5;
    break;
    }
//Monstret börjar!! han slÄr till direkt.
    hurt = (matk - agi) - (def/matk);
    if (hurt < 0) {
      hurt = 0;
    }
    hp = hp - hurt;
    cout<<"The monster hit you for "<<hurt<<" damage.\n";
//Oooooh, gotta hurt!
    if (hp < 1) {
      cout<<"You died. The beast still has "<<mhp<<" hp left.\n";
      cin.get();
      return 0;
      }
 cout<<"You now have "<<hp<<" hp left.\n\n";
    cout<<"What do you want to do?\n1 - Fierce Attack\n2 - Lithe Attack\n3 - Defensive moves\n";
     do{cin>>choice;}while(choice>3 || choice<1);
    switch (choice) {
      case 1:
        atk = rand()%20+10;
    def = rand()%10+10;
    agi = rand()%5;
    break;
      case 2:
        atk = rand()%5+10;
    def = rand()%10+10;
    agi = rand()%15;
        break;
      case 3:
        atk = rand()%10+10;
    def = rand()%20+10;
    agi = rand()%5;
    break;
        }


//HÀr kan han dö.
    mhurt = (atk - magi) - (mdef/atk);
    if (mhurt < 0) {
      mhurt = 0;
    }
    mhp = mhp - mhurt;
    cout<<"You did "<<mhurt<<" damage to the monster!\n";
    cin.get();
//Eller typ hÀr:
    if (mhp < 1) {
      cout<<"You killed the beast!! You won with "<<hp<<" hp left.\n";
      cin.get();
      return 0;
      }
    cout<<"The monster now have "<<mhp<<" hp left.\n";
  } } }

Now let us understand the code:-

  • We will start by writing the header of the code with the required libraries – iostream  (input and output stream), cstdlib (memory management), ctime (time management).
  • We will be declaring variables such as choice, mhp, hp, i, init, atk, def, matk, mdef, hurt, mhurt, agi, and magi first. These variables indicate different battle characteristics and data.
  • We will also initialize values for some variables as well which will be used further on.
  • Now we will be generating random numbers  using the srand() and rand() functions. The random number generator is initialized using srand((unsigned)time(0)).
  • Init, mhp, and hp are all given random values in the code. Which participant begins the combat is determined by init.
  • Now, if the player initiates the combat (init == 1), the code enters a loop in which the battle continues until either the player’s or the monster’s health hits zero.
  • We will be giving the player three options: Fierce Attack, Lithe Attack, or Defensive Moves. The selection is checked to ensure that it falls within the acceptable range. We will use a switch case for the same.
  • The player’s attack (atk), defence (def), and agility (agi) qualities in the program will be modified with random values based on the player’s decision.
  • After that, the program chooses a random attack plan for the monster, adjusting its attack (matk), defence (mdef), and agility (magi) qualities appropriately.
  • We will be calculating Damage for both the player and the monster. The player’s damage to the monster is calculated using the attack and magic defence values, while the monster’s damage to the player is computed using the attack and defence numbers.
  • Then we will remove the computed damage from the health points (hp or mhp) of each player.
  • Here we will display the outcome of the player’s assault, as well as the player’s updated health points.
  • For instance, if the player’s health points (hp) drop to zero, the player is deemed defeated, and the program exits.
  • And If the monster’s health points (mhp) reach zero, the player is proclaimed the winner, and the program exits.
  • Considering the combat hasn’t ended yet, the program moves on to the next iteration of the loop, alternating between the player’s and the monster’s turns.
  • So now if the monster initiates the combat (init!= 1), the code proceeds in a similar manner, but with the order of events reversed.
  • This sums up or project of creating a RPG game using C++.

Creating Advanced Calculator using C++ (Source Code)

Final Output:-

Here is an example to show how this project works.

Simple RPG Monster Game using C++

Simple RPG Monster Game using C++

Simple RPG Monster Game using C++

Simple RPG Monster Game using C++

Video Output:

Conclusion

We have reached the end of this article and have a lot more projects in C++ coming so stay tuned. We have started with awesome and fun projects for you all to understand C++. Learning C++ by creating fun projects makes learning easy and interesting.

ShutDown and Restart Computer system in C++ (With source code)

If you enjoyed the article and learned something new today, let us know in the comments.

Thank you.

Happy Reading! 🙂

Follow: CodeWithRandom



Leave a Reply