C++開發控制檯小遊戲之旅(1)——《伏魔記》

2020-09-25 15:00:19

相信每一位學程式設計的朋友,都曾有一顆遊戲夢想,希望能親手打造出屬於自己的遊戲!

如果我可以創作一款結合AR技術的、如**動漫《遊戲王》**中那樣進行決鬥的遊戲,該有多好!

遊戲背景

《伏魔記》曾是一款可在步步高電子詞典上運行的RPG遊戲,我在上小學六年級的時候對其愛不釋手,日夜把玩,終於通關,仍意猶未盡。

如今我萌發心思,欲抽空發展一下愛好,自己動手逐步打造出一款可玩的小遊戲,同時也為廣大遊戲程式設計愛好者們提供略微的參考,一起為了夢想而前行!

本遊戲採用純C++語言進行編寫,可在Windows控制臺Linux終端運行。
使用Visual StudioGCC進行編譯即可。
遊戲內操作均通過鍵盤輸入達成,簡單易上手。
全部源碼檔案僅有1個,直接複製貼上到本地即可輕鬆完成編譯構建,生產可執行程式並開啟遊戲。

注:遊戲源碼在頁面底部。


若諸位喜歡《伏魔記》,並希望改善遊戲體驗(如期待後續增加的遊戲功能、玩法等),可在留言區新增評論,進行吐槽或提供寶貴建議,亦可新增作者微信、QQ,
相互交流,共同進步!
我將認真回復並虛心學習,感謝諸位!

我的聯係方式
QQ:1575335819
微信:CS-huo

遊戲截圖

背景故事
角色屬性
購買道具
戰鬥系統

遊戲特色

  • 角色屬性系統
  • 道具店鋪
  • 戰鬥系統
  • 花式字型顔色

後續待完善

  • 遊戲存檔
  • 道具使用
  • 揹包
  • 角色升級
  • 遊戲職業

原始碼

#include <iostream>
#include <string>
#include <list>
#include <cstdio>
#ifdef _WIN32
#include <Windows.h>
#endif // _WIN32

using namespace std;


enum ValueType
{
	Attack,
	Defend,
	Life,
	CurLife,
	Money
};

struct Hero
{
	string name = "伏零";
	string job = "伏魔師";
	string sex = "男";
	int age = 16;
	int money = 100;
	int life = 100;
	int attack = 10;
	int defend = 10;
	int curLife = 100;

	int skill()
	{
		cout << "你使出兩劍歸宗" << endl;
		return attack * 2;
	}
};

struct Ghost
{
	string name;
	string job;
	string sex;
	int age;
	int money;
	int life;
	int attack;
	int defend;
	int curLife;
	virtual int skill() { return 0; }
};

struct Zombie : Ghost
{
	Zombie()
	{
		name = "喪屍";
		job = "普通怪";
		sex = "男";
		age = 23;
		money = 50;
		life = 50;
		attack = 8;
		defend = 5;
		curLife = 50;
	}

	int skill()
	{
		cout << name << "對你撲咬" << endl;
		return attack * 1.5;
	}
};

struct NagaQueue : Ghost
{
	NagaQueue()
	{
		name = "蛇妖";
		job = "精英怪";
		sex = "女";
		age = 800;
		money = 10000;
		life = 10000;
		attack = 388;
		defend = 100;
		curLife = 388;
	}

	int skill()
	{
		cout << name << "朝你嬌媚一笑,似乎暗藏玄機" << endl;
		return attack * 2;
	}
};

struct NashBaron : Ghost
{
	NashBaron()
	{
		name = "納什男爵";
		job = "Boss";
		sex = "男";
		age = 10000;
		money = 100 * 10000;
		life = 100 * 10000;
		attack = 999;
		defend = 999;
		curLife = 100 * 10000;
	}

	int skill()
	{
		cout << name << "發出怒吼,吐出龍息" << endl;
		return attack * 2;
	}
};


static char key = '0';
static Hero hero;

