当前位置:   article > 正文

C语言实现飞机大战小游戏_c语言大作业飞机大战

c语言大作业飞机大战

先看一下效果:

初始游戏界面具有多个选项:

且具有多个可变设置项,方便个性化调整:

多个模式进行选择:

正式游戏界面,操作流畅,界面简单,且有操作提示:

使用函数封装代码和逻辑控制衔接每个界面,实现连贯性操作,使用逐帧动画的效果展示。

此时博主较忙,所以没有进行代码的讲解,不过原理较为简单,使用全部展示在下面了。

下面是代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <time.h>
  5. #include <windows.h>
  6. #include <stdio.h>
  7. #include <iostream>
  8. #include <iomanip>
  9. #include <string.h>
  10. #include<algorithm>
  11. #include<stdio.h>
  12. #include<string.h>
  13. #include<conio.h>
  14. #include<windows.h>
  15. #include<stdlib.h>
  16. #include <windows.h>
  17. #include<stdbool.h>
  18. using namespace std;
  19. #pragma warning(disable:4996)
  20. #define high 25 //数组高
  21. #define width 60 //数组宽
  22. #define border -1 //边界
  23. #define blank 0 //空白
  24. #define plane 1 //飞机
  25. #define bullet 2 //子弹
  26. #define enemy 3 //敌机
  27. #define destroy 4 //摧毁标记
  28. int canvas[high + 2][width + 2]; //游戏地图的高和宽
  29. int pos_h, pos_w; //战机位置
  30. int enemynum = 3; //敌机一次的刷新数量
  31. int record; //计时器,用次数计时
  32. int rec_move = 5; //敌机移动的次数频率
  33. int rec_new = 40; //敌机刷新的次数频率
  34. int score; //分数
  35. int num;//发射的子弹数目
  36. int Over?; //游戏判断监视
  37. int life = 3;//生命值
  38. void Startup(); //游戏数值初始化
  39. void Show(); //游戏界面输出
  40. //因输入导致的游戏状态更新
  41. void Update();//生存模式
  42. void Update1();//无敌模式
  43. void Update2();//多命模式
  44. //与输入无关的游戏状态更新
  45. void NoUpdate(); //生存模式
  46. void NoUpdate1();//无敌模式
  47. void NoUpdate2();//多命模式
  48. void HideCursor(); //隐藏光标
  49. void gotoxy(int x, int y); //回调光标
  50. int menu();//菜单面
  51. int introduce();//介绍面
  52. int control_introduce();//控制介绍
  53. int background_introduce();//背景介绍
  54. int setting();//设置面
  55. int speed_setting();//敌机移动速度设置
  56. int rec_new_setting();//刷新速度设置
  57. int enemynum_setting();//敌机一次刷新数目设置
  58. int life_setting();//生命数设置
  59. int mode();//模式选择面
  60. void mode1();//生存模式
  61. void mode2();//无敌模式
  62. void mode3();//多命模式
  63. void result();//结束界面
  64. void assist(double shoot);//射击评估
  65. void SetSize(unsigned uCol, unsigned uLine);//窗口大小设置
  66. int main() {
  67. OK:
  68. if (menu()) {
  69. int a = mode();
  70. //根据mode返回的数字判断选择模式
  71. //返回几就对应进入什么模式
  72. if (a == 1) {
  73. mode1();
  74. if (!Over?) {
  75. goto over;
  76. }
  77. }
  78. else if (a == 2) {
  79. mode2();
  80. if (!Over?) {
  81. goto over;
  82. }
  83. }
  84. else if (a == 3) {
  85. mode3();
  86. if (!Over?) {
  87. goto over;
  88. }
  89. }
  90. else if (a == 4) {
  91. goto OK;
  92. }
  93. }
  94. over:
  95. result();
  96. cout << "是否重新开始游戏?" << endl;
  97. cout << "1为是,0为退出游戏" << endl;
  98. int i = 0;
  99. cin >> i;
  100. switch (i) {
  101. case 1: system("cls"); goto OK; break;
  102. case 0: break;
  103. }
  104. return 0;
  105. }
  106. void Startup() { //游戏数值初始化
  107. Over? = 1;//Over?为1代表没死,为0代表死亡
  108. num = 0;//初始发射子弹为0
  109. score = 0; //初始化分数
  110. for (int i = 0; i < high + 2; i++) { //初始化矩阵元素
  111. for (int j = 0; j < width + 2; j++) {
  112. if (i == 0 || i == high + 1 ||
  113. j == 0 || j == width + 1) {
  114. canvas[i][j] = border;
  115. }
  116. else canvas[i][j] = blank;
  117. }
  118. }
  119. pos_h = high / 2; //初始化飞机竖直坐标
  120. pos_w = width / 2; //初始化飞机水平坐标
  121. canvas[pos_h][pos_w] = plane; //初始化飞机位置
  122. srand(time(NULL));
  123. record = 4; //初始化计数器
  124. }
  125. void Show() { //游戏界面输出
  126. HideCursor(); //隐藏光标
  127. gotoxy(1, 1); //回调光标、刷新画面
  128. for (int i = 0; i < high + 2; i++) {
  129. for (int j = 0; j < width + 2; j++) {
  130. if (canvas[i][j] == plane) { //当前位置为飞机位置
  131. printf("W");
  132. }
  133. else if (canvas[i][j] == bullet) { //当前位置为子弹位置
  134. printf("|");
  135. }
  136. else if (canvas[i][j] == enemy) { //当前位置为敌机位置
  137. printf("#");
  138. }
  139. else if (canvas[i][j] == border) { //当前位置为边界
  140. printf("$");
  141. }
  142. else if (canvas[i][j] == blank) { //当前位置无物,且在边界内
  143. printf(" ");
  144. }
  145. else if (canvas[i][j] == destroy) { //当前位置无物,且在边界内
  146. printf("x");
  147. }
  148. }
  149. printf("\n");
  150. }
  151. printf("\n你的得分为:%d", score);
  152. printf("\n已发射子弹数目:%d", num);
  153. printf("\n精准度为:%f", double(score) / double(num));
  154. printf("操作说明: ASDW分别操作 左下右上四个的移动\n");
  155. printf("**空格是发出子弹**\n");
  156. printf("**T退出游戏**\n");
  157. }
  158. void Update() { //因输入导致的游戏状态更新
  159. //GetKeyState如果高位为1表示按键按下,此时返回值为负数(<0);如果高位为0表示按键弹起,此时返回值为正数(>0)
  160. char key_W = GetKeyState('W'), //监测 W 键是否按下
  161. key_S = GetKeyState('S'), //监测 S 键是否按下
  162. key_A = GetKeyState('A'), //监测 A 键是否按下
  163. key_D = GetKeyState('D'), //监测 D 键是否按下
  164. key_attack = GetKeyState(' '),//监测 空格 键是否按下
  165. key_out = GetKeyState('T');//监测 T 键是否按下
  166. if (kbhit()) { //当有键按下时为1,无为0
  167. if (key_W < 0) { //当按下 W 键,上移
  168. if (pos_h > 1) {
  169. canvas[pos_h][pos_w] = blank;
  170. if (canvas[pos_h - 1][pos_w] == enemy) { //下个位置是敌机,撞毁
  171. canvas[pos_h - 1][pos_w] = destroy;
  172. Over? = 0;
  173. }
  174. else canvas[--pos_h][pos_w] = plane;
  175. }
  176. }
  177. if (key_S < 0) { //当按下 S 键,下移
  178. if (pos_h < high) {
  179. canvas[pos_h][pos_w] = blank;
  180. if (canvas[pos_h + 1][pos_w] == enemy) { //下个位置是敌机,撞毁
  181. canvas[pos_h + 1][pos_w] = destroy;
  182. Over? = 0;
  183. }
  184. else canvas[++pos_h][pos_w] = plane;
  185. }
  186. }
  187. if (key_A < 0) { //当按下 A 键,左移
  188. if (pos_w > 1) {
  189. canvas[pos_h][pos_w] = blank;
  190. if (canvas[pos_h][pos_w - 1] == enemy) { //下个位置是敌机,撞毁
  191. canvas[pos_h][pos_w - 1] = destroy;
  192. Over? = 0;
  193. }
  194. else canvas[pos_h][--pos_w] = plane;
  195. }
  196. }
  197. if (key_D < 0) { //当按下 D 键,右移
  198. if (pos_w < width) {
  199. canvas[pos_h][pos_w] = blank;
  200. if (canvas[pos_h][pos_w + 1] == enemy) { //下个位置是敌机,撞毁
  201. canvas[pos_h][pos_w + 1] = destroy;
  202. Over? = 0;
  203. }
  204. else canvas[pos_h][++pos_w] = plane;
  205. }
  206. }
  207. if (key_attack < 0) { //当按下空格键,发射子弹
  208. if (pos_h != 1) {
  209. canvas[pos_h - 1][pos_w] = bullet;
  210. num++;
  211. }
  212. }
  213. if (key_out < 0) {
  214. Over? = 0;
  215. }
  216. }
  217. }
  218. void Update1() { //因输入导致的游戏状态更新
  219. char key_W = GetKeyState('W'), //监测 W 键是否按下
  220. key_S = GetKeyState('S'), //监测 S 键是否按下
  221. key_A = GetKeyState('A'), //监测 A 键是否按下
  222. key_D = GetKeyState('D'), //监测 D 键是否按下
  223. key_attack = GetKeyState(' '), //监测 空格 键是否按下
  224. key_out = GetKeyState('T');//监测 T 键是否按下
  225. if (kbhit()) { //当有键按下时为1,无为0
  226. if (key_W < 0) { //当按下 W 键,上移
  227. if (pos_h > 1) {
  228. canvas[pos_h][pos_w] = blank;
  229. canvas[--pos_h][pos_w] = plane;
  230. }
  231. }
  232. if (key_S < 0) { //当按下 S 键,下移
  233. if (pos_h < high) {
  234. canvas[pos_h][pos_w] = blank;
  235. canvas[++pos_h][pos_w] = plane;
  236. }
  237. }
  238. if (key_A < 0) { //当按下 A 键,左移
  239. if (pos_w > 1) {
  240. canvas[pos_h][pos_w] = blank;
  241. canvas[pos_h][--pos_w] = plane;
  242. }
  243. }
  244. if (key_D < 0) { //当按下 D 键,右移
  245. if (pos_w < width) {
  246. canvas[pos_h][pos_w] = blank;
  247. canvas[pos_h][++pos_w] = plane;
  248. }
  249. }
  250. if (key_attack < 0) { //当按下空格键,发射子弹
  251. if (pos_h != 1) {
  252. canvas[pos_h - 1][pos_w] = bullet;
  253. num++;
  254. }
  255. }
  256. if (key_out < 0) {
  257. Over? = 0;
  258. }
  259. }
  260. }
  261. void Update2() { //因输入导致的游戏状态更新
  262. char key_W = GetKeyState('W'), //监测 W 键是否按下
  263. key_S = GetKeyState('S'), //监测 S 键是否按下
  264. key_A = GetKeyState('A'), //监测 A 键是否按下
  265. key_D = GetKeyState('D'), //监测 D 键是否按下
  266. key_attack = GetKeyState(' '),//监测 空格 键是否按下
  267. key_out = GetKeyState('T');//监测 T 键是否按下
  268. if (kbhit()) { //当有键按下时为1,无为0
  269. if (key_W < 0) { //当按下 W 键,上移
  270. if (pos_h > 1) {
  271. canvas[pos_h][pos_w] = blank;
  272. if (canvas[pos_h - 1][pos_w] == enemy) { //下个位置是敌机,撞毁
  273. canvas[pos_h - 1][pos_w] = destroy;
  274. life--;
  275. if (life == 0) { Over? = 0; }
  276. }
  277. else canvas[--pos_h][pos_w] = plane;
  278. }
  279. }
  280. if (key_S < 0) { //当按下 S 键,下移
  281. if (pos_h < high) {
  282. canvas[pos_h][pos_w] = blank;
  283. if (canvas[pos_h + 1][pos_w] == enemy) { //下个位置是敌机,撞毁
  284. canvas[pos_h + 1][pos_w] = destroy;
  285. life--;
  286. if (life == 0) { Over? = 0; }
  287. }
  288. else canvas[++pos_h][pos_w] = plane;
  289. }
  290. }
  291. if (key_A < 0) { //当按下 A 键,左移
  292. if (pos_w > 1) {
  293. canvas[pos_h][pos_w] = blank;
  294. if (canvas[pos_h][pos_w - 1] == enemy) { //下个位置是敌机,撞毁
  295. canvas[pos_h][pos_w - 1] = destroy;
  296. life--;
  297. if (life == 0) { Over? = 0; }
  298. }
  299. else canvas[pos_h][--pos_w] = plane;
  300. }
  301. }
  302. if (key_D < 0) { //当按下 D 键,右移
  303. if (pos_w < width) {
  304. canvas[pos_h][pos_w] = blank;
  305. if (canvas[pos_h][pos_w + 1] == enemy) { //下个位置是敌机,撞毁
  306. canvas[pos_h][pos_w + 1] = destroy;
  307. life--;
  308. if (life == 0) { Over? = 0; }
  309. }
  310. else canvas[pos_h][++pos_w] = plane;
  311. }
  312. }
  313. if (key_attack < 0) { //当按下空格键,发射子弹
  314. if (pos_h != 1) {
  315. canvas[pos_h - 1][pos_w] = bullet;
  316. num++;
  317. }
  318. }
  319. if (key_out < 0) {
  320. Over? = 0;
  321. }
  322. }
  323. }
  324. void NoUpdate() { //与输入无关的游戏状态更新
  325. int temp[high + 2][width + 2]; //用来判断原位置的临时数组
  326. for (int i = 1; i <= high; i++) {
  327. for (int j = 1; j <= width; j++) {
  328. temp[i][j] = canvas[i][j];
  329. }
  330. }
  331. for (int i = 1; i <= high; i++) { //遍历临时数组,修改数组
  332. for (int j = 1; j <= width; j++) {
  333. if (temp[i][j] == enemy && record % rec_move == 0) { //当前位置为敌机
  334. canvas[i][j] = blank;
  335. if (temp[i + 1][j] == bullet) { //下面为子弹,敌机被击中
  336. canvas[i + 1][j] = blank;
  337. score++;
  338. }
  339. else if (i < high) {
  340. canvas[i + 1][j] = enemy;
  341. }
  342. if (i + 1 == pos_h && j == pos_w) { //下面为飞机,玩家飞机被撞毁
  343. canvas[i + 1][j] = destroy;
  344. Over? = 0;
  345. }
  346. }
  347. if (temp[i][j] == bullet) { //当前位置为子弹
  348. canvas[i][j] = blank;
  349. if (temp[i - 1][j] == enemy) { //下个位置是敌机,敌机被击毁
  350. canvas[i - 1][j] = blank;
  351. score++;
  352. }
  353. else if (i > 1) {
  354. canvas[i - 1][j] = bullet;
  355. }
  356. }
  357. }
  358. }
  359. if (record % rec_new == 0) //刚好到时间间隔
  360. for (int i = 0; i < enemynum; i++) { //新增敌机群
  361. canvas[rand() % 2 + 1][rand() % width + 1] = enemy;
  362. }
  363. if (record <= 100) { //时间间隔计次
  364. record++;
  365. }
  366. else { //时间间隔计次清零
  367. record = 0;
  368. }
  369. }
  370. void NoUpdate1() { //与输入无关的游戏状态更新
  371. int temp[high + 2][width + 2]; //用来判断原位置的临时数组
  372. for (int i = 1; i <= high; i++) {
  373. for (int j = 1; j <= width; j++) {
  374. temp[i][j] = canvas[i][j];
  375. }
  376. }
  377. for (int i = 1; i <= high; i++) { //遍历临时数组,修改数组
  378. for (int j = 1; j <= width; j++) {
  379. if (temp[i][j] == enemy && record % rec_move == 0) { //当前位置为敌机
  380. canvas[i][j] = blank;
  381. if (temp[i + 1][j] == bullet) { //下面为子弹,敌机被击中
  382. canvas[i + 1][j] = blank;
  383. score++;
  384. }
  385. else if (i < high) {
  386. canvas[i + 1][j] = enemy;
  387. }
  388. }
  389. if (temp[i][j] == bullet) { //当前位置为子弹
  390. canvas[i][j] = blank;
  391. if (temp[i - 1][j] == enemy) { //下个位置是敌机,敌机被击毁
  392. canvas[i - 1][j] = blank;
  393. score++;
  394. }
  395. else if (i > 1) {
  396. canvas[i - 1][j] = bullet;
  397. }
  398. }
  399. }
  400. }
  401. if (record % rec_new == 0) //刚好到时间间隔
  402. for (int i = 0; i < enemynum; i++) { //新增敌机群
  403. canvas[rand() % 2 + 1][rand() % width + 1] = enemy;
  404. }
  405. if (record <= 100) { //时间间隔计次
  406. record++;
  407. }
  408. else { //时间间隔计次清零
  409. record = 0;
  410. }
  411. }
  412. void NoUpdate2() { //与输入无关的游戏状态更新
  413. int temp[high + 2][width + 2]; //用来判断原位置的临时数组
  414. for (int i = 1; i <= high; i++) {
  415. for (int j = 1; j <= width; j++) {
  416. temp[i][j] = canvas[i][j];
  417. }
  418. }
  419. for (int i = 1; i <= high; i++) { //遍历临时数组,修改数组
  420. for (int j = 1; j <= width; j++) {
  421. if (temp[i][j] == enemy && record % rec_move == 0) { //当前位置为敌机
  422. canvas[i][j] = blank;
  423. if (temp[i + 1][j] == bullet) { //下面为子弹,敌机被击中
  424. canvas[i + 1][j] = blank;
  425. score++;
  426. }
  427. else if (i < high) {
  428. canvas[i + 1][j] = enemy;
  429. }
  430. if (i + 1 == pos_h && j == pos_w) { //下面为飞机,玩家飞机被撞毁
  431. canvas[i + 1][j] = destroy;
  432. life--;
  433. if (life == 0) {
  434. Over? = 0;
  435. }
  436. }
  437. }
  438. if (temp[i][j] == bullet) { //当前位置为子弹
  439. canvas[i][j] = blank;
  440. if (temp[i - 1][j] == enemy) { //下个位置是敌机,敌机被击毁
  441. canvas[i - 1][j] = blank;
  442. score++;
  443. }
  444. else if (i > 1) {
  445. canvas[i - 1][j] = bullet;
  446. }
  447. }
  448. }
  449. }
  450. if (record % rec_new == 0) //刚好到时间间隔
  451. for (int i = 0; i < enemynum; i++) { //新增敌机群
  452. canvas[rand() % 2 + 1][rand() % width + 1] = enemy;
  453. }
  454. if (record <= 100) { //时间间隔计次
  455. record++;
  456. }
  457. else { //时间间隔计次清零
  458. record = 0;
  459. }
  460. }
  461. void gotoxy(int x, int y) { //回调光标
  462. COORD pos;
  463. pos.X = x - 1;
  464. pos.Y = y - 1;
  465. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  466. }
  467. void HideCursor() { //隐藏光标函数
  468. CONSOLE_CURSOR_INFO cursor;
  469. cursor.bVisible = FALSE;
  470. cursor.dwSize = sizeof(cursor);
  471. HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
  472. SetConsoleCursorInfo(handle, &cursor);
  473. }
  474. int menu()
  475. {
  476. ///**********************************************************
  477. /// 函数名 :menu *
  478. /// 输入参数:无返回值 *
  479. /// 返回值 : 无 *
  480. /// 函数功能: *
  481. /// 用于显示主界面并衔接游戏设置 *
  482. ///**********************************************************
  483. cout << endl;
  484. cout << endl;
  485. cout << endl;
  486. cout << "\t\t\t\t Annihilator Battle " << endl;
  487. cout << "\t\t\t\t|-----------------------------|" << endl;
  488. cout << "\t\t\t\t| 1:开始游戏 |" << endl;
  489. cout << "\t\t\t\t| 2: 设 置 |" << endl;
  490. cout << "\t\t\t\t| 3:游戏介绍 |" << endl;
  491. cout << "\t\t\t\t| 4:退出游戏 |" << endl;
  492. cout << "\t\t\t\t|-----------------------------|" << endl;
  493. HA:
  494. cout << "\t\t\t\t 请输入选项: ";
  495. int choice = 0;
  496. cin >> choice;
  497. switch (choice)
  498. {
  499. case 1:
  500. system("cls");//可以用gotoxy(0,0)
  501. rewind(stdin);
  502. return 1;
  503. break;
  504. case 2:
  505. system("cls");//可以用gotoxy(0,0)
  506. rewind(stdin);
  507. return setting();
  508. break;
  509. case 3:
  510. system("cls");//可以用gotoxy(0,0)
  511. rewind(stdin);
  512. return introduce();
  513. break;
  514. case 4:return 0; break;
  515. default:
  516. cout << "输入错误,请重新输入!!";
  517. goto HA;
  518. break;
  519. }
  520. }
  521. int introduce() {
  522. cout << endl;
  523. cout << endl;
  524. cout << endl;
  525. cout << "\t\t\t\t introduce " << endl;
  526. cout << "\t\t\t\t|-----------------------------|" << endl;
  527. cout << "\t\t\t\t| 1:操作介绍 |" << endl;
  528. cout << "\t\t\t\t| 2:背景介绍 |" << endl;
  529. cout << "\t\t\t\t| 3:返 回 |" << endl;
  530. cout << "\t\t\t\t|-----------------------------|" << endl;
  531. YE:
  532. cout << "\t\t\t\t 请输入选项:";
  533. int select;
  534. cin >> select;
  535. switch (select)
  536. {
  537. case 1:return control_introduce(); break;
  538. case 2:return background_introduce(); break;
  539. case 3:system("cls"); return menu(); break;
  540. default: cout << "输入错误,请重新输入!!";
  541. goto YE; break;
  542. }
  543. }
  544. int control_introduce() {
  545. system("cls");
  546. printf("操作说明: ASDW分别操作 左下右上四个的移动\n");
  547. printf("**空格是发出子弹**\n");
  548. printf("**T退出游戏**\n");
  549. cout << endl << "是否返回主菜单?" << endl << "返回主菜单为1,退出游戏为0" << endl;
  550. cout << "你的选择是:";
  551. int i;
  552. cin >> i;
  553. if (i == 1) {
  554. system("cls");
  555. return menu();
  556. }
  557. if (i == 0) {
  558. system("cls");
  559. return 0;
  560. }
  561. }
  562. int background_introduce() {
  563. system("cls");
  564. printf("飞机游戏是一款风靡全球的电视机游戏和掌上游戏机产品,\n");
  565. printf("曾几何时,它打造了一个无数人回忆的梦,\n");
  566. printf("曾几何时,它创造了一个无法企及的游戏巅峰,\n");
  567. printf("也曾影响了一代产业链。虽然它辉煌的业绩在历史的涡轮中渐渐远去,\n");
  568. printf("但这款游戏每每提及,总会令人爱不释手,魂牵梦绕。 \n");
  569. cout << endl << "是否返回主菜单?" << endl << "返回主菜单为1,返回设置为0" << endl;
  570. cout << "你的选择是:";
  571. int i;
  572. cin >> i;
  573. if (i == 1) {
  574. system("cls");
  575. return menu();
  576. }
  577. if (i == 0) {
  578. system("cls");
  579. return setting();
  580. }
  581. }
  582. int setting()
  583. {
  584. cout << endl;
  585. cout << endl;
  586. cout << endl;
  587. cout << "\t\t\t\t Setting " << endl;
  588. cout << "\t\t\t\t|-----------------------------|" << endl;
  589. cout << "\t\t\t\t| 1:敌机移动速度 |" << endl;
  590. cout << "\t\t\t\t| 2:敌机刷新速度 |" << endl;
  591. cout << "\t\t\t\t| 3:敌机数目 |" << endl;
  592. cout << "\t\t\t\t| 4:生命数目 |" << endl;
  593. cout << "\t\t\t\t| 5:返 回 |" << endl;
  594. cout << "\t\t\t\t|-----------------------------|" << endl;
  595. RE:
  596. cout << "\t\t\t\t 请输入选项:";
  597. int select;
  598. cin >> select;
  599. switch (select)
  600. {
  601. case 1:return speed_setting(); break;
  602. case 2:return rec_new_setting(); break;
  603. case 3:return enemynum_setting(); break;
  604. case 4:return life_setting(); break;
  605. case 5:system("cls"); return menu(); break;
  606. default: cout << "输入错误,请重新输入!!";
  607. goto RE; break;
  608. }
  609. }
  610. int speed_setting() {
  611. system("cls");
  612. cout << "提示:敌机速度尽量设置在0-15,且数值越大移动越慢" << endl;
  613. cout << "敌机的移动速度设置为:";
  614. cin >> rec_move;
  615. cout << "已修改速度为:" << rec_move;
  616. cout << endl << "是否返回主菜单?" << endl << "返回主菜单为1,返回设置为0" << endl;
  617. cout << "你的选择是:";
  618. int i;
  619. cin >> i;
  620. if (i == 1) {
  621. system("cls");
  622. return menu();
  623. }
  624. if (i == 0) {
  625. system("cls");
  626. return setting();
  627. }
  628. }
  629. int rec_new_setting() {
  630. system("cls");
  631. cout << "提示:敌机刷新速度尽量设置在1-20," << endl;
  632. cout << "备注:数值越小,刷新速度越快,但上限不超过设置的敌机数目!" << endl;
  633. cout << "敌机的刷新速度设置为:";
  634. cin >> rec_new;
  635. cout << "已修改敌机刷新速度为:" << rec_new;
  636. cout << endl << "是否返回主菜单?" << endl << "返回主菜单为1,返回设置为0" << endl;
  637. cout << "你的选择是:";
  638. int i;
  639. cin >> i;
  640. if (i == 1) {
  641. system("cls");
  642. return menu();
  643. }
  644. if (i == 0) {
  645. system("cls");
  646. return setting();
  647. }
  648. }
  649. int enemynum_setting() {
  650. system("cls");
  651. cout << "提示:敌机数目尽量设置在2-30," << endl;
  652. cout << "敌机的数目设置为:";
  653. cin >> enemynum;
  654. cout << "已修改敌机数目为:" << enemynum;
  655. cout << endl << "是否返回主菜单?" << endl << "返回主菜单为1,返回设置为0" << endl;
  656. cout << "你的选择是:";
  657. int i;
  658. cin >> i;
  659. if (i == 1) {
  660. system("cls");
  661. return menu();
  662. }
  663. if (i == 0) {
  664. system("cls");
  665. return setting();
  666. }
  667. }
  668. int life_setting() {
  669. system("cls");
  670. cout << "提示:生命数目尽量设置在2-30," << endl;
  671. cout << "生命的数目设置为:";
  672. cin >> life;
  673. cout << "已修改敌机数目为:" << life;
  674. cout << endl << "是否返回主菜单?" << endl << "返回主菜单为1,返回设置为0" << endl;
  675. cout << "你的选择是:";
  676. int i;
  677. cin >> i;
  678. if (i == 1) {
  679. system("cls");
  680. return menu();
  681. }
  682. if (i == 0) {
  683. system("cls");
  684. return setting();
  685. }
  686. }
  687. int mode()
  688. {
  689. cout << endl;
  690. cout << endl;
  691. cout << endl;
  692. cout << "\t\t\t\t Mode " << endl;
  693. cout << "\t\t\t\t|-----------------------------|" << endl;
  694. cout << "\t\t\t\t| 1:生存模式 |" << endl;
  695. cout << "\t\t\t\t| 2:无敌模式 |" << endl;
  696. cout << "\t\t\t\t| 3:多命模式 |" << endl;
  697. cout << "\t\t\t\t| 4:主菜单 |" << endl;
  698. cout << "\t\t\t\t|-----------------------------|" << endl;
  699. WH:
  700. int select;
  701. cout << "\t\t\t\t 请输入选项:";
  702. cin >> select;
  703. switch (select)
  704. {
  705. case 1:
  706. system("cls");
  707. rewind(stdin);//清空输入缓冲区
  708. return 1;
  709. break;
  710. case 2:
  711. system("cls");
  712. rewind(stdin);
  713. return 2;
  714. break;
  715. case 3:
  716. system("cls");
  717. rewind(stdin);
  718. return 3;
  719. break;
  720. case 4:
  721. system("cls");
  722. rewind(stdin);
  723. return 4; break;
  724. default:
  725. cout << "输入错误,请重新输入!!";
  726. goto WH; break;
  727. }
  728. }
  729. void mode1() {
  730. SetSize(width + 10, high + 9);
  731. Startup(); //初始化
  732. while (Over?) { //游戏循环
  733. Update();
  734. NoUpdate();
  735. Show();
  736. }
  737. }
  738. void mode2() {
  739. SetSize(width + 10, high + 9);
  740. Startup(); //初始化
  741. while (Over?) { //游戏循环
  742. Update1();
  743. NoUpdate1();
  744. Show();
  745. }
  746. }
  747. void mode3() {
  748. SetSize(width + 10, high + 9);
  749. Startup(); //初始化
  750. while (Over?) { //游戏循环
  751. Update2();
  752. NoUpdate2();
  753. Show();
  754. cout << "剩余生命为:" << life;
  755. }
  756. }
  757. void result() {
  758. system("cls");
  759. double shoot = double(score) / double(num);
  760. cout << endl;
  761. cout << endl;
  762. cout << endl;
  763. cout << "\t\t\t\t game over! " << endl;
  764. cout << "\t\t\t\t|-----------------------------" << endl;
  765. printf("\t\t\t\t| 你的得分为:%d \n", score);
  766. printf("\t\t\t\t| 已发射子弹数目:%d \n", num);
  767. printf("\t\t\t\t| 精准度为:%f \n", shoot);
  768. cout << "\t\t\t\t|-----------------------------" << endl;
  769. //评价
  770. assist(shoot);
  771. cout << endl;
  772. Sleep(2500); //暂停游戏结束界面(毫秒)
  773. system("pause");
  774. }
  775. void assist(double shoot) {
  776. if (shoot > 0 && shoot <= 0.4)
  777. {
  778. cout << "\t\t\t\t| 你的精准度较低!!";
  779. }
  780. else if (shoot > 0.4 && shoot <= 0.7) {
  781. cout << "\t\t\t\t| 你的精准度较好!!";
  782. }
  783. else if (shoot > 0.7 && shoot <= 1.0) {
  784. cout << "\t\t\t\t| 你的精准度优秀!!";
  785. }
  786. }
  787. void SetSize(unsigned uCol, unsigned uLine)//控制台大小控制
  788. {
  789. char cmd[64];
  790. sprintf(cmd, "mode con cols=%d lines=%d", uCol, uLine);
  791. system(cmd);
  792. }

分享结束,谢谢!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/358956
推荐阅读
相关标签
  

闽ICP备14008679号