[TextMode Game Create] Text based game

Today we are going to study about Text based game in C++. It is a simple text based adventure game in C++, where players navigate through rooms, collect items, and face challenges.

Table of Contents

Text based adventure games have been a staple in the world of gaming for decades. These games challenge players to navigate through a series of scenarios using only text-based commands and descriptions.

Introduction of C++ Game

Our text based adventure game will be a simple game where the player starts in a dark room with two doors. Behind one door lies treasure, and behind the other door, a fierce monster awaits. As the player progresses through the game, they will encounter locked chests, keys, and a sleeping dragon guarding the treasure.

Game Structure

Rooms and Choices

The game begins with the player choosing between two doors: the left door and the right door. Depending on their choice, the player will enter different rooms with unique challenges and outcomes.

text based game

Inventory System

We’ve incorporated an inventory system to allow the player to collect and manage items throughout the game. In our game, the player can find a key to unlock a door and progress further.

C++ game with functionality

After completing the game or meeting an unfortunate end, players have the option to play again, resetting the game and their inventory.

GitHub Repository

you can get the source code of text based adventure game through our GitHub Repository by clicking on this button.

Source Code

Before download code please make sure to disable AD-BLOCKER because the ad revenue is used for hosting our Website. The download will automatically starts after 10 seconds of stay on redirected page.

Download Code

Implementing the Game

Main Function

The main() function initializes the game by welcoming the player and prompting them to choose a door.

Rooms in Text based game

Room 1

In the first room, the player encounters a locked chest and a key on the table. The player can choose to take the key or leave the room. If the player takes the key, it will be added to their inventory.

C++ game for beginners

Room 2

The second room contains a sleeping dragon. If the player has the key in their inventory, they can use it to unlock the door and escape quietly. Otherwise, the dragon wakes up and eats the player.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

C++ game

Room 3

The final room is filled with treasures. If the player successfully navigates through the challenges and reaches this room, they win the game.

Play Again Function

After completing the game or meeting an unfortunate end, players can choose to play again or exit the game.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
void play_again() {
    while (true) {
        string choice;
        cout << "Do you want to play again? (yes/no) ";
        cin >> choice;
 
        if (choice == "yes") {
            inventory.clear();
            main();
            break;
        }
        else if (choice == "no") {
            cout << "Thanks for playing!\n";
            break;
        }
        else {
            cout << "Invalid choice. Please enter 'yes' or 'no'.\n";
        }
    }
}

How do I win the game?

In our example game, to win, you need to collect the key from room1(), use it to unlock the door in room2(), and then proceed to room3() to claim the treasure. If you successfully navigate through the rooms and make the right choices, you will win the game.

How do I play the text-based adventure game?

To play the text-based adventure game, read the prompts and make choices by typing the corresponding commands. Follow the story, collect items, and navigate through rooms to reach the end goal. In our example game, you start in a dark room with two doors and make choices to progress through rooms with challenges and rewards.

What is a text based adventure game?

A text based adventure game is a type of video game that uses text descriptions to present the game world to the player. Players interact with the game by typing commands and making choices to navigate through the story and solve puzzles.

Conclusion

Creating a text-based adventure game in C++ is a fun and educational project that allows you to practice your programming skills while building an interactive game. Our simple game includes rooms, choices, an inventory system, and play again functionality, providing a foundation for you to expand and enhance the game further.

Whether you’re a beginner learning C++ or an experienced programmer looking for a fun project, creating a text-based adventure game is a rewarding experience that showcases the versatility and creativity of programming. So, grab your keyboard and start coding your own adventure!

Code of text base game in C++

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <string>
#include <vector>
using namespace std;
 
void room1();
void room2();
void room3();
void left_door();
void right_door();
void play_again();
 
vector<string> inventory;
 
int main() {
    cout << "Welcome to the Text-based Adventure Game!\n";
    cout << "You find yourself in a dark room.\n";
    cout << "You see two doors in front of you, one on the left and one on the right.\n";
 
    while (true) {
        string choice;
        cout << "Which door do you choose? (left/right) ";
        cin >> choice;
        
        if (choice == "left") {
            left_door();
            break;
        }
        else if (choice == "right") {
            right_door();
            break;
        }
        else {
            cout << "Invalid choice. Please enter 'left' or 'right'.\n";
        }
    }
 
    return 0;
}
 
void left_door() {
    cout << "\nYou chose the left door.\n";
    room1();
}
 
void right_door() {
    cout << "\nYou chose the right door.\n";
    room2();
}
 
