Only two layers are written , But the basic functions can be realized , Some renderings are as follows :
Put a little code casually , If you need complete code, you can add QQ skirt 【 eight zero six zero four / one five nine nine 】 receive
#include "header.h" #include <iostream> using namespace std; #include
<conio.h> // Monster array name Blood volume attack defense Add experience Add gold coins Monster monsterArray[3] = { { " Little monster ", 40,
5, 2, 5, 10 }, { " Little monster ", 50, 10, 5, 10, 30 }, { " Big strange ", 200, 50, 35, 30, 60 } };
// Item array Goods goodsArray[6] = { { " Ruby ", 0, 25, 0 }, { " sapphire ", 0,0, 25 }, { " The sword ",
0, 20, 0}, { " shield ", 0, 0, 20 }, { " Red bottle ", 200, 0, 0 }, { " Blue bottle ", 400, 0, 0} }; //
Hero movement direction Vec2 hero_up = { -1, 0 }; Vec2 hero_down = { 1, 0 }; Vec2 hero_left = {
0, -1 }; Vec2 hero_right = { 0, 1 }; // Hero structure object Hero g_hero; // Current level short
g_nLv = 1; // Initialize hero void initHero() { cout << " Please enter a nickname :" << endl; cin >>
g_hero.name; g_hero.lv = 1; g_hero.hp = 100; g_hero.atk = 10; g_hero.def = 5;
g_hero.gold = 0; g_hero.exp = 0; g_hero.rKey = 0; g_hero.bKey = 0; g_hero.yKey
= 0; } // Render hero information void RenderHero() {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);// yellow cout <<
"********************" << endl; cout << g_hero.name << " Grade :" << g_hero.lv << "
Blood volume :" << g_hero.hp << endl; cout << " attack :" << g_hero.atk << " defense :" << g_hero.def
<< endl; cout << " experience :" << g_hero.exp << " Gold coin :" << g_hero.gold << endl; cout <<
"********************" << endl; cout << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white cout << " Red key :"
<< g_hero.rKey << " Blue key :" << g_hero.bKey << " Yellow key :" << g_hero.yKey << endl; cout
<< endl; cout << "\t Section " << g_nLv << " layer " << endl; cout << endl; } // Render UI //
Render operation information // Get the element at the specified location int getElementOfPos(const Vec2& dir) { return
g_mapArray[g_nLv - 1][dir.x][dir.y]; } // Update the specified element at the specified location of the map void updateMap(const
Vec2& pos, MapElement e) { g_mapArray[g_nLv - 1][pos.x][pos.y] = e; } Goods*
createrGoods(MapElement e) { Goods* pGoods = new Goods; (*pGoods) =
goodsArray[e%HELP_A]; return pGoods; } // Create a monster of the specified type Monster*
createMonster(MapElement e) { Monster* pMonster = new Monster; // e%MONSTER_A
(*pMonster) = monsterArray[e%MONSTER_A]; return pMonster; switch (e) { case
MONSTER_A: (*pMonster) = monsterArray[0]; break; case MONSTER_B: (*pMonster) =
monsterArray[1]; break; case MONSTER_C: (*pMonster) = monsterArray[2]; break;
default: break; } } // Killing monsters increases the corresponding attributes of heroes void add(Monster* pMonster) { g_hero.exp
+= pMonster->addexp; g_hero.gold += pMonster->addgold; } // battle ( parameter : monster ) bool
pk(Monster* pMonster) { // Create a monster of the corresponding type while (true) { // Damage caused by heroes to monsters short hurt =
g_hero.atk - pMonster->def; hurt = hurt < 0 ? 0 : hurt;// Avoid negative injury pMonster->hp
-= hurt; cout << g_hero.name << " Launch a general attack " << pMonster->name << " Caused " << hurt <<
" Injury of , Blood volume is " << pMonster->hp << endl; if (pMonster->hp <= 0) { // Increase hero attribute
add(pMonster); // Release the monster delete pMonster; pMonster = nullptr; return true; }
Sleep(500); // Damage caused by monsters to heroes hurt = pMonster->atk - g_hero.def; hurt = hurt < 0 ?
0 : hurt;// Avoid negative injury g_hero.hp -= hurt; cout << pMonster->name << " yes " <<
g_hero.name << " Knock , Caused " << hurt << " Injury of , Blood volume is " << g_hero.hp << endl; if
(g_hero.hp <= 0) { // Release the monster delete pMonster; pMonster = nullptr; return false; }
Sleep(500); } } // Add equipment attributes void AddGoods(Goods *newGoods) { g_hero.hp +=
newGoods->addhp; g_hero.atk += newGoods->addatk; g_hero.def +=
newGoods->adddef; } // Move function void operate(const Vec2& dir) { // Next position Vec2
nextpos = { g_hero.pos.x + dir.x, g_hero.pos.y + dir.y }; // Get the element of the next location int e =
getElementOfPos(nextpos); switch (e) { case ROAD: // Update map data
updateMap(g_hero.pos, ROAD); updateMap(nextpos, HERO); break; case MONSTER_A:
case MONSTER_B: case MONSTER_C: {// be careful : If yes case Define variables in the statement and initialize them with braces // Battle function
Monster* pMonster = createMonster((MapElement)e); if (g_hero.atk >
pMonster->def) { if (pk(pMonster)) { // Update map data updateMap(g_hero.pos, ROAD);
updateMap(nextpos, HERO); } } else{ cout << " Get stronger come on !" << endl; Sleep(500); } }
break; case UP: g_nLv++; // Set hero coordinates break; case DOWN: g_nLv--; // Set hero coordinates break;
case KEY_B: // Update map data updateMap(g_hero.pos, ROAD); updateMap(nextpos, HERO);
g_hero.bKey++; break; case KEY_R: // Update map data updateMap(g_hero.pos, ROAD);
updateMap(nextpos, HERO); g_hero.rKey++; break; case KEY_Y: // Update map data
updateMap(g_hero.pos, ROAD); updateMap(nextpos, HERO); g_hero.yKey++; break;
case DOOR_B: if (g_hero.bKey > 0) {// Update map data updateMap(g_hero.pos, ROAD);
updateMap(nextpos, HERO); g_hero.bKey--; } else cout << " Go to get the corresponding key and open the door again !" <<
endl; Sleep(500); break; case DOOR_R: if (g_hero.rKey > 0) {// Update map data
updateMap(g_hero.pos, ROAD); updateMap(nextpos, HERO); g_hero.rKey--; } else
cout << " Go to get the corresponding key and open the door again !" << endl; Sleep(500); break; case DOOR_Y: if
(g_hero.yKey > 0) {// Update map data updateMap(g_hero.pos, ROAD); updateMap(nextpos,
HERO); g_hero.yKey--; } else cout << " Go to get the corresponding key and open the door again !" << endl; Sleep(500);
break; case HELP_A: case HELP_B: case HELP_C: case HELP_D: case HELP_E: case
HELP_F: { Goods* pGoods = createrGoods((MapElement)e); AddGoods(pGoods); //
Update map data updateMap(g_hero.pos, ROAD); updateMap(nextpos, HERO); } break; default:
break; } } // Get the user's key information void KeyBoardListener() { char ch = _getch(); switch
(ch) { case 'w': case 'W': operate(hero_up); break; case 's': case 'S':
operate(hero_down); break; case 'a': case 'A': operate(hero_left); break; case
'd': case 'D': operate(hero_right); break; default: break; } } // Game initialization void
GameInit() { initHero(); } // Game rendering void GameRender() { system("CLS");
RenderHero(); // Render map RenderMap(); // ui Hero data .. cout << endl; cout << " W" <<
endl; cout << " Press A S D Move " << endl; cout << " Get the key of the corresponding color (";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);// red cout << "K";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white ) cout <<
") Open the gate ("; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);// red cout
<< "‖"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white ) cout
<< ")" << endl; cout << " Through gem (";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);// red cout << "◆";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white cout <<
") Increase strength , Defeat the monster ("; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);// red
cout << "※"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white
cout << ") bar !" << endl; } // Game update void GameUpdate() { // Monitor keyboard
KeyBoardListener(); } // Render map void RenderMap() { for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) { switch (g_mapArray[g_nLv - 1][i][j]) { case
ROAD: cout << " "; break; case WALL: cout << "■"; break; case MONSTER_A:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);// red cout << "※";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white break; case
MONSTER_B: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);// red cout
<< "※"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white break;
case MONSTER_C: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);// red
cout << "※"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white
break; case HERO: cout << "♀"; // Update hero coordinates g_hero.pos.x = i; g_hero.pos.y = j;
break; case UP: cout << " upper "; break; case DOWN: cout << " lower "; break; case DOOR_R:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);// red cout << "‖";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white break; case
DOOR_B: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);// Light blue cout
<< "‖"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white break;
case DOOR_Y: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);// yellow
cout << "‖"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white
break; case KEY_B: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
3);// Light blue cout << "K "; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
7);// white break; case KEY_R:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);// red cout << "K ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white break; case
KEY_Y: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);// yellow cout <<
"K "; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white break;
case HELP_A: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);// red
cout << "◇"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white
break; case HELP_B: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
3);// Light blue cout << "◇"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
7);// white break; case HELP_C:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);// yellow cout << "◇";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white break; case
HELP_D: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);// yellow cout <<
"◇"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white break;
case HELP_E: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);// red
cout << "◆"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);// white
break; case HELP_F : SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
3);// Light blue cout << "◆"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
7);// white break; break; default: cout << " wrong "; break; } } cout << endl; } }
#include <windows.h>
Technology