Simple C++ text-based RPG featuring inventory, quests, and experience system for leveling up. Ideal starting point for expanding RPG projects.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
// Class representing the player with inventory, experience, and level
class Player {
private:
std::vector<std::string> inventario; // List of owned items
int esperienza;
int livello;
public:
Player() : esperienza(0), livello(1) {}
// Adds an item to the inventory
void aggiungiOggetto(const std::string& oggetto) {
inventario.push_back(oggetto);
std::cout << "You obtained: " << oggetto << "\n";
}
// Displays the inventory
void mostraInventario() const {
std::cout << "\n--- Inventory ---\n";
if (inventario.empty()) {
std::cout << "Inventory is empty.\n";
} else {
for (size_t i = 0; i < inventario.size(); ++i) {
std::cout << i + 1 << ". " << inventario[i] << "\n";
}
}
std::cout << "-----------------\n";
}
// Adds experience and manages level
void aggiungiEsperienza(int punti) {
esperienza += punti;
std::cout << "You gained " << punti << " experience points!\n";
// Check for level up every 100 points
while (esperienza >= 100) {
esperienza -= 100;
livello++;
std::cout << "Congratulations! You reached level " << livello << "!\n";
}
}
int getLivello() const {
return livello;
}
int getEsperienza() const {
return esperienza;
}
};
// Enum for quest state
enum class StatoMissione {
NON_INIZIATA,
IN_CORSO,
COMPLETATA
};
// Class representing a quest
class Mission {
private:
std::string nome;
std::string descrizione;
StatoMissione stato;
std::string ricompensaOggetto;
int ricompensaEsperienza;
public:
Mission(const std::string& n, const std::string& d, const std::string& ricompOggetto, int ricompExp)
: nome(n), descrizione(d), stato(StatoMissione::NON_INIZIATA),
ricompensaOggetto(ricompOggetto), ricompensaEsperienza(ricompExp) {}
// Starts the quest
void inizia() {
if (stato == StatoMissione::NON_INIZIATA) {
stato = StatoMissione::IN_CORSO;
std::cout << "Quest started: " << nome << "\n";
std::cout << descrizione << "\n";
} else {
std::cout << "The quest has already started or completed.\n";
}
}
// Completes the quest and assigns rewards
void completa(Player& giocatore) {
if (stato == StatoMissione::IN_CORSO) {
stato = StatoMissione::COMPLETATA;
std::cout << "Quest completed: " << nome << "!\n";
giocatore.aggiungiOggetto(ricompensaOggetto);
giocatore.aggiungiEsperienza(ricompensaEsperienza);
} else if (stato == StatoMissione::NON_INIZIATA) {
std::cout << "You must start the quest first.\n";
} else {
std::cout << "Quest already completed.\n";
}
}
StatoMissione getStato() const {
return stato;
}
std::string getNome() const {
return nome;
}
};
// Function to show the main menu
void mostraMenu() {
std::cout << "\n=== Role-Playing Game Menu ===\n";
std::cout << "1. Show inventory\n";
std::cout << "2. Start quest\n";
std::cout << "3. Complete quest\n";
std::cout << "4. Show player status\n";
std::cout << "5. Exit\n";
std::cout << "Choose an option: ";
}
int main() {
Player giocatore;
Mission missione1("Retrieve the Lost Sword", "Find the legendary sword hidden in the forest.", "Legendary Sword", 120);
bool giocoAttivo = true;
while (giocoAttivo) {
mostraMenu();
int scelta;
std::cin >> scelta;
switch (scelta) {
case 1:
giocatore.mostraInventario();
break;
case 2:
missione1.inizia();
break;
case 3:
missione1.completa(giocatore);
break;
case 4:
std::cout << "\nLevel: " << giocatore.getLivello() << "\n";
std::cout << "Experience: " << giocatore.getEsperienza() << "/100\n";
break;
case 5:
std::cout << "Exiting game. Goodbye!\n";
giocoAttivo = false;
break;
default:
std::cout << "Invalid option. Please try again.\n";
break;
}
}
return 0;
}