void startGame();
void endGame();
void archiveGame();
void setColor(int n);
void restoreColor();
void tellStory();
void up();
void chooseMainAction();
void purchaseTool();
void vanquishGhost();
void heroInfo();
bool checkPurchase(int n);
void changeValue(ValueType type, int n);
void fightZombie();
void fightNagaQueue();
void fightNashBaron();
void fight(Ghost *ghost);
void fightInfo(Ghost *ghost);
void heroFightRound(Ghost *ghost);
void ghostFightRound(Ghost *ghost);
void endFight(Ghost *ghost);


int main()
{
	startGame();

	return 0;
}

void startGame()
{
	// 遊戲背景講述
	tellStory();

	// 遊戲主線
	while (key != 'q')
	{
		chooseMainAction();
	}
}

void endGame()
{
	key = 'q';
	string str = "勝敗乃兵家常事,少俠請重新來過!";
	setColor(43);
	setColor(31);
	cout << str << endl;
	restoreColor();
}

void archiveGame()
{

}

#ifdef __linux
void setColor(int n)
{
	cout << "\033[" + to_string(n) + "m";
}

void restoreColor()
{
	cout << "\033[39m";
	cout << "\033[49m";
}
#endif // __linux
#ifdef _WIN32
void setColor(int n)
{
	WORD attr;
	if (n == 31) attr = FOREGROUND_RED;
	else if (n == 34) attr = FOREGROUND_BLUE;
	else if (n > 30 && n < 39) attr = FOREGROUND_GREEN;
	else if (n == 41) attr = BACKGROUND_RED;
	else if (n == 42) attr = BACKGROUND_GREEN;
	else if (n > 40 && n < 49) attr = BACKGROUND_BLUE;

	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), attr);
}

void restoreColor()
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
}
#endif // _WIN32

void tellStory()
{
	string story = "很久很久以前,華夏大地遍佈妖魔鬼怪,它們侵蝕著人類的地盤。\n"
		"為了謀得一絲存活空間,英雄們站了出來,紛紛成立正道門派,以對抗妖魔!\n"
		"這些門派中,有一個小門派名為伏魔派,以降妖伏魔為己任,雖力量弱小,卻義無反顧。\n"
		"我們故事的主角,便是你——" + hero.name + ",來自伏魔派的入門弟子。\n"
		"如今天下大亂,是時候出發了……";
	setColor(44);
	setColor(32);
	cout << story << endl;
	restoreColor();
}

void up()
{
	setColor(34);
	cout << "按`0`鍵返回上一級" << endl;
	restoreColor();
	cin >> key;
}

void chooseMainAction()
{
	string act = "1.打怪升級刷裝備\n"
		"2.購買道具\n"
		"3.角色資訊\n"
		"按`Q`鍵退出遊戲\n";
	setColor(32);
	cout << endl << act << endl;
	restoreColor();

	cin >> key;
	if (key == 'q' || key == 'Q') endGame();
	else if (key == '1') vanquishGhost();
	else if (key == '2') purchaseTool();
	else if (key == '3') heroInfo();
}

void heroInfo()
{
	string info = "名稱:" + hero.name + "\n"
		"職業:" + hero.job + "\n"
		"性別:" + hero.sex + "\n"
		"年齡:" + to_string(hero.age) + "\n"
		"金錢:" + to_string(hero.money) + "\n"
		"生命值:" + to_string(hero.curLife) + "/" + to_string(hero.life) + "\n"
		"攻擊力:" + to_string(hero.attack) + "\n"
		"防禦力:" + to_string(hero.defend);
	setColor(32);
	cout << info << endl;
	restoreColor();

	up();
	while (key != '0') cin >> key;
}

