当前位置:   article > 正文

6个C++游戏代码,Dev-C++都可以运行,可复制。_代码大全可复制免费

代码大全可复制免费

Dev-C++很局限,但还是有很多大佬愿意用它写游戏。

今天我就为大家奉上6个Dev-C++游戏代码,亲测可编译运行!

先来个最简单的,猜猜数字:

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. #include<windows.h>
  5. using namespace std;
  6. int main(){
  7. srand(time(0));
  8. int n=rand()%1000;
  9. int c=-1,ans=0;
  10. cout<<"你要猜的数字为:***"<<endl<<endl;
  11. while(c!=n){
  12. cout<<"你猜的数是:";
  13. cin>>c;
  14. ans++;
  15. if(c>n){
  16. Sleep(500);
  17. cout<<"大了!"<<endl<<endl;
  18. } else if(c<n){
  19. Sleep(500);
  20. cout<<"小了!"<<endl<<endl;
  21. }
  22. }
  23. cout<<"恭喜猜中!!!"<<endl<<"共用了"<<ans<<"次!!!"<<endl;
  24. system("pause");
  25. return 0;
  26. }

是不是很简单?下一个,飞机大战:

  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. }
  111. //在[a, b)之间产生一个随机整数
  112. int random(int a, int b)
  113. {
  114. int c=(rand() % (a-b))+ a;
  115. return c;
  116. }
  117. //在两个坐标包括的矩形框内随机产生一个坐标
  118. COORD random(COORD a, COORD b)
  119. {
  120. int x=random(a.X, b.X);
  121. int y=random(a.Y, b.Y);
  122. COORD c={x, y};
  123. return c;
  124. }
  125. bool judgeCoordInFrame(Frame frame, COORD spot)
  126. {
  127. if(spot.X>=frame.position[0].X)
  128. if(spot.X<=frame.position[1].X)
  129. if(spot.Y>=frame.position[0].Y)
  130. if(spot.Y<=frame.position[0].Y)
  131. return true;
  132. return false;
  133. }
  134. void printCoord(COORD a)
  135. {
  136. cout <<"( "<<a.X<<" , "<<a.Y<<" )";
  137. }
  138. void printFrameCoord(Frame a)
  139. {
  140. printCoord(a.position[0]);
  141. cout <<" - ";
  142. printCoord(a.position[1]);
  143. }
  144. int drawMenu()
  145. {
  146. SetPos(30, 1);
  147. cout<<"P l a n e W a r";
  148. drawRow(3, 0, 79, '-');
  149. drawRow(5, 0, 79, '-');
  150. SetPos(28, 4);
  151. cout<<"w 和 s 选择, k 确定";
  152. SetPos(15, 11);
  153. cout<<"1. 简单的敌人";
  154. SetPos(15, 13);
  155. cout<<"2. 冷酷的敌人";
  156. drawRow(20, 0, 79, '-');
  157. drawRow(22, 0, 79, '-');
  158. SetPos(47, 11);
  159. cout<<"简单的敌人:";
  160. SetPos(51, 13);
  161. cout<<"简单敌人有着较慢的移动速度。";
  162. SetPos(24, 21);
  163. cout<<"制作:LJF神犇";
  164. int j=11;
  165. SetPos(12, j);
  166. cout<<">>";
  167. //drawFrame(45, 9, 79, 17, '=', '|');
  168. while(1)
  169. { if( _kbhit() )
  170. {
  171. char x=_getch();
  172. switch (x)
  173. {
  174. case 'w' :
  175. {
  176. if( j == 13)
  177. {
  178. SetPos(12, j);
  179. cout<<" ";
  180. j = 11;
  181. SetPos(12, j);
  182. cout<<">>";
  183. SetPos(51, 13);
  184. cout<<"            ";
  185. SetPos(47, 11);
  186. cout<<"简单的敌人:";
  187. SetPos(51, 13);
  188. cout<<"简单敌人有着较慢的移动速度,容易对付。";
  189. }
  190. break;
  191. }
  192. case 's' :
  193. {
  194. if( j == 11 )
  195. {
  196. SetPos(12, j);
  197. cout<<" ";
  198. j = 13;
  199. SetPos(12, j);
  200. cout<<">>";
  201. SetPos(51, 13);
  202. cout<<"              ";
  203. SetPos(47, 11);
  204. cout<<"冷酷的敌人:";
  205. SetPos(51, 13);
  206. cout<<"冷酷的敌人移动速度较快,难对付哟!!!";
  207. }
  208. break;
  209. }
  210. case 'k' :
  211. {
  212. if (j == 8) return 1;
  213. else return 2;
  214. }
  215. }
  216. }
  217. }
  218. }
  219. /*
  220. DWORD WINAPI MusicFun(LPVOID lpParamte)
  221. {
  222. //DWORD OBJ;
  223. sndPlaySound(TEXT("bgm.wav"), SND_FILENAME|SND_ASYNC);
  224. return 0;
  225. }
  226. */
  227. /*================== the Game Class ==================*/
  228. class Game
  229. {
  230. public:
  231. COORD position[10];
  232. COORD bullet[10];
  233. Frame enemy[8];
  234. int score;
  235. int rank;
  236. int rankf;
  237. string title;
  238. int flag_rank;
  239. Game ();
  240. //初始化所有
  241. void initPlane();
  242. void initBullet();
  243. void initEnemy();
  244. //初始化其中一个
  245. //void initThisBullet( COORD );
  246. //void initThisEnemy( Frame );
  247. void planeMove(char);
  248. void bulletMove();
  249. void enemyMove();
  250. //填充所有
  251. void drawPlane();
  252. void drawPlaneToNull();
  253. void drawBullet();
  254. void drawBulletToNull();
  255. void drawEnemy();
  256. void drawEnemyToNull();
  257. //填充其中一个
  258. void drawThisBulletToNull( COORD );
  259. void drawThisEnemyToNull( Frame );
  260. void Pause();
  261. void Playing();
  262. void judgePlane();
  263. void judgeEnemy();
  264. void Shoot();
  265. void GameOver();
  266. void printScore();
  267. };
  268. Game::Game()
  269. {
  270. initPlane();
  271. initBullet();
  272. initEnemy();
  273. score = 0;
  274. rank = 25;
  275. rankf = 0;
  276. flag_rank = 0;
  277. }
  278. void Game::initPlane()
  279. {
  280. COORD centren={39, 22};
  281. position[0].X=position[5].X=position[7].X=position[9].X=centren.X;
  282. position[1].X=centren.X-2;
  283. position[2].X=position[6].X=centren.X-1;
  284. position[3].X=position[8].X=centren.X+1;
  285. position[4].X=centren.X+2;
  286. for(int i=0; i<=4; i++)
  287. position[i].Y=centren.Y;
  288. for(int i=6; i<=8; i++)
  289. position[i].Y=centren.Y+1;
  290. position[5].Y=centren.Y-1;
  291. position[9].Y=centren.Y-2;
  292. }
  293. void Game::drawPlane()
  294. {
  295. for(int i=0; i<9; i++)
  296. {
  297. SetPos(position[i]);
  298. if(i!=5)
  299. cout<<"O";
  300. else if(i==5)
  301. cout<<"|";
  302. }
  303. }
  304. void Game::drawPlaneToNull()
  305. {
  306. for(int i=0; i<9; i++)
  307. {
  308. SetPos(position[i]);
  309. cout<<" ";
  310. }
  311. }
  312. void Game::initBullet()
  313. {
  314. for(int i=0; i<10; i++)
  315. bullet[i].Y = 30;
  316. }
  317. void Game::drawBullet()
  318. {
  319. for(int i=0; i<10; i++)
  320. {
  321. if( bullet[i].Y != 30)
  322. {
  323. SetPos(bullet[i]);
  324. cout<<"^";
  325. }
  326. }
  327. }
  328. void Game::drawBulletToNull()
  329. {
  330. for(int i=0; i<10; i++)
  331. if( bullet[i].Y != 30 )
  332. {
  333. COORD pos={bullet[i].X, bullet[i].Y+1};
  334. SetPos(pos);
  335. cout<<" ";
  336. }
  337. }
  338. void Game::initEnemy()
  339. {
  340. COORD a={1, 1};
  341. COORD b={45, 15};
  342. for(int i=0; i<8; i++)
  343. {
  344. enemy[i].position[0] = random(a, b);
  345. enemy[i].position[1].X = enemy[i].position[0].X + 3;
  346. enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
  347. }
  348. }
  349. void Game::drawEnemy()
  350. {
  351. for(int i=0; i<8; i++)
  352. drawFrame(enemy[i].position[0], enemy[i].position[1], '-', '|');
  353. }
  354. void Game::drawEnemyToNull()
  355. {
  356. for(int i=0; i<8; i++)
  357. {
  358. drawFrame(enemy[i].position[0], enemy[i].position[1], ' ', ' ');
  359. }
  360. }
  361. void Game::Pause()
  362. {
  363. SetPos(61,2);
  364. cout<<" ";
  365. SetPos(61,2);
  366. cout<<"暂停中...";
  367. char c=_getch();
  368. while(c!='p')
  369. c=_getch();
  370. SetPos(61,2);
  371. cout<<" ";
  372. }
  373. void Game::planeMove(char x)
  374. {
  375. if(x == 'a')
  376. if(position[1].X != 1)
  377. for(int i=0; i<=9; i++)
  378. position[i].X -= 2;
  379. if(x == 's')
  380. if(position[7].Y != 23)
  381. for(int i=0; i<=9; i++)
  382. position[i].Y += 1;
  383. if(x == 'd')
  384. if(position[4].X != 47)
  385. for(int i=0; i<=9; i++)
  386. position[i].X += 2;
  387. if(x == 'w')
  388. if(position[5].Y != 3)
  389. for(int i=0; i<=9; i++)
  390. position[i].Y -= 1;
  391. }
  392. void Game::bulletMove()
  393. {
  394. for(int i=0; i<10; i++)
  395. {
  396. if( bullet[i].Y != 30)
  397. {
  398. bullet[i].Y -= 1;
  399. if( bullet[i].Y == 1 )
  400. {
  401. COORD pos={bullet[i].X, bullet[i].Y+1};
  402. drawThisBulletToNull( pos );
  403. bullet[i].Y=30;
  404. }
  405. }
  406. }
  407. }
  408. void Game::enemyMove()
  409. {
  410. for(int i=0; i<8; i++)
  411. {
  412. for(int j=0; j<2; j++)
  413. enemy[i].position[j].Y++;
  414. if(24 == enemy[i].position[1].Y)
  415. {
  416. COORD a={1, 1};
  417. COORD b={45, 3};
  418. enemy[i].position[0] = random(a, b);
  419. enemy[i].position[1].X = enemy[i].position[0].X + 3;
  420. enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
  421. }
  422. }
  423. }
  424. void Game::judgePlane()
  425. {
  426. for(int i = 0; i < 8; i++)
  427. for(int j=0; j<9; j++)
  428. if(judgeCoordInFrame(enemy[i], position[j]))
  429. {
  430. SetPos(62, 1);
  431. cout<<"坠毁";
  432. drawFrame(enemy[i], '+', '+');
  433. Sleep(1000);
  434. GameOver();
  435. break;
  436. }
  437. }
  438. void Game::drawThisBulletToNull( COORD c)
  439. {
  440. SetPos(c);
  441. cout<<" ";
  442. }
  443. void Game::drawThisEnemyToNull( Frame f )
  444. {
  445. drawFrame(f, ' ', ' ');
  446. }
  447. void Game::judgeEnemy()
  448. {
  449. for(int i = 0; i < 8; i++)
  450. for(int j = 0; j < 10; j++)
  451. if( judgeCoordInFrame(enemy[i], bullet[j]) )
  452. {
  453. score += 5;
  454. drawThisEnemyToNull( enemy[i] );
  455. COORD a={1, 1};
  456. COORD b={45, 3};
  457. enemy[i].position[0] = random(a, b);
  458. enemy[i].position[1].X = enemy[i].position[0].X + 3;
  459. enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
  460. drawThisBulletToNull( bullet[j] );
  461. bullet[j].Y = 30;
  462. }
  463. }
  464. void Game::Shoot()
  465. {
  466. for(int i=0; i<10; i++)
  467. if(bullet[i].Y == 30)
  468. {
  469. bullet[i].X = position[5].X;
  470. bullet[i].Y = position[5].Y-1;
  471. break;
  472. }
  473. }
  474. void Game::printScore()
  475. {
  476. if(score == 120 && flag_rank == 0)
  477. {
  478. rank -= 3;
  479. flag_rank = 1;
  480. }
  481. else if( score == 360 && flag_rank == 1)
  482. {
  483. rank -= 5;
  484. flag_rank = 2;
  485. }
  486. else if( score == 480 && flag_rank == 2)
  487. {
  488. rank -= 5;
  489. flag_rank = 3;
  490. }
  491. int x=rank/5;
  492. SetPos(60, 6);
  493. cout<<score;
  494. if( rank!=rankf )
  495. {
  496. SetPos(60, 7);
  497. if( x == 5)
  498. title="初级飞行员";
  499. else if( x == 4)
  500. title="中级飞行员";
  501. else if( x == 3)
  502. title="高级飞行员";
  503. else if( x == 2 )
  504. title="王牌飞行员";
  505. cout<<title;
  506. }
  507. rankf = rank;
  508. }
  509. void Game::Playing()
  510. {
  511. //HANDLE MFUN;
  512. //MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL);
  513. drawEnemy();
  514. drawPlane();
  515. int flag_bullet = 0;
  516. int flag_enemy = 0;
  517. while(1)
  518. {
  519. Sleep(8);
  520. if(_kbhit())
  521. {
  522. char x = _getch();
  523. if ('a' == x || 's' == x || 'd' == x || 'w' == x)
  524. {
  525. drawPlaneToNull();
  526. planeMove(x);
  527. drawPlane();
  528. judgePlane();
  529. }
  530. else if ('p' == x)
  531. Pause();
  532. else if( 'k' == x)
  533. Shoot();
  534. else if( 'e' == x)
  535. {
  536. //CloseHandle(MFUN);
  537. GameOver();
  538. break;
  539. }
  540. }
  541. /* 处理子弹 */
  542. if( 0 == flag_bullet )
  543. {
  544. bulletMove();
  545. drawBulletToNull();
  546. drawBullet();
  547. judgeEnemy();
  548. }
  549. flag_bullet++;
  550. if( 5 == flag_bullet )
  551. flag_bullet = 0;
  552. /* 处理敌人 */
  553. if( 0 == flag_enemy )
  554. {
  555. drawEnemyToNull();
  556. enemyMove();
  557. drawEnemy();
  558. judgePlane();
  559. }
  560. flag_enemy++;
  561. if( flag_enemy >= rank )
  562. flag_enemy = 0;
  563. /* 输出得分 */
  564. printScore();
  565. }
  566. }
  567. void Game::GameOver()
  568. {
  569. system("cls");
  570. COORD p1={28,9};
  571. COORD p2={53,15};
  572. drawFrame(p1, p2, '=', '|');
  573. SetPos(36,12);
  574. string str="Game Over!";
  575. for(int i=0; i<str.size(); i++)
  576. {
  577. Sleep(80);
  578. cout<<str[i];
  579. }
  580. Sleep(1000);
  581. system("cls");
  582. drawFrame(p1, p2, '=', '|');
  583. SetPos(31, 11);
  584. cout<<"击落敌机:"<<score/5<<" 架";
  585. SetPos(31, 12);
  586. cout<<"得  分:"<<score;
  587. SetPos(31, 13);
  588. cout<<"获得称号:"<<title;
  589. SetPos(30, 16);
  590. Sleep(1000);
  591. cout<<"继续? 是(y)| 否(n)制作:最牛的LJF";
  592. as:
  593. char x=_getch();
  594. if (x == 'n')
  595. exit(0);
  596. else if (x == 'y')
  597. {
  598. system("cls");
  599. Game game;
  600. int a = drawMenu();
  601. if(a == 2)
  602. game.rank = 20;
  603. system("cls");
  604. drawPlaying();
  605. game.Playing();
  606. }
  607. else goto as;
  608. }
  609. /*================== the main function ==================*/
  610. int main()
  611. {
  612. //游戏准备
  613. srand((int)time(0)); //随机种子
  614. HideCursor(); //隐藏光标
  615. Game game;
  616. int a = drawMenu();
  617. if(a == 2)
  618. game.rank = 20;
  619. system("cls");
  620. drawPlaying();
  621. game.Playing();
  622. }

好长啊,下一个,贪吃蛇:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <conio.h>
  6. #include <cmath>
  7. #include <windows.h>
  8. using namespace std;
  9. /*** 光标定位 ***/
  10. HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
  11. COORD coord;
  12. void locate(int x,int y)
  13. {
  14. coord.X=y;
  15. coord.Y=x;
  16. SetConsoleCursorPosition(hout,coord);
  17. };
  18. /*** 隐藏光标 ***/
  19. void hide()
  20. {
  21. CONSOLE_CURSOR_INFO cursor_info={1,0};
  22. SetConsoleCursorInfo(hout, &cursor_info);
  23. }
  24. /*** 生成随机数 ***/
  25. double random(double start, double end)
  26. {
  27. return start+(end-start)*rand()/(RAND_MAX + 1.0);
  28. }
  29. /*** 定义地图的长宽,蛇的坐标,长度,方向,食物的位置 ***/
  30. int m,n;
  31. struct node
  32. {
  33. int x,y;
  34. }snake[1000];
  35. int snake_length,dir;
  36. node food;
  37. int direct[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
  38. /*** 输出墙 ***/
  39. void print_wall()
  40. {
  41. cout << " ";
  42. for (int i=1;i<=n;i++)
  43. cout << "-";
  44. cout << endl;
  45. for (int j=0;j<=m-1;j++)
  46. {
  47. cout << "|";
  48. for (int i=1;i<=n;i++) cout << " ";
  49. cout << "|" << endl;
  50. }
  51. cout << " ";
  52. for (int i=1;i<=n;i++)
  53. cout << "-";
  54. }
  55. /*** 首次输出蛇,其中snake[0]代表头 ***/
  56. void print_snake()
  57. {
  58. locate(snake[0].x,snake[0].y);
  59. cout << "@";
  60. for (int i=1;i<=snake_length-1;i++)
  61. {
  62. locate(snake[i].x,snake[i].y);
  63. cout << "*";
  64. }
  65. }
  66. /*** 判断是否撞墙或者自撞 ***/
  67. bool is_correct()
  68. {
  69. if (snake[0].x==0 || snake[0].y==0 || snake[0].x==m+1 || snake[0].y==n+1) return false;
  70. for (int i=1;i<=snake_length-1;i++)
  71. {
  72. if (snake[0].x==snake[i].x && snake[0].y==snake[i].y) return false;
  73. }
  74. return true;
  75. }
  76. /*** 随机生成并输出食物位置 ***/
  77. bool print_food()
  78. {
  79. srand((unsigned)time(0));
  80. bool e;
  81. while (1)
  82. {
  83. e=true;
  84. int i=(int) random(0,m)+1,j=(int) random(0,n)+1;
  85. food.x=i;food.y=j;
  86. for (int k=0;k<=snake_length-1;k++)
  87. {
  88. if (snake[k].x==food.x && snake[k].y==food.y)
  89. {
  90. e=false;break;
  91. }
  92. }
  93. if (e) break;
  94. }
  95. locate(food.x,food.y);
  96. cout << "$";
  97. return true;
  98. }
  99. /*** 蛇的前进 ***/
  100. bool go_ahead()
  101. {
  102. node temp;
  103. bool e=false;
  104. temp=snake[snake_length-1];
  105. for (int i=snake_length-1;i>=1;i--)
  106. snake[i]=snake[i-1];
  107. snake[0].x+=direct[dir][0];
  108. snake[0].y+=direct[dir][1];
  109. locate(snake[1].x,snake[1].y);
  110. cout << "*";
  111. /*** 吃到了食物 ***/
  112. if (snake[0].x==food.x && snake[0].y==food.y)
  113. {
  114. snake_length++;
  115. e=true;
  116. snake[snake_length-1]=temp;
  117. }
  118. /*** 输出此时蛇状态 ***/
  119. if (!e)
  120. {
  121. locate(temp.x,temp.y);
  122. cout << " ";
  123. }
  124. else
  125. print_food();
  126. locate(snake[0].x,snake[0].y);
  127. cout << "@";
  128. /*** 如果自撞 ***/
  129. if (!is_correct())
  130. {
  131. system("cls");
  132. cout << "You lose!" << endl << "Length: " << snake_length << endl;
  133. return false;
  134. }
  135. return true;
  136. }
  137. /*** 主函数 ***/
  138. int main()
  139. {
  140. cout << "--------------------贪吃蛇---------------------" << endl;
  141. cout << "请注意窗口大小,以免发生错位.建议将窗口调为最大." << endl;
  142. cout << "先选择难度.请在1-10中输入1个数,1最简单,10则最难" << endl;
  143. cout << "然后进入游戏画面,以方向键控制方向.祝你游戏愉快!" << endl;
  144. cout << "-----------------------------------------------" << endl;
  145. m=25;
  146. n=40;
  147. if (m<10 || n<10 || m>25 || n>40)
  148. {
  149. cout << "ERROR" << endl;
  150. system("pause");
  151. return 0;
  152. }
  153. int hard;
  154. cin >> hard;
  155. if (hard<=0 || hard>100)
  156. {
  157. cout << "ERROR" << endl;
  158. system("pause");
  159. return 0;
  160. }
  161. /*** 数据全部初始化,包括蛇长,位置,方向 ***/
  162. snake_length=5;
  163. clock_t a,b;
  164. char ch;
  165. double hard_len;
  166. for (int i=0;i<=4;i++)
  167. {
  168. snake[i].x=1;
  169. snake[i].y=5-i;
  170. }
  171. dir=3;
  172. /*** 输出初始地图,蛇与食物 ***/
  173. system("cls");
  174. hide();
  175. print_wall();
  176. print_food();
  177. print_snake();
  178. locate(m+2,0);
  179. cout << "Now length: ";
  180. /*** 开始游戏 ***/
  181. while (1)
  182. {
  183. /*** 难度随长度增加而提高 ***/
  184. hard_len=(double)snake_length/(double) (m*n);
  185. /*** 调节时间,单位是ms ***/
  186. a=clock();
  187. while (1)
  188. {
  189. b=clock();
  190. if (b-a>=(int)(400-30*hard)*(1-sqrt(hard_len))) break;
  191. }
  192. /*** 接受键盘输入的上下左右,并以此改变方向 ***/
  193. if (kbhit())
  194. {
  195. ch=getch();
  196. if (ch==-32)
  197. {
  198. ch=getch();
  199. switch(ch)
  200. {
  201. case 72:
  202. if (dir==2 || dir==3)
  203. dir=0;
  204. break;
  205. case 80:
  206. if (dir==2 || dir==3)
  207. dir=1;
  208. break;
  209. case 75:
  210. if (dir==0 || dir==1)
  211. dir=2;
  212. break;
  213. case 77:
  214. if (dir==0 || dir==1)
  215. dir=3;
  216. break;
  217. }
  218. }
  219. }
  220. /*** 前进 ***/
  221. if (!go_ahead()) break;
  222. /*** 在最后输出此时长度 ***/
  223. locate(m+2,12);
  224. cout << snake_length;
  225. }
  226. system("pause");
  227. return 0;
  228. }

下一个,著名的2048:

  1. #include<iostream>
  2. #include<vector>
  3. #include<ctime>
  4. #include<cstdlib>
  5. using namespace std;
  6. class Game_2048
  7. {
  8. public:
  9. Game_2048();
  10. ~Game_2048();
  11. void introduction();
  12. bool judgeOver(); //判断游戏是否结束
  13. void reSize();
  14. void printBoard(); //打印函数
  15. void getRand(); //随机在棋盘上生成2,4;
  16. void slide(); //滑动
  17. private:
  18. int m=4, n=4;
  19. char op; //用户操作
  20. vector< vector<int> > board; //棋盘
  21. vector<int> row;
  22. bool judgeInsert(int x,int y);
  23. bool judgeSlide(); //判断是否能滑动,(未写完)
  24. void copyBoard(vector< vector<int> > &newBoard,vector< vector<int> > &board);
  25. void inputOp();
  26. char getOp(); //返回操作符
  27. bool judgeLeftSlide(bool mark=true);
  28. void leftSlide(); //左滑动
  29. bool judgeRightSlide(bool mark = true);
  30. void rightSlide();
  31. bool judgeUpSlide(bool mark = true);
  32. void upSlide();
  33. bool judgeDownSlide(bool mark = true);
  34. void downSlide();
  35. void reStart();
  36. void enlarge(); //将值扩大二倍
  37. };
  38. int main()
  39. {
  40. Game_2048 NB;
  41. NB.introduction();
  42. NB.getRand();
  43. NB.printBoard();
  44. while (!NB.judgeOver())
  45. {
  46. NB.slide();
  47. NB.getRand();
  48. NB.printBoard();
  49. }
  50. cout << "游戏结束!!!\n";
  51. system("pause");
  52. return 0;
  53. }
  54. void Game_2048::introduction()
  55. {
  56. cout << "这是一个2048游戏,规则如下:\n";
  57. cout << "上划:W;\n下滑:S;\n左划:A;\n右划:D;\n退出:Q;\n重新开始:R;\n请输入下次操作,\n";
  58. }
  59. void Game_2048::slide()
  60. {
  61. inputOp();
  62. switch (getOp())
  63. {
  64. case 'a':
  65. case 'A':
  66. if (judgeLeftSlide())
  67. do
  68. leftSlide();
  69. while (judgeLeftSlide(false));
  70. else
  71. {
  72. cout << "无法左滑动,请重试!!!\n";
  73. slide();
  74. }
  75. break;
  76. case 'd':
  77. case 'D':
  78. if (judgeRightSlide())
  79. do
  80. rightSlide();
  81. while (judgeRightSlide(false));
  82. else
  83. {
  84. cout << "无法右滑动,请重试!!!\n";
  85. slide();
  86. }
  87. break;
  88. case 'w':
  89. case 'W':
  90. if(judgeUpSlide())
  91. do
  92. upSlide();
  93. while (judgeUpSlide(false));
  94. else
  95. {
  96. cout << "无法上滑动,请重试!!!\n";
  97. slide();
  98. }
  99. break;
  100. case 's':
  101. case 'S':
  102. if(judgeDownSlide())
  103. do
  104. downSlide();
  105. while (judgeDownSlide(false));
  106. else
  107. {
  108. cout << "无法下滑动,请重试!!!\n";
  109. slide();
  110. }
  111. break;
  112. case 'p':
  113. case 'P':
  114. enlarge();
  115. break;
  116. case 'q':
  117. case 'Q':
  118. exit(0);
  119. break;
  120. case 'r':
  121. case 'R':
  122. reStart();
  123. break;
  124. default:
  125. cout << "输入错误,作为惩罚,随机生成一个数!\n";
  126. break;
  127. }
  128. }
  129. void Game_2048::reStart()
  130. {
  131. for (int i = 0; i < m; i++)
  132. for (int j = 0; j < n; j++) {
  133. board[i][j] = 0;
  134. }
  135. }
  136. void Game_2048::enlarge()
  137. {
  138. for (int i = 0; i < m; i++)
  139. for (int j = 0; j < n; j++)
  140. {
  141. board[i][j] *= 2;
  142. }
  143. }
  144. char Game_2048::getOp()
  145. {
  146. return op;
  147. }
  148. void Game_2048::upSlide()
  149. {
  150. for (int j = 0; j < n; j++)
  151. for (int i = m - 1; i > 0; i--) { //n-1!!
  152. if (board[i][j] != 0 && board[i - 1][j] == 0) //移动
  153. {
  154. board[i - 1][j] = board[i][j];
  155. board[i][j] = 0;
  156. }
  157. }
  158. for (int j = 0; j < n; j++)
  159. for (int i = m - 1; i > 0; i--) {
  160. if (board[i][j] != 0 && board[i-1][j] == board[i][j]) //覆盖
  161. {
  162. board[i-1][j] += board[i][j];
  163. board[i][j] = 0;
  164. }
  165. }
  166. }
  167. bool Game_2048::judgeUpSlide(bool mark)
  168. {
  169. if (mark)
  170. {
  171. for (int i = 0; i < m; i++)
  172. for (int j = n - 1; j > 0; j--)
  173. {
  174. if (board[i][j] == 0)
  175. return true;
  176. }
  177. }
  178. for (int j = 0; j < n; j++)
  179. for (int i = m - 1; i > 0; i--) { //n-1!!
  180. if (board[i][j] != 0 && board[i - 1][j] == 0) //移动
  181. return true;
  182. if (board[i][j] != 0 && board[i - 1][j] == board[i][j]) //覆盖
  183. return true;
  184. }
  185. return false;
  186. }
  187. bool Game_2048::judgeDownSlide(bool mark)
  188. {
  189. if (mark) {
  190. for (int i = 0; i < m; i++)
  191. for (int j = n - 1; j > 0; j--)
  192. {
  193. if (board[i][j] == 0)
  194. return true;
  195. }
  196. }
  197. for (int j = 0; j < n; j++)
  198. for (int i = 0; i < m - 1; i++) { //n-1!!
  199. if (board[i][j] != 0 && board[i + 1][j] == 0) //移动
  200. return true;
  201. if (board[i][j] != 0 && board[i + 1][j] == board[i][j]) //覆盖
  202. return true;
  203. }
  204. return false;
  205. }
  206. void Game_2048::downSlide()
  207. {
  208. for (int j = 0; j < n; j++)
  209. for (int i = 0; i < m - 1; i++) {
  210. if (board[i][j] != 0 && board[i + 1][j] == 0) //移动
  211. {
  212. board[i + 1][j] = board[i][j];
  213. board[i][j] = 0;
  214. }
  215. }
  216. for (int j = 0; j < n; j++)
  217. for (int i = 0; i < m - 1; i++) {
  218. if (board[i][j] != 0 && board[i + 1][j] == board[i][j]) //覆盖
  219. {
  220. board[i + 1][j] += board[i][j];
  221. board[i][j] = 0;
  222. }
  223. }
  224. }
  225. void Game_2048::rightSlide()
  226. {
  227. for (int i = 0; i < m; i++)
  228. for (int j = 0; j < n - 1; j++) { //n-1!!
  229. if (board[i][j] != 0 && board[i][j + 1] == 0) //移动
  230. {
  231. board[i][j + 1] = board[i][j];
  232. board[i][j] = 0;
  233. }
  234. }
  235. for (int i = 0; i < m; i++)
  236. for (int j = 0; j < n - 1; j++) {
  237. if (board[i][j] != 0 && board[i][j + 1] == board[i][j]) //覆盖
  238. {
  239. board[i][j + 1] += board[i][j];
  240. board[i][j] = 0;
  241. }
  242. }
  243. }
  244. bool Game_2048::judgeRightSlide(bool mark )
  245. {
  246. if (mark) {
  247. for (int i = 0; i < m; i++)
  248. for (int j = n - 1; j > 0; j--)
  249. {
  250. if (board[i][j] == 0)
  251. return true;
  252. }
  253. }
  254. for (int i = 0; i < m; i++)
  255. for (int j = 0; j < n - 1; j++) { //n-1!!
  256. if (board[i][j] != 0 && board[i][j + 1] == board[i][j]) //覆盖
  257. return true;
  258. if (board[i][j] != 0 && board[i][j + 1] == 0)
  259. return true;
  260. }
  261. return false;
  262. }
  263. void Game_2048::leftSlide()
  264. {
  265. for (int i = 0; i < m; i++)
  266. for (int j = 1; j < n; j++) { //n-1!!
  267. if (board[i][j] != 0 && board[i][j - 1] == 0) //移动
  268. {
  269. board[i][j - 1] = board[i][j];
  270. board[i][j] = 0;
  271. }
  272. }
  273. for (int i = 0; i < m; i++)
  274. for (int j = 1; j < n; j++) {
  275. if (board[i][j] != 0 && board[i][j - 1] == board[i][j]) //覆盖
  276. {
  277. board[i][j - 1] += board[i][j];
  278. board[i][j] = 0;
  279. }
  280. }
  281. }
  282. bool Game_2048::judgeLeftSlide(bool mark)
  283. {
  284. if (mark) {
  285. for (int i = 0; i < m; i++)
  286. for (int j = n - 1; j > 0; j--)
  287. {
  288. if (board[i][j] == 0)
  289. return true;
  290. }
  291. }
  292. for (int i = 0; i < m; i++)
  293. for (int j = n - 1; j > 0; j--) { //n-1!!
  294. if (board[i][j] != 0 && board[i][j - 1] == 0) //移动
  295. return true;
  296. if (board[i][j] != 0 && board[i][j - 1] == board[i][j]) //覆盖
  297. return true;
  298. }
  299. return false;
  300. }
  301. bool Game_2048::judgeOver()
  302. {
  303. if (op == 'q' || op == 'Q')
  304. return true;
  305. for (int i = 0; i < m; i++)
  306. for (int j = 0; j < n; j++) {
  307. if (board[i][j] == 2048)
  308. {
  309. printBoard();
  310. cout << "有数字达到了2048,恭喜!!!\n";
  311. return true;
  312. }
  313. }
  314. for (int i = 0; i < m; i++)
  315. for (int j = 0; j < n; j++) {
  316. if (board[i][j] == 0)
  317. return false;
  318. }
  319. if (judgeSlide())
  320. return false;
  321. else
  322. {
  323. cout << "无法再滑动\n";
  324. return true;
  325. }
  326. }
  327. bool Game_2048::judgeSlide()
  328. {
  329. vector< vector<int> > copyBoard; //棋盘
  330. vector<int> copyRow;
  331. for (int i = 0; i < n; i++) {
  332. copyRow.push_back(0);
  333. }
  334. for (int i = 0; i < m; i++) {
  335. copyBoard.push_back(copyRow);
  336. }
  337. copyBoard = board;
  338. upSlide();
  339. downSlide();
  340. leftSlide();
  341. rightSlide();
  342. for (int i = 0; i < m; i++)
  343. for (int j = 0; j < n; j++)
  344. {
  345. if (board[i][j] == 0) {
  346. board = copyBoard;
  347. return true;
  348. }
  349. }
  350. return false;
  351. }
  352. void Game_2048::copyBoard(vector< vector<int> >& newBoard, vector< vector<int> >& board)
  353. {
  354. for (int i = 0; i < m; i++)
  355. for (int j = 0; j < n; j++)
  356. newBoard[i][j] = board[i][j];
  357. }
  358. bool Game_2048::judgeInsert(int x,int y)
  359. {
  360. if (board[x][y] == 0)
  361. return true;
  362. else
  363. return false;
  364. }
  365. void Game_2048::getRand()
  366. {
  367. srand(time(0));
  368. int x, y,val;
  369. do
  370. {
  371. x = rand() % m;
  372. y = rand() % n;
  373. } while (!judgeInsert(x,y));
  374. val = (rand() % 2 + 1)*2;
  375. board[x][y] = val;
  376. }
  377. void Game_2048::inputOp()
  378. {
  379. cin >> op;
  380. }
  381. void Game_2048::reSize()
  382. {
  383. cout << "请输入棋盘大小m*n\n";
  384. cin >> m >> n;
  385. Game_2048();
  386. }
  387. Game_2048::~Game_2048()
  388. {
  389. }
  390. Game_2048::Game_2048()
  391. {
  392. for (int i = 0; i < n; i++){
  393. row.push_back(0);
  394. }
  395. for (int i = 0; i < m; i++){
  396. board.push_back(row);
  397. }
  398. }
  399. void Game_2048::printBoard()
  400. {
  401. cout << "\n--------------\n";
  402. for (int i = 0; i < m; i++) {
  403. for (int j = 0; j < n; j++) {
  404. cout << board[i][j];
  405. if (j < n-1)
  406. {
  407. cout << "—";
  408. }
  409. if (j == n-1 && i < m-1)
  410. {
  411. cout << endl;
  412. int count = 0;
  413. while (count++ < n-1)
  414. {
  415. cout << "| ";
  416. }
  417. cout << "|" << endl;
  418. }
  419. }
  420. }
  421. cout << "\n--------------\n" ;
  422. }

勇者游戏:

  1. #include<stdio.h>
  2. #include<ctime>
  3. #include<time.h> //suiji
  4. #include<windows.h> //SLEEP函数
  5. struct Player //玩家结构体,并初始化player
  6. {
  7. char name[21];
  8. int attack;
  9. int defense;
  10. int health;
  11. long int max_health;
  12. int level;
  13. int exp;
  14. int range_exp;
  15. long int max_exp;
  16. } player= {"勇者",50,40,100,100,1,0,0,100};
  17. struct Enemy //怪的结构体,并初始化各种怪
  18. {
  19. char name[20];
  20. char wupin[12];
  21. int attack;
  22. int defense;
  23. int health;
  24. int money;
  25. long int exp;
  26. int wupin_sign;
  27. int wupinpro;
  28. int double_attack;
  29. int miss;
  30. } strongman= {"森林巨人","黄金圣衣",40,50,350,200,100,1,2,1,0},
  31. witch= {"森林女巫","银甲",25,15,100,50,50,2,2,1,1},
  32. xiyi= {"森林蜥蜴","铁甲",18,10,50,30,35,3,3,2,2},
  33. big_strongman= {"森林巨人王","巨人晶石",40*5,50*5,200*5,200*5,100*5,4,4,2,0},
  34. lion= {"草原雄狮","绝世好剑",60,30,280,200,100,5,2,1,0},
  35. horse= {"草原野马","碧血剑",28,12,90,50,50,6,2,1,1},
  36. bee= {"草原黄蜂","长剑",17,11,60,30,35,7,3,2,2},
  37. shitu= {"使徒","\0",60*8,30*8,280*8,200*8,100*8,9,1,1,0},
  38. guai= {"\0","\0",0,0,0,0,0,0,0,0,0};
  39. struct Place
  40. {
  41. int bar,hotel,forest1,forest2,forest3,grass1,grass2,grass3;
  42. } place= {1,2,3,4,5,6,7,8};
  43. int max_exp=0;
  44. int choose_number=0,s=0,strongman_arm=0,battle=0,money=500,place_sign=9;
  45. int cao=3,jijiubao=2,baiyao=2,superbaiyao=1,boom=3,dubiao=2,atom_boom=1;
  46. int fang=0,fang1=10,fang1n=0,fang2=20,fang2n=0,fang3=40,fang3n=0,fang4=100,fang4n=0;
  47. int gong=0,gong1=8,gong1n=0,gong2=15,gong2n=0,gong3=25,gong3n=0,gong4=60,gong4n=0;
  48. int jingyancao=0,jingyanbao=0,jingyanshi=0;
  49. char gongname[20]="无",fangname[20]="无";
  50. char proof;
  51. void AddWupin(int);
  52. int AttackResult();
  53. void BattleAct();
  54. void ChooseWupin();
  55. void DisplayState();
  56. void OrdinaryAct();
  57. int SuiJi();
  58. int SuiJi100();
  59. void WhetherLevelUp();
  60. void SlowDisplay(char *);
  61. int main()
  62. {
  63. int i=0,j=0,k=0;
  64. char player_name[21];
  65. Sleep(1000);
  66. printf("--------------------------欢迎来到 [苍穹世界] 2.2 测试版-----------------------\n\n\n");
  67. //如果想使用外挂,名字请输入:“圣战斗士 ”。
  68. Sleep(1000);
  69. printf("这里是苍穹世界! 雅莉萨斯国的罗茜公主被陌生人绑架了!\n\n\n 伟大的勇者啊~拿起你们的武器,营救公主!\n\n\n输入你的名字: (20个字符)\n\n\n");
  70. scanf("%s",player_name);
  71. strncpy(player.name,player_name,20);
  72. if(strcmp(player.name,"圣战斗士")==0)
  73. {
  74. printf("\n\n\n封印多年的圣剑血统啊!你终于觉醒了!\n\n\n圣战斗士,你成为了天选之人,请你救出公主吧!\n\n\n");
  75. player.attack=999;
  76. player.defense=999;
  77. player.health=9999;
  78. player.max_health=9999;
  79. }
  80. getchar();
  81. OrdinaryAct();
  82. return 0;
  83. }
  84. int SuiJi()
  85. {
  86. srand((unsigned)time(NULL));
  87. return rand()%10;
  88. }
  89. int SuiJi100()
  90. {
  91. srand((unsigned)time(NULL));
  92. return rand()%100;
  93. }
  94. void ChooseWupin() //选择物品 并使用
  95. {
  96. printf("物品: 1,止血草%d个 2,急救包%d个 3,云南白药%d个 4,超级云南白药%d个 5,手雷%d个 6,毒标%d个 7,手抛式原子弹%d个 8,经验草%d个 9,经验包%d个 10,经验石%d个 11,巨人晶石%d个 0,返回\n\n\n",cao,jijiubao,baiyao,superbaiyao,boom,dubiao,atom_boom,jingyancao,jingyanbao,jingyanshi,strongman_arm);
  97. switch(scanf("%d",&choose_number),choose_number)
  98. {
  99. case 1:
  100. if(cao>0)
  101. {
  102. printf("使用止血草,HP增加60\n\n\n");
  103. cao--;
  104. if(player.health+60>player.max_health)player.health=player.max_health;
  105. else player.health+=60;
  106. }
  107. else printf("没有止血草了\n\n\n");
  108. break;
  109. case 2:
  110. if(jijiubao>0)
  111. {
  112. printf("使用急救包,HP增加80\n\n\n");
  113. jijiubao--;
  114. if(player.health+80>player.max_health)player.health=player.max_health;
  115. else player.health+=80;
  116. }
  117. else printf("没有急救包了\n\n\n");
  118. break;
  119. case 3:
  120. if(baiyao>0)
  121. {
  122. printf("使用云南白药,HP增加120\nz\n\n");
  123. baiyao--;
  124. if(player.health+120>player.max_health)player.health=player.max_health;
  125. else player.health+=120;
  126. }
  127. else printf("没有云南白药了\n\n\n");
  128. break;
  129. case 4:
  130. if(superbaiyao>0)
  131. {
  132. printf("使用超级云南白药,HP增加200\n\n\n");
  133. superbaiyao--;
  134. if(player.health+200>player.max_health)player.health=player.max_health;
  135. else player.health+=200;
  136. }
  137. else printf("没有超级云南白药了\n\n\n");
  138. break;
  139. case 5:
  140. if(battle) //在战斗中(battle=1),否则(battle=0)不能使用攻击性物品
  141. {
  142. if(boom>0)
  143. {
  144. printf("使用手雷,敌人HP减少100\n\n\n");
  145. boom--;
  146. guai.health-=100;
  147. AttackResult();
  148. }
  149. }
  150. else printf("非战斗状态,不能使用手雷!\n\n\n");
  151. break;
  152. case 6:
  153. if(battle) //在战斗中(battle=1),否则(battle=0)不能使用攻击性物品
  154. {
  155. if(dubiao>0)
  156. {
  157. printf("使用毒标,敌人HP减少200\n\n\n");
  158. dubiao--;
  159. guai.health-=200;
  160. AttackResult();
  161. }
  162. }
  163. else printf("非战斗状态,不能使用毒标!\n\n\n");
  164. break;
  165. case 7:
  166. if(battle) //在战斗中(battle=1),否则(battle=0)不能使用攻击性物品
  167. {
  168. if(atom_boom>0)
  169. {
  170. printf("使用手抛式原子弹,敌人HP减少666666666\n\n\n");
  171. atom_boom--;
  172. guai.health-=666666666;
  173. AttackResult();
  174. }
  175. }
  176. else printf("非战斗状态,不能使用手抛式原子弹!\n\n\n");
  177. break;
  178. case 8:
  179. if(jingyancao>0 && player.level<1000)
  180. {
  181. printf("使用经验草,等级增加10级\n\n\n");
  182. jingyancao--;
  183. player.level+=10;
  184. printf("等级:%d\n",player.level);
  185. }
  186. else if(jingyancao<1)
  187. {
  188. printf("没有经验草了\n\n\n");
  189. }
  190. else printf("等级超过45级,修为太高,无法使用。\n\n\n");
  191. break;
  192. case 9:
  193. if(jingyanbao>0 && player.level<1000)
  194. {
  195. if(player.level>44&&player.level<1000)
  196. {
  197. int sheng;
  198. sheng=45-player.level;
  199. player.level+=sheng;
  200. printf("使用经验包,等级增加%d级",sheng);
  201. printf("等级:%d\n",player.level);
  202. }
  203. else
  204. {
  205. printf("使用经验包,等级增加2级\n\n\n");
  206. jingyanbao--;
  207. player.level+=2;
  208. printf("等级:%d\n",player.level);
  209. }
  210. }
  211. else if(jingyanbao<1)
  212. {
  213. printf("没有经验包了");
  214. }
  215. else printf("等级超过45级,修为太高,无法使用。\n\n\n");
  216. break;
  217. case 10:
  218. if(jingyanshi>0 && player.level<1000)
  219. {
  220. if(player.level>42&&player.level<1000)
  221. {
  222. int sheng;
  223. sheng=45-player.level;
  224. player.level+=sheng;
  225. printf("使用经验石,等级增加%d级\n",sheng);
  226. printf("等级:%d\n",player.level);
  227. }
  228. else
  229. {
  230. printf("使用经验石,等级增加10级\n");
  231. jingyanshi--;
  232. player.level+=10;
  233. }
  234. }
  235. else if(jingyanshi<1)
  236. {
  237. printf("没有经验石了\n\n\n");
  238. }
  239. else printf("等级超过45级,修为太高,无法使用。\n\n\n");
  240. break;
  241. case 11:
  242. if(strongman_arm>0 && player.level<10000)
  243. {
  244. if(player.level>29&&player.level<10000)
  245. {
  246. int sheng;
  247. sheng=45-player.level;
  248. player.level+=sheng;
  249. printf("使用巨人晶石,等级增加%d级",sheng);
  250. printf("等级:%d\n",player.level);
  251. }
  252. else
  253. {
  254. printf("使用巨人晶石,等级增加16级\n\n\n");
  255. strongman_arm--;
  256. player.level+=16;
  257. printf("等级:%d\n",player.level);
  258. }
  259. }
  260. else if(strongman_arm<1)
  261. {
  262. printf("没有巨人晶石了。\n\n\n");
  263. }
  264. else printf("等级超过45级,修为太高,无法使用。\n\n\n");
  265. break;
  266. case 0:
  267. break;
  268. default:
  269. printf("ChooseWupin error!\n\n\n");
  270. }
  271. }
  272. int AttackResult() //攻击结果:判断是否获胜 是否获得物品 和 是否升级
  273. {
  274. if(guai.health<=0)
  275. {
  276. battle=0;
  277. printf("战斗胜利!获得金币%d,经验%d\n\n\n",guai.money,guai.exp);
  278. player.exp+=guai.exp;
  279. player.range_exp+=guai.exp;
  280. money+=guai.money;
  281. s=SuiJi();
  282. if(s<guai.wupinpro)
  283. {
  284. printf("从敌人尸骸中发现");
  285. printf("%s\n\n\n",guai.wupin);
  286. AddWupin(guai.wupin_sign);
  287. }
  288. WhetherLevelUp();
  289. if(strcmp(guai.name,"使徒")==0)
  290. {
  291. printf("战斗胜利,救出公主!!!");
  292. getchar();
  293. getchar();
  294. exit(0);
  295. }
  296. return 1; //攻击有结果了返回1,否则返回0,用于判断是否继续做战斗行为
  297. }
  298. else
  299. {
  300. int s=SuiJi();
  301. if((guai.attack+s-player.defense/3)<0)
  302. {
  303. player.health-=1;
  304. printf("%s反击,你的HP减少了 1\n\n",guai.name);
  305. }
  306. else
  307. {
  308. player.health-=guai.attack+s-player.defense/3;
  309. printf("%s反击,你的HP减少了%d\n\n",guai.name,guai.attack+s-player.defense/3);
  310. }
  311. if(player.health<0)
  312. {
  313. battle=0;
  314. printf("%s战死!金币掉落%d\n\n\n",player.name,player.level*500);
  315. money-=player.level*500;
  316. player.health=player.max_health/5;
  317. OrdinaryAct();//
  318. return 1;
  319. }
  320. }
  321. return 0;
  322. }
  323. void AddWupin(int wupin_sign)
  324. {
  325. switch(wupin_sign)
  326. {
  327. case 1:
  328. fang4n++;
  329. break;
  330. case 2:
  331. fang3n++;
  332. break;
  333. case 3:
  334. fang2n++;
  335. break;
  336. case 4:
  337. strongman_arm=1;
  338. break;
  339. case 5:
  340. gong4n++;
  341. break;
  342. case 6:
  343. gong3n++;
  344. break;
  345. case 7:
  346. gong2n++;
  347. break;
  348. default:
  349. printf("AddWupin error\n\n\n");
  350. }
  351. }
  352. void WhetherLevelUp()
  353. {
  354. int i=0,j=0;
  355. int l1=player.range_exp/100;
  356. int l2=player.range_exp/300;
  357. int l3=player.range_exp/600;
  358. if(player.level<=15&&l1>0) //15级以下,经验足够 都满足则升级
  359. {
  360. if(l1==1)
  361. {
  362. printf("%s",player.name);
  363. printf(" 升级!\n\n\n攻击力+3, 防御力+2, HP上限+20\n\n\n");
  364. player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
  365. player.attack+=3;
  366. player.defense+=2;
  367. player.max_health+=20;
  368. player.health=player.max_health;
  369. player.level++;
  370. player.range_exp=0;
  371. player.exp=player.max_exp;
  372. player.max_exp+=100;
  373. }
  374. else
  375. {
  376. printf("好厉害!连升%d级!",l1);
  377. printf("攻击力+%d, 防御力+%d, HP上限+%d\n\n\n",3*l1,2*l1,20*l1);
  378. player.exp=(player.exp+guai.exp) || player.exp-((player.exp+guai.exp) || player.exp)%100;
  379. player.attack+=3*l1;
  380. player.defense+=2*l1;
  381. player.max_health+=20*l1;
  382. player.health=player.max_health;
  383. player.level+=l1;
  384. player.range_exp=0;
  385. player.exp=player.max_exp;
  386. player.max_exp+=100*l1;
  387. }
  388. }
  389. else if(player.level<=40&&l2>0)
  390. {
  391. if(l2==1)
  392. {
  393. printf("%s",player.name);
  394. printf(" 升级!\n\n\n攻击力+3, 防御力+2, HP上限+20\n\n\n");
  395. player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
  396. player.attack+=3;
  397. player.defense+=2;
  398. player.max_health+=20;
  399. player.health=player.max_health;
  400. player.level++;
  401. player.range_exp=0;
  402. player.exp=player.max_exp;
  403. player.max_exp+=300;
  404. }
  405. else
  406. {
  407. printf("好厉害!连升%d级!",l1);
  408. printf("攻击力+%d, 防御力+%d, HP上限+%d\n\n\n",3*l2,2*l2,20*l2);
  409. player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
  410. player.attack+=3*l2;
  411. player.defense+=2*l2;
  412. player.max_health+=20*l2;
  413. player.health=player.max_health;
  414. player.level+=l2;
  415. player.range_exp=0;
  416. player.exp=player.max_exp;
  417. player.max_exp+=300*l2;
  418. }
  419. }
  420. else if(l3>0)
  421. {
  422. if(l3==1)
  423. {
  424. printf("%s",player.name);
  425. printf(" 升级!\n\n\n攻击力+3, 防御力+2, HP上限+20\n\n\n");
  426. player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
  427. player.attack+=3;
  428. player.defense+=2;
  429. player.max_health+=20;
  430. player.health=player.max_health;
  431. player.level++;
  432. player.range_exp=0;
  433. player.exp=player.max_exp;
  434. player.max_exp+=600;
  435. }
  436. else
  437. {
  438. printf("好厉害!连升%d级!",l1);
  439. printf("攻击力+%d, 防御力+%d, HP上限+%d\n\n\n",3*l3,2*l3,20*l3);
  440. player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
  441. player.attack+=3*l3;
  442. player.defense+=2*l3;
  443. player.max_health+=20*l3;
  444. player.health=player.max_health;
  445. player.level+=l3;
  446. player.range_exp=0;
  447. player.exp=player.max_exp;
  448. player.max_exp+=600*l3;
  449. }
  450. }
  451. }
  452. void OrdinaryAct() //正常行为菜单(移动,物品,对话,查看状态,装备,退出游戏)
  453. {
  454. while(1)
  455. {
  456. // \(1000);
  457. // system("cls");
  458. puts("=============================================================================");
  459. printf("要做什么?\n\n\n 1,移动 2,道具 3,对话 4,查看状态 5,装备 6,关于游戏 0,退出游戏\n\n\n");
  460. puts("=============================================================================");
  461. switch(scanf("%d",&choose_number),choose_number)
  462. {
  463. case 1: //显示移动菜单
  464. printf("要去哪里?\n\n\n");
  465. printf("1,happy酒吧 2,诺亚方舟酒店 3,北朝商会 4,红玉拍卖行 5,冒险荒野\n\n\n");
  466. switch(scanf("%d",&choose_number),choose_number)
  467. {
  468. case 1:
  469. place_sign=place.bar; //记录目前位置-酒吧
  470. // OrdinaryAct();
  471. break;
  472. case 2:
  473. place_sign=place.hotel; //进入旅店
  474. printf("金币:%d",money);
  475. printf("要开房吗? 200个金币 1,是 0,否\n\n\n");
  476. choose_number=1;
  477. switch(scanf("%d",&choose_number),choose_number)
  478. {
  479. case 1:
  480. if(money-200<0) //判断钱是否够
  481. {
  482. printf("Sorry,你的钱不够~\n\n\n");
  483. printf("金币:%d",money);
  484. }
  485. else
  486. {
  487. printf("好好休息\nHP满\n第二天了\n\n");
  488. printf("金币:%d\n",money);
  489. money-=200; //花费200住店费
  490. player.health=player.max_health; //体力满
  491. }
  492. break;
  493. case 0:
  494. printf("下次再来!\n\n\n");
  495. break;
  496. default:
  497. printf("hotel talk error!\n\n\n");
  498. }
  499. place_sign=0;
  500. break;
  501. case 3:
  502. int yongju,gong,fang;
  503. printf("请问您要购买什么类型的物品?\n\n\n 1,攻击装备 2,防御装备 3,一次性伤害武器\n\n\n");
  504. scanf("%d",&yongju);
  505. switch(yongju)
  506. {
  507. case 1:
  508. printf("请问您要购买什么武器?\n\n\n 1,匕首¥300 2,长剑¥500 3,碧血剑¥1000\n\n\n");
  509. scanf("%d",&gong);
  510. switch(gong)
  511. {
  512. case 1:
  513. if(money>=300)
  514. {
  515. gong1n++;
  516. money=money-300;
  517. printf ("匕首+1\n");
  518. printf("匕首:%d个\n",gong1n);
  519. printf("金币:%d\n",money);
  520. break;
  521. }
  522. else
  523. {
  524. printf("钱不够!\n");
  525. printf("金币:%d\n",money);
  526. break;
  527. }
  528. case 2:
  529. if(money>=500)
  530. {
  531. gong2n++;
  532. money=money-500;
  533. printf ("长剑+1\n");
  534. printf("长剑:%d个\n",gong2n);
  535. printf("金币:%d\n",money);
  536. break;
  537. }
  538. else
  539. {
  540. printf("钱不够!\n");
  541. printf("金币:%d\n",money);
  542. break;
  543. }
  544. case 3:
  545. if(money>=1000)
  546. {
  547. gong3n++;
  548. money=money-1000;
  549. printf ("碧血剑+1\n");
  550. printf("碧血剑:%d个\n",gong3n);
  551. printf("金币:%d\n",money);
  552. break;
  553. }
  554. else
  555. {
  556. printf("钱不够!\n");
  557. printf("金币:%d\n",money);
  558. break;
  559. }
  560. default:
  561. printf("对不起,我们只会打造以上武器。");
  562. break;
  563. }
  564. break;
  565. case 2:
  566. int fang;
  567. printf("请问您要购买什么防具?\n\n\n 1,布衣¥300 2,铁甲¥500 3,银甲¥1000\n\n\n");
  568. scanf("%d",&fang);
  569. switch(fang)
  570. {
  571. case 1:
  572. if(money>=300)
  573. {
  574. fang1n++;
  575. money=money-300;
  576. printf ("布衣+1\n");
  577. printf("布衣:%d个\n",fang1n);
  578. printf("金币:%d\n",money);
  579. }
  580. else
  581. {
  582. printf("钱不够!\n");
  583. printf("金币:%d\n",money);
  584. }
  585. break;
  586. case 2:
  587. if(money>=500)
  588. {
  589. fang2n++;
  590. money=money-500;
  591. printf ("铁甲+1\n");
  592. printf("铁甲:%d个\n",fang2n);
  593. printf("金币:%d\n",money);
  594. }
  595. else
  596. {
  597. printf("钱不够!\n");
  598. printf("金币:%d",money);
  599. }
  600. break;
  601. case 3:
  602. if(money>=1000)
  603. {
  604. fang3n++;
  605. money=money-1000;
  606. printf ("银甲+1\n");
  607. printf("银甲:%d个\n",fang3n);
  608. printf("金币:%d\n",money);
  609. }
  610. else
  611. {
  612. printf("钱不够!\n");
  613. printf("金币:%d\n",money);
  614. }
  615. default:
  616. printf("对不起,我们只会打造以上防具。");
  617. break;
  618. }
  619. printf("金币:%d\n",money);
  620. break;
  621. case 3:
  622. printf("请问您要购买什么一次性伤害武器?\n 1,手雷 2,毒镖 3,手抛式原子弹\n\n\n");
  623. int yi;
  624. scanf("%d",&yi);
  625. switch(yi)
  626. {
  627. case 1:
  628. if(money>=300 && boom<5)
  629. {
  630. boom++;
  631. money=money-300;
  632. printf("手雷+1\n");
  633. printf("手雷:%d\n",boom);
  634. printf("金币:%d\n",money);
  635. }
  636. else
  637. {
  638. printf("钱不够!\n");
  639. printf("金币:%d",money);
  640. }
  641. break;
  642. case 2:
  643. if(money>=600 && dubiao<4)
  644. {
  645. dubiao++;
  646. money=money-600;
  647. printf("毒镖+1\n");
  648. printf("毒镖:%d\n",dubiao);
  649. printf("金币:%d\n",money);
  650. }
  651. else
  652. {
  653. printf("钱不够!\n");
  654. printf("金币:%d\n",money);
  655. }
  656. break;
  657. case 3:
  658. if(money>=0 && atom_boom<23333333333)
  659. {
  660. atom_boom=atom_boom+233;
  661. money=money+1500;
  662. printf("手抛式原子弹+2\n");
  663. printf("手抛式原子弹:%d\n",atom_boom);
  664. printf("金币:%d\n",money);
  665. }
  666. else
  667. {
  668. printf("钱不够!\n\n\n");
  669. printf("金币:%d\n",money);
  670. }
  671. break;
  672. }
  673. }
  674. break;
  675. case 4:
  676. printf ("欢迎您光临本拍卖行,请问您要卖什么东西?\n\n");
  677. printf("攻击装备: 1,匕首:%d个 2,长剑:%d个 3,碧血剑:%d个 4,绝世好剑:%d个\n",gong1n,gong2n,gong3n,gong4n);
  678. printf("防御装备: 5,布衣:%d个 6,铁甲:%d个 7,银甲:%d个 8,黄金圣衣:%d个\n9,巨人晶石:%d个 0,返回\n\n\n",fang1n,fang2n,fang3n,fang4n,strongman_arm);
  679. int pai,shu,i;
  680. scanf("%d",&pai);
  681. switch(pai)
  682. {
  683. case 1:
  684. printf("请问您要出售几件?");
  685. scanf("%d",&shu);
  686. if(gong1n>=shu)
  687. {
  688. gong1n=gong1n-shu;
  689. money=money+shu*240;
  690. printf("匕首:%d\n",gong1n);
  691. printf("金币:%d\n",money);
  692. break;
  693. }
  694. else
  695. {
  696. printf("装备数不够,无法出售!\n");
  697. break;
  698. }
  699. break;
  700. case 2:
  701. printf("请问您要出售几件?\n");
  702. scanf("%d",&shu);
  703. if(gong2n>=shu)
  704. {
  705. gong2n=gong2n-shu;
  706. money=money+shu*400;
  707. printf("长剑:%d\n",gong2n);
  708. printf("金币:%d\n",money);
  709. break;
  710. }
  711. else
  712. {
  713. printf("装备数不够,无法出售!\n");
  714. break;
  715. }
  716. case 3:
  717. printf("请问您要出售几件?\n");
  718. scanf("%d",&shu);
  719. if(gong3n>=shu)
  720. {
  721. gong3n=gong3n-shu;
  722. money=money+shu*800;
  723. printf("碧血剑:%d\n",gong3n);
  724. printf("金币:%d\n",money);
  725. break;
  726. }
  727. else
  728. {
  729. printf("装备数不够,无法出售!\n");
  730. break;
  731. }
  732. case 4:
  733. printf("请问您要出售几件?\n");
  734. scanf("%d",&shu);
  735. if(gong4n>=shu)
  736. {
  737. gong4n=gong4n-shu;
  738. money=money+shu*1500;
  739. printf("绝世好剑:%d\n",gong4n);
  740. printf("金币:%d\n",money);
  741. break;
  742. }
  743. else
  744. {
  745. printf("装备数不够,无法出售!\n");
  746. break;
  747. }
  748. case 5:
  749. printf("请问您要出售几件?\n");
  750. scanf("%d",&shu);
  751. if(fang1n>=shu)
  752. {
  753. fang1n=fang1n-shu;
  754. money=money+shu*240;
  755. printf("布衣:%d\n",fang1n);
  756. printf("金币:%d\n",money);
  757. break;
  758. }
  759. else
  760. {
  761. printf("装备数不够,无法出售!\n");
  762. break;
  763. }
  764. case 6:
  765. printf("请问您要出售几件?\n");
  766. scanf("%d",&shu);
  767. if(fang2n>=shu)
  768. {
  769. fang2n=fang2n-shu;
  770. money=money+shu*500;
  771. printf("铁甲:%d\n",fang2n);
  772. printf("金币:%d\n",money);
  773. break;
  774. }
  775. else
  776. {
  777. printf("装备数不够,无法出售!\n");
  778. break;
  779. }
  780. case 7:
  781. printf("请问您要出售几件?\n");
  782. scanf("%d",&shu);
  783. if(fang3n>=shu)
  784. {
  785. fang3n=fang3n-shu;
  786. money=money+shu*800;
  787. printf("银甲:%d\n",fang3n);
  788. printf("金币:%d\n",money);
  789. break;
  790. }
  791. else
  792. {
  793. printf("装备数不够,无法出售!\n");
  794. break;
  795. }
  796. break;
  797. case 8:
  798. printf("请问您要出售几件?\n");
  799. scanf("%d",&shu);
  800. if(fang1n>=shu)
  801. {
  802. fang4n=fang4n-shu;
  803. money=money+shu*1500;
  804. printf("黄金圣衣:%d\n",fang4n);
  805. printf("金币:%d\n",money);
  806. break;
  807. }
  808. else
  809. {
  810. printf("装备数不够,无法出售!\n");
  811. break;
  812. }
  813. case 9:
  814. printf("请问您要出售几颗?");
  815. scanf("%d",&shu);
  816. if(strongman_arm>=shu)
  817. {
  818. strongman_arm=strongman_arm-shu;
  819. money=money+shu*2000;
  820. printf("巨人晶石:%d\n",strongman_arm);
  821. printf("金币:%d\n",money);
  822. }
  823. else
  824. {
  825. printf("晶石数不够,无法出售!\n");
  826. break;
  827. }
  828. break;
  829. case 0:
  830. break;
  831. break;
  832. default:
  833. printf("没有该装备,无法出售!\n");
  834. break;
  835. }
  836. break;
  837. case 5:
  838. int yewai;
  839. while(1)
  840. {
  841. puts("=============================================================================");
  842. printf("要去哪冒险呢?");
  843. printf("\n\n 1,神秘沼泽 危险程度:★\n\n 2,星耀草原 危险程度:★\n\n 3,诡异森林 危险程度:★★★\n\n 4,荒漠矿场 危险程度:★★★\n\n 5,炽热炎洞 危险程度:★★★★\n\n 6,花朵宫殿 危险程度:★★★★★\n\n 0,离开\n");
  844. puts("=============================================================================");
  845. scanf("%d",&yewai);
  846. switch(yewai)
  847. {
  848. case 1:
  849. place_sign=place.forest1;
  850. s=SuiJi();
  851. if(s<7)
  852. {
  853. battle=1;
  854. guai=xiyi;
  855. printf("%s扑了过来!\n\n\n",guai.name);
  856. BattleAct();
  857. }
  858. else if(s<9)
  859. {
  860. battle=1;
  861. guai=witch;
  862. printf("%s扑了过来!\n\n\n",guai.name);
  863. BattleAct();
  864. }
  865. else
  866. {
  867. printf("这里安全\n\n\n");
  868. //不用调用OAct函数,会自动执行OAct函数;
  869. }
  870. break;
  871. case 3:
  872. place_sign=place.forest2;
  873. s=SuiJi();
  874. if(s<7)
  875. {
  876. battle=1;
  877. guai=witch;
  878. printf("%s扑了过来!\n\n\n",guai.name);
  879. BattleAct();
  880. }
  881. else if(s<9)
  882. {
  883. battle=1;
  884. guai=strongman;
  885. printf("%s扑了过来!\n\n\n",guai.name);
  886. BattleAct();
  887. }
  888. else
  889. {
  890. printf("这里安全\n\n\n");
  891. }
  892. break;
  893. case 5:
  894. place_sign=place.forest3;
  895. s=SuiJi();
  896. if(s<7)
  897. {
  898. battle=1;
  899. guai=strongman;
  900. printf("%s扑了过来!\n\n\n",guai.name);
  901. BattleAct();
  902. }
  903. else if(s<9)
  904. {
  905. battle=1;
  906. guai=big_strongman;
  907. printf("%s扑了过来!\n\n\n",guai.name);
  908. BattleAct();
  909. }
  910. else
  911. {
  912. printf("这里安全\n\n\n");
  913. }
  914. break;
  915. case 2:
  916. place_sign=place.grass1;
  917. s=SuiJi();
  918. if(s<7)
  919. {
  920. battle=1;
  921. guai=bee;
  922. printf("%s扑了过来!\n\n\n",guai.name);
  923. BattleAct();
  924. }
  925. else if(s<9)
  926. {
  927. battle=1;
  928. guai=horse;
  929. printf("%s扑了过来!\n\n\n",guai.name);
  930. BattleAct();
  931. }
  932. else
  933. {
  934. printf("这里安全\n\n\n");
  935. }
  936. break;
  937. case 4:
  938. place_sign=place.grass2;
  939. s=SuiJi();
  940. if(s<7)
  941. {
  942. battle=1;
  943. guai=horse;
  944. printf("%s扑了过来!\n\n\n",guai.name);
  945. BattleAct();
  946. }
  947. else if(s<9)
  948. {
  949. battle=1;
  950. guai=lion;
  951. printf("%s扑了过来!\n\n\n",guai.name);
  952. BattleAct();
  953. }
  954. else
  955. {
  956. printf("这里安全\n\n\n");
  957. }
  958. break;
  959. case 6:
  960. place_sign=place.grass3;
  961. s=SuiJi();
  962. if(s<7)
  963. {
  964. battle=1;
  965. guai=lion;
  966. printf("%s扑了过来!\n\n\n",guai.name);
  967. BattleAct();
  968. }
  969. else if(s<9)
  970. {
  971. battle=1;
  972. if(strongman_arm)
  973. {
  974. printf("神秘老人:\n\n\n 哈哈,年轻人,做的不错,不过...嘿嘿,你上当啦!巨人晶石我要了,公主你也别想带走!\n\n\n");
  975. guai=shitu;
  976. printf("%s扑了过来!\n\n\n",guai.name);
  977. BattleAct();
  978. }
  979. else printf("神秘老人:\n\n\n 年轻人,你好啊.如果你有巨人晶石,我可以告诉你公主的下落哦~\n\n\n");
  980. }
  981. else
  982. {
  983. printf("这里安全\n\n\n");
  984. }
  985. break;
  986. if(yewai!=0)
  987. {
  988. printf("该区域为未知区域,无法进入。\n\n\n");
  989. break;
  990. }
  991. }
  992. if(yewai==0)
  993. {
  994. break;
  995. printf("已离开荒野。");
  996. }
  997. }
  998. }
  999. break;
  1000. case 2:
  1001. ChooseWupin();
  1002. break; //显示道具,并可以使用.
  1003. case 3: //对话选项
  1004. if(place_sign==place.bar)
  1005. {
  1006. printf("要和谁说话?\n\n\n1,红发女郎 2,赏金猎人 3,酒吧老板 4,药品商人\n\n\n"); //显示对话人物
  1007. switch(scanf("%d",&choose_number),choose_number)
  1008. {
  1009. case 1:
  1010. printf("红发女郎:\n\n\n 吧台边那个Hunter好帅啊!(~脸红~)\n\n\n听说他经常外出打猎,外面的路他应该很熟悉的!\n\n\n");
  1011. break;
  1012. case 2:
  1013. if(fang1n<1&&gong1n<1)
  1014. {
  1015. printf("赏金猎人:\n\n\n 你要救公主啊!好胆量!\n\n\n 不过外面的世界很险恶,而且越深越危险,这是匕首和布衣,对你会有些帮助的,拿去吧!\n\n\n");
  1016. printf("%s心想:哇,这位大叔人真好啊!\n\n\n)",player.name);
  1017. gong1n++;
  1018. fang1n++;
  1019. }
  1020. else printf("赏金猎人:\n\n\n 加油吧,年轻人!\n\n\n 不要被外面世界所吓倒!\n\n\n");
  1021. break;
  1022. case 3:
  1023. printf("要喝点什么?\n\n\n 1,二锅头25金币 HP+20 2,XO酒80金币 HP+50 3,人头马面150金币 HP+100 0,返回\n\n\n");
  1024. choose_number=1;
  1025. while(choose_number)
  1026. {
  1027. switch(scanf("%d",&choose_number),choose_number)
  1028. {
  1029. case 1:
  1030. if(money<25)
  1031. {
  1032. printf("钱不够!");
  1033. }
  1034. else
  1035. {
  1036. if(player.health+20<=player.max_health)
  1037. {
  1038. printf("HP+20.");
  1039. money-=25;
  1040. player.health+=20;
  1041. }
  1042. else
  1043. {
  1044. printf("HP满了");
  1045. player.health=player.max_health;
  1046. }
  1047. }
  1048. break;
  1049. case 2:
  1050. if(money<80)
  1051. {
  1052. printf("钱不够!");
  1053. }
  1054. else
  1055. {
  1056. if(player.health+50<=player.max_health)
  1057. {
  1058. printf("HP+50.");
  1059. money-=80;
  1060. player.health+=50;
  1061. }
  1062. else
  1063. {
  1064. printf("HP满了");
  1065. player.health=player.max_health;
  1066. }
  1067. }
  1068. break;
  1069. case 3:
  1070. if(money<150)
  1071. {
  1072. printf("钱不够!");
  1073. }
  1074. else
  1075. {
  1076. if(player.health+100<=player.max_health)
  1077. {
  1078. printf("HP+100.");
  1079. money-=150;
  1080. player.health+=100;
  1081. }
  1082. else
  1083. {
  1084. printf("HP满了");
  1085. player.health=player.max_health;
  1086. }
  1087. }
  1088. break;
  1089. case 0:
  1090. printf("下次再来!\n");
  1091. break;
  1092. default:
  1093. printf("输入错误\n\n\n");
  1094. break;
  1095. }
  1096. break;
  1097. }
  1098. break;
  1099. case 4:
  1100. printf("你要干什么?\n\n\n 1,买东西 2,聊天 \n\n\n");
  1101. int mai;
  1102. scanf("%d",&mai);
  1103. if(mai==1)
  1104. {
  1105. printf("买点什么呢?\n1,止血草¥100 HP+60\n2,急救包¥150 HP+80 \n3,云南白药¥250 HP+120\n4,超级云南白药¥400 HP+200 \n5,经验草¥150 经验+300 \n6,经验包¥600 经验+600\n7,经验石¥500 经验+1000 \n0,拜拜\n");
  1106. int dongxi;
  1107. scanf("%d",&dongxi);
  1108. switch(dongxi)
  1109. {
  1110. case 1:
  1111. if(money>=100&&cao<6)
  1112. {
  1113. cao++;
  1114. money=money-100;
  1115. printf ("止血草+1\n");
  1116. }
  1117. else
  1118. {
  1119. printf("钱不够!\n");
  1120. }
  1121. break;
  1122. case 2:
  1123. if(money>=150&&jijiubao<5)
  1124. {
  1125. jijiubao++;
  1126. money=money-150;
  1127. printf ("急救包+1\n");
  1128. }
  1129. else
  1130. {
  1131. printf("钱不够!\n");
  1132. }
  1133. break;
  1134. case 3:
  1135. if(money>=250&&baiyao<4)
  1136. {
  1137. baiyao++;
  1138. money=money-250;
  1139. printf ("云南白药+1\n");
  1140. }
  1141. else
  1142. {
  1143. printf("钱不够!\n");
  1144. }
  1145. break;
  1146. case 4:
  1147. if(money>=400&&superbaiyao<3)
  1148. {
  1149. superbaiyao++;
  1150. money=money-400;
  1151. printf ("超级云南白药+1\n");
  1152. }
  1153. else
  1154. {
  1155. printf("钱不够!\n");
  1156. }
  1157. break;
  1158. case 5:
  1159. if(money>=150)
  1160. {
  1161. jingyancao++;
  1162. money=money-150;
  1163. printf ("经验草+1\n");
  1164. }
  1165. else
  1166. {
  1167. printf("钱不够!\n");
  1168. }
  1169. break;
  1170. case 6:
  1171. if(money>=300)
  1172. {
  1173. jingyanbao++;
  1174. money=money-300;
  1175. printf ("经验包+1\n");
  1176. }
  1177. else
  1178. {
  1179. printf("钱不够!\n");
  1180. }
  1181. break;
  1182. case 7:
  1183. if(money>=500)
  1184. {
  1185. jingyanshi++;
  1186. money=money+500;
  1187. printf ("经验石+1\n");
  1188. }
  1189. else
  1190. {
  1191. printf("钱不够!\n");
  1192. }
  1193. break;
  1194. }
  1195. case 0:
  1196. printf("金币:%d\n",money);
  1197. printf("再见,欢迎下次再来!\n");
  1198. break;
  1199. }
  1200. if(mai==2)
  1201. {
  1202. printf("药品商人:去去去,老子没时间陪你聊。\n");
  1203. }
  1204. }
  1205. }
  1206. else if(place_sign==place.hotel)
  1207. printf("“老板娘!我...”\n\n\n“我忙着呢,没空理你~”\n\n\n");
  1208. else printf("这里好像没人可以聊天\n\n\n");
  1209. break;
  1210. case 4:
  1211. DisplayState();
  1212. break; //显示状态
  1213. case 5: //装备
  1214. printf("攻击装备: 1,匕首:%d个 2,长剑:%d个 3,碧血剑:%d个 4,绝世好剑:%d个\n\n\n",gong1n,gong2n,gong3n,gong4n);
  1215. printf("防御装备: 5,布衣:%d个 6,铁甲:%d个 7,银甲:%d个 8,黄金圣衣:%d个\t\t0,返回\n\n\n",fang1n,fang2n,fang3n,fang4n);
  1216. printf("选择要装备的武器或防具:\n\n\n");
  1217. switch(scanf("%d",&choose_number),choose_number)
  1218. {
  1219. case 1:
  1220. if(gong1n>=1)
  1221. {
  1222. printf("拿起了匕首\n\n\n");
  1223. gong=gong1;
  1224. strcpy(gongname,"匕首");
  1225. }
  1226. else printf("你没有匕首可以装备\n\n\n");
  1227. break;
  1228. case 2:
  1229. if(gong2n>=1)
  1230. {
  1231. printf("拿起了长剑\n\n\n");
  1232. gong=gong2;
  1233. strcpy(gongname,"长剑");
  1234. }
  1235. else printf("你没有长剑可以装备\n\n\n");
  1236. break;
  1237. case 3:
  1238. if(gong3n>=1)
  1239. {
  1240. printf("拿起了碧血剑\n\n\n");
  1241. gong=gong3;
  1242. strcpy(gongname,"碧血剑");
  1243. }
  1244. else printf("你没有碧血剑可以装备\n\n\n");
  1245. break;
  1246. case 4:
  1247. if(gong4n>=1)
  1248. {
  1249. printf("拿起了绝世好剑\n\n\n");
  1250. gong=gong4;
  1251. strcpy(gongname,"绝世好剑");
  1252. }
  1253. else printf("你没有绝世好剑可以装备\n\n\n");
  1254. break;
  1255. case 5:
  1256. if(fang1n>=1)
  1257. {
  1258. printf("穿上了布衣\n\n\n");
  1259. fang=fang1;
  1260. strcpy(fangname,"布衣");
  1261. }
  1262. else printf("你没有布衣可以装备\n\n\n");
  1263. break;
  1264. case 6:
  1265. if(fang2>=1)
  1266. {
  1267. printf("穿上了铁甲\n\n\n");
  1268. fang=fang2;
  1269. strcpy(fangname,"铁甲");
  1270. }
  1271. else printf("你没有铁甲可以装备\n\n\n");
  1272. break;
  1273. case 7:
  1274. if(fang3n>=1)
  1275. {
  1276. printf("穿上了银甲\n\n\n");
  1277. fang=fang3;
  1278. strcpy(fangname,"银甲");
  1279. }
  1280. else printf("你没有银甲可以装备\n\n\n");
  1281. break;
  1282. case 8:
  1283. if(fang4n>=1)
  1284. {
  1285. printf("穿上了黄金圣衣\n\n\n");
  1286. fang=fang4;
  1287. strcpy(fangname,"黄金圣衣");
  1288. }
  1289. else printf("你没有黄金圣衣可以装备\n\n\n");
  1290. break;
  1291. case 0:
  1292. printf("未更换装备\n\n\n");
  1293. break;
  1294. default:
  1295. printf("change error!");
  1296. }
  1297. break;
  1298. case 6:
  1299. printf(" 您好,欢迎您玩苍穹世界。为了给您更好的游戏体验,本团队时不时会优化本游戏,优化后会尽快发布在网上。关于外挂方面,开启外挂的方式是设定勇者姓名时,输入“圣战斗士 ”(不包括双引号)。由于2.0版本的buy,我们在2.0的基础上进行修改,已修复该buy。并且新增了经验草等有助于升级的道具,希望大家喜欢。在这里要感谢离陌同学,他给了我们许多宝贵的建议,谢谢。\n");
  1300. break;
  1301. case 0:
  1302. printf("确定退出游戏?(Y/N)\n\n\n");
  1303. getchar();
  1304. proof=getchar();
  1305. if(proof=='y'||proof=='Y')
  1306. {
  1307. printf("数据存储中...");
  1308. //向文件中更新数据;
  1309. getchar();
  1310. printf("按回车退出");
  1311. getchar();
  1312. return;
  1313. }
  1314. else if(proof=='n'||proof=='N')printf("继续游戏!\n\n\n");
  1315. else printf("继续!\n\n\n");
  1316. break;
  1317. default:
  1318. printf("输入错误!\n\n\n");
  1319. }
  1320. }
  1321. }
  1322. void DisplayState()
  1323. {
  1324. printf("%s 攻击力:%d+%d=%d 防御力:%d+%d=%d HP:%d/%d \n\n\n",player.name,player.attack,gong,player.attack+gong,player.defense,fang,player.defense+fang,player.health,player.max_health);
  1325. printf("武器: %s 防具: %s \n\n\n",gongname,fangname);
  1326. printf("等级:%d 经验:%d/%d 还需要%d经验升级 金币:%d \n\n\n",player.level,player.exp,player.max_exp,player.max_exp-player.exp,money);
  1327. }
  1328. void BattleAct()
  1329. {
  1330. while(1)
  1331. {
  1332. puts("=============================================================================");
  1333. printf("要怎么办?\n\n\n 1,攻击 2,物品 3,查看状态 4,逃跑\n\n\n");
  1334. switch(scanf("%d",&choose_number),choose_number)
  1335. {
  1336. case 1:
  1337. s=SuiJi();
  1338. printf("%s攻击! %sHP减少%d\n\n\n",player.name,guai.name,player.attack+s+gong-guai.defense/3);
  1339. guai.health-=player.attack+s+gong-guai.defense/3;
  1340. if(AttackResult())return; //如果攻击有结果(敌人或玩家战死)退出函数
  1341. else continue;
  1342. case 2:
  1343. ChooseWupin();
  1344. break; //选择物品,可以使用,战斗中允许使用攻击性物品
  1345. case 3:
  1346. DisplayState();
  1347. break; //显示状态
  1348. case 4:
  1349. s=SuiJi();
  1350. if(s<4) //40%的概率可以逃跑
  1351. {
  1352. printf("%s逃跑了~\n\n\n",player.name);
  1353. battle=0;
  1354. return;
  1355. }
  1356. else printf("%s逃跑失败!\n\n\n",player.name);
  1357. break;
  1358. default:
  1359. printf("输入错误,重新输入!\n\n\n");
  1360. }
  1361. }
  1362. }
  1363. void printf(char *p)
  1364. {
  1365. while(1)
  1366. {
  1367. if(*p!=0)
  1368. printf("%c",*p++);
  1369. else
  1370. break;
  1371. Sleep(100);
  1372. }
  1373. }

下个五子棋吧:

  1. #include<iostream>
  2. #include<time.h>
  3. #include<stdlib.h>
  4. using namespace std;
  5. const int N = 15;//棋盘大小
  6. const char ChessBoardFlag = ' ';
  7. const char flag1 = 'O';
  8. const char flag2 = 'X';
  9. typedef struct Coordinate
  10. {
  11. int x;
  12. int y;
  13. }ChessCoordi;
  14. class FiveChess
  15. {
  16. public:
  17. FiveChess()
  18. {
  19. InitChessBoard();
  20. }
  21. void Play()
  22. {
  23. ChessCoordi Pos1;
  24. ChessCoordi Pos2;
  25. while (1){
  26. int mode = ChoseMode();
  27. while (1){
  28. if (mode == 1){//玩家VS电脑
  29. static size_t count = 1;
  30. PalyerGo(Pos1, 1, flag1);
  31. if (count++ >= 9 && GetWiner(Pos1, 1, flag1))
  32. break;
  33. ComputerGo(Pos2, flag2);
  34. if (count++ >= 10 && GetWiner(Pos2, 0, flag2))
  35. break;
  36. }
  37. else if (mode == 2){//玩家VS玩家
  38. static size_t count = 1;
  39. PalyerGo(Pos1, 1, flag1);
  40. if (count++ >= 9 && GetWiner(Pos1,1, flag1))
  41. break;
  42. PalyerGo(Pos2,2 ,flag2);
  43. if (count++ >= 10 && GetWiner(Pos2,2, flag2))
  44. break;
  45. }
  46. }
  47. cout << "再来一局 y or no" << endl;
  48. char chose = 'y';
  49. cin >> chose;
  50. if (chose == 'n')
  51. break;
  52. }
  53. }
  54. void PrintChessBoard()
  55. {
  56. system("cls");
  57. for (size_t i = 0; i < N + 1; ++i)
  58. {
  59. for (size_t j = 0; j < N + 1; ++j)
  60. {
  61. if (i == 0){
  62. if (j != 0)
  63. printf("%d ", j);
  64. else if (j == 0)
  65. printf(" ");
  66. }
  67. else if (j == 0){
  68. if (i != 0)
  69. printf("%2d", i);
  70. }
  71. else{
  72. printf("%c |", ChessBoard[i][j]);
  73. }
  74. }
  75. cout << endl;
  76. cout << " ";
  77. for (size_t i = 1; i < N + 1; ++i){
  78. cout << "---+";
  79. }
  80. cout << endl;
  81. }
  82. }
  83. void InitChessBoard()
  84. {
  85. for (size_t i = 0; i < N + 1; ++i){
  86. for (size_t j = 0; j < N + 1; ++j){
  87. ChessBoard[i][j] = ChessBoardFlag;
  88. }
  89. }
  90. }
  91. protected:
  92. int ChoseMode()
  93. {
  94. system("cls");
  95. InitChessBoard();
  96. cout << "请选择 1.玩家VS电脑 2.玩家VS玩家 3.退出" << endl;
  97. int chose = 0;
  98. cin >> chose;
  99. while (1){
  100. if (chose == 1)
  101. return chose;
  102. else if (chose == 2)
  103. return chose;
  104. else if (chose == 3)
  105. exit(1);
  106. else
  107. cout << "对不起 您的输入有误。。" << endl;
  108. }
  109. }
  110. void PalyerGo(ChessCoordi& Pos, int player, char flag)
  111. {
  112. PrintChessBoard();
  113. int x = 0;
  114. int y = 0;
  115. while (1){
  116. cout << "请玩家" << player << "下一步棋" << endl;
  117. cin >> Pos.x >> Pos.y;
  118. if (JudgePos(Pos))
  119. break;
  120. else
  121. cout << "玩家输入错误 ! 请重新输入" << endl;
  122. }
  123. ChessBoard[Pos.x][Pos.y] = flag;
  124. }
  125. void ComputerGo(ChessCoordi& Pos, char flag)
  126. {
  127. PrintChessBoard();
  128. int x = 0;
  129. int y = 0;
  130. while (1){
  131. x = rand() % N + 1;
  132. srand((unsigned int)time(NULL));
  133. y = rand() % N + 1;
  134. srand((unsigned int)time(NULL));//这种方式下生成的x,y一定在棋盘上
  135. if (ChessBoard[x][y] == ChessBoardFlag)
  136. break;
  137. }
  138. Pos.x = x;
  139. Pos.y = y;
  140. ChessBoard[Pos.x][Pos.y] = flag;
  141. }
  142. int GetVictory(ChessCoordi Pos, char flag)//判断是否有赢家
  143. {
  144. int begin = 0;//在检查对角线时 作为行坐标的开始 结束
  145. int end = 0;
  146. //检查行是否连续5个子
  147. int beginl = 0;//在检查对角线时 作为列坐标的开始 结束
  148. int endl = 0;
  149. (Pos.y - 4) > 0 ? begin = Pos.y - 4 : begin = 1;
  150. (Pos.y + 4) < N ? end = Pos.y + 4 : end = N;
  151. for (size_t i = Pos.x, j = begin; j + 4 <= end; ++j)
  152. {
  153. if (flag == ChessBoard[i][j] && flag == ChessBoard[i][j + 1] && \
  154. flag == ChessBoard[i][j + 2] && flag == ChessBoard[i][j + 3] && \
  155. flag == ChessBoard[i][j + 4])
  156. return 1;
  157. }
  158. //检查列是否连续5个子
  159. (Pos.x - 4) > 0 ? begin = Pos.x - 4 : begin = 1;
  160. (Pos.x + 4) > N ? end = Pos.x + 4 : end = N;
  161. for (size_t j = Pos.y, i = begin; i + 4 <= end; ++i)
  162. {
  163. if (flag == ChessBoard[i][j] && flag == ChessBoard[i + 1][j] && \
  164. flag == ChessBoard[i + 2][j] && flag == ChessBoard[i + 3][j] && \
  165. flag == ChessBoard[i + 4][j])
  166. return 1;
  167. }
  168. int len = 0;
  169. //检查主对角线是否满五个子
  170. (Pos.x > Pos.y) ? len = Pos.y - 1 : len = Pos.x - 1;
  171. if (len > 4)//找落子点到上 左两边的垂直距离较短的地方 如果其大于4 取4 不大于4 取其本身
  172. len = 4;
  173. begin = Pos.x - len;//向上 左移动适当距离找可能的五连子的起始位置
  174. beginl = Pos.y - len;
  175. (Pos.x > Pos.y) ? len = N - Pos.x : len = N - Pos.y;
  176. if (len > 4)
  177. len = 4;
  178. end = Pos.x + len;//向下 右移动适当距离找可能的五连子的终止位置
  179. endl = Pos.y + len;
  180. for (size_t i = begin, j = beginl; i + 4 <= end && j + 4 <= endl; ++i, ++j)
  181. {
  182. if (flag == ChessBoard[i][j] && flag == ChessBoard[i + 1][j + 1] && \
  183. flag == ChessBoard[i + 2][j + 2] && flag == ChessBoard[i + 3][j + 3] && \
  184. flag == ChessBoard[i + 4][j + 4])
  185. return 1;
  186. }
  187. //检查副对角线是否满五个子
  188. (Pos.x - 1 > N - Pos.y) ? len = N - Pos.y : Pos.x - 1;
  189. if (len > 4)//找落子点到右 下两边的垂直距离较短的地方 如果其大于4 取4 不大于4 取其本身
  190. len = 4;
  191. begin = Pos.x - len;//向上 右移动适当距离找可能的五连子的起始位置
  192. beginl = Pos.y + len;
  193. (N - Pos.x > Pos.y - 1) ? len = Pos.y - 1 : len = N - Pos.x;
  194. end = Pos.x + len;//向下 左移动适当距离找可能的五连子的最终位置
  195. endl = Pos.y - len;
  196. for (size_t i = begin, j = beginl; i + 4 <= end && j - 4 >= endl; ++i, ++j)
  197. {
  198. if (flag == ChessBoard[i][j] && flag == ChessBoard[i + 1][j - 1] && \
  199. flag == ChessBoard[i + 2][j - 2] && flag == ChessBoard[i + 3][j - 3] && \
  200. flag == ChessBoard[i + 4][j - 4])
  201. return 1;
  202. }
  203. //检查棋盘是否已满
  204. for (size_t i = 1; i < N + 1; ++i){
  205. for (size_t j = 1; j < N + 1; ++j){
  206. if (ChessBoard[i][j] == ChessBoardFlag)
  207. return 0;//表示棋盘没满
  208. }
  209. }
  210. //和棋
  211. return -1;
  212. }
  213. bool GetWiner(ChessCoordi& Pos, int player, char flag)//判断是谁赢了
  214. {
  215. int n = 0;
  216. n = GetVictory(Pos, flag);
  217. PrintChessBoard();
  218. if (1 == n){
  219. if (0 == player)
  220. cout << "玩家1获胜" << endl;
  221. else if (1 == player)
  222. cout << "玩家1获胜" << endl;
  223. else
  224. cout << "电脑获胜"<<endl;
  225. return true;
  226. }
  227. else if (-1 == n){
  228. cout << "和棋" << endl;
  229. return true;
  230. }
  231. else{
  232. //还未分出胜负
  233. return false;
  234. }
  235. }
  236. bool JudgePos(const ChessCoordi& Pos)
  237. {
  238. if (Pos.x < N + 1 && Pos.x > 0 && Pos.y < N + 1 && Pos.x > 0\
  239. && ChessBoard[Pos.x][Pos.y] == ChessBoardFlag)
  240. return true;
  241. return false;
  242. }
  243. private:
  244. char ChessBoard[N + 1][N + 1];
  245. };
  246. //#include"FiveChess.cpp"
  247. int main()
  248. {
  249. //char a[] = "exit";
  250. //for (size_t i = 0; i < sizeof(a) / sizeof(char);++i)
  251. //printf(":%d", a[i]);
  252. FiveChess a;
  253. a.InitChessBoard();
  254. a.PrintChessBoard();
  255. a.Play();
  256. system("pause");
  257. return 0;
  258. }

六个代码分享完了,你觉得怎么样?不喜勿喷。

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

闽ICP备14008679号