C语言魔塔游戏的实现代码

时间:2021-07-16 12:53:08

很早就很想写这个,今天终于写完了。

游戏截图:

C语言魔塔游戏的实现代码

C语言魔塔游戏的实现代码

C语言魔塔游戏的实现代码

编译环境: VS2017

游戏需要一些图片,如果有想要的或者对游戏有什么看法的可以加我的QQ 2985486630 讨论,如果暂时没有回应,可以在博客下方留言,到时候我会看到。觉得麻烦的直接下载C-mota_jb51.rar

解压后点击sln文件直接可以运行

下面我来介绍一下游戏的主要功能和实现方式

首先是玩家的定义,使用结构体,这个名字是可以自己改变的

  1. struct gamerole
  2. {
  3. char name[20] = "黑蛋"; //玩家名字
  4. int HP; //血量
  5. int MP;
  6. int DEF; //防御
  7. int ATT; //攻击
  8. int Lv; //等级
  9. int Exp; //经验
  10. int Num_Blue_Key; //蓝钥匙数量
  11. int Num_Yellow_Key;
  12. }player;

在游戏的右边显示任务的各项属性

C语言魔塔游戏的实现代码

函数:

  1. void SetPlayer()
  2. {
  3. putimage(60 * 13, 0, &Message);
  4. outtextxy(60 * 13 + 12, 100, player.name);
  5. outtextxy(60 * 13 + 12, 180, intToString(player.Lv));
  6. outtextxy(60 * 13 + 12, 235, intToString(player.Exp));
  7. outtextxy(60 * 13 + 12, 362, intToString(player.HP));
  8. outtextxy(60 * 13 + 12, 425, intToString(player.MP));
  9. outtextxy(60 * 13 + 12, 517, intToString(player.ATT));
  10. outtextxy(60 * 13 + 12, 567, intToString(player.DEF));
  11. outtextxy(60 * 13 + 12, 689, intToString(player.Num_Yellow_Key));
  12. outtextxy(60 * 13 + 12, 759, intToString(player.Num_Blue_Key));
  13. }

由于这个函数要求属性必须是字符串,所以我写了一个把数字转化成字符串的函数

  1. //整数转换为字符
  2. char *intToString(int Number)
  3. {
  4. int len = 0;
  5.  
  6. if (Number == 0) {
  7. str[0] = '0';
  8. len++;
  9. }
  10. while (Number)
  11. {
  12. str[len++] = Number % 10+'0';
  13. Number /= 10;
  14. }
  15. for (int i = 0; i < len/2; i++) {
  16. char t = str[i];
  17. str[i] = str[len - i - 1];
  18. str[len - i - 1] = t;
  19. }
  20. str[len] = '\0';
  21. return str;
  22. }

怪物属性的定义

  1. struct monster
  2. {
  3. int HP; //血量
  4. int ATT; //攻击
  5. int DEF; //防御
  6. int Exp; //经验
  7. };

接下来就是定义画布,然后加载图片,我用一个二维数组存下了地图,不同的数字代表不同的图片,然后根据二维数组的值把不同的地方贴上不同的图片。

  1. void SetMap()
  2. {
  3. for (int i = 0; i < 13; i++)
  4. {
  5. for (int j = 0; j < 13; j++)
  6. {
  7. switch (map[i][j])
  8. {
  9.  
  10. case 0:
  11. putimage(j * 60, i * 60, &Wall); //墙
  12. break;
  13. case 1:
  14. putimage(j * 60, i * 60, &Ground); //地板
  15. break;
  16. case 2:
  17. putimage(j * 60, i * 60, &Blue_door); //蓝门
  18. break;
  19. case 3:
  20. putimage(j * 60, i * 60, &Yellow_door); //黄门
  21. break;
  22. case 4:
  23. putimage(j * 60, i * 60, &Blue_Cry); //蓝水晶
  24. break;
  25. case 5:
  26. putimage(j * 60, i * 60, &Red_Cry); //红水晶
  27. break;
  28. case 6:
  29. putimage(j * 60, i * 60, &Blue_Key); //蓝钥匙
  30. break;
  31. case 7:
  32. putimage(j * 60, i * 60, &Yellow_Key); //黄钥匙
  33. break;
  34. case 8:
  35. putimage(j * 60, i * 60, &Red_Med); //红药水
  36. break;
  37. case 9:
  38. putimage(j * 60, i * 60, &Blue_Med); //蓝药水
  39. break;
  40. case 10:
  41. putimage(j * 60, i * 60, &Small_Bat); //小蝙蝠
  42. break;
  43. case 11:
  44. putimage(j * 60, i * 60, &Small_Wizard); //小巫师
  45. break;
  46. case 12:
  47. putimage(j * 60, i * 60, &Small_Skull); //小骷髅
  48. break;
  49. case 13:
  50. putimage(j * 60, i * 60, &Big_Skull); //大骷髅
  51. break;
  52. case 14:
  53. putimage(j * 60, i * 60, &Green_Slime); //绿史莱姆
  54. break;
  55. case 15:
  56. putimage(j * 60, i * 60, &Red_Slime); //红史莱姆
  57. break;
  58. case 98:
  59. putimage(j * 60, i * 60, &Ladder); //*
  60. break;
  61. case 99:
  62. putimage(j * 60, i * 60, &Player); //玩家
  63. break;
  64. }
  65.  
  66. }
  67. }
  68. }

