当前位置:首页 > 通信资讯 > 正文

c语言魔塔程序源代码(魔塔C语言代码)

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

游戏截图:

c语言魔塔程序源代码(魔塔C语言代码)

c语言魔塔程序源代码(魔塔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语言魔塔程序源代码(魔塔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. if (Number == 0) {
  6. str[0] = '0';
  7. len++;
  8. }
  9. while (Number)
  10. {
  11. str[len++] = Number % 10+'0';
  12. Number /= 10;
  13. }
  14. for (int i = 0; i < len/2; i++) {
  15. char t = str[i];
  16. str[i] = str[len - i - 1];
  17. str[len - i - 1] = t;
  18. }
  19. str[len] = '\0';
  20. return str;
  21. }

怪物属性的定义

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

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

  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语言魔塔程序源代码(魔塔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. else {
  15. MessageBox(hwnd, "", "打不过", MB_YESNO);
  16. return 0;
  17. }
  18. }

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

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

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

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

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。