C++实现飞机大战

清疚 2023-01-02 12:22 278阅读 0赞

C++小游戏飞机大战

废话不多说,直接看代码吧(转载)

  1. #include<iostream>
  2. #include<windows.h>
  3. #include<conio.h>
  4. #include<time.h>
  5. #include<string>
  6. using namespace std;
  7. /*=============== all the structures ===============*/
  8. typedef struct Frame
  9. {
  10. COORD position[2];
  11. int flag;
  12. }Frame;
  13. /*=============== all the functions ===============*/
  14. void SetPos(COORD a)// set cursor
  15. {
  16. HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
  17. SetConsoleCursorPosition(out, a);
  18. }
  19. void SetPos(int i, int j)// set cursor
  20. {
  21. COORD pos={ i, j};
  22. SetPos(pos);
  23. }
  24. void HideCursor()
  25. {
  26. CONSOLE_CURSOR_INFO cursor_info = { 1, 0};
  27. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
  28. }
  29. //把第y行,[x1, x2) 之间的坐标填充为 ch
  30. void drawRow(int y, int x1, int x2, char ch)
  31. {
  32. SetPos(x1,y);
  33. for(int i = 0; i <= (x2-x1); i++)
  34. cout<<ch;
  35. }
  36. //在a, b 纵坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
  37. void drawRow(COORD a, COORD b, char ch)
  38. {
  39. if(a.Y == b.Y)
  40. drawRow(a.Y, a.X, b.X, ch);
  41. else
  42. {
  43. SetPos(0, 25);
  44. cout<<"error code 01:无法填充行,因为两个坐标的纵坐标(x)不相等";
  45. system("pause");
  46. }
  47. }
  48. //把第x列,[y1, y2] 之间的坐标填充为 ch
  49. void drawCol(int x, int y1, int y2, char ch)
  50. {
  51. int y=y1;
  52. while(y!=y2+1)
  53. {
  54. SetPos(x, y);
  55. cout<<ch;
  56. y++;
  57. }
  58. }
  59. //在a, b 横坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
  60. void drawCol(COORD a, COORD b, char ch)
  61. {
  62. if(a.X == b.X)
  63. drawCol(a.X, a.Y, b.Y, ch);
  64. else
  65. {
  66. SetPos(0, 25);
  67. cout<<"error code 02:无法填充列,因为两个坐标的横坐标(y)不相等";
  68. system("pause");
  69. }
  70. }
  71. //左上角坐标、右下角坐标、用row填充行、用col填充列
  72. void drawFrame(COORD a, COORD b, char row, char col)
  73. {
  74. drawRow(a.Y, a.X+1, b.X-1, row);
  75. drawRow(b.Y, a.X+1, b.X-1, row);
  76. drawCol(a.X, a.Y+1, b.Y-1, col);
  77. drawCol(b.X, a.Y+1, b.Y-1, col);
  78. }
  79. void drawFrame(int x1, int y1, int x2, int y2, char row, char col)
  80. {
  81. COORD a={ x1, y1};
  82. COORD b={ x2, y2};
  83. drawFrame(a, b, row, col);
  84. }
  85. void drawFrame(Frame frame, char row, char col)
  86. {
  87. COORD a = frame.position[0];
  88. COORD b = frame.position[1];
  89. drawFrame(a, b, row, col);
  90. }
  91. void drawPlaying()
  92. {
  93. drawFrame(0, 0, 48, 24, '=', '|');// draw map frame;
  94. drawFrame(49, 0, 79, 4, '-', '|');// draw output frame
  95. drawFrame(49, 4, 79, 9, '-', '|');// draw score frame
  96. drawFrame(49, 9, 79, 20, '-', '|');// draw operate frame
  97. drawFrame(49, 20, 79, 24, '-', '|');// draw other message frame
  98. SetPos(52, 6);
  99. cout<<"得分:";
  100. SetPos(52, 7);
  101. cout<<"称号:";
  102. SetPos(52,10);
  103. cout<<"操作方式:";
  104. SetPos(52,12);
  105. cout<<" a,s,d,w 控制战机移动。";
  106. SetPos(52,14);
  107. cout<<" p 暂停游戏。";
  108. SetPos(52,16);
  109. cout<<" e 退出游戏。";
  110. SetPos(52,18);
  111. cout<<" k攻击。";
  112. }
  113. //在[a, b)之间产生一个随机整数
  114. int random(int a, int b)
  115. {
  116. int c=(rand() % (a-b))+ a;
  117. return c;
  118. }
  119. //在两个坐标包括的矩形框内随机产生一个坐标
  120. COORD random(COORD a, COORD b)
  121. {
  122. int x=random(a.X, b.X);
  123. int y=random(a.Y, b.Y);
  124. COORD c={ x, y};
  125. return c;
  126. }
  127. bool judgeCoordInFrame(Frame frame, COORD spot)
  128. {
  129. if(spot.X>=frame.position[0].X)
  130. if(spot.X<=frame.position[1].X)
  131. if(spot.Y>=frame.position[0].Y)
  132. if(spot.Y<=frame.position[0].Y)
  133. return true;
  134. return false;
  135. }
  136. void printCoord(COORD a)
  137. {
  138. cout <<"( "<<a.X<<" , "<<a.Y<<" )";
  139. }
  140. void printFrameCoord(Frame a)
  141. {
  142. printCoord(a.position[0]);
  143. cout <<" - ";
  144. printCoord(a.position[1]);
  145. }
  146. int drawMenu()
  147. {
  148. SetPos(30, 1);
  149. cout<<"P l a n e W a r";
  150. drawRow(3, 0, 79, '-');
  151. drawRow(5, 0, 79, '-');
  152. SetPos(28, 4);
  153. cout<<"w 和 s 选择, k 确定";
  154. SetPos(15, 11);
  155. cout<<"1. 简单的敌人";
  156. SetPos(15, 13);
  157. cout<<"2. 冷酷的敌人";
  158. drawRow(20, 0, 79, '-');
  159. drawRow(22, 0, 79, '-');
  160. SetPos(47, 11);
  161. cout<<"简单的敌人:";
  162. SetPos(51, 13);
  163. cout<<"简单敌人有着较慢的移动速度。";
  164. SetPos(24, 21);
  165. int j=11;
  166. SetPos(12, j);
  167. cout<<">>";
  168. //drawFrame(45, 9, 79, 17, '=', '|');
  169. while(1)
  170. { if( _kbhit() )
  171. {
  172. char x=_getch();
  173. switch (x)
  174. {
  175. case 'w' :
  176. {
  177. if( j == 13)
  178. {
  179. SetPos(12, j);
  180. cout<<" ";
  181. j = 11;
  182. SetPos(12, j);
  183. cout<<">>";
  184. SetPos(51, 13);
  185. cout<<"            ";
  186. SetPos(47, 11);
  187. cout<<"简单的敌人:";
  188. SetPos(51, 13);
  189. cout<<"简单敌人有着较慢的移动速度,比较容易对付";
  190. }
  191. break;
  192. }
  193. case 's' :
  194. {
  195. if( j == 11 )
  196. {
  197. SetPos(12, j);
  198. cout<<" ";
  199. j = 13;
  200. SetPos(12, j);
  201. cout<<">>";
  202. SetPos(51, 13);
  203. cout<<"              ";
  204. SetPos(47, 11);
  205. cout<<"冷酷的敌人:";
  206. SetPos(51, 13);
  207. cout<<"冷酷的敌人移动速度较快,难对付哟。";
  208. }
  209. break;
  210. }
  211. case 'k' :
  212. {
  213. if (j == 8) return 1;
  214. else return 2;
  215. }
  216. }
  217. }
  218. }
  219. }
  220. /* DWORD WINAPI MusicFun(LPVOID lpParamte) { //DWORD OBJ; sndPlaySound(TEXT("bgm.wav"), SND_FILENAME|SND_ASYNC); return 0; } */
  221. /*================== the Game Class ==================*/
  222. class Game
  223. {
  224. public:
  225. COORD position[10];
  226. COORD bullet[10];
  227. Frame enemy[8];
  228. int score;
  229. int rank;
  230. int rankf;
  231. string title;
  232. int flag_rank;
  233. Game ();
  234. //初始化所有
  235. void initPlane();
  236. void initBullet();
  237. void initEnemy();
  238. //初始化其中一个
  239. //void initThisBullet( COORD );
  240. //void initThisEnemy( Frame );
  241. void planeMove(char);
  242. void bulletMove();
  243. void enemyMove();
  244. //填充所有
  245. void drawPlane();
  246. void drawPlaneToNull();
  247. void drawBullet();
  248. void drawBulletToNull();
  249. void drawEnemy();
  250. void drawEnemyToNull();
  251. //填充其中一个
  252. void drawThisBulletToNull( COORD );
  253. void drawThisEnemyToNull( Frame );
  254. void Pause();
  255. void Playing();
  256. void judgePlane();
  257. void judgeEnemy();
  258. void Shoot();
  259. void GameOver();
  260. void printScore();
  261. };
  262. Game::Game()
  263. {
  264. initPlane();
  265. initBullet();
  266. initEnemy();
  267. score = 0;
  268. rank = 25;
  269. rankf = 0;
  270. flag_rank = 0;
  271. }
  272. void Game::initPlane()
  273. {
  274. COORD centren={ 39, 22};
  275. position[0].X=position[5].X=position[7].X=position[9].X=centren.X;
  276. position[1].X=centren.X-2;
  277. position[2].X=position[6].X=centren.X-1;
  278. position[3].X=position[8].X=centren.X+1;
  279. position[4].X=centren.X+2;
  280. for(int i=0; i<=4; i++)
  281. position[i].Y=centren.Y;
  282. for(int i=6; i<=8; i++)
  283. position[i].Y=centren.Y+1;
  284. position[5].Y=centren.Y-1;
  285. position[9].Y=centren.Y-2;
  286. }
  287. void Game::drawPlane()
  288. {
  289. for(int i=0; i<9; i++)
  290. {
  291. SetPos(position[i]);
  292. if(i!=5)
  293. cout<<"@";
  294. else if(i==5)
  295. cout<<"|";
  296. }
  297. }
  298. void Game::drawPlaneToNull()
  299. {
  300. for(int i=0; i<9; i++)
  301. {
  302. SetPos(position[i]);
  303. cout<<" ";
  304. }
  305. }
  306. void Game::initBullet()
  307. {
  308. for(int i=0; i<10; i++)
  309. bullet[i].Y = 30;
  310. }
  311. void Game::drawBullet()
  312. {
  313. for(int i=0; i<10; i++)
  314. {
  315. if( bullet[i].Y != 30)
  316. {
  317. SetPos(bullet[i]);
  318. cout<<".";
  319. }
  320. }
  321. }
  322. void Game::drawBulletToNull()
  323. {
  324. for(int i=0; i<10; i++)
  325. if( bullet[i].Y != 30 )
  326. {
  327. COORD pos={ bullet[i].X, bullet[i].Y+1};
  328. SetPos(pos);
  329. cout<<" ";
  330. }
  331. }
  332. void Game::initEnemy()
  333. {
  334. COORD a={ 1, 1};
  335. COORD b={ 45, 15};
  336. for(int i=0; i<8; i++)
  337. {
  338. enemy[i].position[0] = random(a, b);
  339. enemy[i].position[1].X = enemy[i].position[0].X + 3;
  340. enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
  341. }
  342. }
  343. void Game::drawEnemy()
  344. {
  345. for(int i=0; i<8; i++)
  346. drawFrame(enemy[i].position[0], enemy[i].position[1], '-', '|');
  347. }
  348. void Game::drawEnemyToNull()
  349. {
  350. for(int i=0; i<8; i++)
  351. {
  352. drawFrame(enemy[i].position[0], enemy[i].position[1], ' ', ' ');
  353. }
  354. }
  355. void Game::Pause()
  356. {
  357. SetPos(61,2);
  358. cout<<" ";
  359. SetPos(61,2);
  360. cout<<"暂停中...";
  361. char c=_getch();
  362. while(c!='p')
  363. c=_getch();
  364. SetPos(61,2);
  365. cout<<" ";
  366. }
  367. void Game::planeMove(char x)
  368. {
  369. if(x == 'a')
  370. if(position[1].X != 1)
  371. for(int i=0; i<=9; i++)
  372. position[i].X -= 2;
  373. if(x == 's')
  374. if(position[7].Y != 23)
  375. for(int i=0; i<=9; i++)
  376. position[i].Y += 1;
  377. if(x == 'd')
  378. if(position[4].X != 47)
  379. for(int i=0; i<=9; i++)
  380. position[i].X += 2;
  381. if(x == 'w')
  382. if(position[5].Y != 3)
  383. for(int i=0; i<=9; i++)
  384. position[i].Y -= 1;
  385. }
  386. void Game::bulletMove()
  387. {
  388. for(int i=0; i<10; i++)
  389. {
  390. if( bullet[i].Y != 30)
  391. {
  392. bullet[i].Y -= 1;
  393. if( bullet[i].Y == 1 )
  394. {
  395. COORD pos={ bullet[i].X, bullet[i].Y+1};
  396. drawThisBulletToNull( pos );
  397. bullet[i].Y=30;
  398. }
  399. }
  400. }
  401. }
  402. void Game::enemyMove()
  403. {
  404. for(int i=0; i<8; i++)
  405. {
  406. for(int j=0; j<2; j++)
  407. enemy[i].position[j].Y++;
  408. if(24 == enemy[i].position[1].Y)
  409. {
  410. COORD a={ 1, 1};
  411. COORD b={ 45, 3};
  412. enemy[i].position[0] = random(a, b);
  413. enemy[i].position[1].X = enemy[i].position[0].X + 3;
  414. enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
  415. }
  416. }
  417. }
  418. void Game::judgePlane()
  419. {
  420. for(int i = 0; i < 8; i++)
  421. for(int j=0; j<9; j++)
  422. if(judgeCoordInFrame(enemy[i], position[j]))
  423. {
  424. SetPos(62, 1);
  425. cout<<"坠毁";
  426. drawFrame(enemy[i], '+', '+');
  427. Sleep(1000);
  428. GameOver();
  429. break;
  430. }
  431. }
  432. void Game::drawThisBulletToNull( COORD c)
  433. {
  434. SetPos(c);
  435. cout<<" ";
  436. }
  437. void Game::drawThisEnemyToNull( Frame f )
  438. {
  439. drawFrame(f, ' ', ' ');
  440. }
  441. void Game::judgeEnemy()
  442. {
  443. for(int i = 0; i < 8; i++)
  444. for(int j = 0; j < 10; j++)
  445. if( judgeCoordInFrame(enemy[i], bullet[j]) )
  446. {
  447. score += 5;
  448. drawThisEnemyToNull( enemy[i] );
  449. COORD a={ 1, 1};
  450. COORD b={ 45, 3};
  451. enemy[i].position[0] = random(a, b);
  452. enemy[i].position[1].X = enemy[i].position[0].X + 3;
  453. enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
  454. drawThisBulletToNull( bullet[j] );
  455. bullet[j].Y = 30;
  456. }
  457. }
  458. void Game::Shoot()
  459. {
  460. for(int i=0; i<10; i++)
  461. if(bullet[i].Y == 30)
  462. {
  463. bullet[i].X = position[5].X;
  464. bullet[i].Y = position[5].Y-1;
  465. break;
  466. }
  467. }
  468. void Game::printScore()
  469. {
  470. if(score == 120 && flag_rank == 0)
  471. {
  472. rank -= 3;
  473. flag_rank = 1;
  474. }
  475. else if( score == 360 && flag_rank == 1)
  476. {
  477. rank -= 5;
  478. flag_rank = 2;
  479. }
  480. else if( score == 480 && flag_rank == 2)
  481. {
  482. rank -= 5;
  483. flag_rank = 3;
  484. }
  485. int x=rank/5;
  486. SetPos(60, 6);
  487. cout<<score;
  488. if( rank!=rankf )
  489. {
  490. SetPos(60, 7);
  491. if( x == 5)
  492. title="初级飞行员";
  493. else if( x == 4)
  494. title="中级飞行员";
  495. else if( x == 3)
  496. title="高级飞行员";
  497. else if( x == 2 )
  498. title="王牌飞行员";
  499. cout<<title;
  500. }
  501. rankf = rank;
  502. }
  503. void Game::Playing()
  504. {
  505. //HANDLE MFUN;
  506. //MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL);
  507. drawEnemy();
  508. drawPlane();
  509. int flag_bullet = 0;
  510. int flag_enemy = 0;
  511. while(1)
  512. {
  513. Sleep(8);
  514. if(_kbhit())
  515. {
  516. char x = _getch();
  517. if ('a' == x || 's' == x || 'd' == x || 'w' == x)
  518. {
  519. drawPlaneToNull();
  520. planeMove(x);
  521. drawPlane();
  522. judgePlane();
  523. }
  524. else if ('p' == x)
  525. Pause();
  526. else if( 'k' == x)
  527. Shoot();
  528. else if( 'e' == x)
  529. {
  530. //CloseHandle(MFUN);
  531. GameOver();
  532. break;
  533. }
  534. }
  535. /* 处理子弹 */
  536. if( 0 == flag_bullet )
  537. {
  538. bulletMove();
  539. drawBulletToNull();
  540. drawBullet();
  541. judgeEnemy();
  542. }
  543. flag_bullet++;
  544. if( 5 == flag_bullet )
  545. flag_bullet = 0;
  546. /* 处理敌人 */
  547. if( 0 == flag_enemy )
  548. {
  549. drawEnemyToNull();
  550. enemyMove();
  551. drawEnemy();
  552. judgePlane();
  553. }
  554. flag_enemy++;
  555. if( flag_enemy >= rank )
  556. flag_enemy = 0;
  557. /* 输出得分 */
  558. printScore();
  559. }
  560. }
  561. void Game::GameOver()
  562. {
  563. system("cls");
  564. COORD p1={ 28,9};
  565. COORD p2={ 53,15};
  566. drawFrame(p1, p2, '=', '|');
  567. SetPos(36,12);
  568. string str="Game Over!";
  569. for(int i=0; i<str.size(); i++)
  570. {
  571. Sleep(80);
  572. cout<<str[i];
  573. }
  574. Sleep(1000);
  575. system("cls");
  576. drawFrame(p1, p2, '=', '|');
  577. SetPos(31, 11);
  578. cout<<"击落敌机:"<<score/5<<" 架";
  579. SetPos(31, 12);
  580. cout<<"得  分:"<<score;
  581. SetPos(31, 13);
  582. cout<<"获得称号:"<<title;
  583. SetPos(30, 16);
  584. Sleep(1000);
  585. cout<<"继续? 是(y)| 否(n)";
  586. as:
  587. char x=_getch();
  588. if (x == 'n')
  589. exit(0);
  590. else if (x == 'y')
  591. {
  592. system("cls");
  593. Game game;
  594. int a = drawMenu();
  595. if(a == 2)
  596. game.rank = 20;
  597. system("cls");
  598. drawPlaying();
  599. game.Playing();
  600. }
  601. else goto as;
  602. }
  603. /*================== the main function ==================*/
  604. int main()
  605. {
  606. //游戏准备
  607. srand((int)time(0)); //随机种子
  608. HideCursor(); //隐藏光标
  609. Game game;
  610. int a = drawMenu();
  611. if(a == 2)
  612. game.rank = 20;
  613. system("cls");
  614. drawPlaying();
  615. game.Playing();
  616. }
  617. #include<iostream>
  618. #include<windows.h>
  619. #include<conio.h>
  620. #include<time.h>
  621. #include<string>
  622. using namespace std;
  623. /*=============== all the structures ===============*/
  624. typedef struct Frame
  625. {
  626. COORD position[2];
  627. int flag;
  628. }Frame;
  629. /*=============== all the functions ===============*/
  630. void SetPos(COORD a)// set cursor
  631. {
  632. HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
  633. SetConsoleCursorPosition(out, a);
  634. }
  635. void SetPos(int i, int j)// set cursor
  636. {
  637. COORD pos={ i, j};
  638. SetPos(pos);
  639. }
  640. void HideCursor()
  641. {
  642. CONSOLE_CURSOR_INFO cursor_info = { 1, 0};
  643. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
  644. }
  645. //把第y行,[x1, x2) 之间的坐标填充为 ch
  646. void drawRow(int y, int x1, int x2, char ch)
  647. {
  648. SetPos(x1,y);
  649. for(int i = 0; i <= (x2-x1); i++)
  650. cout<<ch;
  651. }
  652. //在a, b 纵坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
  653. void drawRow(COORD a, COORD b, char ch)
  654. {
  655. if(a.Y == b.Y)
  656. drawRow(a.Y, a.X, b.X, ch);
  657. else
  658. {
  659. SetPos(0, 25);
  660. cout<<"error code 01:无法填充行,因为两个坐标的纵坐标(x)不相等";
  661. system("pause");
  662. }
  663. }
  664. //把第x列,[y1, y2] 之间的坐标填充为 ch
  665. void drawCol(int x, int y1, int y2, char ch)
  666. {
  667. int y=y1;
  668. while(y!=y2+1)
  669. {
  670. SetPos(x, y);
  671. cout<<ch;
  672. y++;
  673. }
  674. }
  675. //在a, b 横坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
  676. void drawCol(COORD a, COORD b, char ch)
  677. {
  678. if(a.X == b.X)
  679. drawCol(a.X, a.Y, b.Y, ch);
  680. else
  681. {
  682. SetPos(0, 25);
  683. cout<<"error code 02:无法填充列,因为两个坐标的横坐标(y)不相等";
  684. system("pause");
  685. }
  686. }
  687. //左上角坐标、右下角坐标、用row填充行、用col填充列
  688. void drawFrame(COORD a, COORD b, char row, char col)
  689. {
  690. drawRow(a.Y, a.X+1, b.X-1, row);
  691. drawRow(b.Y, a.X+1, b.X-1, row);
  692. drawCol(a.X, a.Y+1, b.Y-1, col);
  693. drawCol(b.X, a.Y+1, b.Y-1, col);
  694. }
  695. void drawFrame(int x1, int y1, int x2, int y2, char row, char col)
  696. {
  697. COORD a={ x1, y1};
  698. COORD b={ x2, y2};
  699. drawFrame(a, b, row, col);
  700. }
  701. void drawFrame(Frame frame, char row, char col)
  702. {
  703. COORD a = frame.position[0];
  704. COORD b = frame.position[1];
  705. drawFrame(a, b, row, col);
  706. }
  707. void drawPlaying()
  708. {
  709. drawFrame(0, 0, 48, 24, '=', '|');// draw map frame;
  710. drawFrame(49, 0, 79, 4, '-', '|');// draw output frame
  711. drawFrame(49, 4, 79, 9, '-', '|');// draw score frame
  712. drawFrame(49, 9, 79, 20, '-', '|');// draw operate frame
  713. drawFrame(49, 20, 79, 24, '-', '|');// draw other message frame
  714. SetPos(52, 6);
  715. cout<<"得分:";
  716. SetPos(52, 7);
  717. cout<<"称号:";
  718. SetPos(52,10);
  719. cout<<"操作方式:";
  720. SetPos(52,12);
  721. cout<<" a,s,d,w 控制战机移动。";
  722. SetPos(52,14);
  723. cout<<" p 暂停游戏。";
  724. SetPos(52,16);
  725. cout<<" e 退出游戏。";
  726. SetPos(52,18);
  727. cout<<" k攻击。";
  728. }
  729. //在[a, b)之间产生一个随机整数
  730. int random(int a, int b)
  731. {
  732. int c=(rand() % (a-b))+ a;
  733. return c;
  734. }
  735. //在两个坐标包括的矩形框内随机产生一个坐标
  736. COORD random(COORD a, COORD b)
  737. {
  738. int x=random(a.X, b.X);
  739. int y=random(a.Y, b.Y);
  740. COORD c={ x, y};
  741. return c;
  742. }
  743. bool judgeCoordInFrame(Frame frame, COORD spot)
  744. {
  745. if(spot.X>=frame.position[0].X)
  746. if(spot.X<=frame.position[1].X)
  747. if(spot.Y>=frame.position[0].Y)
  748. if(spot.Y<=frame.position[0].Y)
  749. return true;
  750. return false;
  751. }
  752. void printCoord(COORD a)
  753. {
  754. cout <<"( "<<a.X<<" , "<<a.Y<<" )";
  755. }
  756. void printFrameCoord(Frame a)
  757. {
  758. printCoord(a.position[0]);
  759. cout <<" - ";
  760. printCoord(a.position[1]);
  761. }
  762. int drawMenu()
  763. {
  764. SetPos(30, 1);
  765. cout<<"P l a n e W a r";
  766. drawRow(3, 0, 79, '-');
  767. drawRow(5, 0, 79, '-');
  768. SetPos(28, 4);
  769. cout<<"w 和 s 选择, k 确定";
  770. SetPos(15, 11);
  771. cout<<"1. 简单的敌人";
  772. SetPos(15, 13);
  773. cout<<"2. 冷酷的敌人";
  774. drawRow(20, 0, 79, '-');
  775. drawRow(22, 0, 79, '-');
  776. SetPos(47, 11);
  777. cout<<"简单的敌人:";
  778. SetPos(51, 13);
  779. cout<<"简单敌人有着较慢的移动速度。";
  780. SetPos(24, 21);
  781. int j=11;
  782. SetPos(12, j);
  783. cout<<">>";
  784. //drawFrame(45, 9, 79, 17, '=', '|');
  785. while(1)
  786. { if( _kbhit() )
  787. {
  788. char x=_getch();
  789. switch (x)
  790. {
  791. case 'w' :
  792. {
  793. if( j == 13)
  794. {
  795. SetPos(12, j);
  796. cout<<" ";
  797. j = 11;
  798. SetPos(12, j);
  799. cout<<">>";
  800. SetPos(51, 13);
  801. cout<<"            ";
  802. SetPos(47, 11);
  803. cout<<"简单的敌人:";
  804. SetPos(51, 13);
  805. cout<<"简单敌人有着较慢的移动速度,比较容易对付";
  806. }
  807. break;
  808. }
  809. case 's' :
  810. {
  811. if( j == 11 )
  812. {
  813. SetPos(12, j);
  814. cout<<" ";
  815. j = 13;
  816. SetPos(12, j);
  817. cout<<">>";
  818. SetPos(51, 13);
  819. cout<<"              ";
  820. SetPos(47, 11);
  821. cout<<"冷酷的敌人:";
  822. SetPos(51, 13);
  823. cout<<"冷酷的敌人移动速度较快,难对付哟。";
  824. }
  825. break;
  826. }
  827. case 'k' :
  828. {
  829. if (j == 8) return 1;
  830. else return 2;
  831. }
  832. }
  833. }
  834. }
  835. }
  836. /* DWORD WINAPI MusicFun(LPVOID lpParamte) { //DWORD OBJ; sndPlaySound(TEXT("bgm.wav"), SND_FILENAME|SND_ASYNC); return 0; } */
  837. /*================== the Game Class ==================*/
  838. class Game
  839. {
  840. public:
  841. COORD position[10];
  842. COORD bullet[10];
  843. Frame enemy[8];
  844. int score;
  845. int rank;
  846. int rankf;
  847. string title;
  848. int flag_rank;
  849. Game ();
  850. //初始化所有
  851. void initPlane();
  852. void initBullet();
  853. void initEnemy();
  854. //初始化其中一个
  855. //void initThisBullet( COORD );
  856. //void initThisEnemy( Frame );
  857. void planeMove(char);
  858. void bulletMove();
  859. void enemyMove();
  860. //填充所有
  861. void drawPlane();
  862. void drawPlaneToNull();
  863. void drawBullet();
  864. void drawBulletToNull();
  865. void drawEnemy();
  866. void drawEnemyToNull();
  867. //填充其中一个
  868. void drawThisBulletToNull( COORD );
  869. void drawThisEnemyToNull( Frame );
  870. void Pause();
  871. void Playing();
  872. void judgePlane();
  873. void judgeEnemy();
  874. void Shoot();
  875. void GameOver();
  876. void printScore();
  877. };
  878. Game::Game()
  879. {
  880. initPlane();
  881. initBullet();
  882. initEnemy();
  883. score = 0;
  884. rank = 25;
  885. rankf = 0;
  886. flag_rank = 0;
  887. }
  888. void Game::initPlane()
  889. {
  890. COORD centren={ 39, 22};
  891. position[0].X=position[5].X=position[7].X=position[9].X=centren.X;
  892. position[1].X=centren.X-2;
  893. position[2].X=position[6].X=centren.X-1;
  894. position[3].X=position[8].X=centren.X+1;
  895. position[4].X=centren.X+2;
  896. for(int i=0; i<=4; i++)
  897. position[i].Y=centren.Y;
  898. for(int i=6; i<=8; i++)
  899. position[i].Y=centren.Y+1;
  900. position[5].Y=centren.Y-1;
  901. position[9].Y=centren.Y-2;
  902. }
  903. void Game::drawPlane()
  904. {
  905. for(int i=0; i<9; i++)
  906. {
  907. SetPos(position[i]);
  908. if(i!=5)
  909. cout<<"@";
  910. else if(i==5)
  911. cout<<"|";
  912. }
  913. }
  914. void Game::drawPlaneToNull()
  915. {
  916. for(int i=0; i<9; i++)
  917. {
  918. SetPos(position[i]);
  919. cout<<" ";
  920. }
  921. }
  922. void Game::initBullet()
  923. {
  924. for(int i=0; i<10; i++)
  925. bullet[i].Y = 30;
  926. }
  927. void Game::drawBullet()
  928. {
  929. for(int i=0; i<10; i++)
  930. {
  931. if( bullet[i].Y != 30)
  932. {
  933. SetPos(bullet[i]);
  934. cout<<".";
  935. }
  936. }
  937. }
  938. void Game::drawBulletToNull()
  939. {
  940. for(int i=0; i<10; i++)
  941. if( bullet[i].Y != 30 )
  942. {
  943. COORD pos={ bullet[i].X, bullet[i].Y+1};
  944. SetPos(pos);
  945. cout<<" ";
  946. }
  947. }
  948. void Game::initEnemy()
  949. {
  950. COORD a={ 1, 1};
  951. COORD b={ 45, 15};
  952. for(int i=0; i<8; i++)
  953. {
  954. enemy[i].position[0] = random(a, b);
  955. enemy[i].position[1].X = enemy[i].position[0].X + 3;
  956. enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
  957. }
  958. }
  959. void Game::drawEnemy()
  960. {
  961. for(int i=0; i<8; i++)
  962. drawFrame(enemy[i].position[0], enemy[i].position[1], '-', '|');
  963. }
  964. void Game::drawEnemyToNull()
  965. {
  966. for(int i=0; i<8; i++)
  967. {
  968. drawFrame(enemy[i].position[0], enemy[i].position[1], ' ', ' ');
  969. }
  970. }
  971. void Game::Pause()
  972. {
  973. SetPos(61,2);
  974. cout<<" ";
  975. SetPos(61,2);
  976. cout<<"暂停中...";
  977. char c=_getch();
  978. while(c!='p')
  979. c=_getch();
  980. SetPos(61,2);
  981. cout<<" ";
  982. }
  983. void Game::planeMove(char x)
  984. {
  985. if(x == 'a')
  986. if(position[1].X != 1)
  987. for(int i=0; i<=9; i++)
  988. position[i].X -= 2;
  989. if(x == 's')
  990. if(position[7].Y != 23)
  991. for(int i=0; i<=9; i++)
  992. position[i].Y += 1;
  993. if(x == 'd')
  994. if(position[4].X != 47)
  995. for(int i=0; i<=9; i++)
  996. position[i].X += 2;
  997. if(x == 'w')
  998. if(position[5].Y != 3)
  999. for(int i=0; i<=9; i++)
  1000. position[i].Y -= 1;
  1001. }
  1002. void Game::bulletMove()
  1003. {
  1004. for(int i=0; i<10; i++)
  1005. {
  1006. if( bullet[i].Y != 30)
  1007. {
  1008. bullet[i].Y -= 1;
  1009. if( bullet[i].Y == 1 )
  1010. {
  1011. COORD pos={ bullet[i].X, bullet[i].Y+1};
  1012. drawThisBulletToNull( pos );
  1013. bullet[i].Y=30;
  1014. }
  1015. }
  1016. }
  1017. }
  1018. void Game::enemyMove()
  1019. {
  1020. for(int i=0; i<8; i++)
  1021. {
  1022. for(int j=0; j<2; j++)
  1023. enemy[i].position[j].Y++;
  1024. if(24 == enemy[i].position[1].Y)
  1025. {
  1026. COORD a={ 1, 1};
  1027. COORD b={ 45, 3};
  1028. enemy[i].position[0] = random(a, b);
  1029. enemy[i].position[1].X = enemy[i].position[0].X + 3;
  1030. enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
  1031. }
  1032. }
  1033. }
  1034. void Game::judgePlane()
  1035. {
  1036. for(int i = 0; i < 8; i++)
  1037. for(int j=0; j<9; j++)
  1038. if(judgeCoordInFrame(enemy[i], position[j]))
  1039. {
  1040. SetPos(62, 1);
  1041. cout<<"坠毁";
  1042. drawFrame(enemy[i], '+', '+');
  1043. Sleep(1000);
  1044. GameOver();
  1045. break;
  1046. }
  1047. }
  1048. void Game::drawThisBulletToNull( COORD c)
  1049. {
  1050. SetPos(c);
  1051. cout<<" ";
  1052. }
  1053. void Game::drawThisEnemyToNull( Frame f )
  1054. {
  1055. drawFrame(f, ' ', ' ');
  1056. }
  1057. void Game::judgeEnemy()
  1058. {
  1059. for(int i = 0; i < 8; i++)
  1060. for(int j = 0; j < 10; j++)
  1061. if( judgeCoordInFrame(enemy[i], bullet[j]) )
  1062. {
  1063. score += 5;
  1064. drawThisEnemyToNull( enemy[i] );
  1065. COORD a={ 1, 1};
  1066. COORD b={ 45, 3};
  1067. enemy[i].position[0] = random(a, b);
  1068. enemy[i].position[1].X = enemy[i].position[0].X + 3;
  1069. enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
  1070. drawThisBulletToNull( bullet[j] );
  1071. bullet[j].Y = 30;
  1072. }
  1073. }
  1074. void Game::Shoot()
  1075. {
  1076. for(int i=0; i<10; i++)
  1077. if(bullet[i].Y == 30)
  1078. {
  1079. bullet[i].X = position[5].X;
  1080. bullet[i].Y = position[5].Y-1;
  1081. break;
  1082. }
  1083. }
  1084. void Game::printScore()
  1085. {
  1086. if(score == 120 && flag_rank == 0)
  1087. {
  1088. rank -= 3;
  1089. flag_rank = 1;
  1090. }
  1091. else if( score == 360 && flag_rank == 1)
  1092. {
  1093. rank -= 5;
  1094. flag_rank = 2;
  1095. }
  1096. else if( score == 480 && flag_rank == 2)
  1097. {
  1098. rank -= 5;
  1099. flag_rank = 3;
  1100. }
  1101. int x=rank/5;
  1102. SetPos(60, 6);
  1103. cout<<score;
  1104. if( rank!=rankf )
  1105. {
  1106. SetPos(60, 7);
  1107. if( x == 5)
  1108. title="初级飞行员";
  1109. else if( x == 4)
  1110. title="中级飞行员";
  1111. else if( x == 3)
  1112. title="高级飞行员";
  1113. else if( x == 2 )
  1114. title="王牌飞行员";
  1115. cout<<title;
  1116. }
  1117. rankf = rank;
  1118. }
  1119. void Game::Playing()
  1120. {
  1121. //HANDLE MFUN;
  1122. //MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL);
  1123. drawEnemy();
  1124. drawPlane();
  1125. int flag_bullet = 0;
  1126. int flag_enemy = 0;
  1127. while(1)
  1128. {
  1129. Sleep(8);
  1130. if(_kbhit())
  1131. {
  1132. char x = _getch();
  1133. if ('a' == x || 's' == x || 'd' == x || 'w' == x)
  1134. {
  1135. drawPlaneToNull();
  1136. planeMove(x);
  1137. drawPlane();
  1138. judgePlane();
  1139. }
  1140. else if ('p' == x)
  1141. Pause();
  1142. else if( 'k' == x)
  1143. Shoot();
  1144. else if( 'e' == x)
  1145. {
  1146. //CloseHandle(MFUN);
  1147. GameOver();
  1148. break;
  1149. }
  1150. }
  1151. /* 处理子弹 */
  1152. if( 0 == flag_bullet )
  1153. {
  1154. bulletMove();
  1155. drawBulletToNull();
  1156. drawBullet();
  1157. judgeEnemy();
  1158. }
  1159. flag_bullet++;
  1160. if( 5 == flag_bullet )
  1161. flag_bullet = 0;
  1162. /* 处理敌人 */
  1163. if( 0 == flag_enemy )
  1164. {
  1165. drawEnemyToNull();
  1166. enemyMove();
  1167. drawEnemy();
  1168. judgePlane();
  1169. }
  1170. flag_enemy++;
  1171. if( flag_enemy >= rank )
  1172. flag_enemy = 0;
  1173. /* 输出得分 */
  1174. printScore();
  1175. }
  1176. }
  1177. void Game::GameOver()
  1178. {
  1179. system("cls");
  1180. COORD p1={ 28,9};
  1181. COORD p2={ 53,15};
  1182. drawFrame(p1, p2, '=', '|');
  1183. SetPos(36,12);
  1184. string str="Game Over!";
  1185. for(int i=0; i<str.size(); i++)
  1186. {
  1187. Sleep(80);
  1188. cout<<str[i];
  1189. }
  1190. Sleep(1000);
  1191. system("cls");
  1192. drawFrame(p1, p2, '=', '|');
  1193. SetPos(31, 11);
  1194. cout<<"击落敌机:"<<score/5<<" 架";
  1195. SetPos(31, 12);
  1196. cout<<"得  分:"<<score;
  1197. SetPos(31, 13);
  1198. cout<<"获得称号:"<<title;
  1199. SetPos(30, 16);
  1200. Sleep(1000);
  1201. cout<<"继续? 是(y)| 否(n)";
  1202. as:
  1203. char x=_getch();
  1204. if (x == 'n')
  1205. exit(0);
  1206. else if (x == 'y')
  1207. {
  1208. system("cls");
  1209. Game game;
  1210. int a = drawMenu();
  1211. if(a == 2)
  1212. game.rank = 20;
  1213. system("cls");
  1214. drawPlaying();
  1215. game.Playing();
  1216. }
  1217. else goto as;
  1218. }
  1219. /*================== the main function ==================*/
  1220. int main()
  1221. {
  1222. //游戏准备
  1223. srand((int)time(0)); //随机种子
  1224. HideCursor(); //隐藏光标
  1225. Game game;
  1226. int a = drawMenu();
  1227. if(a == 2)
  1228. game.rank = 20;
  1229. system("cls");
  1230. drawPlaying();
  1231. game.Playing();
  1232. }

发表评论

表情:
评论列表 (有 0 条评论,278人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java飞机

    一、项目分析 1、设计步骤 在一个Java Swing的小游戏中,首先需要考虑的是组件的绘制,其次是如何让需要运动的组件运动起来,最后是如何检测物体的碰撞等。 (1)