接下来就是人物的移动和战斗了,人物的移动我就是直接对上下左右四种情况分别讨论, 在人物走动的时候要判断能不能走,不能走就不处理,如果能走,就把走到的那个位置上变成人,把之前人的位置变成地板。

  1. case 'w':
  2. case 72:
  3. if (map[playerx - 1][playery] == 1) { //下一步是地板
  4. map[playerx - 1][playery] = 99;
  5. map[playerx][playery] = 1;
  6. playerx--;
  7. }

需要处理的就是钥匙,门, 水晶, 药水, 怪物。

如果是钥匙,把对应的钥匙数量加1。

如果是门,判断一下对应颜色的钥匙是否足够,如果足够,钥匙数量减1,然后把对应位置上的门变为空地。

如果是药水,吃了之后会增加生命。

如果是水晶,根据水晶的颜色加对应的属性。

当遇到怪物的时候回产生战斗,对于不同的怪物分开处理,下面是小蝙蝠的处理

  1. case 10:
  2. ID = MessageBox(hwnd, "小蝙蝠", "是否攻击?", MB_YESNO);
  3. if (ID == IDYES)
  4. {
  5. if (VS(player.HP, player.ATT, player.DEF, Small_Bat_Pro.HP, Small_Bat_Pro.ATT, Small_Bat_Pro.DEF)) {
  6. player.Exp += Small_Bat_Pro.Exp;
  7. return 1;
  8. }
  9. }
  10. break;

遇到怪物是会弹出对应的对话框

C语言魔塔游戏的实现代码

此处有一个VS函数,用于计算战斗是否成功,如果成功,会加相应的属性,如果失败,则会弹出打不过的窗口。

  1. int VS(int playHP, int playATT, int playDEF, int monHP, int monATT, int monDEF)
  2. {
  3. while (playHP > 0 || monHP > 0)
  4. {
  5. monHP -= (playATT - monDEF);
  6. if (monHP < 0)
  7. break;
  8. playHP -= (monATT - playDEF);
  9. }
  10. if (playHP > 0) {
  11. player.HP = playHP;
  12. return 1;
  13. }
  14.  
  15. else {
  16. MessageBox(hwnd, "", "打不过", MB_YESNO);
  17. return 0;
  18. }
  19. }

在每一次敲击键盘后更新地图信息和人物信息 :

  1. SetMap(); //重新显示地图
  2. SetPlayer(); //重新显示角色信息

到了这里,游戏的内容也就说的差不多了,虽然我只写出了第一个地图。但是,只要添加地图即可有更多的玩法,有兴趣的同学可以自制关卡,实现更多内容。