void purchaseTool()
{
	string str = "1.木劍(攻擊力+10) - 10銅幣\n"
		"2.銅劍(攻擊力+30) - 30銅幣\n"
		"3.干將莫邪(攻擊力+999) - 10000金幣\n"
		"4.屠龍寶刀(攻擊力+1333,防禦力-300) - 10000金幣\n"
		"5.布衣(防禦力+10) - 10銅幣\n"
		"6.鎖子甲(防禦力+50) - 88銀幣\n"
		"7.白開水(可回覆50點生命值)- 5銅幣\n"
		"8.天山雪蓮(提高生命上限100點) - 100金幣\n"
		"9.復活十字架(戰鬥死亡後,可自動重生一次) - 1000金幣";
	setColor(32);
	cout << endl << str << endl;
	restoreColor();

	// Tip how to choose
	up();
	while (!(key >= '0' && key <= '9'))
	{
		purchaseTool();
	}

	// Deal with choose
	if (key == '0') return;
	bool res = checkPurchase(key - '0');
	if (!res)
	{
		purchaseTool();
		return;
	}

	if (key == '1') changeValue(Attack, 10);
	else if (key == '2') changeValue(Attack, 30);
	else if (key == '3') changeValue(Attack, 999);
	else if (key == '4')
	{
		changeValue(Attack, 1333);
		changeValue(Defend, -300);
	}
	else if (key == '5') changeValue(Defend, 10);
	else if (key == '6') changeValue(Defend, 50);
	else if (key == '7') changeValue(CurLife, 50);
	else if (key == '8') changeValue(Life, 100);

	// You can purchase again
	purchaseTool();
}

void vanquishGhost()
{
	string info = "1.喪屍\n"
		"2.蛇妖\n"
		"3.納什男爵";
	setColor(31);
	cout << info << endl;
	restoreColor();

	up();
	while (key != '0')
	{
		if (key == '1') fightZombie();
		else if (key == '2') fightNagaQueue();
		else if (key == '3') fightNashBaron();
		else cout << "未找到怪物" << endl;

		// You can fight again
		vanquishGhost();
	}
}

bool checkPurchase(int n)
{
	int price = 0;
	string name = "";

	switch (n)
	{
	case 1:
		price = 10;
		name = "木劍";
		break;
	case 2:
		price = 30;
		name = "銅劍";
		break;
	case 3:
		price = 10000 * 10000;
		name = "干將莫邪";
		break;
	case 4:
		price = 10000 * 10000;
		name = "屠龍寶刀";
		break;
	case 5:
		price = 10;
		name = "布衣";
		break;
	case 6:
		price = 88 * 100;
		name = "鎖子甲";
		break;
	case 7:
		price = 5;
		name = "白開水";
		break;
	case 8:
		price = 100 * 10000;
		name = "天山雪蓮";
		break;
	case 9:
		price = 1000 * 10000;
		name = "復活十字架";
		break;
	default:
		break;
	}

	// Check the price with your money
	string info;
	if (price > hero.money)
	{
		info = "金幣不夠,購買失敗!";
		setColor(33);
		cout << info << endl;
		restoreColor();
		return false;
	}
	else
	{
		setColor(32);
		info = "購買成功,獲得" + name;
		restoreColor();
	}
	cout << info << endl;

	// Change gold
	changeValue(Money, -price);
	return true;
}

void changeValue(ValueType type, int n)
{
	string info;
	string valueName = "";
	int *value = nullptr;

	switch (type)
	{
	case Attack:
		valueName = "攻擊力";
		value = &hero.attack;
		break;
	case Defend:
		valueName = "防禦力";
		value = &hero.defend;
		break;
	case Life:
		valueName = "生命上限";
		value = &hero.life;
		break;
	case CurLife:
		valueName = "當前生命值";
		value = &hero.curLife;
		break;
	case Money:
		valueName = "金錢數量";
		value = &hero.money;
		break;
	default:
		cout << "發現奇奇怪怪的東東" << endl;
		return;
	}

	if (n > 0) info = valueName + "+" + to_string(n);
	else if (n == 0) info = valueName + "不變";
	else if (n < 0) info = valueName + "-" + to_string(n);

	setColor(35);
	cout << info << endl;
	*value += n;
	cout << "你的" + valueName + "由" << to_string(*value - n) << "變為了" << to_string(*value) << endl;
	restoreColor();
}

void fightZombie()
{
	string info = "站在你面前的是一隻樣貌醜陋的喪屍,它嘴裡流著噁心的液體,扭曲著四肢,頂著碎掉一半的腦子向你撲了過來……";
	setColor(34);
	cout << endl << info << endl;
	restoreColor();

	Zombie zombie;
	fight(&zombie);
}

void fightNagaQueue()
{
	string info = "你看向那妖豔的蛇發女子,不由地怔住了……";
	setColor(35);
	cout << endl << info << endl;
	restoreColor();

	NagaQueue nagaQueue;
	fight(&nagaQueue);
}

void fightNashBaron()
{
	string info = "峽谷傳來一聲怒吼,整個地面都開始顫動起來,一頭巨龍高昂起頭顱,口吐龍息,聲勢滔天。你定了定神,握緊手中長劍,踏步向前……";
	setColor(31);
	cout << endl << info << endl;
	restoreColor();

	NashBaron nashBaron;
	fight(&nashBaron);
}

void fight(Ghost *ghost)
{
	while (hero.curLife > 0 && ghost->curLife > 0)
	{
		// Hero's fight round
		fightInfo(ghost);
		heroFightRound(ghost);

		if (hero.curLife <= 0 || ghost->curLife <= 0) break;
		// Ghost's fight round
		fightInfo(ghost);
		ghostFightRound(ghost);
	}

	// End fight
	endFight(ghost);
}

void fightInfo(Ghost *ghost)
{
	string info = "戰鬥資訊:\n"
		+ ghost->name + "(" + to_string(ghost->curLife) + "/" + to_string(ghost->life) + ")\n"
		+ hero.name + "(" + to_string(hero.curLife) + "/" + to_string(hero.life) + ")\n"
		"-------------------------";
	setColor(46);
	setColor(33);
	cout << endl << info << endl;
	restoreColor();
}

void heroFightRound(Ghost *ghost)
{
	setColor(36);
	cout << "你的回合……" << endl;
	string str = "1.普通攻擊(預設) 2.技能 3.逃跑";
	setColor(33);
	cout << str << endl;
	restoreColor();

	cin >> key;
	if (key == '2')
	{
		int n = hero.skill();
		int damage = (n - ghost->defend < 1 ? 1 : n - ghost->defend);
		setColor(31);
		cout << "對" << ghost->name << "造成了" << damage << "點傷害" << endl;
		restoreColor();
		ghost->curLife -= damage;
	}
	// No use now
	else if (key == '3')
	{
		setColor(31);
		cout << "逃跑失敗!" << endl;
		restoreColor();
	}
	else
	{
		int n = hero.attack;
		int damage = (n - ghost->defend < 1 ? 1 : n - ghost->defend);
		setColor(31);
		cout << "你使出普通攻擊,對" << ghost->name << "造成了" << damage << "點傷害" << endl;
		restoreColor();
		ghost->curLife -= damage;
	}
}

void ghostFightRound(Ghost *ghost)
{
	setColor(36);
	cout << ghost->name << "的回合……" << endl;
	int n = ghost->skill();
	int damage = (n - hero.defend < 1 ? 1 : n - hero.defend);
	setColor(31);
	cout << ghost->name << "對你" << "造成了" << damage << "點傷害" << endl;
	restoreColor();
	hero.curLife -= damage;
}

void endFight(Ghost *ghost)
{
	if (ghost->curLife <= 0)
	{
		setColor(32);
		cout << "戰鬥勝利!你擊敗了" << ghost->name << ",獲得了" << ghost->money << "個銅幣" << endl;
		restoreColor();
		hero.money += ghost->money;
		return;
	}

	// If you die, then end the game
	if (hero.curLife <= 0)
	{
		setColor(31);
		cout << "惜敗!你被" << ghost->name << "殺死了!" << endl;
		restoreColor();
		endGame();
	}
}