【通用 】--一些游戏中常用的宏、函数和枚举
1 #ifndef _MARCO_H_ 2 #define _MARCO_H_ 3 4 //------------------------常用系统库--------------------------------- 5 #include <stdlib.h> 6 #include<windows.h> 7 #include<string.h> 8 #include<conio.h> 9 #include<iostream> 10 #include<time.h> 11 using namespace std; 12 13 extern double uintSpeed ;//1秒1格 14 15 //------------------------自定义宏--------------------------------- 16 17 //纯虚函数(实现空函数) 18 #define CC_PROPERTY_VIRTUAL(varType,varName,FunName)\ 19 public: virtual varType Get##FunName() = 0; \ 20 public: virtual void Set##FunName(varType var) = 0; 21 22 //一般属性 23 #define CC_PROPERTY(varType, varName, FunName) \ 24 private: varType varName; \ 25 public: virtual varType Get##FunName(){ return varName; }\ 26 public: virtual void Set##FunName(varType var){ varName = var; } 27 28 //一般属性(私有、非负数) 29 #define PROPERTY_UNSIGNED(varType, varName, FunName) \ 30 private: varType varName; \ 31 public: virtual varType Get##FunName(){ return varName; }\ 32 public: virtual void Set##FunName(varType var){ varName = (var>0) ? var : 0; } 33 34 //只读属性 35 #define CC_PROPERTY_READ_ONLY(varType, varName, FunName)\ 36 private:varType varName; \ 37 public: virtual varType Get##FunName(){ return varName; } 38 39 //二维数组属性 40 #define CC_PROPERTY_ARR(varType,uintType, varName, FunName)\ 41 protected: varType varName; \ 42 public: virtual varType Get##FunName(){ return varName; }\ 43 public: virtual void Set##FunName(varType var){ varName = var; }\ 44 public: virtual void SetUnit##FunName(int i, int j, uintType var){ varName[i][j] = var; }\ 45 public: virtual uintType GetUnit##FunName(int i, int j){ return varName[i][j]; } 46 47 48 49 //-------------------------枚举------------------------------------ 50 enum ShapeType //外观形状 51 { 52 //============ 组合图案 =============== 53 // ---- 消耗品 ----- 54 ST_Bandage , //绷带 55 ST_Injector, //注射剂 56 ST_Bullet = '_', //子弹 57 ST_Clip, //弹夹 58 ST_MedKit, //医疗箱 59 ST_Grenade = '+', //手榴弹 60 ST_Bomb, //炸弹 61 ST_AP, //穿甲弹 62 63 //----- 武器类 --- 64 ST_Dagger, //匕首 65 ST_Pistol, //手枪 66 ST_Pillbox, //机关枪 67 ST_SniperRifle, //狙击枪 68 69 //----- 道具类 --- 70 ST_Armor, //防弹衣 71 ST_NightVision, //夜视仪 72 ST_FlashLight, //手电筒 73 ST_Radar, //雷达 74 ST_Cord = 't', //绳索 75 ST_Backpack, //背包 76 ST_Bugle, //军号 77 ST_Armory, //军械库 78 ST_GoldCoin, //金币 79 ST_Civilian, //平民 80 ST_Milltia, //民兵 81 ST_Lighten, //照明灯 82 83 //============ 单一图案 ================ 84 85 SPACE = '0', //空格 86 bBOSS = '3', //"魔" 87 sBOOS = '4', //"妖" 88 MONSTER = '+', //"¤" 89 Gate = 'c', //"∩" 90 iGate = 'y', //"∪" 91 NPC = 't', //"♂" 92 HERO = 'r', //"♀" 93 LADDER = 'u', //"⊥" 94 lLadder = 'l', //"〓" 95 ROPE = 'k', //"↓" 96 WALL = 'q', //"■" 97 WATER = 's', //"≈" 98 TREE = 'w', //"※" 99 rArrow = 'g', //"→" 100 lArrow = 'h', //"←" 101 uArrow = 'j', //"↑" 102 Rain = 'n', //"〃" 103 FOG = '8', //"∷" 104 105 }; 106 107 108 enum BodyType //物品类型 109 { 110 // ---- 消耗品 -------- 111 ET_Bandage, //绷带 112 ET_Injector, //注射剂 113 ET_Bullet = '_',//手枪子弹 114 ET_PillBt = 'C',//机关枪子弹 115 ET_SniperBt = 'D',//狙击枪子弹 116 ET_Clip, //弹夹 117 ET_MedKit, //医疗箱 118 ET_Grenade = '+',//手榴弹 119 ET_Bomb, //炸弹 120 ET_AP, //穿甲弹 121 122 //----- 道具类 -------- 123 ET_Armor, //防弹衣 124 ET_Caliga, //战靴 125 ET_NightVision, //夜视仪 126 ET_FlashLight, //手电筒 127 ET_Radar, //雷达 128 ET_Cord = 't', //绳索 129 ET_Backpack, //背包 130 ET_Bugle, //军号 131 ET_Armory, //军械库 132 ET_GoldCoin, //金币 133 ET_Civilian, //平民 134 ET_Milltia, //民兵 135 ET_Lighten, //照亮 136 ET_NUM , //血字 137 //----- 武器类 -------- 138 ET_Dagger, //匕首 139 ET_Pistol, //手枪 140 ET_Pillbox, //机关枪 141 ET_SniperRifle, //狙击枪 142 143 144 //----- 任务物品 -------- 145 ET_TASKITEM, 146 147 148 }; 149 150 enum ColorType //颜色 151 { 152 CT_Black, //黑 153 CT_TBlue, //深蓝色 154 CT_Green, //绿色 155 CT_BlueGreen, //蓝绿色 156 CT_Brown, //咖啡色 157 CT_Violet, //紫色 158 CT_Khaki, //卡其色 159 CT_OffWhite, //灰白色 160 CT_Grey, //灰色 161 CT_Blue, //蓝色 162 CT_LGreen, //亮绿色 163 CT_SkyBlue, //天蓝色 164 CT_Red, //红色 165 CT_Pink, //梅红色 166 CT_Yellow, //黄色 167 CT_White, //白色 168 169 }; 170 171 //物理属性 172 struct PhysicalProps 173 { 174 ShapeType ST; //形状 175 ColorType CT; //颜色 176 }; 177 178 179 enum ArmType //兵种 180 { 181 AT_Dog, //猎犬:撕咬(会自爆) 182 AT_Spearman, //枪兵:手枪 183 AT_Cavalry, //装甲车(防高、血厚) 184 AT_Artillery, //炮兵:攻击高 防低 185 AT_Boss, //boss 186 AT_Hero, //将军 187 }; 188 189 190 enum RaceType //国籍类型 191 { 192 RT_CHINA, //中国 193 RT_USA, //美国 194 RT_RUSSIA, //俄罗斯 195 RT_JAPAN, //日本 196 RT_IS, //伊斯兰国 197 }; 198 199 200 201 //------------------------全局函数--------------------------------- 202 //光标移动 203 void GotoXY(int, int); 204 205 //颜色函数 206 void Color(int, int); 207 208 //光标打印 209 void Print(char, int, int); 210 211 //光标打印物体 212 void PrintBody(PhysicalProps, int, int); 213 214 215 //时间函数 216 217 //物品图标图形库 218 void GetGraphicLib(char** Graphic, BodyType); 219 220 221 222 //---------开辟内存,释放内存--------- 223 //开辟内存 224 char **GetCharMemory(int row, int colu); 225 int** GetIntMemory(int row, int colu); 226 bool **GetBoolMemory(int row, int colu); 227 228 //释放内存 229 void DeleteCharMemory(char** pArr, int row); 230 void DeleteIntMemory(int** pArr, int row); 231 void DeleteBoolMemory(bool** pArr, int row); 232 233 234 235 236 237 238 239 240 241 #endif // _MARCO_H_
1 #include "Marco.h" 2 3 4 //光标移动 5 void GotoXY(int x, int y) 6 { 7 COORD pos = { 2 * y, x }; 8 HANDLE phand = GetStdHandle(STD_OUTPUT_HANDLE); 9 SetConsoleCursorPosition(phand, pos); 10 } 11 12 //颜色函数 13 void Color(int back, int before) 14 { 15 HANDLE hand = GetStdHandle(STD_OUTPUT_HANDLE); 16 SetConsoleTextAttribute(hand, before + back * 0x10); 17 } 18 19 20 //打印函数 21 void Print(char type, int X, int Y) 22 { 23 Color(CT_White, CT_Black); 24 GotoXY(X, Y); 25 switch (type) 26 { 27 case '0': cout << " "; break; 28 case 'z': cout << "…"; break; 29 case '3': cout << "★"; break; 30 case '4': cout << "☆"; break; 31 case '5': cout << "○"; break; 32 case '8': cout << "∷"; break; 33 case 'c': cout << "∩"; break; 34 case 'e': cout << "⊙"; break; 35 case 't': cout << "♂"; break; 36 case 'r': cout << "♀"; break; 37 case 'v': cout << "∧"; break; 38 case 'b': cout << "∨"; break; 39 case 'y': cout << "∪"; break; 40 case 'u': cout << "⊥"; break; 41 case 'i': cout << "⌒"; break; 42 case 'a': cout << "≡"; break; 43 case 's': cout << "≈"; break; 44 case 'd': cout << "∽"; break; 45 case 'f': cout << "卍"; break; 46 case 'm': cout << "▓"; break; 47 case 'n': cout << "〃"; break; 48 case '2': cout << "●"; break; 49 case '1': cout << "◎"; break; 50 case '6': cout << "◇"; break; 51 case '7': cout << "◆"; break; 52 case '9': cout << "□"; break; 53 case 'q': cout << "■"; break; 54 case 'o': cout << "△"; break; 55 case 'p': cout << "▲"; break; 56 case 'w': cout << "※"; break; 57 case 'g': cout << "→"; break; 58 case 'h': cout << "←"; break; 59 case 'j': cout << "↑"; break; 60 case 'k': cout << "↓"; break; 61 case 'l': cout << "〓"; break; 62 case 'x': cout << "×"; break; 63 64 //子弹、手雷 65 case 'C': cout << "·"; break; 66 case 'D': cout << "━"; break; 67 case '_': cout << "о"; break; 68 case '+': cout << "¤"; break; 69 70 //制表符 71 case '!': cout << "┏"; break; 72 case '&': cout << "┳"; break; 73 case '@': cout << "┓"; break; 74 case '=': cout << "┫"; break; 75 case '$': cout << "┛"; break; 76 case ')': cout << "┻"; break; 77 case '#': cout << "┗"; break; 78 case '-': cout << "┣"; break; 79 case '(': cout << "╋"; break; 80 case '*': cout << "┃"; break; 81 case '%': cout << "━"; break; 82 //额外 83 case 'A': cout << "╱"; break; 84 case 'B': cout << "╲"; break; 85 } 86 } 87 88 //光标打印物体 89 void PrintBody(PhysicalProps pProps, int X, int Y) 90 { 91 Color(15, pProps.CT); 92 Print(pProps.ST, X, Y); 93 } 94 95 //---------地图数据开辟内存,释放内存--------- 96 //开辟内存new 97 char** GetCharMemory(int row, int colu) 98 { 99 char **pArr = new char*[row]; 100 if (pArr == nullptr) 101 return nullptr; 102 103 for (int i = 0; i < row; i++) 104 { 105 pArr[i] = new char[colu]; 106 if (pArr[i] == nullptr) 107 return nullptr; 108 } 109 //开辟成功并制空 110 for (int i = 0; i < row; i++) 111 for (int j = 0; j < colu; j++) 112 pArr[i][j] = '0'; 113 114 return pArr; 115 } 116 117 int** GetIntMemory(int row, int colu) 118 { 119 int **pArr = new int*[row]; 120 if (pArr == nullptr) 121 return nullptr; 122 123 for (int i = 0; i < row; i++) 124 { 125 pArr[i] = new int[colu]; 126 if (pArr[i] == nullptr) 127 return nullptr; 128 } 129 return pArr; 130 } 131 132 bool** GetBoolMemory(int row, int colu) 133 { 134 bool **pArr = new bool*[row]; 135 if (pArr == nullptr) 136 return nullptr; 137 138 for (int i = 0; i < row; i++) 139 { 140 pArr[i] = new bool[colu]; 141 if (pArr[i] == nullptr) 142 return nullptr; 143 } 144 145 //开辟成功并制空 146 for (int i = 0; i < row; i++) 147 for (int j = 0; j < colu; j++) 148 pArr[i][j] = true; 149 150 return pArr; 151 } 152 153 //释放内存 154 void DeleteCharMemory(char** pArr, int row) 155 { 156 for (int i = 0; i < row; i++) 157 { 158 if (pArr[i] != nullptr) 159 { 160 delete[] pArr[i]; 161 pArr[i] = nullptr; 162 } 163 } 164 165 if (pArr != nullptr) 166 { 167 delete[] pArr; 168 pArr = nullptr; 169 } 170 } 171 172 void DeleteIntMemory(int** pArr, int row) 173 { 174 for (int i = 0; i < row; i++) 175 { 176 if (pArr[i] != nullptr) 177 { 178 delete[] pArr[i]; 179 pArr[i] = nullptr; 180 } 181 } 182 183 if (pArr != nullptr) 184 { 185 delete[] pArr; 186 pArr = nullptr; 187 } 188 } 189 190 void DeleteBoolMemory(bool** pArr, int row) 191 { 192 for (int i = 0; i < row; i++) 193 { 194 if (pArr[i] != nullptr) 195 { 196 delete[] pArr[i]; 197 pArr[i] = nullptr; 198 } 199 } 200 201 if (pArr != nullptr) 202 { 203 delete[] pArr; 204 pArr = nullptr; 205 } 206 } 207 208 209 //物品图形库 210 void GetGraphicLib(char** Graphic, BodyType bodyType) 211 { 212 switch (bodyType) 213 { 214 case ET_Bandage: strcpy(Graphic[0], "∽∽"); strcpy(Graphic[1], "绷带"); break; 215 case ET_Injector: strcpy(Graphic[0], "♂注"); strcpy(Graphic[1], "射器"); break; 216 case ET_Bullet: strcpy(Graphic[0], "ⅲⅲ"); strcpy(Graphic[1], "子弹"); break; 217 case ET_Clip: strcpy(Graphic[0], "ㄖㄖ"); strcpy(Graphic[1], "弹夹"); break; 218 case ET_MedKit: strcpy(Graphic[0], "╋医"); strcpy(Graphic[1], "疗箱"); break; 219 case ET_Grenade: strcpy(Graphic[0], "ΦΦ"); strcpy(Graphic[1], "手雷"); break; 220 case ET_Bomb: strcpy(Graphic[0], "BB"); strcpy(Graphic[1], "炸弹"); break; 221 case ET_AP: strcpy(Graphic[0], "医疗"); strcpy(Graphic[1], "绷带"); break; 222 case ET_Armor: strcpy(Graphic[0], "¥防"); strcpy(Graphic[1], "弹衣"); break; 223 case ET_Caliga: strcpy(Graphic[0], "足足"); strcpy(Graphic[1], "战靴"); break; 224 case ET_NightVision:strcpy(Graphic[0], "⊙夜"); strcpy(Graphic[1], "视仪"); break; 225 case ET_FlashLight: strcpy(Graphic[0], "⊕手"); strcpy(Graphic[1], "电筒"); break; 226 case ET_Radar: strcpy(Graphic[0], "¤¤"); strcpy(Graphic[1], "雷达"); break; 227 case ET_Cord: strcpy(Graphic[0], "§§"); strcpy(Graphic[1], "绳索"); break; 228 case ET_Backpack: strcpy(Graphic[0], "囧囧"); strcpy(Graphic[1], "背包"); break; 229 case ET_Bugle: strcpy(Graphic[0], "≤≤"); strcpy(Graphic[1], "号角"); break; 230 case ET_Armory: strcpy(Graphic[0], "医疗"); strcpy(Graphic[1], "绷带"); break; 231 case ET_GoldCoin: strcpy(Graphic[0], "医疗"); strcpy(Graphic[1], "绷带"); break; 232 233 case ET_Civilian: strcpy(Graphic[0], "医疗"); strcpy(Graphic[1], "绷带"); break; 234 case ET_Milltia: strcpy(Graphic[0], "医疗"); strcpy(Graphic[1], "绷带"); break; 235 case ET_Lighten: strcpy(Graphic[0], "医疗"); strcpy(Graphic[1], "绷带"); break; 236 case ET_Dagger: strcpy(Graphic[0], "丿丿"); strcpy(Graphic[1], "匕首"); break; 237 case ET_Pistol: strcpy(Graphic[0], "┍┍"); strcpy(Graphic[1], "手枪"); break; 238 case ET_Pillbox: strcpy(Graphic[0], "╤机"); strcpy(Graphic[1], "关枪"); break; 239 case ET_SniperRifle:strcpy(Graphic[0], "┮狙"); strcpy(Graphic[1], "击枪"); break; 240 case ET_TASKITEM: strcpy(Graphic[0], "一封"); strcpy(Graphic[1], "密信"); break; 241 } 242 }
【主函数】--游戏运行入口
1 #include "Marco.h" 2 #include "GameMgr//GameScene.h" 3 #include "Body//Props.h" 4 #include "Body//Equip.h" 5 #include "Role//Dog.h" 6 #include "Role//SpearMan.h" 7 #include "Role//Hero.h" 8 #include "Backpack//Backpack.h" 9 #include "Body//Pistol.h" 10 #include "Body//Bandage.h" 11 #include "Body//Dagger.h" 12 #include "Body//Armor.h" 13 #include "GameMgr//GameOI.h" 14 #include "Map//Map.h" 15 #include "Map//GateWay.h" 16 #include "Map//Map_1.h" 17 #include "Map//Map_11.h" 18 #include "Map//Map_111.h" 19 #include "Map//Map_12.h" 20 #include "Map//Map_121.h" 21 #include "Body//NightVision.h" 22 #include "Body//Grenade.h" 23 #include "Body//PillBox.h" 24 #include "Body//SniperRifle.h" 25 #include "Body//Cord.h" 26 #include "GameUI//LandSurface.h" 27 28 29 #pragma comment(lib,"WINMM.LIB") 30 //全局变量 31 int GameTime = 0; 32 33 void main() 34 { 35 WCHAR mciCmd[] = TEXT("open .//music//background//HeisAPirate.mp3 alias background"); //.mp3格式的 36 mciSendString(mciCmd, NULL, 0, NULL); 37 mciSendString(TEXT("play background repeat"), NULL, 0, NULL); 38 39 // 音乐 mp3 40 //WCHAR mciCmd[] = TEXT("open .//music//background//gamestartup.mp3 alias background"); //.mp3格式的 41 //mciSendString(mciCmd, NULL, 0, NULL); 42 //mciSendString(TEXT("play background repeat"), NULL, 0, NULL); 43 44 45 srand((unsigned)time(nullptr)); 46 //初始化登陆系统 47 MainMenu* pMainMenu = LandSurface::InitLandSurface(); 48 49 pMainMenu->OutputFrame(); 50 51 pMainMenu->OutputButton(); 52 53 pMainMenu->ChooseButton(); 54 55 56 }
【背包类】-----整个游戏中很单一的类,没有继承体系
1 #ifndef _BACKPACK_H_ 2 #define _BACKPACK_H_ 3 4 #include "..//Marco.h" 5 class Role; 6 class Body; 7 8 class Backpack 9 { 10 public: 11 Backpack(Role* pHero); 12 ~Backpack(); 13 14 public://外部接口函数 15 void OutputSurface(); //打印外观 16 void CloseSurface(); //关闭外观 17 void PushBackBody(Body* pBody); //添加物品 18 void ChooseBody(); //选择物品 19 void GetCapacity(); //背包扩容 20 Body** GetBodyArr(); //获取物品容器 21 int Size(){ return m_capacity; } 22 23 private://内部操作函数 24 void InitFrame(); //导入背包框架 25 void OutputAllBody(); //打印所有物体 26 void OutputCurBody(); //醒目打印当前指向物体 27 void CleanCurBody(); //清除当前指向物体底色 28 void OutputBodyInfo(Body* pBody); //打印物品信息 29 void EraseBodyInfo(); //清除物品信息 30 void UseBody(); //使用物品 31 32 private://背包存储物品数据成员 33 Role* m_role; //背包归属 34 Body** m_bodyArr; //物品容器 35 int m_capacity; //目前容量(基础容量16) 36 int m_index; //目前指向器的物品(当前所显示物品) 37 int m_pos[64][2]; //每个物品坐标(最大容量64) 38 39 private://用于打印显示的属性 40 char** m_surface; //背包外观 41 int m_row; //背包高度 42 int m_colu; //背包宽度 43 int m_x; //背包打开位置x 44 int m_y; //背包打开位置y 45 46 }; 47 48 #endif //! _BACKPACK_H_
1 #include <string.h> 2 #include <conio.h> 3 #include "..//Marco.h" 4 #include "..//Body//Body.h" 5 #include "..//Role//Hero.h" 6 #include "..//GameMgr///GameOI.h" 7 #include "Backpack.h" 8 #include "..//Map/Map.h" 9 10 //======= 构造函数 =============== 11 Backpack::Backpack(Role* pRole) 12 { 13 //初始化 14 m_role = pRole; 15 16 m_bodyArr = new Body*[16]; //初始背包 17 if (m_bodyArr == nullptr) return; 18 for (int i = 0; i < 16; i++) 19 m_bodyArr[i] = nullptr; 20 21 m_capacity = 16; //背包容量 22 m_row = m_capacity/2 + 10; //背包高度 23 m_colu = 26; //背包宽度 24 25 m_index = 1; //默认指向第一个 26 m_surface = nullptr; //默认框架数据nullptr 27 28 m_x = 0; 29 m_y = 0; 30 31 //初始化背包框架数据 32 this->InitFrame(); 33 } 34 35 Backpack::~Backpack() 36 { 37 if (m_bodyArr != nullptr) 38 { 39 delete[] m_bodyArr; 40 m_bodyArr = nullptr; 41 } 42 } 43 44 45 46 //==================================== 外部接口函数 =================================== 47 //======= 打印外观 =============== 48 void Backpack::OutputSurface() 49 { 50 //设置打印颜色 51 Color(ColorType::CT_White, ColorType::CT_Violet); 52 //打印框架 53 for (int i = 0; i < m_row; i++) 54 for (int j = 0; j < m_colu - 1; j++) 55 Print(m_surface[i][j], i + m_x, j + m_y); 56 57 //打印文字-上部分 58 GotoXY(m_x, m_y + 6); 59 cout << "【" << m_role->GetName() << "】的背包"; 60 Color(ColorType::CT_White, ColorType::CT_TBlue); 61 GotoXY(m_x, m_y + 19); 62 cout<<"〔容量"<<m_capacity<<"格〕"; 63 GotoXY(m_x + 2, m_y + 1); 64 cout << "【物品名称】 【数量】"; 65 GotoXY(m_x + 2, m_y + 13); 66 cout << "【物品名称】 【数量】"; 67 68 //打印文字-下部分 69 GotoXY(m_x + 2 + m_capacity / 2 + 6, m_y + 1); 70 cout << "【金币】: " << m_role->GetMoney(); 71 GotoXY(m_x + 2 + m_capacity / 2 + 6, m_y + 14); 72 cout << "【Y使用物品/ B退出】"; 73 74 75 //打印显示所有的物品 76 this->OutputAllBody(); 77 78 //每次初始打印背包外观,醒目显示当前指向物体 79 this->OutputCurBody(); 80 81 //背包操作函数 82 this->ChooseBody(); 83 } 84 85 //======= 关闭外观 =============== 86 void Backpack::CloseSurface() 87 { 88 Map* map = m_role->GetGameOI()->GetMap(); 89 90 for (int i = 0; i < m_row; i++) 91 for (int j = 0; j < m_colu - 1; j++) 92 map->OutputMap(i+m_x, j + m_y); 93 } 94 95 //======= 添加物品 =============== 96 void Backpack::PushBackBody(Body* pBody) 97 { 98 if (pBody == nullptr)//传进无效数据 99 return; 100 101 pBody->SetOwner(m_role); 102 103 int indexNull = 0;//标记第1个空格子 104 105 for (int i = 0; i < m_capacity;i++) //遍历每一个物品 106 { 107 //标记找到的第一个空格子 108 if (!indexNull && m_bodyArr[i] == nullptr) 109 indexNull = i + 1; 110 111 //如果物品相同(名字判断) 112 if (m_bodyArr[i]!= nullptr && strcmp(m_bodyArr[i]->GetName(), pBody->GetName()) == 0) 113 { 114 int countMax = m_bodyArr[i]->GetCountMax(); //物体最大叠加数 115 int pTempCount = m_bodyArr[i]->GetCountCur(); //目标物体当前数 116 int pCount = pBody->GetCountCur(); //添加物体当前数 117 118 //当数量不超过最大堆叠数时,直接添加进去 119 if (pTempCount + pCount <= countMax) 120 { 121 m_bodyArr[i]->SetCountCur(pTempCount + pCount); 122 //释放该物品 123 delete pBody; 124 pBody = nullptr; 125 } 126 else //否则 pTempCount + pCount > countMax 127 { 128 pBody->SetCountCur(pTempCount + pCount - countMax); 129 m_bodyArr[i]->SetCountCur(countMax); 130 } 131 } 132 } 133 134 //还有剩余没装完,且存在空格子时 135 if ( indexNull && pBody != nullptr) 136 m_bodyArr[indexNull - 1] = pBody; 137 } 138 139 //======= 扩容函数 =============== 140 void Backpack::GetCapacity() 141 { 142 if (m_capacity == 64) //已经达到最大容量 143 return; 144 145 Body** bodyArrTemp = new Body*[m_capacity + 16]; //初始背包 146 if (bodyArrTemp == nullptr) return; 147 148 for (int i = 0; i < m_capacity + 16; i++) 149 bodyArrTemp[i] = i< m_capacity ? m_bodyArr[i]:nullptr; 150 151 m_capacity = m_capacity + 16; //容量加num 152 m_row = m_row + 8; //背包高度 153 154 delete[] m_bodyArr; 155 m_bodyArr = bodyArrTemp; 156 157 158 this->InitFrame(); //录入新的框架数据 159 } 160 161 //======= 选择物品 =============== 162 void Backpack::ChooseBody() 163 { 164 while (true) 165 { 166 char choose = getch(); //选择按键 167 if (choose == -32) choose = getch(); //屏蔽-32 168 169 //当按了方向键,移动刷子 170 if (choose == 72 || choose == 75 || choose == 77 || choose == 80) 171 { 172 int dPos = 0; 173 switch (choose) 174 { 175 case 72: if (m_index > 2) dPos = -2; break; 176 case 80: if (m_index < m_capacity - 1) dPos = +2; break; 177 case 75: if (m_index > 1) dPos = -1; break; 178 case 77: if (m_index < m_capacity) dPos = +1; break; 179 } 180 if (dPos != 0) //发生了变动 181 { 182 this->CleanCurBody(); //清除原位置 183 this->EraseBodyInfo(); //清除原物品信息 184 185 m_index += dPos; 186 this->OutputCurBody(); //打印新位置 187 } 188 } 189 190 //按Y 使用物品 191 if (choose == 'y' || choose == 'Y') 192 this->UseBody(); 193 194 //按B 退出背包 195 if (choose == 'B' || choose == 'b') 196 { 197 this->CloseSurface(); 198 return; 199 } 200 } 201 } 202 203 //获取物品容器 204 Body** Backpack::GetBodyArr() 205 { 206 return m_bodyArr; 207 } 208 209 210 //====================================== private 内部操作函数 ===================================================== 211 //=======导入背包框架=============== 212 void Backpack::InitFrame() 213 { 214 const int colu = 26; 215 char PackHead[3][colu] = //头部 216 { "0000000000000000000000000", 217 "mmmmmmmmmmmmmmmmmmmmmmmmm", 218 "m00000000000*00000000000m" }; 219 220 char PackMid[colu] = //背包部分 221 { "m00000000000*00000000000m" }; 222 223 char PackBack[7][colu] = //底部 224 { "m%%%%%%%%%%%)%%%%%%%%%%%m", 225 "m00000000000000000000000m", 226 "m00000000000000000000000m", 227 "m00000000000000000000000m", 228 "m%%%%%%%%%%%&%%%%%%%%%%%m", 229 "m00000000000*00000000000m", 230 "mmmmmmmmmmmmmmmmmmmmmmmmm" }; 231 232 //将新数据存到背包 233 if (m_surface != nullptr) 234 DeleteCharMemory(m_surface, m_row - 8); 235 m_surface = GetCharMemory(m_row, m_colu); 236 if (m_surface == nullptr) return; 237 238 //记录每个元素的坐标 239 for (int i = 0; i < m_capacity; i += 2) 240 { 241 m_pos[i][0] = 3 + i / 2; m_pos[i][1] = 1; 242 m_pos[i + 1][0] = 3 + i / 2; m_pos[i + 1][1] = 13; 243 } 244 245 //框架数据赋值(i是框架surface的行,h是Head的行,b是Back的行) 246 for (int i = 0, h = 0, b = 0; i < m_row; i++) 247 { 248 if (i >= 0 && i < 3) //头部赋值 249 strcpy(m_surface[i], PackHead[h++]); 250 else if (i >= 3 + m_capacity / 2) //尾部赋值 251 strcpy(m_surface[i], PackBack[b++]); 252 else//( i>= 3 + m_capacity / 2 ) //中部赋值 253 strcpy(m_surface[i], PackMid); 254 } 255 } 256 257 //打印显示所有的物品 258 void Backpack::OutputAllBody() 259 { 260 for (int i = 0; i < m_capacity; i++) 261 { 262 if (m_bodyArr[i] == nullptr) 263 continue; 264 265 //打印物品名字 266 GotoXY(m_pos[i][0] + m_x, m_pos[i][1] + m_y); 267 cout << m_bodyArr[i]->GetName(); 268 //打印物品数量 269 GotoXY(m_pos[i][0] + m_x, m_pos[i][1] + 8 + m_y); 270 printf("%02d/%02d", m_bodyArr[i]->GetCountCur(), m_bodyArr[i]->GetCountMax()); 271 } 272 } 273 274 //打印当前指向元素(改变醒目颜色) 275 void Backpack::OutputCurBody() 276 { 277 //改醒目颜色 278 Color(ColorType::CT_LGreen, ColorType::CT_Red); 279 280 if (m_bodyArr[m_index - 1] != nullptr) 281 { 282 //打印物品名字 283 GotoXY(m_pos[m_index - 1][0] + m_x, m_pos[m_index - 1][1] + m_y); 284 cout << m_bodyArr[m_index - 1]->GetName(); 285 //打印物品数量 286 GotoXY(m_pos[m_index - 1][0] + m_x, m_pos[m_index - 1][1] + 8 + m_y); 287 printf("%02d/%02d", m_bodyArr[m_index - 1]->GetCountCur(), m_bodyArr[m_index - 1]->GetCountMax()); 288 //打印物品信息 289 this->OutputBodyInfo(m_bodyArr[m_index - 1]); 290 } 291 else 292 { 293 GotoXY(m_pos[m_index - 1][0] + m_x, m_pos[m_index - 1][1] + m_y); 294 cout << " "; 295 } 296 //恢复系统色 297 Color(ColorType::CT_White, ColorType::CT_TBlue); 298 } 299 300 //恢复当前指向元素醒目颜色 301 void Backpack::CleanCurBody() 302 { 303 if (m_bodyArr[m_index - 1] != nullptr) 304 { 305 //打印物品名字 306 GotoXY(m_pos[m_index - 1][0] + m_x, m_pos[m_index - 1][1] + m_y); 307 cout << m_bodyArr[m_index - 1]->GetName(); 308 //打印物品数量 309 GotoXY(m_pos[m_index - 1][0] + m_x, m_pos[m_index - 1][1] + 8 + m_y); 310 printf("%02d/%02d", m_bodyArr[m_index - 1]->GetCountCur(), m_bodyArr[m_index - 1]->GetCountMax()); 311 //打印物品信息 312 this->EraseBodyInfo(); 313 } 314 else 315 { 316 GotoXY(m_pos[m_index - 1][0] + m_x, m_pos[m_index - 1][1] + m_y); 317 cout << " "; 318 } 319 } 320 321 //打印物品信息 322 void Backpack::OutputBodyInfo(Body* pBody) 323 { 324 pBody->BodyInfo(m_x + 4 + m_capacity / 2, m_y + 1); 325 pBody->OutputTip(m_x + 5 + m_capacity / 2, m_y + 1); 326 } 327 328 //清除物品信息 329 void Backpack::EraseBodyInfo() 330 { 331 for (int i = 0; i < 3; i++) 332 { 333 GotoXY(m_x + 4 + m_capacity / 2 + i, m_y + 1); 334 for (int j = 1; j <= m_colu - 3; j++) 335 cout << " "; 336 } 337 } 338 339 //使用物品 340 void Backpack::UseBody() 341 { 342 m_role->GetGameOI()->AppendBody(m_bodyArr[m_index - 1], m_index); 343 344 this->OutputCurBody(); 345 m_role->EquipSurface(); 346 }