最后,加上所有代码,注释上说的也比较清楚。

  1. #include <stdlib.h>
  2. #include <graphics.h>
  3. #include <windows.h>
  4. #include<conio.h>
  5. #include<graphics.h>
  6. #include<windows.h>
  7. #include <stdio.h>
  8.  
  9. void initgamePicture(); //加载游戏图片
  10. void SetPlayer(); //显示角色信息
  11. void initPlayer(); //初始化游戏角色
  12. void SetMap(); //加载游戏地图
  13. char *intToString(int Number); //把整数转化成字符串
  14. void playGame(); //开始游戏
  15. int Combat(int x);
  16. int VS(int playHP, int playATT, int playDEF, int monHP, int monATT, int monDEF);
  17.  
  18. int playerx, playery;
  19. char str[20] = "";
  20. //地图1
  21. int map[13][13] = {
  22. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  23. { 0, 98, 1, 14, 15, 14, 1, 1, 1, 1, 1, 1, 0 },
  24. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
  25. { 0, 8, 1, 1, 2, 1, 0, 5, 6, 1, 0, 1, 0 },
  26. { 0, 1, 12, 1, 0, 1, 0, 4, 8, 1, 0, 1, 0 },
  27. { 0, 0, 2, 0, 0, 1, 0, 0, 0, 14, 0, 1, 0 },
  28. { 0, 6, 1, 1, 0, 1, 3, 10, 11, 10, 0, 1, 0 },
  29. { 0, 1, 13, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0 },
  30. { 0, 0, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0 },
  31. { 0, 1, 1, 1, 0, 0, 14, 0, 0, 0, 14, 0, 0 },
  32. { 0, 8, 1, 6, 0, 7, 1, 6, 0, 1, 10, 1, 0 },
  33. { 0, 8, 1, 6, 0, 1, 99, 1, 0, 14, 9, 14, 0 },
  34. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  35. };
  36. IMAGE Wall, Ground, Green_Slime, Red_Slime, Blue_Cry, Red_Cry, Blue_Key, Yellow_Key,
  37. Red_Med, Blue_Med, Ladder, Small_Skull, Big_Skull, Small_Bat, Small_Wizard,
  38. Blue_door, Yellow_door, Player, Message;
  39. HWND hwnd;
  40. struct gamerole
  41. {
  42. char name[20] = "黑蛋"; //玩家名字
  43. int HP; //血量
  44. int MP;
  45. int DEF; //防御
  46. int ATT; //攻击
  47. int Lv; //等级
  48. int Exp; //经验
  49. int Num_Blue_Key; //蓝钥匙数量
  50. int Num_Yellow_Key;
  51. }player;
  52.  
  53. struct monster
  54. {
  55. int HP; //血量
  56. int ATT; //攻击
  57. int DEF; //防御
  58. int Exp; //经验
  59. };
  60. struct monster Green_Slime_Pro = { 50,10,12,100 }; //绿史莱姆属性
  61. struct monster Red_Slime_Pro = { 100, 50, 12, 500 }; //红史莱姆属性
  62. struct monster Small_Wizard_Pro = { 100, 30, 9, 400 };//小巫师属性
  63. struct monster Small_Bat_Pro = { 20, 10, 9, 50 }; //小蝙蝠属性
  64. struct monster Small_Skull_Pro = {30, 20, 10, 200}; //小骷髅属性
  65. struct monster Big_Skull_Pro = {60, 50, 25, 300}; //大骷髅属性
  66.  
  67. int main()
  68. {
  69. initPlayer();
  70. hwnd = initgraph(60 * 14, 60 * 13);
  71. initgamePicture();
  72.  
  73. while (1) {
  74. SetMap();
  75. SetPlayer();
  76. playGame();
  77. }
  78.  
  79. return 0;
  80. }
  81.  
  82. /*
  83. *显示角色信息
  84. */
  85. void SetPlayer()
  86. {
  87. putimage(60 * 13, 0, &Message);
  88. outtextxy(60 * 13 + 12, 100, player.name);
  89. outtextxy(60 * 13 + 12, 180, intToString(player.Lv));
  90. outtextxy(60 * 13 + 12, 235, intToString(player.Exp));
  91. outtextxy(60 * 13 + 12, 362, intToString(player.HP));
  92. outtextxy(60 * 13 + 12, 425, intToString(player.MP));
  93. outtextxy(60 * 13 + 12, 517, intToString(player.ATT));
  94. outtextxy(60 * 13 + 12, 567, intToString(player.DEF));
  95. outtextxy(60 * 13 + 12, 689, intToString(player.Num_Yellow_Key));
  96. outtextxy(60 * 13 + 12, 759, intToString(player.Num_Blue_Key));
  97. }
  98.  
  99. /*
  100. * 加载游戏图片
  101. */
  102. void initgamePicture()
  103. {
  104. loadimage(&Wall, "墙.jpg", 60, 60);
  105. loadimage(&Ground, "地板.jpg", 60, 60);
  106. loadimage(&Green_Slime, "绿史莱姆.jpg", 60, 60);
  107. loadimage(&Red_Slime, "红史莱姆.jpg", 60, 60);
  108.  
  109. loadimage(&Blue_Cry, "蓝水晶.jpg", 60, 60);
  110. loadimage(&Red_Cry, "红水晶.jpg", 60, 60);
  111.  
  112. loadimage(&Blue_Key, "蓝钥匙.jpg", 60, 60);
  113. loadimage(&Yellow_Key, "黄钥匙.jpg", 60, 60);
  114.  
  115. loadimage(&Red_Med, "小红药水.jpg", 60, 60);
  116. loadimage(&Blue_Med, "小蓝药水.jpg", 60, 60);
  117.  
  118. loadimage(&Ladder, "*.jpg", 60, 60);
  119. loadimage(&Small_Bat, "小蝙蝠.jpg", 60, 60);
  120. loadimage(&Small_Wizard, "小巫师.jpg", 60, 60);
  121. loadimage(&Small_Skull, "骷髅兵.jpg", 60, 60);
  122. loadimage(&Big_Skull, "大骷髅兵.jpg", 60, 60);
  123.  
  124. loadimage(&Blue_door, "蓝门.jpg", 60, 60);
  125. loadimage(&Yellow_door, "黄门.jpg", 60, 60);
  126. loadimage(&Player, "人.jpg", 60, 60);
  127. loadimage(&Message, "info.jpg");
  128. }
  129.  
  130. /*
  131. *初始化游戏角色
  132. */
  133. void initPlayer()
  134. {
  135. player.Lv = 0;
  136. player.ATT = 50;
  137. player.DEF = 50;
  138. player.Num_Blue_Key = 0;
  139. player.Num_Yellow_Key = 0;
  140. player.HP = 500;
  141. player.MP = 250;
  142. player.Exp = 0;
  143. playerx = 11;
  144. playery = 6;
  145. }
  146.  
  147. //整数转换为字符
  148. char *intToString(int Number)
  149. {
  150. int len = 0;
  151.  
  152. if (Number == 0) {
  153. str[0] = '0';
  154. len++;
  155. }
  156. while (Number)
  157. {
  158. str[len++] = Number % 10+'0';
  159. Number /= 10;
  160. }
  161. for (int i = 0; i < len/2; i++) {
  162. char t = str[i];
  163. str[i] = str[len - i - 1];
  164. str[len - i - 1] = t;
  165. }
  166. str[len] = '\0';
  167. return str;
  168. }
  169.  
  170. /*
  171. *加载游戏地图
  172. *
  173. */
  174. void SetMap()
  175. {
  176. for (int i = 0; i < 13; i++)
  177. {
  178. for (int j = 0; j < 13; j++)
  179. {
  180. switch (map[i][j])
  181. {
  182.  
  183. case 0:
  184. putimage(j * 60, i * 60, &Wall); //墙
  185. break;
  186. case 1:
  187. putimage(j * 60, i * 60, &Ground); //地板
  188. break;
  189. case 2:
  190. putimage(j * 60, i * 60, &Blue_door); //蓝门
  191. break;
  192. case 3:
  193. putimage(j * 60, i * 60, &Yellow_door); //黄门
  194. break;
  195. case 4:
  196. putimage(j * 60, i * 60, &Blue_Cry); //蓝水晶
  197. break;
  198. case 5:
  199. putimage(j * 60, i * 60, &Red_Cry); //红水晶
  200. break;
  201. case 6:
  202. putimage(j * 60, i * 60, &Blue_Key); //蓝钥匙
  203. break;
  204. case 7:
  205. putimage(j * 60, i * 60, &Yellow_Key); //黄钥匙
  206. break;
  207. case 8:
  208. putimage(j * 60, i * 60, &Red_Med); //红药水
  209. break;
  210. case 9:
  211. putimage(j * 60, i * 60, &Blue_Med); //蓝药水
  212. break;
  213. case 10:
  214. putimage(j * 60, i * 60, &Small_Bat); //小蝙蝠
  215. break;
  216. case 11:
  217. putimage(j * 60, i * 60, &Small_Wizard); //小巫师
  218. break;
  219. case 12:
  220. putimage(j * 60, i * 60, &Small_Skull); //小骷髅
  221. break;
  222. case 13:
  223. putimage(j * 60, i * 60, &Big_Skull); //大骷髅
  224. break;
  225. case 14:
  226. putimage(j * 60, i * 60, &Green_Slime); //绿史莱姆
  227. break;
  228. case 15:
  229. putimage(j * 60, i * 60, &Red_Slime); //红史莱姆
  230. break;
  231. case 98:
  232. putimage(j * 60, i * 60, &Ladder); //*
  233. break;
  234. case 99:
  235. putimage(j * 60, i * 60, &Player); //玩家
  236. break;
  237. }
  238.  
  239. }
  240. }
  241. }
  242.  
  243. int Combat(int x)
  244. {
  245. int ID;
  246. switch (x) {
  247. case 10:
  248. ID = MessageBox(hwnd, "小蝙蝠", "是否攻击?", MB_YESNO);
  249. if (ID == IDYES)
  250. {
  251. if (VS(player.HP, player.ATT, player.DEF, Small_Bat_Pro.HP, Small_Bat_Pro.ATT, Small_Bat_Pro.DEF)) {
  252. player.Exp += Small_Bat_Pro.Exp;
  253. return 1;
  254. }
  255. }
  256. break;
  257. case 11:
  258. ID = MessageBox(hwnd, "遇到小巫师", "是否攻击?", MB_YESNO);
  259. if (ID == IDYES)
  260. {
  261. if (VS(player.HP, player.ATT, player.DEF, Small_Wizard_Pro.HP, Small_Wizard_Pro.ATT, Small_Wizard_Pro.DEF)) {
  262. player.Exp += Small_Wizard_Pro.Exp;
  263. return 1;
  264. }
  265. }
  266. break;
  267. case 12:
  268. ID = MessageBox(hwnd, "遇到小骷髅", "是否攻击?", MB_YESNO);
  269. if (ID == IDYES)
  270. {
  271. if (VS(player.HP, player.ATT, player.DEF, Small_Skull_Pro.HP, Small_Skull_Pro.ATT, Small_Skull_Pro.DEF)) {
  272. player.Exp += Small_Skull_Pro.Exp;
  273. return 1;
  274. }
  275. }
  276. break;
  277. case 13:
  278. ID = MessageBox(hwnd, "遇到大骷髅", "是否攻击?", MB_YESNO);
  279. if (ID == IDYES)
  280. {
  281. if (VS(player.HP, player.ATT, player.DEF, Big_Skull_Pro.HP, Big_Skull_Pro.ATT, Big_Skull_Pro.DEF)) {
  282. player.Exp += Big_Skull_Pro.Exp;
  283. return 1;
  284. }
  285. }
  286. break;
  287. case 14:
  288. ID = MessageBox(hwnd, "遇到绿史莱姆", "是否攻击?", MB_YESNO);
  289. if (ID == IDYES)
  290. {
  291. if (VS(player.HP, player.ATT, player.DEF, Green_Slime_Pro.HP, Green_Slime_Pro.ATT, Green_Slime_Pro.DEF)) {
  292. player.Exp += Green_Slime_Pro.Exp;
  293. return 1;
  294. }
  295. }
  296. break;
  297. case 15:
  298. ID = MessageBox(hwnd, "遇到红史莱姆", "是否攻击?", MB_YESNO);
  299. if (ID == IDYES)
  300. {
  301. if (VS(player.HP, player.ATT, player.DEF, Red_Slime_Pro.HP, Red_Slime_Pro.HP, Red_Slime_Pro.HP)) {
  302. player.Exp += Green_Slime_Pro.Exp;
  303. return 1;
  304. }
  305. }
  306. break;
  307.  
  308. }
  309. return 0;
  310. }
  311.  
  312. int VS(int playHP, int playATT, int playDEF, int monHP, int monATT, int monDEF)
  313. {
  314. while (playHP > 0 || monHP > 0)
  315. {
  316. monHP -= (playATT - monDEF);
  317. if (monHP < 0)
  318. break;
  319. playHP -= (monATT - playDEF);
  320. }
  321. if (playHP > 0) {
  322. player.HP = playHP;
  323. return 1;
  324. }
  325.  
  326. else {
  327. MessageBox(hwnd, "", "打不过", MB_YESNO);
  328. return 0;
  329. }
  330. }
  331. void playGame()
  332. {
  333. while (1)
  334. {
  335. char ch = _getch();
  336. switch (ch) {
  337. case 'w':
  338. case 72:
  339. if (map[playerx - 1][playery] == 1) { //下一步是地板
  340. map[playerx - 1][playery] = 99;
  341. map[playerx][playery] = 1;
  342. playerx--;
  343. }
  344. else if (map[playerx-1][playery]== 6) { //下一步是蓝钥匙
  345. player.Num_Blue_Key++;
  346. map[playerx - 1][playery] = 99;
  347. map[playerx][playery] = 1;
  348. playerx--;
  349. }
  350. else if (map[playerx - 1][playery] == 7) { //下一步是黄钥匙
  351. player.Num_Yellow_Key++;
  352. map[playerx - 1][playery] = 99;
  353. map[playerx][playery] = 1;
  354. playerx--;
  355. }
  356. //下一步是怪物
  357. else if (map[playerx - 1][playery] == 10 || map[playerx - 1][playery] == 11 ||
  358. map[playerx - 1][playery] == 12 || map[playerx - 1][playery] == 13 ||
  359. map[playerx - 1][playery] == 14 || map[playerx - 1][playery] == 15)
  360. {
  361. int x = Combat(map[playerx - 1][playery]);
  362. if (x == 1) {
  363. map[playerx - 1][playery] = 99;
  364. map[playerx][playery] = 1;
  365. playerx--;
  366. }
  367. }
  368. //红蓝药水
  369. else if (map[playerx - 1][playery] == 8 || map[playerx - 1][playery] == 9) {
  370. if (map[playerx - 1][playery] == 8)
  371. player.HP += 200;
  372. else
  373. player.HP += 500;
  374. map[playerx - 1][playery] = 99;
  375. map[playerx][playery] = 1;
  376. playerx--;
  377. }
  378. //红蓝门
  379. else if (map[playerx - 1][playery] == 2 || map[playerx - 1][playery] == 3) {
  380. if (map[playerx - 1][playery] == 2 && player.Num_Blue_Key) {
  381. player.Num_Blue_Key--;
  382. map[playerx - 1][playery] = 99;
  383. map[playerx][playery] = 1;
  384. playerx--;
  385. }
  386. if (map[playerx - 1][playery] == 3 && player.Num_Yellow_Key) {
  387. player.Num_Yellow_Key--;
  388. map[playerx - 1][playery] = 99;
  389. map[playerx][playery] = 1;
  390. playerx--;
  391. }
  392. }
  393. //红蓝水晶
  394. //红水晶+2攻击
  395. //蓝水晶+2防御
  396. else if (map[playerx - 1][playery] == 4 || map[playerx - 1][playery] == 5) {
  397. if (map[playerx - 1][playery] == 4)
  398. player.DEF += 2;
  399. else if (map[playerx - 1][playery] == 5)
  400. player.ATT += 2;
  401. map[playerx - 1][playery] = 99;
  402. map[playerx][playery] = 1;
  403. playerx--;
  404. }
  405. break;
  406. case 's':
  407. case 80:
  408. if (map[playerx + 1][playery] == 1) { //下一步是地板
  409. map[playerx + 1][playery] = 99;
  410. map[playerx][playery] = 1;
  411. playerx++;
  412. }
  413. else if (map[playerx + 1][playery] == 6) { //下一步是蓝钥匙
  414. player.Num_Blue_Key++;
  415. map[playerx + 1][playery] = 99;
  416. map[playerx][playery] = 1;
  417. playerx++;
  418. }
  419. else if (map[playerx + 1][playery] == 7) { //下一步是黄钥匙
  420. player.Num_Yellow_Key++;
  421. map[playerx + 1][playery] = 99;
  422. map[playerx][playery] = 1;
  423. playerx++;
  424. }
  425. //下一步是怪物
  426. else if (map[playerx + 1][playery] == 10 || map[playerx + 1][playery] == 11 ||
  427. map[playerx + 1][playery] == 12 || map[playerx + 1][playery] == 13 ||
  428. map[playerx + 1][playery] == 14 || map[playerx + 1][playery] == 15)
  429. {
  430. int x = Combat(map[playerx + 1][playery]);
  431. if (x == 1) {
  432. map[playerx + 1][playery] = 99;
  433. map[playerx][playery] = 1;
  434. playerx++;
  435. }
  436. }
  437. //红蓝药水
  438. else if (map[playerx + 1][playery] == 8 || map[playerx + 1][playery] == 9) {
  439. if (map[playerx + 1][playery] == 8)
  440. player.HP += 200;
  441. else
  442. player.HP += 500;
  443. map[playerx + 1][playery] = 99;
  444. map[playerx][playery] = 1;
  445. playerx++;
  446. }
  447. //红蓝门
  448. else if (map[playerx + 1][playery] == 2 || map[playerx + 1][playery] == 3) {
  449. if (map[playerx + 1][playery] == 2 && player.Num_Blue_Key) {
  450. player.Num_Blue_Key++;
  451. map[playerx + 1][playery] = 99;
  452. map[playerx][playery] = 1;
  453. playerx++;
  454. }
  455. if (map[playerx + 1][playery] == 3 && player.Num_Yellow_Key) {
  456. player.Num_Yellow_Key++;
  457. map[playerx + 1][playery] = 99;
  458. map[playerx][playery] = 1;
  459. playerx++;
  460. }
  461. }
  462. //红蓝水晶
  463. //红水晶+2攻击
  464. //蓝水晶+2防御
  465. else if (map[playerx + 1][playery] == 4 || map[playerx + 1][playery] == 5) {
  466. if (map[playerx + 1][playery] == 4)
  467. player.DEF += 2;
  468. else if (map[playerx + 1][playery] == 5)
  469. player.ATT += 2;
  470. map[playerx + 1][playery] = 99;
  471. map[playerx][playery] = 1;
  472. playerx++;
  473. }
  474. break;
  475. case 'a':
  476. case 75:
  477. if (map[playerx][playery - 1] == 1) { //下一步是地板
  478. map[playerx][playery - 1] = 99;
  479. map[playerx][playery] = 1;
  480. playery--;
  481. }
  482. else if (map[playerx][playery - 1] == 6) { //下一步是蓝钥匙
  483. player.Num_Blue_Key++;
  484. map[playerx][playery - 1] = 99;
  485. map[playerx][playery] = 1;
  486. playery--;
  487. }
  488. else if (map[playerx][playery - 1] == 7) { //下一步是黄钥匙
  489. player.Num_Yellow_Key++;
  490. map[playerx][playery - 1] = 99;
  491. map[playerx][playery] = 1;
  492. playery--;
  493. }
  494. //下一步是怪物
  495. else if (map[playerx][playery - 1] == 10 || map[playerx][playery - 1] == 11 ||
  496. map[playerx][playery - 1] == 12 || map[playerx][playery - 1] == 13 ||
  497. map[playerx][playery - 1] == 14 || map[playerx][playery - 1] == 15)
  498. {
  499. int x = Combat(map[playerx][playery - 1]);
  500. if (x == 1) {
  501. map[playerx][playery - 1] = 99;
  502. map[playerx][playery] = 1;
  503. playery--;
  504. }
  505. }
  506. //红蓝药水
  507. else if (map[playerx][playery - 1] == 8 || map[playerx][playery - 1] == 9) {
  508. if (map[playerx][playery - 1] == 8)
  509. player.HP += 200;
  510. else
  511. player.HP += 500;
  512. map[playerx ][playery- 1] = 99;
  513. map[playerx][playery] = 1;
  514. playery--;
  515. }
  516. //红蓝门
  517. else if (map[playerx][playery - 1] == 2 || map[playerx][playery - 1] == 3) {
  518. if (map[playerx][playery - 1] == 2 && player.Num_Blue_Key) {
  519. player.Num_Blue_Key--;
  520. map[playerx][playery - 1] = 99;
  521. map[playerx][playery] = 1;
  522. playery--;
  523. }
  524. if (map[playerx][playery - 1] == 3 && player.Num_Yellow_Key) {
  525. player.Num_Yellow_Key--;
  526. map[playerx][playery - 1] = 99;
  527. map[playerx][playery] = 1;
  528. playery--;
  529. }
  530. }
  531. //红蓝水晶
  532. //红水晶+2攻击
  533. //蓝水晶+2防御
  534. else if (map[playerx][playery - 1] == 4 || map[playerx][playery - 1] == 5) {
  535. if (map[playerx][playery - 1] == 4)
  536. player.DEF += 2;
  537. else if (map[playerx][playery - 1] == 5)
  538. player.ATT += 2;
  539. map[playerx][playery - 1] = 99;
  540. map[playerx][playery] = 1;
  541. playery--;
  542. }
  543. break;
  544. case 'd':
  545. case 77:
  546. if (map[playerx][playery + 1] == 1) { //下一步是地板
  547. map[playerx][playery + 1] = 99;
  548. map[playerx][playery] = 1;
  549. playery++;
  550. }
  551. else if (map[playerx][playery + 1] == 6) { //下一步是蓝钥匙
  552. player.Num_Blue_Key++;
  553. map[playerx][playery + 1] = 99;
  554. map[playerx][playery] = 1;
  555. playery++;
  556. }
  557. else if (map[playerx][playery + 1] == 7) { //下一步是黄钥匙
  558. player.Num_Yellow_Key++;
  559. map[playerx][playery + 1] = 99;
  560. map[playerx][playery] = 1;
  561. playery++;
  562. }
  563. //下一步是怪物
  564. else if (map[playerx][playery + 1] == 10 || map[playerx][playery + 1] == 11 ||
  565. map[playerx][playery + 1] == 12 || map[playerx][playery + 1] == 13 ||
  566. map[playerx][playery + 1] == 14 || map[playerx][playery + 1] == 15)
  567. {
  568. int x = Combat(map[playerx][playery + 1]);
  569. if (x == 1) {
  570. map[playerx][playery + 1] = 99;
  571. map[playerx][playery] = 1;
  572. playery++;
  573. }
  574. }
  575. //红蓝药水
  576. else if (map[playerx][playery + 1] == 8 || map[playerx][playery + 1] == 9) {
  577. if (map[playerx][playery + 1] == 8)
  578. player.HP += 200;
  579. else
  580. player.HP += 500;
  581. map[playerx][playery + 1] = 99;
  582. map[playerx][playery] = 1;
  583. playery++;
  584. }
  585. //红蓝门
  586. else if (map[playerx][playery + 1] == 2 || map[playerx][playery + 1] == 3) {
  587. if (map[playerx][playery + 1] == 2 && player.Num_Blue_Key) {
  588. player.Num_Blue_Key--;
  589. map[playerx][playery + 1] = 99;
  590. map[playerx][playery] = 1;
  591. playery++;
  592. }
  593. if (map[playerx][playery + 1] == 3 && player.Num_Yellow_Key) {
  594. player.Num_Yellow_Key--;
  595. map[playerx][playery + 1] = 99;
  596. map[playerx][playery] = 1;
  597. playery++;
  598. }
  599. }
  600. //红蓝水晶
  601. //红水晶+2攻击
  602. //蓝水晶+2防御
  603. else if (map[playerx][playery + 1] == 4 || map[playerx][playery + 1] == 5) {
  604. if (map[playerx][playery + 1] == 4)
  605. player.DEF += 2;
  606. else if (map[playerx][playery + 1] == 5)
  607. player.ATT += 2;
  608. map[playerx][playery + 1] = 99;
  609. map[playerx][playery] = 1;
  610. playery++;
  611. }
  612. break;
  613. }
  614. SetMap(); //重新显示地图
  615. SetPlayer(); //重新显示角色信息
  616. }
  617. }

到此这篇关于C语言魔塔游戏的实现代码的文章就介绍到这了,更多相关C语言魔塔游戏内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_41505957/article/details/103138305