当前位置:   article > 正文

10段代码教你玩转C++_c++一些好玩的代码

c++一些好玩的代码

1.随机迷宫:

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <windows.h>
  4. #include <time.h>
  5. #define Height 31 //迷宫的高度,必须为奇数
  6. #define Width 25 //迷宫的宽度,必须为奇数
  7. #define Wall 1
  8. #define Road 0
  9. #define Start 2
  10. #define End 3
  11. #define Esc 5
  12. #define Up 1
  13. #define Down 2
  14. #define Left 3
  15. #define Right 4
  16. int map[Height+2][Width+2];
  17. void gotoxy(int x,int y) //移动坐标
  18. {
  19. /* typedef struct _COORD {
  20. SHORT X;
  21. SHORT Y;
  22. } COORD, *PCOORD; */
  23. COORD coord;
  24. coord.X=x;
  25. coord.Y=y;
  26. SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
  27. }
  28. void hidden()//隐藏光标
  29. {
  30. /* typedef void *HANDLE; */
  31. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  32. /* typedef struct _CONSOLE_CURSOR_INFO {
  33. DWORD dwSize;
  34. BOOL bVisible;
  35. } CONSOLE_CURSOR_INFO *PCONSOLE_CURSOR_INFO; */
  36. CONSOLE_CURSOR_INFO cci;
  37. GetConsoleCursorInfo(hOut,&cci);
  38. cci.bVisible = 0;//赋1为显示,赋0为隐藏
  39. SetConsoleCursorInfo(hOut,&cci);
  40. }
  41. void create(int x,int y) //随机生成迷宫
  42. {
  43. int c[4][2] = {0,1,1,0,0,-1,-1,0}; //四个方向
  44. int i,j,t;
  45. //将方向打乱
  46. for(i=0;i<4;i++)
  47. {
  48. j = rand()%4;
  49. t = c[i][0];
  50. c[i][0] = c[j][0];
  51. c[j][0] = t;
  52. t = c[i][1];
  53. c[i][1] = c[j][1];
  54. c[j][1] = t;
  55. }
  56. map[x][y] = Road;
  57. for(i = 0;i < 4;i++)
  58. if(map[x + 2 * c[i][0]][y + 2 * c[i][1]] == Wall)
  59. {
  60. map[x + c[i][0]][y + c[i][1]] = Road;
  61. create(x+ 2 * c[i][0],y + 2 * c[i][1]);
  62. }
  63. }
  64. int get_key() //接收按键
  65. {
  66. char c;
  67. while(c = getch())
  68. {
  69. if(c == 27)
  70. return Esc; //Esc
  71. if(c != -32)
  72. continue;
  73. c = getch();
  74. if(c == 72)
  75. return Up; //上
  76. if(c == 80)
  77. return Down; //下
  78. if(c ==75 )
  79. return Left; //左
  80. if(c == 77)
  81. return Right; //右
  82. }
  83. return 0;
  84. }
  85. void paint(int x,int y) //画迷宫
  86. {
  87. gotoxy(2 * y - 2,x - 1);
  88. switch(map[x][y])
  89. {
  90. case Start:
  91. printf("入");break; //画入口
  92. case End:
  93. printf("出");break; //画出口
  94. case Wall:
  95. printf("※");break; //画墙
  96. case Road:
  97. printf(" ");break; //画路
  98. }
  99. }
  100. void game()
  101. {
  102. int x=2,y=1; //玩家当前位置,刚开始在入口处
  103. int c; //用来接收按键
  104. while(1)
  105. {
  106. gotoxy(2 * y - 2,x - 1);
  107. printf("☆"); //画出玩家当前位置
  108. if(map[x][y] == End) //判断是否到达出口
  109. {
  110. gotoxy(30,24);
  111. printf("到达终点,按任意键结束");
  112. getch();
  113. break;
  114. }
  115. c = get_key();
  116. if(c == Esc)
  117. {
  118. gotoxy(0,24);
  119. break;
  120. }
  121. switch(c)
  122. {
  123. case Up: //向上走
  124. if(map[x-1][y] != Wall)
  125. {
  126. paint(x,y);
  127. x--;
  128. }
  129. break;
  130. case Down: //向下走
  131. if(map[x+1][y] != Wall)
  132. {
  133. paint(x,y);
  134. x++;
  135. }
  136. break;
  137. case Left: //向左走
  138. if(map[x][y-1] != Wall)
  139. {
  140. paint(x,y);
  141. y--;
  142. }
  143. break;
  144. case Right: //向右走
  145. if(map[x][y+1] != Wall)
  146. {
  147. paint(x,y);
  148. y++;
  149. }
  150. break;
  151. }
  152. }
  153. }
  154. int main()
  155. {
  156. int i,j;
  157. srand((unsigned)time(NULL)); //初始化随即种子
  158. hidden(); //隐藏光标
  159. for(i = 0;i <= Height + 1;i++)
  160. for(j = 0;j <= Width + 1;j++)
  161. if(i == 0 || i == Height + 1 || j == 0 || j == Width + 1) //初始化迷宫
  162. map[i][j] = Road;
  163. else
  164. map[i][j] = Wall;
  165. create(2 * (rand() % (Height / 2)+1),2 * (rand() % (Width / 2) + 1)); //从随机一个点开始生成迷宫,该点行列都为偶数
  166. for(i = 0;i <= Height+1;i++) //边界处理
  167. {
  168. map[i][0]=Wall;
  169. map[i][Width+1]=Wall;
  170. }
  171. for(j=0;j<=Width+1;j++) //边界处理
  172. {
  173. map[0][j]=Wall;
  174. map[Height+1][j]=Wall;
  175. }
  176. map[2][1]=Start; //给定入口
  177. map[Height-1][Width]=End; //给定出口
  178. for(i=1;i<=Height;i++)
  179. for(j=1;j<=Width;j++) //画出迷宫
  180. paint(i,j);
  181. game(); //开始游戏
  182. getch();
  183. return 0;
  184. }

2.文字游戏

  1. #include <iostream>
  2. using namespace std;
  3. double shengmingli=200000000000;//定义主角初始生命力
  4. int gongjili=150000;//定义主角初始攻击力
  5. int fangyuli=2000000;//定义主角初始防御力
  6. int money=2000000;//定义主角初始金钱数量
  7. bool guoguan;//定义是否通关判定
  8. void wuqidian();//定义武器店函数
  9. void yaodian();//定义药店函数
  10. void guaiwu1();//定义小怪物函数
  11. void guaiwu2();//定义大怪物函数
  12. int main()
  13. {
  14. cout<<"欢迎你开始玩打怪物小游戏!\n";
  15. cout<<"小镇\n";
  16. cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
  17. cout<<"和一家武器店。\n";
  18. int xiaozhen;//定义选择项目
  19. cout<<"1.去武器店"<<endl;
  20. cout<<"2.去药品店"<<endl;
  21. cout<<"3.去打小怪物"<<endl;
  22. cout<<"4.去打大怪物"<<endl;
  23. cout<<"5.退出游戏"<<endl;
  24. cout<<"6.显示你的状态"<<endl;
  25. cin>>xiaozhen;
  26. while(xiaozhen!=5)//输入5时退出游戏
  27. {
  28. if(shengmingli<=0)//主角生命力小于等于0时游戏结束
  29. {
  30. cout<<"你死啦!"<<endl;
  31. break;
  32. }
  33. if(guoguan)
  34. {
  35. cout<<"恭喜通关!"<<endl;
  36. break;
  37. }
  38. if(xiaozhen==6)//输入6可检测自己的状态
  39. {
  40. cout<<"你的生命力:"<<shengmingli<<endl;
  41. cout<<"你的攻击力:"<<gongjili<<endl;
  42. cout<<"你的防御力:"<<fangyuli<<endl;
  43. cout<<"你拥有的钱:"<<money<<endl;
  44. }
  45. else
  46. switch(xiaozhen)
  47. {
  48. case 1 : wuqidian();break;
  49. case 2 : yaodian();break;
  50. case 3 : guaiwu1();break;
  51. case 4 : guaiwu2();break;
  52. default : cout<<"请不要乱选!"<<endl;break;
  53. }
  54. cin>>xiaozhen;
  55. }
  56. if(xiaozhen==5)
  57. {
  58. cout<<"正在退出游戏……"<<endl;
  59. }
  60. cin.get();
  61. cin.get();
  62. return 0;
  63. }
  64. void wuqidian()
  65. {
  66. cout<<"欢迎来到武器店!"<<endl;
  67. cout<<"1、买小刀(1M加2攻击力)"<<endl;
  68. cout<<"2、买短剑(2M加20攻击力)"<<endl;
  69. cout<<"3、买大砍刀(5M加40攻击力)"<<endl;
  70. cout<<"4、买双节棍(7M加60攻击力)"<<endl;
  71. cout<<"5、买盾牌(2M加30防御力)"<<endl;
  72. cout<<"6、买铠甲(5M加60防御力)"<<endl;
  73. cout<<"7、离开武器店"<<endl;
  74. int wuqidian;
  75. cin>>wuqidian;
  76. while(wuqidian!=7)//输入7时结束函数
  77. {
  78. switch(wuqidian)
  79. {
  80. case 1 : if(money<10)
  81. cout<<"你的钱不够"<<endl;//钱不够时返回Flase
  82. else
  83. cout<<"购买成功!"<<endl;//钱足够时返回True
  84. gongjili+=2;
  85. money-=1;
  86. break;
  87. case 2 : if(money<80)
  88. cout<<"你的钱不够"<<endl;
  89. else
  90. cout<<"购买成功!"<<endl;
  91. gongjili+=20;
  92. money-=80;
  93. break;
  94. case 3 : if(money<140)
  95. cout<<"你的钱不够"<<endl;
  96. else
  97. cout<<"购买成功!"<<endl;
  98. gongjili+=40;
  99. money-=140;
  100. break;
  101. case 4 : if(money<200)
  102. cout<<"你的钱不够"<<endl;
  103. else
  104. cout<<"购买成功!"<<endl;
  105. gongjili+=60;
  106. money-=200;
  107. break;
  108. case 5 : if(money<60)
  109. cout<<"你的钱不够"<<endl;
  110. else
  111. cout<<"购买成功!"<<endl;
  112. fangyuli+=30;
  113. money-=60;
  114. break;
  115. fangyuli+=60;
  116. money-=100;
  117. break;
  118. default : cout<<"无"<<endl;
  119. break;
  120. }
  121. cin>>wuqidian;
  122. }
  123. if(wuqidian==7)
  124. { //返回main()主函数
  125. cout<<"欢迎下次再来!"<<endl;
  126. cout<<"欢迎你开始玩打怪物小游戏!\n";
  127. cout<<"小镇\n";
  128. cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
  129. cout<<"和一家武器店。\n";
  130. cout<<"1.去武器店"<<endl;
  131. cout<<"2.去药品店"<<endl;
  132. cout<<"3.去打小怪物"<<endl;
  133. cout<<"4.去打大怪物"<<endl;
  134. cout<<"5.退出游戏"<<endl;
  135. cout<<"6.显示你的状态"<<endl;
  136. }
  137. }
  138. /*
  139. yaodian()的设置与wuqidian()相同,可参照阅读.
  140. */
  141. void yaodian()
  142. {
  143. cout<<"欢迎来到药品店!"<<endl;
  144. cout<<"1、买1号补血药(10M加200生命)"<<endl;
  145. cout<<"2、买2号补血药(50M加1000生命力)"<<endl;
  146. cout<<"3、买3号补血药(100M加2200生命力)"<<endl;
  147. cout<<"4、离开武器店"<<endl;
  148. int yaodian;
  149. cin>>yaodian;
  150. while(yaodian!=4)
  151. {
  152. switch(yaodian)
  153. {
  154. case 1 : if(money<10)
  155. cout<<"你的钱不够"<<endl;
  156. else
  157. cout<<"购买成功!"<<endl;
  158. shengmingli+=200;
  159. money-=10;
  160. break;
  161. case 2 : if(money<50)
  162. cout<<"你的钱不够"<<endl;
  163. else
  164. cout<<"购买成功!"<<endl;
  165. shengmingli+=1000;
  166. money-=50;
  167. break;
  168. case 3 : if(money<100)
  169. cout<<"你的钱不够"<<endl;
  170. else
  171. cout<<"购买成功!"<<endl;
  172. shengmingli+=2200;
  173. money-=100;
  174. break;
  175. default : cout<<"无"<<endl;
  176. break;
  177. }
  178. cin>>yaodian;
  179. }
  180. if(yaodian==4)
  181. {
  182. cout<<"欢迎下次再来!"<<endl;
  183. cout<<"欢迎你开始玩打怪物小游戏!\n";
  184. cout<<"小镇\n";
  185. cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
  186. cout<<"和一家武器店。\n";
  187. cout<<"1.去武器店"<<endl;
  188. cout<<"2.去药品店"<<endl;
  189. cout<<"3.去打小怪物"<<endl;
  190. cout<<"4.去打大怪物"<<endl;
  191. cout<<"5.退出游戏"<<endl;
  192. cout<<"6.显示你的状态"<<endl;
  193. }
  194. }
  195. /*这里是两个战斗函数,使用指针来处理.避免造成内存崩溃.*/
  196. void guaiwu1()
  197. {
  198. cout<<"开始与小怪物战斗!!!"<<endl;
  199. double* g_shengmingli=new double;//定义怪物生命
  200. int* g_gongjili=new int;//定义怪物攻击力
  201. int* g_fangyuli=new int;//定义怪物防御力
  202. int* g_money=new int;//定义怪物金钱
  203. *g_shengmingli=100;
  204. *g_gongjili=5;
  205. *g_fangyuli=3;
  206. *g_money=5;
  207. double* tongji1=new double;//用来计算主角对怪物的杀伤
  208. double* tongji2=new double;//用来计算怪物对主角的杀伤
  209. *tongji1=0;
  210. *tongji2=0;
  211. int* huihe=new int;//定义回合数
  212. *huihe=1;
  213. cout<<"你开始对小怪物进行攻击!"<<endl;
  214. int* xuanze=new int;
  215. /*
  216. 攻击计算公式
  217. 杀伤=攻击力*2-防御力
  218. 玩家每回合可以选择攻击与逃跑
  219. */
  220. while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2)
  221. {
  222. cout<<"现在是"<<"第"<<*huihe<<"回合!"<<endl;
  223. cout<<"请选择你的动作:\n";
  224. cout<<"1、攻击\n2、逃跑\n";
  225. cin>>*xuanze;
  226. switch((*xuanze))
  227. {
  228. case 1 : cout<<"你对小怪物发动了攻击!"<<endl;
  229. *g_shengmingli-=gongjili*2-(*g_fangyuli);
  230. *tongji1=gongjili*2-(*g_fangyuli);
  231. cout<<"你打掉了小怪物"<<*tongji1<<"的生命!"<<endl;
  232. cout<<"小怪物还剩"<<(*g_shengmingli)-(*tongji1)<<"点生命"<<endl;
  233. shengmingli-=(*g_gongjili)*2-fangyuli;
  234. *tongji2=(*g_gongjili)*2-fangyuli;
  235. cout<<"小怪物对你发动了攻击!"<<endl;
  236. cout<<"小怪物打掉了你"<<*tongji2<<"的生命!"<<endl;
  237. cout<<"你还剩"<<shengmingli-(*tongji2)<<"点生命"<<endl;break;
  238. case 2 : cout<<"你决定逃跑!"<<endl;
  239. cout<<"逃跑成功!"<<endl;continue;
  240. default : cout<<"请不要乱选!"<<endl;
  241. }
  242. (*huihe)++;
  243. }
  244. if((*g_shengmingli)<=0)
  245. {//杀死怪物后的返回
  246. cout<<"小怪物被你杀死了!你真厉害!!!"<<endl;
  247. money+=(*g_money);
  248. cout<<"欢迎你开始玩打怪物小游戏!\n";
  249. cout<<"小镇\n";
  250. cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
  251. cout<<"和一家武器店。\n";
  252. cout<<"1.去武器店"<<endl;
  253. cout<<"2.去药品店"<<endl;
  254. cout<<"3.去打小怪物"<<endl;
  255. cout<<"4.去打大怪物"<<endl;
  256. cout<<"5.退出游戏"<<endl;
  257. cout<<"6.显示你的状态"<<endl;
  258. }
  259. else
  260. if(shengmingli<=0)
  261. {//被怪物杀死后的返回
  262. cout<<"你被小怪物杀死了!游戏结束!!!"<<endl;
  263. }
  264. else
  265. if((*xuanze)==2)
  266. {//逃跑的返回
  267. cout<<"你逃回了小镇!"<<endl;
  268. cout<<"欢迎你开始玩打怪物小游戏!\n";
  269. cout<<"小镇\n";
  270. cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
  271. cout<<"和一家武器店。\n";
  272. cout<<"1.去武器店"<<endl;
  273. cout<<"2.去药品店"<<endl;
  274. cout<<"3.去打小怪物"<<endl;
  275. cout<<"4.去打大怪物"<<endl;
  276. cout<<"5.退出游戏"<<endl;
  277. cout<<"6.显示你的状态"<<endl;
  278. }
  279. delete g_shengmingli;
  280. delete g_gongjili;
  281. delete g_fangyuli;
  282. delete g_money;
  283. delete tongji1;
  284. delete tongji2;
  285. }
  286. /*
  287. 设置均与void函数guaiwu1()相同,可参照上例阅读.
  288. */
  289. void guaiwu2()
  290. {
  291. cout<<"开始与大怪物战斗!!!"<<endl;
  292. double* g_shengmingli=new double;
  293. int* g_gongjili=new int;
  294. int* g_fangyuli=new int;
  295. *g_shengmingli=3600;
  296. *g_gongjili=500;
  297. *g_fangyuli=500;
  298. double* tongji1=new double;
  299. double* tongji2=new double;
  300. *tongji1=0;
  301. *tongji2=0;
  302. int* huihe=new int;
  303. *huihe=1;
  304. cout<<"你开始对大怪物进行攻击!"<<endl;
  305. int* xuanze=new int;
  306. while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2)
  307. {
  308. cout<<"现在是"<<"第"<<*huihe<<"回合!"<<endl;
  309. cout<<"请选择你的动作:\n";
  310. cout<<"1、攻击\n2、逃跑\n";
  311. cin>>*xuanze;
  312. switch((*xuanze))
  313. {
  314. case 1 : cout<<"你对大怪物发动了攻击!"<<endl;
  315. *g_shengmingli-=gongjili*2-(*g_fangyuli);
  316. *tongji1=gongjili*2-(*g_fangyuli);
  317. cout<<"你打掉了大怪物"<<*tongji1<<"的生命!"<<endl;
  318. cout<<"大怪物还剩"<<(*g_shengmingli)-(*tongji1)<<"点生命"<<endl;
  319. shengmingli-=(*g_gongjili)*2-fangyuli;
  320. *tongji2=(*g_gongjili)*2-fangyuli;
  321. cout<<"大怪物对你发动了攻击!"<<endl;
  322. cout<<"大怪物打掉了你"<<*tongji2<<"的生命!"<<endl;
  323. cout<<"你还剩"<<shengmingli-(*tongji2)<<"点生命"<<endl;break;
  324. case 2 : cout<<"你决定逃跑!"<<endl;
  325. cout<<"逃跑成功!"<<endl;continue;
  326. default : cout<<"请不要乱选!"<<endl;
  327. }
  328. (*huihe)++;
  329. }
  330. if((*g_shengmingli)<=0)
  331. {
  332. cout<<"大怪物被你杀死了!你真厉害!!!"<<endl;
  333. guoguan=true;
  334. cout<<"欢迎你开始玩打怪物小游戏!\n";
  335. cout<<"小镇\n";
  336. cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
  337. cout<<"和一家武器店。\n";
  338. cout<<"1.去武器店"<<endl;
  339. cout<<"2.去药品店"<<endl;
  340. cout<<"3.去打小怪物"<<endl;
  341. cout<<"4.去打大怪物"<<endl;
  342. cout<<"5.退出游戏"<<endl;
  343. cout<<"6.显示你的状态"<<endl;
  344. }
  345. else
  346. if(shengmingli<=0)
  347. {
  348. cout<<"你被大怪物杀死了!游戏结束!!!"<<endl;
  349. }
  350. else
  351. if((*xuanze)==2)
  352. {
  353. cout<<"你逃回了小镇!"<<endl;
  354. cout<<"欢迎你开始玩打怪物小游戏!\n";
  355. cout<<"小镇\n";
  356. cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
  357. cout<<"和一家武器店。\n";
  358. cout<<"1.去武器店"<<endl;
  359. cout<<"2.去药品店"<<endl;
  360. cout<<"3.去打小怪物"<<endl;
  361. cout<<"4.去打大怪物"<<endl;
  362. cout<<"5.退出游戏"<<endl;
  363. cout<<"6.显示你的状态"<<endl;
  364. }
  365. delete g_shengmingli;
  366. delete g_gongjili;
  367. delete g_fangyuli;
  368. delete tongji1;
  369. delete tongji2;
  370. }

3.另一个文字游戏

  1. #include<bits/stdc++.h>
  2. #include<conio.h>
  3. #include<windows.h>
  4. using namespace std;
  5. double shanghai[20]={0.6,1.1,2,3.16,5.5,7,10,20,50,100,146.23,254.13,312,403,601,1023};
  6. double bosshealth[20]={2,3,4,5.9,8,14,19,32,73,157,200,403,801,1200,3630,20123};
  7. double wj_shanghai=5000,wj_health=10000000,wj_max_health=10000000,boss,wj_money=10000000000000000000000000000000;
  8. void chushihua();
  9. void game();
  10. void gongji();
  11. void goumai();
  12. void shangdian();
  13. void zhujiemian();
  14. void fangyu();
  15. void cend();
  16. void chushou();
  17. void print(char[]);
  18. int bishou=0,caidao=0,jian=0,shenjian=0;
  19. double bishou_1=5,caidao_1=17,jian_1=58,shenjian_1=124;
  20. int hat=0,douhui=0,hudun=0,hunjia=0,shendun=0;
  21. double hat_1=7,douhui_1=21,hudun_1=49,hunjia_1=89,shendun_1=210.4;
  22. void cend()
  23. {
  24. system("cls");
  25. print("GAME OVER");
  26. exit(1);
  27. }
  28. void game()
  29. {
  30. int k;
  31. chushihua();
  32. IO:
  33. printf("请输入对手等级 (0~15)\n");
  34. scanf("%d",&k);
  35. if(k>15||k<0)
  36. {
  37. system("cls");
  38. goto IO;
  39. }
  40. boss=bosshealth[k];
  41. system("cls");
  42. while(wj_health>=0)
  43. {
  44. srand(time(NULL));
  45. QP:
  46. printf("1.逃跑 2.进攻\n");
  47. char s=getch();
  48. if(s<'1'||s>'2')
  49. {
  50. system("cls");
  51. goto QP;
  52. }
  53. if(s=='1')
  54. {
  55. system("cls");
  56. zhujiemian();
  57. }
  58. system("cls");
  59. double l=shanghai[k]*((rand()%2)+1)+fabs(double(rand()%100/100-2));
  60. printf("对手对你造成了%lf点伤害\n",l);
  61. wj_health-=l;
  62. printf("你当前剩余血量:%lf\n",wj_health);
  63. if(wj_health<=0)
  64. cend();
  65. double o=wj_shanghai*((rand()%2)+1)+double(rand()%10/10);
  66. boss-=o;
  67. printf("你对对手造成了%lf点伤害\n",o);
  68. printf("对手当前剩余血量:%lf\n\n",boss);
  69. if(boss<=0)
  70. {
  71. printf("胜利!\n获得%lf金币\n\n当前剩余血量:%lf\n",shanghai[k]+3,wj_health);
  72. wj_money+=shanghai[k]+3;
  73. printf("\n余额:%lf\n",wj_money);
  74. getch();
  75. if(k==15)
  76. {
  77. printf("恭喜玩家!游戏胜利!\n");
  78. getch();
  79. exit(1);
  80. }
  81. system("cls");
  82. zhujiemian();
  83. }
  84. }
  85. }
  86. void zhujiemian()
  87. {
  88. PO:
  89. printf("1.商店 2.战斗 3.回血 4.状态\n");
  90. char k=getch();
  91. if(k>'4'||k<'1')
  92. {
  93. system("cls");
  94. goto PO;
  95. }
  96. if(k=='1')
  97. {
  98. system("cls");
  99. shangdian();
  100. return;
  101. }
  102. if(k=='2')
  103. {
  104. system("cls");
  105. game();
  106. return;
  107. }
  108. if(k=='3')
  109. {
  110. system("cls");
  111. if(wj_money>0)
  112. {
  113. wj_money=wj_money*4/5-1;
  114. chushihua();
  115. wj_health=wj_max_health;
  116. printf("回血成功!\n");
  117. getch();
  118. system("cls");
  119. goto PO;
  120. }
  121. else
  122. {
  123. printf("余额不足!\n");
  124. getch();
  125. system("cls");
  126. goto PO;
  127. }
  128. }
  129. if(k=='4')
  130. {
  131. chushihua();
  132. system("cls");
  133. printf("生命值:%lf\n",wj_health);
  134. printf("最大生命值:%lf\n",wj_max_health);
  135. printf("攻击力:%lf\n",wj_shanghai);
  136. printf("金币:%lf\n",wj_money);
  137. getch();
  138. system("cls");
  139. goto PO;
  140. }
  141. if(k=='5')
  142. {
  143. string a;
  144. system("cls");
  145. printf("输入密码!\n");
  146. cin>>a;
  147. if(a=="gu"||a=="gu")
  148. {
  149. wj_money+=1000;
  150. printf("外挂生效\n");
  151. Sleep(1000);
  152. system("cls");
  153. goto PO;
  154. }
  155. printf("外挂失败\n");
  156. Sleep(1000);
  157. system("cls");
  158. goto PO;
  159. }
  160. }
  161. void shangdian()
  162. {
  163. LK:
  164. printf("1.购买 2.返回主界面\n");
  165. char k=getch();
  166. if(k!='1'&&k!='2')
  167. {
  168. system("cls");
  169. goto LK;
  170. }
  171. if(k=='1')
  172. {
  173. system("cls");
  174. goumai();
  175. goto LK;
  176. }
  177. if(k=='2')
  178. {
  179. system("cls");
  180. zhujiemian();
  181. return;
  182. }
  183. }
  184. void goumai()
  185. {
  186. ML:
  187. printf("1.攻击 2.防御 3.返回主界面\n");
  188. char k=getch();
  189. if(k!='1'&&k!='2'&&k!='3')
  190. {
  191. system("cls");
  192. goto ML;
  193. }
  194. if(k=='1')
  195. {
  196. system("cls");
  197. gongji();
  198. goto ML;
  199. }
  200. if(k=='3')
  201. {
  202. system("cls");
  203. zhujiemian();
  204. return;
  205. }
  206. if(k=='2')
  207. {
  208. fangyu();
  209. }
  210. }
  211. void gongji()
  212. {
  213. OP:
  214. system("cls");
  215. printf("0.返回上界面\n");
  216. printf("1.返回主界面\n");
  217. printf("2.匕首 5金币\n");
  218. printf("3.菜刀 17金币\n");
  219. printf("4.剑 68金币\n");
  220. printf("5.圣剑 210金币\n");
  221. printf("提醒:金币价格与伤害成正比\n");
  222. char k=getch();
  223. if(k<'0'||k>'5')
  224. {
  225. system("cls");
  226. goto OP;
  227. }
  228. if(k=='0')
  229. {
  230. system("cls");
  231. goumai();
  232. return;
  233. }
  234. if(k=='1')
  235. {
  236. system("cls");
  237. zhujiemian();
  238. return;
  239. }
  240. if(k=='2')
  241. {
  242. if(wj_money>=bishou_1)
  243. {
  244. chushihua();
  245. system("cls");
  246. wj_money-=bishou_1;
  247. bishou++;
  248. goto OP;
  249. }
  250. system("cls");
  251. printf("余额不足!\n");
  252. getch();
  253. system("cls");
  254. goto OP;
  255. }
  256. if(k=='3')
  257. {
  258. if(wj_money>=caidao_1)
  259. {
  260. chushihua();
  261. system("cls");
  262. wj_money-=caidao_1;
  263. caidao++;
  264. goto OP;
  265. }
  266. system("cls");
  267. printf("余额不足!\n");
  268. getch();
  269. goto OP;
  270. }
  271. if(k=='4')
  272. {
  273. if(wj_money>=jian_1)
  274. {
  275. chushihua();
  276. system("cls");
  277. wj_money-=jian_1;
  278. jian++;
  279. goto OP;
  280. }
  281. system("cls");
  282. printf("余额不足!\n");
  283. getch();
  284. goto OP;
  285. }
  286. if(k=='5')
  287. {
  288. if(wj_money>=shenjian_1)
  289. {
  290. chushihua();
  291. system("cls");
  292. wj_money-=shenjian_1;
  293. shenjian++;
  294. goto OP;
  295. }
  296. system("cls");
  297. printf("余额不足!\n");
  298. getch();
  299. goto OP;
  300. }
  301. }
  302. void fangyu()
  303. {
  304. OP:
  305. system("cls");
  306. printf("0.返回上界面\n");
  307. printf("1.返回主界面\n");
  308. printf("2.帽子 7金币\n");
  309. printf("3.头盔 21金币\n");
  310. printf("4.护盾 49金币\n");
  311. printf("5.盔甲 89金币\n");
  312. printf("6.圣盾 210金币\n");
  313. printf("提醒:金币价格与伤害成正比\n");
  314. char k=getch();
  315. if(k<'0'||k>'6')
  316. {
  317. system("cls");
  318. goto OP;
  319. }
  320. if(k=='0')
  321. {
  322. system("cls");
  323. goumai();
  324. return;
  325. }
  326. if(k=='1')
  327. {
  328. system("cls");
  329. zhujiemian();
  330. return;
  331. }
  332. if(k=='2')
  333. {
  334. if(wj_money>=hat_1)
  335. {
  336. chushihua();
  337. system("cls");
  338. wj_money-=hat_1;
  339. hat++;
  340. goto OP;
  341. }
  342. system("cls");
  343. printf("余额不足!\n");
  344. getch();
  345. system("cls");
  346. goto OP;
  347. }
  348. if(k=='3')
  349. {
  350. if(wj_money>=douhui_1)
  351. {
  352. chushihua();
  353. system("cls");
  354. wj_money-=douhui_1;
  355. douhui++;
  356. goto OP;
  357. }
  358. system("cls");
  359. printf("余额不足!\n");
  360. getch();
  361. goto OP;
  362. }
  363. if(k=='4')
  364. {
  365. if(wj_money>=hudun_1)
  366. {
  367. chushihua();
  368. system("cls");
  369. wj_money-=hudun_1;
  370. hudun++;
  371. goto OP;
  372. }
  373. system("cls");
  374. printf("余额不足!\n");
  375. getch();
  376. goto OP;
  377. }
  378. if(k=='5')
  379. {
  380. chushihua();
  381. if(wj_money>=hunjia_1)
  382. {
  383. system("cls");
  384. wj_money-=hunjia_1;
  385. hunjia++;
  386. goto OP;
  387. }
  388. system("cls");
  389. printf("余额不足!\n");
  390. getch();
  391. goto OP;
  392. }
  393. if(k=='6')
  394. {
  395. if(wj_money>=shendun_1)
  396. {
  397. chushihua();
  398. system("cls");
  399. wj_money-=shendun_1;
  400. shendun++;
  401. goto OP;
  402. }
  403. system("cls");
  404. printf("余额不足!\n");
  405. getch();
  406. goto OP;
  407. }
  408. }
  409. void chushihua()
  410. {
  411. wj_max_health=hat*hat_1+douhui*douhui_1+hudun*hudun_1+hunjia*hunjia_1+shendun*shendun_1+10;
  412. wj_shanghai=bishou*bishou_1+caidao*caidao_1+jian*jian_1+shenjian*shenjian_1+1;
  413. }
  414. void print(char a[])
  415. {
  416. int s=strlen(a);
  417. for(int i=0;i<s;i++)
  418. {
  419. cout<<a[i];
  420. Sleep(400);
  421. }
  422. getch();
  423. system("cls");
  424. }
  425. int main()
  426. {
  427. system("title game");
  428. print("出品:谷游");
  429. zhujiemian();
  430. return 0;
  431. }

4.坦克大战

  1. #include <iostream>
  2. #include <time.h>
  3. #include <windows.h>
  4. #define W 1 //上
  5. #define S 2 //下
  6. #define A 3 //左
  7. #define D 4 //右
  8. #define L 4 // 坦克有4条命
  9. void HideCursor() { //隐藏光标
  10. CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
  11. SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
  12. }
  13. void GoToxy(int x, int y) { //光标移动,X、Y表示横、纵坐标
  14. COORD coord = { x, y };
  15. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  16. }
  17. //全局变量
  18. int map[50][40];//地图二维数组
  19. int B_num; //子弹编号
  20. int Pos; //敌方坦克生成位置,-1为左边,0为中间,1为右边,2为我的坦克位置
  21. int Speed = 7; //游戏速度
  22. int Enemy; //还未出现的敌人
  23. const char* Tank_Model[3][4] ={
  24. {"◢┃ ◣","◢╦ ◣","◢╦ ◣","◢╦ ◣"},
  25. {"╠ █ ╣","╠ █ ╣","━█ ╣","╠ █━"},
  26. {"◥╩ ◤","◥┃ ◤","◥╩ ◤","◥╩ ◤"}
  27. };
  28. //坦克
  29. class Tank{
  30. public:
  31. int x, y; //中心坐标
  32. int Direction; //方向
  33. int Model; //模型
  34. int Revival; //复活次数
  35. int Num; //敌方坦克编号
  36. bool Type; //我方坦克此参数为1
  37. bool Exist; //存活为1,不存活为0
  38. }AI_tank[6], my_tank;
  39. //子弹
  40. class Bullet{
  41. public:
  42. int x, y; //坐标
  43. int Direction; //方向
  44. bool Exist; //1为存在,0不存在
  45. bool Type; //0为敌方子弹,1为我方子弹
  46. }bullet[50] ;
  47. //基本函数
  48. void GoToxy(int x, int y); //光标移动
  49. void HideCursor(); //隐藏光标
  50. void Key(); //键盘输入
  51. void Init(); //初始化
  52. void Pause(); //暂停
  53. void Show(); //打印框架
  54. void Print_Map(); //打印地图
  55. void Cheak_Game(); //检测游戏胜负
  56. void GameOver(); //游戏结束
  57. //坦克
  58. void Creat_AI_T(Tank* AI_tank); //建立坦克
  59. void Creat_My_T(Tank* my_tank);
  60. void Move_AI_T(Tank* AI_tank);//坦克移动
  61. void Move_My_T(int turn);
  62. void Clear_T(int x, int y); //清除坦克
  63. void Print_T(Tank tank); //打印坦克
  64. bool Cheak_T(Tank tank, int direction); //检测障碍,1阻碍
  65. //子弹
  66. void Creat_AI_B(Tank* tank); //敌方坦克发射子弹
  67. void Creat_My_B(Tank tank);//我方坦克发射子弹
  68. void Move_B(Bullet bullet[50]); //子弹移动
  69. void Break_B(Bullet* bullet); //子弹碰撞
  70. void Print_B(int x, int y);//打印子弹
  71. void Clear_B(int x, int y); //清除子弹
  72. int Cheak_B(int x, int y); //子弹前方情况
  73. void Show() { //打印框架
  74. std::cout << " ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁";
  75. std::cout << "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁\n";
  76. for (int i = 0; i < 48; i++) {
  77. std::cout << "▕ ▏\n";
  78. }
  79. std::cout << " ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔";
  80. std::cout << "▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n";
  81. }
  82. void Print_Map() { // 打印地图
  83. int Map[50][40] = {
  84. //map里的值: 0为可通过陆地,1为砖,6为墙,100~105为敌方坦克,200为我的坦克,
  85. { 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4 },
  86. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  87. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  88. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  89. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  90. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  91. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  92. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  93. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  94. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  95. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  96. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  97. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  98. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  99. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  100. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  101. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  102. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  103. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  104. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  105. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,6,6,6,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  106. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,6,6,6,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  107. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  108. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  109. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  110. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  111. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  112. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  113. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  114. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  115. { 4,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,4 },
  116. { 4,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,4 },
  117. { 4,6,6,6,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,6,6,6,4 },
  118. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  119. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  120. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  121. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  122. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  123. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  124. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  125. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  126. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  127. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  128. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  129. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  130. { 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
  131. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,9,9,9,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  132. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,9,9,9,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  133. { 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,9,9,9,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
  134. { 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4 }
  135. };
  136. for (int i = 0; i < 50; i++)
  137. for (int j = 0; j < 40; j++)
  138. map[i][j] = Map[i][j];
  139. for (int i = 0; i < 50; i++)
  140. for (int j = 0; j < 40; j++)
  141. if (map[i][j] == 1) {
  142. GoToxy(2 * j, i);
  143. std::cout << "▓";
  144. }
  145. else if (map[i][j] == 6) {
  146. GoToxy(2 * j, i);
  147. std::cout << "■";
  148. }
  149. GoToxy(38, 46);
  150. std::cout << " ◣◢";
  151. GoToxy(38, 47);
  152. std::cout << "◣█ ◢";
  153. GoToxy(38, 48);
  154. std::cout << "◢█ ◣";
  155. }
  156. void Cheak_Game() {
  157. //敌人坦克全部不存活
  158. if (Enemy <= 0 && !AI_tank[0].Exist && !AI_tank[1].Exist && !AI_tank[2].Exist
  159. && !AI_tank[3].Exist && !AI_tank[4].Exist && !AI_tank[5].Exist)
  160. GameOver();
  161. if (my_tank.Revival >= L)//我复活次数用完
  162. GameOver();//游戏结束
  163. }
  164. void GameOver() {
  165. bool home = 1;
  166. while (home) {
  167. GoToxy(37, 21);
  168. std::cout << "游戏结束!";
  169. if (GetAsyncKeyState(0xD) & 0x8000) { //回车键
  170. system("cls"); //清屏
  171. Show();
  172. Init(); //初始化
  173. break;
  174. }
  175. else if (GetAsyncKeyState(0x1B) & 0x8000) //Esc键退出
  176. exit(0);
  177. }
  178. }
  179. void Creat_My_T(Tank* my_tank) {//建立我的坦克
  180. my_tank->x = 15;
  181. my_tank->y = 47;
  182. my_tank->Direction = 1;
  183. // my_tank->Model = 0;
  184. my_tank->Exist = 1;
  185. my_tank->Type = 1;
  186. Print_T(*my_tank); //打印我的坦克
  187. }
  188. void Move_My_T(int turn) {//turn为Key()函数传入的方向值
  189. Clear_T(my_tank.x, my_tank.y);
  190. my_tank.Direction = turn;
  191. if (Cheak_T(my_tank, my_tank.Direction)) //我方坦克当前方向上无障碍
  192. switch (turn) {
  193. case W: my_tank.y--; break; //上
  194. case S: my_tank.y++; break; //下
  195. case A: my_tank.x--; break; //左
  196. case D: my_tank.x++; break; //右
  197. }
  198. Print_T(my_tank);
  199. }
  200. void Print_T(Tank tank) {//打印
  201. for (int i = 0; i < 3; i++) {
  202. GoToxy((tank.x - 1) * 2, tank.y - 1 + i);//在坦克中心坐标的左边,上中下三行打印
  203. std::cout << Tank_Model[i][tank.Direction - 1]; //打印的是地址,地址既字符串
  204. for (int j = 0; j < 3; j++)
  205. if (tank.Type)//若为我的坦克
  206. map[tank.y + j - 1][tank.x + i - 1] = 200;
  207. //在map上敌方值为100~105,我方为200
  208. else
  209. map[tank.y + j - 1][tank.x + i - 1] = 100 +tank.Num;
  210. //这样可以通过map值读取坦克编号
  211. }
  212. }
  213. void Creat_AI_T(Tank* AI_tank) {
  214. AI_tank->x = 19 + 17 * (Pos); //pos为坦克生成位置,-1为左位置,0为中间,1为右,2为我的坦克位置
  215. AI_tank->y = 2;
  216. AI_tank->Direction = 2; //方向朝下
  217. AI_tank->Revival++; //复活次数+1
  218. AI_tank->Exist = 1;//存在
  219. Pos++;
  220. Enemy--;
  221. if (Pos == 2) //循环重置(pos只能为-1,0,1)
  222. Pos = -1;
  223. Print_T(*AI_tank);
  224. return;
  225. }
  226. void Move_AI_T(Tank* AI_tank) {
  227. if (AI_tank->Exist) { //存在
  228. Clear_T(AI_tank->x, AI_tank->y);
  229. if (Cheak_T(*AI_tank, AI_tank->Direction))//前方无障碍
  230. switch (AI_tank->Direction) {
  231. case W: AI_tank->y--; break; //上
  232. case S: AI_tank->y++; break; //下
  233. case A: AI_tank->x--; break; //左
  234. case D: AI_tank->x++; break; //右
  235. }
  236. else {//前方有障碍
  237. for (int i = rand() % 4 + 1; i <= 4; i++)
  238. if (Cheak_T(*AI_tank, i)){ //循环判断,返1可通过
  239. AI_tank->Direction = i;
  240. break;
  241. }
  242. }
  243. Print_T(*AI_tank); //打印敌方坦克
  244. }
  245. }
  246. bool Cheak_T(Tank tank, int direction) { //检测坦克前方障碍,返1为可通过
  247. switch (direction) {
  248. case W:
  249. if (map[tank.y - 2][tank.x] == 0 && map[tank.y - 2][tank.x - 1] == 0 && map[tank.y - 2][tank.x + 1] == 0)
  250. return 1;
  251. else return 0;
  252. case S:
  253. if (map[tank.y + 2][tank.x] == 0 && map[tank.y + 2][tank.x - 1] == 0 && map[tank.y + 2][tank.x + 1] == 0)
  254. return 1;
  255. else return 0;
  256. case A:
  257. if (map[tank.y][tank.x - 2] == 0 && map[tank.y - 1][tank.x - 2] == 0 && map[tank.y + 1][tank.x - 2] == 0)
  258. return 1;
  259. else return 0;
  260. case D:
  261. if (map[tank.y][tank.x + 2] == 0 && map[tank.y - 1][tank.x + 2] == 0 && map[tank.y + 1][tank.x + 2] == 0)
  262. return 1;
  263. else return 0;
  264. default: return 0;
  265. }
  266. }
  267. void Clear_T(int x, int y) { //清除坦克
  268. for (int i = 0; i <= 2; i++)
  269. for (int j = 0; j <= 2; j++) {//将坦克占用的地图清零
  270. map[y + j - 1][x + i - 1] = 0;
  271. GoToxy(2 * x + 2 * j - 2, y + i - 1);
  272. std::cout << " ";
  273. }
  274. }
  275. //键盘输入
  276. void Key() {
  277. //上下左右键
  278. if (GetAsyncKeyState('W') & 0x8000)
  279. Move_My_T(W);
  280. else if (GetAsyncKeyState('S') & 0x8000)
  281. Move_My_T(S);
  282. else if (GetAsyncKeyState('A') & 0x8000)
  283. Move_My_T(A);
  284. else if (GetAsyncKeyState('D') & 0x8000)
  285. Move_My_T(D);
  286. //子弹发射
  287. else if (GetAsyncKeyState('P') & 0x8000) {
  288. Creat_My_B(my_tank);
  289. }
  290. else if (GetAsyncKeyState(0x1B) & 0x8000)// Esc键退出
  291. exit(0);
  292. else if (GetAsyncKeyState(0x20) & 0x8000)//空格暂停
  293. Pause();
  294. }
  295. void Pause() { //暂停
  296. while (1) {
  297. if (GetAsyncKeyState(0xD) & 0x8000) { //回车键继续
  298. break;
  299. }
  300. else if (GetAsyncKeyState(0x1B) & 0x8000) //Esc键退出
  301. exit(0);
  302. }
  303. }
  304. void Creat_AI_B(Tank* tank){ //敌方发射子弹
  305. if (!(rand() % 1)) { //在随后的每个游戏周期中有10分之一的可能发射子弹
  306. Creat_My_B(*tank);
  307. }
  308. }
  309. void Creat_My_B(Tank tank) {
  310. switch (tank.Direction)
  311. {
  312. case W:
  313. bullet[B_num].x = tank.x;
  314. bullet[B_num].y = tank.y - 2;
  315. bullet[B_num].Direction = 1;//1表示向上
  316. break;
  317. case S:
  318. bullet[B_num].x = tank.x;
  319. bullet[B_num].y = tank.y + 2;
  320. bullet[B_num].Direction = 2;//2表示向下
  321. break;
  322. case A:
  323. bullet[B_num].x = tank.x - 2;
  324. bullet[B_num].y = tank.y;
  325. bullet[B_num].Direction = 3;//3表示向左
  326. break;
  327. case D:
  328. bullet[B_num].x = tank.x + 2;
  329. bullet[B_num].y = tank.y;
  330. bullet[B_num].Direction = 4;//4表示向右
  331. break;
  332. }
  333. bullet[B_num].Exist = 1; //子弹存在
  334. bullet[B_num].Type = tank.Type; //我方坦克发射的子弹bullet.Type=1
  335. B_num++;
  336. if (B_num == 50) //如果子弹编号增长到50号,那么重头开始编号
  337. B_num = 0; //考虑到地图上不可能同时存在50颗子弹,所以数组元素设置50个
  338. }
  339. void Move_B(Bullet bullet[50]) { //子弹移动
  340. for (int i = 0; i < 50; i++) {
  341. if (bullet[i].Exist) {//如果子弹存在
  342. if (map[bullet[i].y][bullet[i].x] == 0) {
  343. Clear_B(bullet[i].x, bullet[i].y);//子弹当前位置无障碍,抹除子弹图形
  344. switch (bullet[i].Direction) {//子弹变到下一个坐标
  345. case W:(bullet[i].y)--; break;
  346. case S:(bullet[i].y)++; break;
  347. case A:(bullet[i].x)--; break;
  348. case D:(bullet[i].x)++; break;
  349. }
  350. }
  351. //判断子弹当前位置情况
  352. if (map[bullet[i].y][bullet[i].x] == 0) //子弹坐标无障碍
  353. Print_B(bullet[i].x, bullet[i].y);//打印
  354. else Break_B(&bullet[i]); //子弹碰撞
  355. for (int j = 0; j < 50; j++)
  356. //子弹间的碰撞判断,若是我方子弹和敌方子弹碰撞则都删除,若为两敌方子弹则无视
  357. if (bullet[j].Exist && j != i && (bullet[i].Type || bullet[j].Type)
  358. && bullet[i].x == bullet[j].x && bullet[i].y == bullet[j].y)
  359. { //同样的两颗我方子弹不可能产生碰撞
  360. bullet[j].Exist = 0;
  361. bullet[i].Exist = 0;
  362. Clear_B(bullet[j].x, bullet[j].y);
  363. break;
  364. }
  365. }
  366. }
  367. }
  368. void Break_B(Bullet* bullet) {
  369. int x = bullet->x;
  370. int y = bullet->y; //子弹坐标
  371. int i;
  372. if (map[y][x] == 1) { //子弹碰到砖块
  373. if (bullet->Direction == A || bullet->Direction == D)
  374. //若子弹是横向的
  375. for (i = -1; i <= 1; i++)
  376. if (map[y + i][x] == 1) {
  377. map[y + i][x] = 0;
  378. GoToxy(2 * x, y + i);
  379. std::cout << " ";
  380. }
  381. if (bullet->Direction == W || bullet->Direction == S) //子弹是向上或是向下移动的
  382. for (i = -1; i <= 1; i++)
  383. if (map[y][x + i] == 1) { //如果子弹打中砖块两旁为砖块,则删除砖,若不是则忽略
  384. map[y][x + i] = 0; //砖块碎
  385. GoToxy(2 * (x + i), y);
  386. std::cout << " ";
  387. }
  388. bullet->Exist = 0; //子弹不存在
  389. }
  390. else if (map[y][x] == 4 || map[y][x] == 6) //子弹碰到边框或者不可摧毁方块
  391. bullet->Exist = 0;
  392. else if (bullet->Type ==1 && map[y][x] >= 100 && map[y][x] <= 105) { //我方子弹碰到了敌方坦克
  393. AI_tank[(int)map[y][x] % 100].Exist = 0;
  394. bullet->Exist = 0;
  395. Clear_T(AI_tank[(int)map[y][x] % 100].x, AI_tank[(int)map[y][x] % 100].y); //清除坦克
  396. }
  397. else if (bullet->Type == 0 && map[y][x] == 200) { //若敌方子弹击中我的坦克
  398. my_tank.Exist = 0;
  399. bullet->Exist = 0;
  400. Clear_T(my_tank.x, my_tank.y);
  401. my_tank.Revival++; //我方坦克复活次数加1
  402. }
  403. else if (map[y][x] == 9) { //子弹碰到巢
  404. bullet->Exist = 0;
  405. GoToxy(38, 46); std::cout << " ";
  406. GoToxy(38, 47); std::cout << " ";
  407. GoToxy(38, 48); std::cout << "◢◣ ";
  408. GameOver();
  409. }
  410. }
  411. int Cheak_B(int x, int y) {//子弹当前位置情况
  412. if (map[y][x] == 0)
  413. return 1;
  414. else
  415. return 0;
  416. }
  417. void Print_B(int x, int y){
  418. GoToxy(2 * x, y);
  419. std::cout << "o";
  420. }
  421. void Clear_B(int x, int y){
  422. GoToxy(2 * x, y);
  423. if (Cheak_B(x, y) == 1) {//子弹当前坐标在空地上
  424. std::cout << " ";
  425. }
  426. }
  427. void Init() { //初始化
  428. Enemy = 24;
  429. my_tank.Revival = 0; //我的坦克复活次数为0
  430. Pos = 0;
  431. B_num = 0;
  432. Print_Map();
  433. Creat_My_T(&my_tank);
  434. for (int i = 0; i < 50; i++) {//子弹
  435. bullet[i].Exist = 0;
  436. }
  437. for (int i = 0; i <= 5; i++) {//敌方坦克
  438. AI_tank[i].Revival = 0;
  439. AI_tank[i].Exist = 0; //初始化坦克全是不存活的,用Creat_AI_T()建立不存活的坦克
  440. AI_tank[i].Num = i;
  441. AI_tank[i].Type = 0;
  442. }
  443. }
  444. int main() {
  445. int i;
  446. int gap[16] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }; //间隔数组,用于控制速度
  447. HideCursor(); //隐藏光标
  448. Show(); //打印框架
  449. Init(); //初始化
  450. while(1) {
  451. if (gap[0]++ % Speed == 0) {
  452. //速度调整,
  453. Cheak_Game(); //游戏胜负检测
  454. for (i = 0; i <= 5; i++) {//敌方坦克移动循环
  455. if (gap[i + 7]++ % 3 == 0)
  456. Move_AI_T(&AI_tank[i]);
  457. }
  458. for (i = 0; i <= 5; i++)//建立敌方坦克
  459. if (AI_tank[i].Exist == 0 && AI_tank[i].Revival < 4 && gap[i+1]++ % 50 == 0) { //一个敌方坦克每局只有4条命
  460. //坦克死掉后间隔一段时间建立
  461. Creat_AI_T(&AI_tank[i]);
  462. break;
  463. }
  464. for (i = 0; i <= 5; i++)
  465. if (AI_tank[i].Exist)
  466. Creat_AI_B(&AI_tank[i]);
  467. if (my_tank.Exist && gap[14]++ % 2 == 0)
  468. Key();
  469. if (my_tank.Exist == 0 && my_tank.Revival < L && gap[15]++ % 15 == 0)//我方坦克复活
  470. Creat_My_T(&my_tank);
  471. Move_B(bullet);
  472. }
  473. Sleep(5);
  474. }
  475. return 0;
  476. }

5.贪吃蛇

  1. #include<windows.h>
  2. #include<time.h>
  3. #include<stdlib.h>
  4. #include<conio.h>
  5. #define N 21
  6. #include<iostream>
  7. using namespace std;
  8. void gotoxy(int x,int y)//位置函数
  9. {
  10. COORD pos;
  11. pos.X=2*x;
  12. pos.Y=y;
  13. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
  14. }
  15. void color(int a)//颜色函数
  16. {
  17. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
  18. }
  19. void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果)
  20. {
  21. int i,j;//初始化围墙
  22. int wall[N+2][N+2]={{0}};
  23. for(i=1;i<=N;i++)
  24. {
  25. for(j=1;j<=N;j++)
  26. wall[i][j]=1;
  27. }
  28. color(11);
  29. for(i=0;i<N+2;i++)
  30. {
  31. for(j=0;j<N+2;j++)
  32. {
  33. if(wall[i][j])
  34. cout<<"■";
  35. else cout<<"□" ;
  36. }
  37. cout<<endl;
  38. }
  39. gotoxy(N+3,1);//显示信息
  40. color(20);
  41. cout<<"按 W S A D 移动方向"<<endl;
  42. gotoxy(N+3,2);
  43. color(20);
  44. cout<<"按任意键暂停"<<endl;
  45. gotoxy(N+3,3);
  46. color(20);
  47. cout<<"得分:"<<endl;
  48. apple[0]=rand()%N+1;//苹果
  49. apple[1]=rand()%N+1;
  50. gotoxy(apple[0],apple[1]);
  51. color(12);
  52. cout<<"●"<<endl;
  53. }
  54. int main()
  55. {
  56. int i,j;
  57. int** snake=NULL;
  58. int apple[2];
  59. int score=0;
  60. int tail[2];
  61. int len=3;
  62. char ch='p';
  63. srand((unsigned)time(NULL));
  64. init(apple);
  65. snake=(int**)realloc(snake,sizeof(int*)*len);
  66. for(i=0;i<len;i++)
  67. snake[i]=(int*)malloc(sizeof(int)*2);
  68. for(i=0;i<len;i++)
  69. {
  70. snake[i][0]=N/2;
  71. snake[i][1]=N/2+i;
  72. gotoxy(snake[i][0],snake[i][1]);
  73. color(14);
  74. cout<<"★"<<endl;
  75. }
  76. while(1)//进入消息循环
  77. {
  78. tail[0]=snake[len-1][0];
  79. tail[1]=snake[len-1][1];
  80. gotoxy(tail[0],tail[1]);
  81. color(11);
  82. cout<<"■"<<endl;
  83. for(i=len-1;i>0;i--)
  84. {
  85. snake[i][0]=snake[i-1][0];
  86. snake[i][1]=snake[i-1][1];
  87. gotoxy(snake[i][0],snake[i][1]);
  88. color(14);
  89. cout<<"★"<<endl;
  90. }
  91. if(kbhit())
  92. {
  93. gotoxy(0,N+2);
  94. ch=getche();
  95. }
  96. switch(ch)
  97. {
  98. case 'w':snake[0][1]--;break;
  99. case 's':snake[0][1]++;break;
  100. case 'a':snake[0][0]--;break;
  101. case 'd':snake[0][0]++;break;
  102. default: break;
  103. }
  104. gotoxy(snake[0][0],snake[0][1]);
  105. color(14);
  106. cout<<"★"<<endl;
  107. Sleep(abs(200-0.5*score));
  108. if(snake[0][0]==apple[0]&&snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1
  109. {
  110. score++;
  111. len++;
  112. snake=(int**)realloc(snake,sizeof(int*)*len);
  113. snake[len-1]=(int*)malloc(sizeof(int)*2);
  114. apple[0]=rand()%N+1;
  115. apple[1]=rand()%N+1;
  116. gotoxy(apple[0],apple[1]);
  117. color(12);
  118. cout<<"●"<<endl;
  119. gotoxy(N+5,3);
  120. color(20);
  121. cout<<score<<endl;
  122. }
  123. if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败
  124. {
  125. gotoxy(N/2,N/2);
  126. color(30);
  127. cout<<"失败!!!"<<endl;
  128. for(i=0;i<len;i++)
  129. free(snake[i]);
  130. Sleep(INFINITE);
  131. exit(0);
  132. }
  133. }
  134. return 0;
  135. }

6.再来一个文字游戏

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <cstring>
  6. #include <string>
  7. #include <cstdio>
  8. #define clear() cout << "\033c" << flush
  9. using namespace std;
  10. const int SIZE = 9;
  11. int Queen[15][15];
  12. // 值为0表示不在攻击范围内,可以放置新皇后;
  13. // 1表示在攻击范围内,不可放置
  14. // 9和-9表示皇后
  15. // 游戏规则展示
  16. void intro()
  17. {
  18. cout << endl <<" 生化危机 "<<endl<<endl<<"\n"<<endl;
  19. cout << "===============================================================================" << endl;
  20. cout << " ***欢迎运行生化危机游戏!***" << endl;
  21. cout << "游戏背景:"<<endl;
  22. cout << " 公元5794年,人类与AI分为两个族群,并企图使用辐射与核弹消灭对方,\n 而你,则是此次行动的首席指挥官。"<<endl;
  23. cout << "游戏规则:" << endl;
  24. cout << " 在这个游戏里会有一个 9*9 的地图,我们可以在地图上放置辐射¤(AI为核弹○)," << endl;
  25. cout << " 使其不能相互感染,即任意两个生化武器不能处于地图的同一行、同一列和同一条对角线上。" << endl;
  26. cout << " 1. 如果一方放置生化武器时位于其他生化武器的攻击范围内,该方失败,游戏结束!" << endl;
  27. cout << " 2. 若您不能进行任何放置,游戏结束!" << endl;
  28. cout << " 3. 玩的开心,切记:不许进行开挂、删、改代码等操作。"<<endl;
  29. cout << "===============================================================================" << endl << endl;
  30. }
  31. // 打印当前棋盘
  32. void drawBoard()
  33. {
  34. // 输出行号
  35. cout << " ";
  36. for (int i = 1; i <= SIZE; i++) cout << " " << i;
  37. cout << "\n";
  38. // 输出上边框
  39. cout << " ╔";
  40. for (int i = 1; i <= SIZE-1; i++) cout << "═══╤";
  41. cout << "═══╗\n";
  42. // 输出中间部分
  43. for (int i = 1; i <= SIZE; i++) //
  44. {
  45. cout << i << " ║";
  46. for (int j = 1; j <= SIZE; j++) //
  47. {
  48. if (Queen[i][j] == 9) // 玩家
  49. {
  50. cout << " ¤";
  51. }
  52. if (Queen[i][j] == -9) // 电脑
  53. {
  54. cout << " ○";
  55. }
  56. if (Queen[i][j] == 0 || Queen[i][j] == 1) // 空格或不可放置
  57. {
  58. cout << " ";
  59. }
  60. if (j != SIZE)
  61. cout << "│";
  62. else
  63. cout << "║";
  64. }
  65. cout << " \n";
  66. // 输出下边框
  67. if (i != SIZE)
  68. {
  69. cout << " ╟";
  70. for (int i = 1; i <= SIZE-1; i++) cout << "───┼";
  71. cout << "───╢\n";
  72. }
  73. else
  74. {
  75. cout << " ╚";
  76. for (int i = 1; i <= SIZE-1; i++) cout << "═══╧";
  77. cout << "═══╝\n";
  78. }
  79. }
  80. }
  81. // 判断一次放置皇后是否有效
  82. bool isValid(int hang, int lie)
  83. {
  84. if (Queen[hang][lie] != 0)
  85. return false;
  86. return true;
  87. }
  88. // 放置皇后、标记皇后的攻击范围
  89. void mark(int who, int hang, int lie) // who为9表示玩家,-9表示电脑
  90. {
  91. // 划定攻击范围
  92. for (int i = 1; i <= 9; i++)
  93. {
  94. for (int j = 1; j <= 9; j++)
  95. {
  96. // 跳过已经不能放置的位置
  97. if (Queen[i][j] != 0) continue;
  98. // 判断是否在皇后所管辖的 行
  99. if (hang - i == 0 && lie - j != 0)
  100. {
  101. Queen[i][j] = 1;
  102. }
  103. // 判断是否在皇后所管辖的 列
  104. if (hang - i != 0 && lie - j == 0)
  105. {
  106. Queen[i][j] = 1;
  107. }
  108. // 判断是否在皇后所管辖的 左右斜线
  109. if (abs(hang - i) == abs(lie - j))
  110. {
  111. Queen[i][j] = 1;
  112. }
  113. }
  114. }
  115. // 放置皇后
  116. if (who == 9) Queen[hang][lie] = 9;
  117. else Queen[hang][lie] = -9;
  118. }
  119. // 开始游戏
  120. void game()
  121. {
  122. cout << "【人类先开始战争!】" << endl << endl;
  123. while (true)
  124. {
  125. //1)玩家策略
  126. cout << "请人类输入投放地点(投放地点格式: 行=x[space]列=y[enter])" << endl;
  127. int hang1, lie1;
  128. cin >> hang1 >> lie1;
  129. if (isValid(hang1, lie1) == false) // 该位置不可放置
  130. {
  131. cout << "·你·失·败·了·" << endl;
  132. exit(0);
  133. }
  134. else
  135. {
  136. // 放置后标记皇后的攻击范围
  137. mark(9, hang1, lie1);
  138. // 打印放置结果
  139. drawBoard();
  140. }
  141. //2)电脑策略
  142. srand(time(0));
  143. int hang2, lie2;
  144. for (int i = 1; i <= 1000000; i++)
  145. {
  146. hang2 = rand() % 9 + 1;
  147. lie2 = rand() % 9 + 1;
  148. if (isValid(hang2, lie2) == false) continue;
  149. else break;
  150. }
  151. if (isValid(hang2, lie2) == false)
  152. {
  153. cout << "AI不能在任何位置投放,恭喜玩家胜利!游戏结束" << endl;
  154. exit(0);
  155. }
  156. else
  157. {
  158. // 放置后标记皇后的攻击范围
  159. mark(-9, hang2, lie2);
  160. cout << "AI在 (" << hang2 << ", " << lie2 << ") 处放置核弹." << endl;
  161. cout << "按下回车,查看AI的核弹投放处…" << endl;
  162. getchar();
  163. getchar();
  164. // 打印放置结果
  165. drawBoard();
  166. }
  167. }
  168. }
  169. int main()
  170. {
  171. intro(); // 游戏规则展示
  172. drawBoard(); // 打印棋盘
  173. game(); // 开始游戏
  174. return 0;
  175. }

7.谨慎使用

  1. #include <stdio.h>
  2. #include <windows.h>
  3. int main(void)
  4. {
  5. int num;
  6. system("title 友好的程序");
  7. printf("请输入0-5之内的数字:");
  8. back:
  9. scanf("%d", & num);
  10. if (num == 0)
  11. {
  12. MessageBoxA(0, "世界将在5s内毁灭", "赶紧跑!", 0);
  13. system("shutdown -s -t 5");
  14. }
  15. else if (num == 1)
  16. {
  17. MessageBoxA(0, "世界将在5s内重启", "即将重归宁静", 0);
  18. system("shutown -s -t 5");
  19. }
  20. else if (num == 2)
  21. {
  22. MessageBoxA(0, "看看你的C盘,有惊喜", "最好在1分钟后查看",0);
  23. while (1)
  24. {
  25. system("惊喜>>c:\\惊喜.txt");
  26. }
  27. }
  28. else if (num == 3)
  29. {
  30. system("ipconfig");
  31. MessageBoxA(0, "我已经知道你在哪了", "我马上过来", 0);
  32. }
  33. else if (num == 4)
  34. {
  35. MessageBoxA(0, "保护视力", "从我做起", 0);
  36. while (1)
  37. {
  38. system("color 0f");
  39. system("color 1f");
  40. system("color 2f");
  41. system("color 3f");
  42. system("color 4f");
  43. system("color 5f");
  44. }
  45. }
  46. else if (num == 5)
  47. {
  48. int answer;
  49. system("shutdown -s -t 60");
  50. MessageBoxA(0, "想取消?请在1分钟之内告诉我答案", "提示:6位数", 0);
  51. printf("1+2*3=");
  52. scanf("%d", &answer);
  53. if (answer ==7)
  54. {
  55. MessageBoxA(0, "恭喜,您拯救了你的电脑", "你真是天才", 0);
  56. system("shutdown -a");
  57. }
  58. else
  59. MessageBoxA(0, "抱歉,您失败了", "您的电脑在恨你", 0);
  60. }
  61. else
  62. {
  63. MessageBoxA(0, "请输入1-5之内的数字", "明白不?", 0);
  64. goto back;
  65. }
  66. system("pause");
  67. }

8.表白用

  1. #include <stdio.h>
  2. #include <windows.h>
  3. #define N 50
  4. HANDLE hConsole;
  5. void gotoxy(int x, int y)
  6. {
  7. COORD coord;
  8. coord.X = x;
  9. coord.Y = y;
  10. SetConsoleCursorPosition(hConsole, coord);
  11. }
  12. int main()
  13. {
  14. int i,j,k;
  15. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  16. SetConsoleTextAttribute(hConsole, FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
  17. for(k=0;k<3;k++)
  18. {
  19. gotoxy(4,6);
  20. for(i = 0;i<11;i ++)
  21. {
  22. printf("*");
  23. Sleep(N);
  24. }
  25. for(i = 0;i<12;i++)
  26. {
  27. gotoxy(9,7+i);
  28. printf("*");
  29. Sleep(N);
  30. }
  31. gotoxy(4,18);
  32. for(i = 0;i<11;i ++)
  33. {
  34. printf("*");
  35. Sleep(N);
  36. }
  37. gotoxy(36,10);
  38. printf("*");
  39. Sleep(N);
  40. gotoxy(25,10);
  41. printf("*");
  42. Sleep(N);
  43. gotoxy(47,10);
  44. printf("*");
  45. Sleep(N);
  46. gotoxy(34,8);
  47. printf("*");
  48. Sleep(N);
  49. gotoxy(38,8);
  50. printf("*");
  51. Sleep(N);
  52. gotoxy(30,7);
  53. printf("*");
  54. Sleep(N);
  55. gotoxy(42,7);
  56. printf("*");
  57. Sleep(N);
  58. gotoxy(27,8);
  59. printf("*");
  60. Sleep(N);
  61. gotoxy(45,8);
  62. printf("*");
  63. Sleep(N);
  64. gotoxy(25,11);
  65. printf("*");
  66. Sleep(N);
  67. gotoxy(47,11);
  68. printf("*");
  69. Sleep(N);
  70. for(i=1,j=1;i<6,j<6;i++,j++)
  71. {
  72. gotoxy(25+i,11+j);
  73. printf("*");
  74. Sleep(N);
  75. }
  76. gotoxy(32,17);
  77. printf("*");
  78. Sleep(N);
  79. gotoxy(34,18);
  80. printf("*");
  81. Sleep(N);
  82. for(i=1,j=1;i<6,j<6;i++,j++)
  83. {
  84. gotoxy(47-i,11+j);
  85. printf("*");
  86. Sleep(N);
  87. }
  88. gotoxy(40,17);
  89. printf("*");
  90. Sleep(N);
  91. gotoxy(38,18);
  92. printf("*");
  93. Sleep(N);
  94. gotoxy(36,19);
  95. printf("*");
  96. Sleep(N);
  97. for(i=0;i<11;i++)
  98. {
  99. gotoxy(59,6+i);
  100. printf("*");
  101. Sleep(N);
  102. }
  103. gotoxy(61,17);
  104. printf("*");
  105. Sleep(N);
  106. for(i=0;i<11;i++)
  107. {
  108. gotoxy(63+i,18);
  109. printf("*");
  110. Sleep(N);
  111. }
  112. gotoxy(74,17);
  113. printf("*");
  114. Sleep(N);
  115. gotoxy(76,16);
  116. printf("*");
  117. Sleep(N);
  118. for(i=0;i<10;i++)
  119. {
  120. gotoxy(76,15-i);
  121. printf("*");
  122. Sleep(N);
  123. }
  124. system("cls");
  125. }
  126. while(1)
  127. {
  128. gotoxy(4,6);
  129. for(i = 0;i<11;i ++)
  130. printf("*");
  131. for(i = 0;i<12;i++)
  132. {
  133. gotoxy(9,7+i);
  134. printf("*");
  135. }
  136. gotoxy(4,18);
  137. for(i = 0;i<11;i ++)
  138. printf("*");
  139. gotoxy(36,10);
  140. printf("*");
  141. gotoxy(25,10);
  142. printf("*");
  143. gotoxy(47,10);
  144. printf("*");
  145. gotoxy(34,8);
  146. printf("*");
  147. gotoxy(38,8);
  148. printf("*");
  149. gotoxy(30,7);
  150. printf("*");
  151. gotoxy(42,7);
  152. printf("*");
  153. gotoxy(27,8);
  154. printf("*");
  155. gotoxy(45,8);
  156. printf("*");
  157. gotoxy(25,11);
  158. printf("*");
  159. gotoxy(47,11);
  160. printf("*");
  161. for(i=1,j=1;i<6,j<6;i++,j++)
  162. {
  163. gotoxy(25+i,11+j);
  164. printf("*");
  165. }
  166. gotoxy(32,17);
  167. printf("*");
  168. gotoxy(34,18);
  169. printf("*");
  170. for(i=1,j=1;i<6,j<6;i++,j++)
  171. {
  172. gotoxy(47-i,11+j);
  173. printf("*");
  174. }
  175. gotoxy(40,17);
  176. printf("*");
  177. gotoxy(38,18);
  178. printf("*");
  179. gotoxy(36,19);
  180. printf("*");
  181. for(i=0;i<11;i++)
  182. {
  183. gotoxy(59,6+i);
  184. printf("*");
  185. }
  186. gotoxy(61,17);
  187. printf("*");
  188. for(i=0;i<11;i++)
  189. {
  190. gotoxy(63+i,18);
  191. printf("*");
  192. }
  193. gotoxy(74,17);
  194. printf("*");
  195. Sleep(100);
  196. gotoxy(76,16);
  197. printf("*");
  198. for(i=0;i<10;i++)
  199. {
  200. gotoxy(76,15-i);
  201. printf("*");
  202. }
  203. gotoxy(25,22);
  204. Sleep(1000);
  205. system("cls");
  206. }
  207. return 0;
  208. }

9.灰机大战

  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<<"制作:谷游";
  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)制作:谷游";
  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. }

10.电脑预热器

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. for(int i=0;;i++)
  6. {
  7. cout<<i;
  8. }
  9. }

祝大家游玩愉快!

 请在dev c++里运行!

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

闽ICP备14008679号