void room1() {
    cout << "You enter a room with a locked chest.\n";
    cout << "You notice a key on the table.\n";
 
    while (true) {
        cin.ignore();
        cout << "What would you like to do? (take key/leave) ";
        string choice;
        getline(cin, choice);
 
 
        if (choice == "take key") {
            inventory.push_back("key");
            cout << "You took the key.\n";
        }
 
        else if (choice == "leave") {
            cout << "You leave the room.\n";
            right_door();
            break;
        }
        else {
            cout << "Invalid choice.\n";
            break;
        }
         
    }
}
void room2() {
    cout << "You enter a room with a sleeping dragon.\n";
 
    if (std::find(inventory.begin(), inventory.end(), "key") != inventory.end()) {
        cout << "You use the key to unlock the door and escape quietly.\n";
        room3();
    }
    else {
        cout << "The dragon wakes up and eats you!\n";
        play_again();
    }
}
 
void room3() {
    cout << "You enter a room filled with treasures!\n";
    cout << "Congratulations, you win!\n";
    play_again();
}
 
void play_again() {
    while (true) {
        string choice;
        cout << "Do you want to play again? (yes/no) ";
        cin >> choice;
 
        if (choice == "yes") {
            inventory.clear();
            main();
            break;
        }
        else if (choice == "no") {
            cout << "Thanks for playing!\n";
            break;
        }
        else {
            cout << "Invalid choice. Please enter 'yes' or 'no'.\n";
        }
    }
}
 
 
 

[출처] https://guiprojects.com/text-based-game/

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
45 [게임 일반] 선택 기반 게임의 표준 패턴 : Standard Patterns in Choice-Based Games 졸리운_곰 2025.01.13 236
44 [게임 일반] SharpMoku a Gomoku/Five in a Row Written in C# : SharpMoku a Gomoku/Five in a Row C#로 작성됨 : 오목게임 개발 file 졸리운_곰 2024.10.31 311
43 [게임 마켓플레이스] 스팀에 ‘게임’을 출시하고 싶다면? file 졸리운_곰 2024.09.07 316
42 [게임 일반] 카드 덱 이미지 (트럼프 카드) 졸리운_곰 2024.05.27 224
41 [게임 개발 일반] 콘솔은 어려울수록, 모바일은 쉬울수록 '돈' 쓴다 file 졸리운_곰 2023.10.30 264
40 [게임 개발 일반] 게임 초기 기획서 작성 방법 졸리운_곰 2023.07.16 250
39 [게임 일반] 하이퍼 캐주얼 모바일 게임 퍼블리셔가 말하는 개발자의 필수 역량은? file 졸리운_곰 2023.02.08 245
38 [게임 일반] 외신, 2D 게임 개발에 적합한 엔진 10종 선정 file 졸리운_곰 2023.01.15 206
37 [게임 일반] 성공적인 하이퍼 캐주얼 게임 제작을 위해 알아야 할 모든 것 졸리운_곰 2023.01.11 192
36 [게임 일반] 게임의 정의와 요소 file 졸리운_곰 2023.01.02 185
35 [게임 일반] 게임(Game), 일상에서 만드는 놀이의 즐거움 file 졸리운_곰 2023.01.02 205
34 [게임 일반] 우리는 왜 게임에 빠지는가 - 게임의 요소와 게임 변천의 역사 졸리운_곰 2023.01.02 250
33 [게임 일반] 재미있는 게임이란? 10부. 게임의 6단계 file 졸리운_곰 2023.01.02 194
32 [게임 일반] 재미있는 게임이란? 9부 -스타2의 패턴전략- file 졸리운_곰 2023.01.02 214
31 [게임 일반] 재미있는 게임이란? 8부 -블리자드의 패턴전략- file 졸리운_곰 2023.01.02 265
30 [게임 일반] 재미있는 게임이란? 7부 -Wow의 패턴전략- file 졸리운_곰 2023.01.02 162
29 [게임 일반] 재미있는 게임이란? 6부 -패턴과 리소스- file 졸리운_곰 2023.01.02 228
28 [게임 일반] 재미있는 게임이란? 5부 -카오스 시스템- file 졸리운_곰 2023.01.02 266
27 [게임 일반] 재미있는 게임이란? 4부 -패턴과 상호작용-' file 졸리운_곰 2023.01.02 186
26 [게임 일반] 재미있는 게임이란? 3부 '게임이 나아가야 할 방향' file 졸리운_곰 2023.01.02 162
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED