当前位置:   article > 正文

分享一个超级玛丽源码_经典游戏超级玛丽android再现 源码

经典游戏超级玛丽android再现 源码

先看看整体运行效果

【C/C++项目】C语言打造超级玛丽(不是),曾经有一只勇敢蘑菇,后来它...

源码在下面,你没有看错,就是单cpp战士

  1. #include <graphics.h>
  2. #include <conio.h>
  3. #include<math.h>
  4. #include "MyTimer.h" //一个保证精确延时的类。下载于easyx官网
  5. #pragma comment(lib,"Winmm.lib") //给游戏添加音乐要用到它
  6. #define G 9.8 //重力加速度
  7. #define XSIZE 512 //屏幕大小
  8. #define YSIZE 384
  9. #define X 64 //主角起始位置
  10. #define Y 192
  11. #define W 32 //主角的宽和高
  12. #define H 32
  13. #define STEP 4 //主角走一步相距的像素个数
  14. #define HIGH (2*role.w+1) //主角跳跃的最大高度
  15. #define CMD_LEFT 1 //方向键的宏定义
  16. #define CMD_RIGHT 2
  17. #define CMD_UP 4
  18. #define CMD_DOWN 8
  19. #define CMD_SHOOT 16
  20. #define CMD_ESC 32
  21. int life; //全局变量,主角共有多少条生命
  22. int score; //全局变量,主角获得的分数
  23. struct ROLE
  24. {
  25. int id;
  26. int x;//横坐标
  27. int y;//纵坐标
  28. int w;//图片宽度
  29. int h;//图片高度
  30. int xleft;//水平运动的左界限
  31. int xright;//水平运动的右界限
  32. int turn;//精灵的运动方向
  33. int jump;//精灵是否跳跃
  34. int iframe;//加载第几副精灵图,这样就能让精灵看上去动起来了
  35. };
  36. struct MAP //储存地图的结构体
  37. {
  38. int id;
  39. int x;
  40. int y;
  41. };
  42. struct BULLET //子弹的结构体
  43. {
  44. int x;
  45. int y;
  46. int turn;
  47. int iframe;
  48. int id;
  49. };
  50. struct COINT //硬币的结构体
  51. {
  52. int x;
  53. int y;
  54. double iframe;
  55. };
  56. struct ENEMY //敌人的结构体
  57. {
  58. int id;
  59. int x;
  60. int y;
  61. int turn;
  62. int iframe;
  63. };
  64. class game //整个游戏只设置了这一个类
  65. {
  66. private:
  67. ROLE role;
  68. MAP map[350];
  69. BULLET bullet[20];
  70. COINT coint[50];
  71. ENEMY enemy[20];
  72. IMAGE img_mapsky,img_p,img_map,img_ani,img_mapbk,img_home;
  73. int xmapsky; //背景天空的起始横坐标
  74. int xmap; //地图的起始坐标
  75. double v0; //精灵跳跃的初速度
  76. double h; //精灵跳跃的高度
  77. double t; //精灵跳跃的时间
  78. int ibullet; //第几颗子弹
  79. int xbullet; //子弹的x坐标
  80. int ybullet; //子弹的y坐标
  81. int get_bullet; //是否获得武器,0表示没有获得,1表示已获得
  82. POINT icoint; //储存硬币的坐标
  83. POINT bomb[20]; //储存哪些地方爆炸了的坐标
  84. POINT temp; //临时坐标。储存哪些地方爆炸了的坐标
  85. double score_frame; //下面3个double型的变量用于控制各自图片的帧,以实现动画的效果。如画面中的流水
  86. double bomb_frame;
  87. double mapbk_frame;
  88. int win; //玩家是否过关
  89. int pause; //玩家是否按Esc(暂停键)
  90. public:
  91. game();
  92. ~game();
  93. void start(); //处理游戏开始的界面,和按暂停键后的界面
  94. void init(); //初始化各项变量
  95. void move(); //控制主角移动
  96. void show(); //显示画面
  97. int isdie(); //判断主角是否已死
  98. int GetCommand(); // 获取控制命令。参阅easyx
  99. void left(); //主角向左运动
  100. void right(); //主角向右运动
  101. void up(); //主角跳跃
  102. void init_shoot(); //初始化发射子弹
  103. void fall(); //主角自由落体或者向上跳跃
  104. int is_l_touch(int id);//主角的左边是否碰到墙或敌人,以及敌人是否碰到陆地的左边界
  105. int is_r_touch(int id);//主角的右边是否碰到墙或敌人,以及敌人是否碰到陆地的右边界
  106. int is_t_touch(); //主角的头是否碰到墙
  107. int is_b_touch(int id);//主角是否踩到敌人。
  108. int is_touch(); //主角是否吃到金币
  109. int is_land(ENEMY e); //敌人是否站在陆地上
  110. void getbullet(); //获取子弹
  111. void shoot(); //发射子弹
  112. int eat(BULLET b); //子弹是否打到敌人或者墙壁
  113. void end(); //处理游戏结束
  114. };
  115. game::game()
  116. {
  117. initgraph(XSIZE,YSIZE);
  118. }
  119. game::~game()
  120. {
  121. closegraph();
  122. }
  123. void game::start()
  124. {
  125. if(pause==1)//如果按了暂停键
  126. {
  127. BeginBatchDraw();
  128. int points[8]={XSIZE/2-45,YSIZE/3,XSIZE/2+45,YSIZE/3,XSIZE/2+45,YSIZE/3+90,XSIZE/2-45,YSIZE/3+90};
  129. setfillstyle(GREEN);
  130. fillpoly(4, points);
  131. setbkmode(TRANSPARENT);
  132. setfont(20,0,"黑体");
  133. RECT r2={XSIZE/2-45,YSIZE/3,XSIZE/2+45,YSIZE/3+30};rectangle(XSIZE/2-45,YSIZE/3,XSIZE/2+45,YSIZE/3+30);
  134. drawtext("回到游戏", &r2, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  135. RECT r3={XSIZE/2-45,YSIZE/3+30,XSIZE/2+45,YSIZE/3+60};rectangle(XSIZE/2-45,YSIZE/3+30,XSIZE/2+45,YSIZE/3+60);
  136. drawtext("重新开始", &r3, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  137. RECT r4={XSIZE/2-45,YSIZE/3+60,XSIZE/2+45,YSIZE/3+90};rectangle(XSIZE/2-45,YSIZE/3+60,XSIZE/2+45,YSIZE/3+90);
  138. drawtext(" 主 菜 单 ", &r4, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  139. FlushBatchDraw();
  140. MOUSEMSG m;
  141. while(true)
  142. {
  143. BeginBatchDraw();
  144. m=GetMouseMsg();
  145. switch(m.uMsg)
  146. {
  147. case WM_LBUTTONDOWN:
  148. EndBatchDraw();
  149. if(m.x>XSIZE/2-45&&m.x<XSIZE/2+45&&m.y>YSIZE/3&&m.y<YSIZE/3+30)
  150. return;
  151. else if(m.x>XSIZE/2-45&&m.x<XSIZE/2+45&&m.y>YSIZE/3+30&&m.y<YSIZE/3+60)
  152. {
  153. mciSendString("close all", NULL, 0, NULL);
  154. pause=0;
  155. score=0;
  156. return;
  157. }
  158. else if(m.x>XSIZE/2-45&&m.x<XSIZE/2+45&&m.y>YSIZE/3+60&&m.y<YSIZE/3+90)
  159. {
  160. mciSendString("close all", NULL, 0, NULL);
  161. pause=0;
  162. score=0;
  163. life=0;
  164. cleardevice();
  165. break;
  166. }
  167. else
  168. break;
  169. case WM_MOUSEMOVE:
  170. RECT r;
  171. int i;
  172. for(i=0;i<3;i++)
  173. {
  174. if(m.x>XSIZE/2-45&&m.x<XSIZE/2+45&&m.y>YSIZE/3+i*30&&m.y<YSIZE/3+30+i*30)
  175. {
  176. r.left=XSIZE/2-45;
  177. r.top=YSIZE/3+i*30;
  178. r.right=XSIZE/2+45;
  179. r.bottom=YSIZE/3+30+i*30;
  180. int points[8]={r.left,r.top,r.right,r.top,r.right,r.bottom,r.left,r.bottom};
  181. setfillstyle(RED);
  182. fillpoly(4, points);
  183. setbkmode(TRANSPARENT);
  184. switch(i)
  185. {
  186. case 0:
  187. drawtext("回到游戏", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  188. break;
  189. case 1:
  190. drawtext("重新开始", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  191. break;
  192. case 2:
  193. drawtext(" 主 菜 单 ", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  194. break;
  195. }
  196. }
  197. else
  198. {
  199. if(getpixel(XSIZE/2-45+1,YSIZE/3+i*30+1)==RED)
  200. {
  201. r.left=XSIZE/2-45;
  202. r.top=YSIZE/3+i*30;
  203. r.right=XSIZE/2+45;
  204. r.bottom=YSIZE/3+30+i*30;
  205. int points[8]={r.left,r.top,r.right,r.top,r.right,r.bottom,r.left,r.bottom};
  206. setfillstyle(GREEN);
  207. fillpoly(4, points);
  208. setbkmode(TRANSPARENT);
  209. switch(i)
  210. {
  211. case 0:
  212. drawtext("回到游戏", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  213. break;
  214. case 1:
  215. drawtext("重新开始", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  216. break;
  217. case 2:
  218. drawtext(" 主 菜 单 ", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  219. break;
  220. }
  221. }
  222. FlushBatchDraw();
  223. }
  224. }
  225. }
  226. if(pause==0)
  227. break;
  228. }
  229. }
  230. if(life==1||life==2)
  231. return;
  232. life=3;
  233. score=0;
  234. setfont(40,0,"方正舒体");
  235. RECT r1 = {0, 0, XSIZE, YSIZE/3};
  236. drawtext("超级蘑菇", &r1, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  237. setfont(20,0,"宋体");
  238. RECT r2={XSIZE/2-45,YSIZE/3,XSIZE/2+45,YSIZE/3+30};rectangle(XSIZE/2-45,YSIZE/3,XSIZE/2+45,YSIZE/3+30);
  239. drawtext("开始游戏", &r2, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  240. RECT r3={XSIZE/2-45,YSIZE/3+30,XSIZE/2+45,YSIZE/3+60};rectangle(XSIZE/2-45,YSIZE/3+30,XSIZE/2+45,YSIZE/3+60);
  241. drawtext("游戏介绍", &r3, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  242. RECT r4={XSIZE/2-45,YSIZE/3+60,XSIZE/2+45,YSIZE/3+90};rectangle(XSIZE/2-45,YSIZE/3+60,XSIZE/2+45,YSIZE/3+90);
  243. drawtext("操作说明", &r4, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  244. RECT r5={XSIZE/2-45,YSIZE/3+90,XSIZE/2+45,YSIZE/3+120};rectangle(XSIZE/2-45,YSIZE/3+90,XSIZE/2+45,YSIZE/3+120);
  245. drawtext("退出游戏", &r5, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  246. int flag1=1,flag2=0,flag3=0;
  247. MOUSEMSG m;
  248. while(flag1==1)
  249. {
  250. BeginBatchDraw();
  251. m=GetMouseMsg();
  252. switch(m.uMsg)
  253. {
  254. case WM_LBUTTONDOWN:
  255. EndBatchDraw();
  256. if(m.x>XSIZE/2-45&&m.x<XSIZE/2+45&&m.y>YSIZE/3&&m.y<YSIZE/3+30&&flag1==1&&flag2==0&&flag3==0)
  257. {
  258. flag1=0;
  259. break;
  260. }
  261. else if(m.x>XSIZE/2-45&&m.x<XSIZE/2+45&&m.y>YSIZE/3+30&&m.y<YSIZE/3+60&&flag1==1&&flag3==0)
  262. {
  263. flag2=1;
  264. cleardevice();
  265. rectangle(50,50,213,220);
  266. outtextxy(52,52,"游戏介绍:");
  267. outtextxy(52,82,"超级玛丽变");
  268. outtextxy(52,102,"身超级蘑菇。");
  269. outtextxy(52,132,"开发者:");
  270. outtextxy(52,152,"空弦");
  271. RECT R1={XSIZE-46,YSIZE-26,XSIZE-2,YSIZE-2};rectangle(XSIZE-46,YSIZE-26,XSIZE-2,YSIZE-2);
  272. drawtext("返回", &R1, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  273. break;
  274. }
  275. else if(m.x>XSIZE/2-45&&m.x<XSIZE/2+45&&m.y>YSIZE/3+60&&m.y<YSIZE/3+90&&flag1==1&&flag2==0)
  276. {
  277. flag3=1;
  278. cleardevice();
  279. rectangle(50,50,213,220);
  280. outtextxy(52,52,"操作说明:");
  281. outtextxy(52,72,"左移:A键");
  282. outtextxy(52,92,"右移:D键");
  283. outtextxy(52,112,"发射:J键");
  284. outtextxy(52,132,"跳跃:W键/K键");
  285. outtextxy(52,152,"暂停:Esc键");
  286. RECT R2={XSIZE-46,YSIZE-26,XSIZE-2,YSIZE-2};rectangle(XSIZE-46,YSIZE-26,XSIZE-2,YSIZE-2);
  287. drawtext("返回", &R2, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  288. break;
  289. }
  290. else if(m.x>XSIZE/2-45&&m.x<XSIZE/2+45&&m.y>YSIZE/3+90&&m.y<YSIZE/3+120&&flag1==1&&flag2==0&&flag3==0)
  291. exit(0);
  292. else if(m.x>XSIZE-46&&m.x<XSIZE-3&&m.y>YSIZE-26&&m.y<YSIZE-3&&(flag2==1||flag3==1))
  293. {
  294. cleardevice();
  295. flag1=0,flag2=0,flag3=0;
  296. start();
  297. }
  298. else
  299. break;
  300. case WM_MOUSEMOVE:
  301. RECT r;
  302. if(flag2==1||flag3==1)
  303. {
  304. if(m.x>XSIZE-46&&m.x<XSIZE-3&&m.y>YSIZE-26&&m.y<YSIZE-3)
  305. {
  306. r.left=XSIZE-46;
  307. r.top=YSIZE-26;
  308. r.right=XSIZE-2;
  309. r.bottom=YSIZE-2;
  310. int points[8]={r.left,r.top,r.right,r.top,r.right,r.bottom,r.left,r.bottom};
  311. setfillstyle(RED);
  312. fillpoly(4, points);
  313. setbkmode(TRANSPARENT);
  314. drawtext("返回", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  315. }
  316. else
  317. {
  318. if(getpixel(XSIZE-46+1,YSIZE-26+1)==RED)
  319. {
  320. r.left=XSIZE-46;
  321. r.top=YSIZE-26;
  322. r.right=XSIZE-2;
  323. r.bottom=YSIZE-2;
  324. int points[8]={r.left,r.top,r.right,r.top,r.right,r.bottom,r.left,r.bottom};
  325. setfillstyle(BLACK);
  326. fillpoly(4, points);
  327. setbkmode(TRANSPARENT);
  328. drawtext("返回", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  329. }
  330. }
  331. }
  332. else
  333. {
  334. for(int i=0;i<4;i++)
  335. {
  336. if(m.x>XSIZE/2-45&&m.x<XSIZE/2+45&&m.y>YSIZE/3+i*30&&m.y<YSIZE/3+30+i*30)
  337. {
  338. r.left=XSIZE/2-45;
  339. r.top=YSIZE/3+i*30;
  340. r.right=XSIZE/2+45;
  341. r.bottom=YSIZE/3+30+i*30;
  342. int points[8]={r.left,r.top,r.right,r.top,r.right,r.bottom,r.left,r.bottom};
  343. setfillstyle(RED);
  344. fillpoly(4, points);
  345. setbkmode(TRANSPARENT);
  346. switch(i)
  347. {
  348. case 0:
  349. drawtext("开始游戏", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  350. break;
  351. case 1:
  352. drawtext("游戏介绍", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  353. break;
  354. case 2:
  355. drawtext("操作说明", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  356. break;
  357. case 3:
  358. drawtext("退出游戏", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  359. break;
  360. }
  361. }
  362. else
  363. {
  364. if(getpixel(XSIZE/2-45+1,YSIZE/3+i*30+1)==RED)
  365. {
  366. r.left=XSIZE/2-45;
  367. r.top=YSIZE/3+i*30;
  368. r.right=XSIZE/2+45;
  369. r.bottom=YSIZE/3+30+i*30;
  370. int points[8]={r.left,r.top,r.right,r.top,r.right,r.bottom,r.left,r.bottom};
  371. setfillstyle(BLACK);
  372. fillpoly(4, points);
  373. setbkmode(TRANSPARENT);
  374. switch(i)
  375. {
  376. case 0:
  377. drawtext("开始游戏", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  378. break;
  379. case 1:
  380. drawtext("游戏介绍", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  381. break;
  382. case 2:
  383. drawtext("操作说明", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  384. break;
  385. case 3:
  386. drawtext("退出游戏", &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  387. break;
  388. }
  389. }
  390. }
  391. }
  392. }
  393. FlushBatchDraw();
  394. break;
  395. default:
  396. break;
  397. }
  398. }
  399. }
  400. void game::init()
  401. {
  402. if(pause==1)
  403. return;
  404. role.id=1;
  405. role.x=X;
  406. role.y=Y;
  407. role.w=W;
  408. role.h=H;
  409. role.xleft=0;
  410. role.xright=role.w*6+STEP;
  411. role.iframe=1;
  412. role.turn=1;
  413. role.jump=0;
  414. xmapsky=0;
  415. xmap=0;
  416. v0=0;
  417. h=0;
  418. t=0;
  419. ibullet=-1;
  420. icoint.x=-1;
  421. icoint.y=-1;
  422. score_frame=0;
  423. bomb_frame=1;
  424. mapbk_frame=1;
  425. temp.x=-1;
  426. temp.y=-1;
  427. xbullet=41*role.w-10;
  428. ybullet=4*role.h-25;
  429. get_bullet=0;
  430. win=0;
  431. pause=0;
  432. score=0;
  433. int i;
  434. for(i=0;i<350;i++)
  435. {
  436. map[i].id=0;
  437. map[i].x=-1;
  438. map[i].y=-1;
  439. if(i<50)
  440. {
  441. coint[i].x=-1;
  442. coint[i].y=-1;
  443. coint[i].iframe=1;
  444. }
  445. if(i<20)
  446. {
  447. bullet[i].id=0;
  448. bullet[i].x=-1;
  449. bullet[i].y=-1;
  450. bullet[i].iframe=1;
  451. bullet[i].turn=-1;
  452. enemy[i].id=0;
  453. enemy[i].x=-1;
  454. enemy[i].y=-1;
  455. enemy[i].turn=1;
  456. enemy[i].iframe=1;
  457. bomb[i].x=-1;
  458. bomb[i].y=-1;
  459. }
  460. }
  461. loadimage(&img_mapsky,"res\\mapsky.bmp",XSIZE,YSIZE*4);
  462. loadimage(&img_p,"res\\role.bmp");
  463. loadimage(&img_map,"res\\map.bmp");
  464. loadimage(&img_ani,"res\\ani.bmp");
  465. loadimage(&img_mapbk,"res\\mapbk.bmp");
  466. loadimage(&img_home,"res\\home.bmp",XSIZE,YSIZE*5);
  467. mciSendString("open 背景音乐.mp3 alias mymusic1", NULL, 0, NULL);
  468. mciSendString("open 子弹.mp3 alias mymusic2", NULL, 0, NULL);
  469. mciSendString("open 金币.mp3 alias mymusic3", NULL, 0, NULL);
  470. mciSendString("open 跳.mp3 alias mymusic4", NULL, 0, NULL);
  471. mciSendString("open 子弹打到敌人.mp3 alias mymusic5", NULL, 0, NULL);
  472. mciSendString("open 子弹撞墙.mp3 alias mymusic6", NULL, 0, NULL);
  473. mciSendString("open 踩敌人.mp3 alias mymusic7", NULL, 0, NULL);
  474. mciSendString("open 吃到武器.mp3 alias mymusic8", NULL, 0, NULL);
  475. mciSendString("open 胜利.mp3 alias mymusic9", NULL, 0, NULL);
  476. mciSendString("open 死亡1.mp3 alias mymusic10", NULL, 0, NULL);
  477. mciSendString("open 死亡2.mp3 alias mymusic11", NULL, 0, NULL);
  478. for(i=0;i<300;i++) //以下都是编辑地图
  479. {
  480. map[i].id=1;
  481. map[i].x=i%100*role.w;
  482. if(i<100)
  483. map[i].y=9*role.h;
  484. else if(i>=100&&i<200)
  485. map[i].y=10*role.h;
  486. else
  487. map[i].y=11*role.h;
  488. }
  489. map[15].id=1,map[15].x=18*role.w,map[15].y=8*role.h;
  490. map[115].id=1,map[115].x=19*role.w,map[115].y=8*role.h;
  491. map[215].id=1,map[215].x=20*role.w,map[215].y=8*role.h;
  492. map[16].id=1,map[16].x=21*role.w,map[16].y=8*role.h;
  493. map[116].id=1,map[116].x=22*role.w,map[116].y=8*role.h;
  494. map[216].id=1,map[216].x=23*role.w,map[216].y=8*role.h;
  495. map[17].id=1,map[17].x=24*role.w,map[17].y=8*role.h;
  496. map[117].id=1,map[117].x=25*role.w,map[117].y=8*role.h;
  497. map[217].id=1,map[217].x=26*role.w,map[217].y=8*role.h;
  498. map[300].id=2,map[300].x=10*role.w,map[300].y=6*role.h;
  499. map[301].id=2,map[301].x=11*role.w,map[301].y=6*role.h;
  500. map[302].id=2,map[302].x=12*role.w,map[302].y=6*role.h;
  501. map[303].id=3,map[303].x=36*role.w,map[303].y=7*role.h;
  502. map[304].id=3,map[304].x=44*role.w,map[304].y=7*role.h;
  503. map[305].id=2,map[305].x=40*role.w,map[305].y=4*role.h;
  504. map[306].id=2,map[306].x=41*role.w,map[306].y=4*role.h;
  505. map[307].id=2,map[307].x=42*role.w,map[307].y=4*role.h;
  506. map[308].id=2,map[308].x=13*role.w,map[308].y=6*role.h;
  507. map[309].id=4,map[309].x=15*role.w,map[309].y=10*role.h;
  508. map[310].id=5,map[310].x=19*role.w,map[310].y=6*role.h;
  509. map[311].id=5,map[311].x=23*role.w,map[311].y=6*role.h;
  510. map[312].id=5,map[312].x=32*role.w,map[312].y=7*role.h;
  511. map[313].id=5,map[313].x=48*role.w,map[313].y=7*role.h;
  512. map[314].id=5,map[314].x=52*role.w,map[314].y=7*role.h;
  513. map[315].id=5,map[315].x=56*role.w,map[315].y=7*role.h;
  514. map[316].id=3,map[316].x=80*role.w,map[316].y=7*role.h;
  515. map[317].id=3,map[317].x=90*role.w,map[317].y=7*role.h;
  516. map[318].id=2,map[318].x=62*role.w,map[318].y=6*role.h;
  517. map[319].id=2,map[319].x=65*role.w,map[319].y=3*role.h;
  518. map[320].id=2,map[320].x=66*role.w,map[320].y=3*role.h;
  519. map[321].id=2,map[321].x=67*role.w,map[321].y=3*role.h;
  520. map[322].id=2,map[322].x=68*role.w,map[322].y=3*role.h;
  521. map[323].id=2,map[323].x=69*role.w,map[323].y=3*role.h;
  522. map[349].id=6,map[349].x=97*role.w,map[349].y=7*role.h;
  523. for(i=64;i<300;i+=100)
  524. {
  525. map[i].id=0;map[i].x=-1;map[i].y=-1;
  526. map[i+1].id=0;map[i+1].x=-1;map[i+1].y=-1;
  527. map[i+2].id=0;map[i+2].x=-1;map[i+2].y=-1;
  528. map[i+7].id=0;map[i].x=-1;map[i].y=-1;
  529. map[i+8].id=0;map[i+1].x=-1;map[i+1].y=-1;
  530. map[i+9].id=0;map[i+1].x=-1;map[i+1].y=-1;
  531. map[i+11].id=0;map[i].x=-1;map[i].y=-1;
  532. map[i+12].id=0;map[i+1].x=-1;map[i+1].y=-1;
  533. map[i+13].id=0;map[i+1].x=-1;map[i+1].y=-1;
  534. }
  535. map[64].id=4,map[64].x=64*role.w,map[64].y=10*role.h;
  536. map[71].id=4,map[71].x=71*role.w,map[71].y=10*role.h;
  537. map[75].id=4,map[75].x=75*role.w,map[75].y=10*role.h;
  538. enemy[0].id=1;enemy[0].x=6*role.w;enemy[0].y=8*role.h;enemy[0].turn=1;enemy[0].iframe=1;
  539. enemy[1].id=1;enemy[1].x=8*role.w;enemy[1].y=8*role.h;enemy[1].turn=1;enemy[1].iframe=1;
  540. enemy[2].id=1;enemy[2].x=27*role.w;enemy[2].y=8*role.h;enemy[2].turn=1;enemy[2].iframe=1;
  541. enemy[3].id=1;enemy[3].x=29*role.w;enemy[3].y=8*role.h;enemy[3].turn=1;enemy[3].iframe=1;
  542. enemy[4].id=1;enemy[4].x=31*role.w;enemy[4].y=8*role.h;enemy[4].turn=1;enemy[4].iframe=1;
  543. enemy[5].id=1;enemy[5].x=33*role.w;enemy[5].y=8*role.h;enemy[5].turn=1;enemy[5].iframe=1;
  544. enemy[6].id=1;enemy[6].x=35*role.w;enemy[6].y=8*role.h;enemy[6].turn=1;enemy[6].iframe=1;
  545. enemy[7].id=1;enemy[7].x=40*role.w;enemy[7].y=8*role.h;enemy[7].turn=1;enemy[7].iframe=1;
  546. enemy[8].id=1;enemy[8].x=82*role.w;enemy[8].y=8*role.h;enemy[8].turn=1;enemy[8].iframe=1;
  547. enemy[9].id=1;enemy[9].x=65*role.w;enemy[9].y=2*role.h;enemy[9].turn=1;enemy[9].iframe=1;
  548. enemy[10].id=1;enemy[10].x=69*role.w;enemy[10].y=2*role.h;enemy[10].turn=1;enemy[10].iframe=1;
  549. enemy[11].id=1;enemy[11].x=85*role.w;enemy[11].y=8*role.h;enemy[11].turn=1;enemy[11].iframe=1;
  550. for(i=0;i<4;i++)
  551. {
  552. coint[i].x=(10+i)*role.w;
  553. coint[i].y=5*role.h;
  554. coint[i+4].x=(67+i)*role.w;
  555. coint[i+4].y=8*role.w;
  556. coint[i+8].x=74*role.w;
  557. coint[i+8].y=(4+i)*role.w;
  558. }
  559. for(i=12;i<18;i++)
  560. {
  561. coint[i].x=(83-12+i)*role.w;
  562. coint[i].y=6*role.h;
  563. coint[i+6].x=(83-12+i)*role.w;
  564. coint[i+6].y=7*role.w;
  565. }
  566. }
  567. void game::move()
  568. {
  569. MyTimer tt;
  570. int c;
  571. int k=0; //控制发射子弹的频率和敌人的移动速度
  572. int n=0; //控制发射子弹的频率
  573. while(true)
  574. {
  575. tt.Sleep(25);
  576. t=sqrt(2*HIGH/G)/14;
  577. k++;
  578. if(k==1000)
  579. k=0;
  580. if(kbhit()&&win==0)
  581. {
  582. c=GetCommand();
  583. if(c&CMD_LEFT)
  584. left();
  585. if(c&CMD_RIGHT)
  586. right();
  587. if((c&CMD_UP)&&role.jump==0)
  588. up();
  589. if(c&CMD_ESC)
  590. {
  591. pause=1;
  592. break;
  593. }
  594. if(c&CMD_SHOOT&&get_bullet==1)
  595. {
  596. if(n==0)
  597. {
  598. init_shoot();
  599. n=1;
  600. }
  601. n++;
  602. if(k%10==0&&n>10)
  603. {
  604. init_shoot();
  605. }
  606. }
  607. else
  608. n=0;
  609. }
  610. if(-xmap+role.x==97*role.w)
  611. {
  612. mciSendString("stop mymusic1", NULL, 0, NULL);
  613. mciSendString("play mymusic9", NULL, 0, NULL);
  614. }
  615. if(-xmap+role.x>95*role.w)
  616. {
  617. win=1;
  618. role.x+=STEP;
  619. if(role.x-STEP>XSIZE)
  620. break;
  621. }
  622. if(is_b_touch(1)==0)
  623. role.jump=1;
  624. if(role.jump==1)
  625. fall();
  626. if(isdie()==1)
  627. {
  628. mciSendString("stop mymusic1", NULL, 0, NULL);
  629. mciSendString("play mymusic11", NULL, 0, NULL);
  630. life--;
  631. return;
  632. }
  633. if(k%2==0) //敌人的运动
  634. {
  635. for(int i=0;i<20;i++)
  636. {
  637. if(enemy[i].id==1)
  638. {
  639. if(is_land(enemy[i])==1)
  640. {
  641. if(enemy[i].turn==1)
  642. enemy[i].x+=STEP;
  643. else
  644. enemy[i].x-=STEP;
  645. }
  646. if(is_land(enemy[i])==0||is_l_touch(3)==1||is_r_touch(3)==1)
  647. {
  648. if(enemy[i].turn==1)
  649. enemy[i].x-=STEP;
  650. else
  651. enemy[i].x+=STEP;
  652. enemy[i].turn*=-1;
  653. }
  654. enemy[i].iframe*=-1;
  655. }
  656. }
  657. }
  658. int boom=0;
  659. if(is_b_touch(2)==1) //如果主角“踩到”敌人
  660. boom=1;
  661. getbullet(); //获取子弹
  662. if(get_bullet==1)
  663. shoot();
  664. BeginBatchDraw();
  665. show();
  666. FlushBatchDraw();
  667. if((is_l_touch(2)==1||is_r_touch(2)==1))
  668. {
  669. mciSendString("stop mymusic1", NULL, 0, NULL);
  670. mciSendString("play mymusic10", NULL, 0, NULL);
  671. life--;
  672. pause=0;
  673. putimage(role.x,role.y,role.w,role.h,&img_p,2*role.w,role.h,SRCAND);
  674. putimage(role.x,role.y,role.w,role.h,&img_p,2*role.w,0,SRCPAINT);
  675. return;
  676. }
  677. }
  678. }
  679. void game::show()
  680. {
  681. if(xmapsky==-XSIZE)
  682. xmapsky=0;
  683. putimage(xmapsky,0,&img_mapsky); //显示背景
  684. putimage(XSIZE+xmapsky,0,&img_mapsky);
  685. if(is_touch()==1)
  686. score_frame=1;
  687. if(score_frame!=0) //碰到硬币,显示得分
  688. {
  689. switch((int)score_frame)
  690. {
  691. case 1:
  692. putimage(xmap+icoint.x,icoint.y,role.w,role.h,&img_ani,0,11*role.h,SRCAND);
  693. putimage(xmap+icoint.x,icoint.y,role.w,role.h,&img_ani,0,10*role.h,SRCPAINT);
  694. break;
  695. case 2:
  696. putimage(xmap+icoint.x,icoint.y,role.w,role.h,&img_ani,role.w,11*role.h,SRCAND);
  697. putimage(xmap+icoint.x,icoint.y,role.w,role.h,&img_ani,role.w,10*role.h,SRCPAINT);
  698. break;
  699. case 3:
  700. putimage(xmap+icoint.x,icoint.y,role.w,role.h,&img_ani,2*role.w,11*role.h,SRCAND);
  701. putimage(xmap+icoint.x,icoint.y,role.w,role.h,&img_ani,2*role.w,10*role.h,SRCPAINT);
  702. break;
  703. case 4:
  704. putimage(xmap+icoint.x,icoint.y,role.w,role.h,&img_ani,3*role.w,11*role.h,SRCAND);
  705. putimage(xmap+icoint.x,icoint.y,role.w,role.h,&img_ani,3*role.w,10*role.h,SRCPAINT);
  706. break;
  707. default:
  708. break;
  709. }
  710. score_frame+=0.2;
  711. if(score_frame==5)
  712. score_frame=0;
  713. }
  714. int i;
  715. for(i=0;i<350;i++) //显示地图,天空上的地图和硬币
  716. {
  717. if(map[i].id==1)
  718. {
  719. putimage(xmap+map[i].x,map[i].y,role.w,role.h,&img_map,0,0);
  720. }
  721. else if(map[i].id==2)
  722. {
  723. putimage(xmap+map[i].x,map[i].y,role.w,role.h,&img_map,0,role.h);
  724. }
  725. else if(map[i].id==3)
  726. {
  727. putimage(xmap+map[i].x,map[i].y,2*role.w,2*role.h,&img_map,0,9*role.h);
  728. }
  729. else
  730. {
  731. if(map[i].id==4)
  732. {
  733. switch((int)mapbk_frame)
  734. {
  735. case 1:
  736. putimage(xmap+map[i].x,map[i].y,3*role.w,2*role.h,&img_mapbk,0,10*role.h,SRCAND);
  737. putimage(xmap+map[i].x,map[i].y,3*role.w,2*role.h,&img_mapbk,0,8*role.h,SRCPAINT);
  738. break;
  739. case 2:
  740. putimage(xmap+map[i].x,map[i].y,3*role.w,2*role.h,&img_mapbk,3*role.w,10*role.h,SRCAND);
  741. putimage(xmap+map[i].x,map[i].y,3*role.w,2*role.h,&img_mapbk,3*role.w,8*role.h,SRCPAINT);
  742. break;
  743. default:
  744. break;
  745. }
  746. }
  747. else if(map[i].id==5)
  748. {
  749. switch((int)mapbk_frame)
  750. {
  751. case 1:
  752. putimage(xmap+map[i].x,map[i].y,3*role.w,2*role.h,&img_mapbk,0,2*role.h,SRCAND);
  753. putimage(xmap+map[i].x,map[i].y,3*role.w,2*role.h,&img_mapbk,0,0,SRCPAINT);
  754. break;
  755. case 2:
  756. putimage(xmap+map[i].x,map[i].y,3*role.w,2*role.h,&img_mapbk,3*role.w,2*role.h,SRCAND);
  757. putimage(xmap+map[i].x,map[i].y,3*role.w,2*role.h,&img_mapbk,3*role.w,0,SRCPAINT);
  758. break;
  759. default:
  760. break;
  761. }
  762. }
  763. else if(map[i].id==6)
  764. {
  765. switch((int)mapbk_frame)
  766. {
  767. case 1:
  768. putimage(xmap+map[i].x,map[i].y,3*role.w,2*role.h,&img_mapbk,0,6*role.h,SRCAND);
  769. putimage(xmap+map[i].x,map[i].y,3*role.w,2*role.h,&img_mapbk,0,4*role.h,SRCPAINT);
  770. break;
  771. case 2:
  772. putimage(xmap+map[i].x,map[i].y,3*role.w,2*role.h,&img_mapbk,3*role.w,6*role.h,SRCAND);
  773. putimage(xmap+map[i].x,map[i].y,3*role.w,2*role.h,&img_mapbk,3*role.w,4*role.h,SRCPAINT);
  774. break;
  775. default:
  776. break;
  777. }
  778. }
  779. mapbk_frame+=0.003;
  780. if(mapbk_frame>2.9)
  781. {
  782. mapbk_frame=1;
  783. }
  784. }
  785. if(i<50)
  786. {
  787. if(coint[i].x!=-1||coint[i].y!=-1)
  788. {
  789. switch((int)coint[i].iframe)
  790. {
  791. case 1:
  792. putimage(xmap+coint[i].x,coint[i].y,role.w,role.h,&img_ani,0,9*role.h,SRCAND);
  793. putimage(xmap+coint[i].x,coint[i].y,role.w,role.h,&img_ani,0,8*role.h,SRCPAINT);
  794. break;
  795. case 2:
  796. putimage(xmap+coint[i].x,coint[i].y,role.w,role.h,&img_ani,role.w,9*role.h,SRCAND);
  797. putimage(xmap+coint[i].x,coint[i].y,role.w,role.h,&img_ani,role.w,8*role.h,SRCPAINT);
  798. break;
  799. case 3:
  800. putimage(xmap+coint[i].x,coint[i].y,role.w,role.h,&img_ani,2*role.w,9*role.h,SRCAND);
  801. putimage(xmap+coint[i].x,coint[i].y,role.w,role.h,&img_ani,2*role.w,8*role.h,SRCPAINT);
  802. break;
  803. case 4:
  804. putimage(xmap+coint[i].x,coint[i].y,role.w,role.h,&img_ani,3*role.w,9*role.h,SRCAND);
  805. putimage(xmap+coint[i].x,coint[i].y,role.w,role.h,&img_ani,3*role.w,8*role.h,SRCPAINT);
  806. break;
  807. default:
  808. break;
  809. }
  810. coint[i].iframe+=0.125;
  811. if(coint[i].iframe==5)
  812. coint[i].iframe=1;
  813. }
  814. }
  815. }
  816. if(get_bullet==0)
  817. {
  818. switch((int)mapbk_frame)
  819. {
  820. case 1:
  821. putimage(xmap+xbullet,ybullet,52,25,&img_ani,0,12*role.h+25,SRCAND);
  822. putimage(xmap+xbullet,ybullet,52,25,&img_ani,0,12*role.h,SRCPAINT);
  823. break;
  824. case 2:
  825. putimage(xmap+xbullet,ybullet,52,25,&img_ani,52,12*role.h+25,SRCAND);
  826. putimage(xmap+xbullet,ybullet,52,25,&img_ani,52,12*role.h,SRCPAINT);
  827. break;
  828. default:
  829. break;
  830. }
  831. }
  832. for(i=0;i<20;i++) //显示子弹
  833. {
  834. if(get_bullet==1)
  835. {
  836. if(bullet[i].id==1)
  837. {
  838. if(bullet[i].iframe==1)
  839. {
  840. putimage(bullet[i].x,bullet[i].y,role.w,role.h,&img_ani,0,3*role.h,SRCAND);
  841. putimage(bullet[i].x,bullet[i].y,role.w,role.h,&img_ani,0,2*role.h,SRCPAINT);
  842. }
  843. else
  844. {
  845. putimage(bullet[i].x,bullet[i].y,role.w,role.h,&img_ani,role.w,3*role.h,SRCAND);
  846. putimage(bullet[i].x,bullet[i].y,role.w,role.h,&img_ani,role.w,2*role.h,SRCPAINT);
  847. }
  848. }
  849. }
  850. if(enemy[i].id==1)
  851. {
  852. if(enemy[i].iframe==1) //显示敌人
  853. {
  854. putimage(xmap+enemy[i].x,enemy[i].y,role.w,role.h,&img_ani,0,role.h,SRCAND);
  855. putimage(xmap+enemy[i].x,enemy[i].y,role.w,role.h,&img_ani,0,0,SRCPAINT);
  856. }
  857. else
  858. {
  859. putimage(xmap+enemy[i].x,enemy[i].y,role.w,role.h,&img_ani,role.w,role.h,SRCAND);
  860. putimage(xmap+enemy[i].x,enemy[i].y,role.w,role.h,&img_ani,role.w,0,SRCPAINT);
  861. }
  862. }
  863. if(bomb[i].x!=-1||bomb[i].y!=-1)
  864. {
  865. switch((int)bomb_frame)
  866. {
  867. case 1:
  868. putimage(xmap+bomb[i].x-role.w/2,bomb[i].y-role.h/2,2*role.w,2*role.h,&img_ani,0,6*role.h,SRCAND);
  869. putimage(xmap+bomb[i].x-role.w/2,bomb[i].y-role.h/2,2*role.w,2*role.h,&img_ani,0,4*role.h,SRCPAINT);
  870. break;
  871. case 2:
  872. putimage(xmap+bomb[i].x-role.w/2,bomb[i].y-role.h/2,2*role.w,2*role.h,&img_ani,2*role.w,6*role.h,SRCAND);
  873. putimage(xmap+bomb[i].x-role.w/2,bomb[i].y-role.h/2,2*role.w,2*role.h,&img_ani,2*role.w,4*role.h,SRCPAINT);
  874. break;
  875. case 3:
  876. putimage(xmap+bomb[i].x-role.w/2,bomb[i].y-role.h/2,2*role.w,2*role.h,&img_ani,4*role.w,6*role.h,SRCAND);
  877. putimage(xmap+bomb[i].x-role.w/2,bomb[i].y-role.h/2,2*role.w,2*role.h,&img_ani,4*role.w,4*role.h,SRCPAINT);
  878. break;
  879. case 4:
  880. putimage(xmap+bomb[i].x-role.w/2,bomb[i].y-role.h/2,2*role.w,2*role.h,&img_ani,6*role.w,6*role.h,SRCAND);
  881. putimage(xmap+bomb[i].x-role.w/2,bomb[i].y-role.h/2,2*role.w,2*role.h,&img_ani,6*role.w,4*role.h,SRCPAINT);
  882. break;
  883. default:
  884. break;
  885. }
  886. bomb_frame+=0.25;
  887. if(bomb_frame==5)
  888. {
  889. bomb[i].x=-1;
  890. bomb[i].y=-1;
  891. bomb_frame=1;
  892. }
  893. }
  894. }
  895. int n=score;
  896. char s1[20]="当前得分:";
  897. char s2[10];
  898. itoa(n,s2,10);
  899. RECT r1={10,10,110,40};
  900. RECT r2={110,10,150,40};
  901. setfont(20, 0,"宋体");
  902. drawtext(s1, &r1, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  903. drawtext(s2, &r2, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  904. if(role.iframe==1) //显示主角
  905. {
  906. if(role.turn==1)
  907. {
  908. putimage(role.x,role.y,role.w,role.h,&img_p,0,role.h,SRCAND);
  909. putimage(role.x,role.y,role.w,role.h,&img_p,0,0,SRCPAINT);
  910. }
  911. else
  912. {
  913. putimage(role.x,role.y,role.w,role.h,&img_p,4*role.w,role.h,SRCAND);
  914. putimage(role.x,role.y,role.w,role.h,&img_p,4*role.w,0,SRCPAINT);
  915. }
  916. }
  917. else
  918. {
  919. if(role.turn==1)
  920. {
  921. putimage(role.x,role.y,role.w,role.h,&img_p,role.w,role.h,SRCAND);
  922. putimage(role.x,role.y,role.w,role.h,&img_p,role.w,0,SRCPAINT);
  923. }
  924. else
  925. {
  926. putimage(role.x,role.y,role.w,role.h,&img_p,3*role.w,role.h,SRCAND);
  927. putimage(role.x,role.y,role.w,role.h,&img_p,3*role.w,0,SRCPAINT);
  928. }
  929. }
  930. }
  931. int game::isdie()
  932. {
  933. if(role.y>=YSIZE)
  934. return 1;
  935. else
  936. return 0;
  937. }
  938. int game::GetCommand()
  939. {
  940. int c = 0;
  941. if (GetAsyncKeyState('A') & 0x8000)
  942. c |= CMD_LEFT;
  943. if (GetAsyncKeyState('D') & 0x8000)
  944. c |= CMD_RIGHT;
  945. if ((GetAsyncKeyState('W') & 0x8000)||(GetAsyncKeyState('K') & 0x8000))
  946. c |= CMD_UP;
  947. if (GetAsyncKeyState('S') & 0x8000)
  948. c |= CMD_DOWN;
  949. if (GetAsyncKeyState('J') & 0x8000)
  950. c |= CMD_SHOOT;
  951. if (GetAsyncKeyState(VK_ESCAPE) & 0x8000)
  952. c |= CMD_ESC;
  953. return c;
  954. }
  955. void game::left()
  956. {
  957. role.iframe*=-1;
  958. role.turn=-1;
  959. role.x-=STEP;
  960. if(is_l_touch(1)==1)
  961. role.x+=STEP;
  962. if(role.x<role.xleft)
  963. role.x+=STEP;
  964. }
  965. void game::right()
  966. {
  967. role.iframe*=-1;
  968. role.turn=1;
  969. role.x+=STEP;
  970. if(is_r_touch(1)==1)
  971. role.x-=STEP;
  972. if(role.x>role.xright&&(-xmap+role.x<90*role.w))
  973. {
  974. role.x-=STEP;
  975. xmapsky-=1;
  976. xmap-=STEP;
  977. }
  978. }
  979. void game::up()
  980. {
  981. mciSendString("play mymusic4 from 0", NULL, 0, NULL);
  982. role.iframe*=-1;
  983. v0=-sqrt(2*G*HIGH);
  984. role.jump=1;
  985. }
  986. void game::init_shoot()
  987. {
  988. mciSendString("play mymusic2 from 0", NULL, 0, NULL);
  989. ibullet++;
  990. if(ibullet==20)
  991. ibullet=0;
  992. bullet[ibullet].id=1;
  993. bullet[ibullet].y=role.y+8;
  994. bullet[ibullet].turn=role.turn;
  995. if(bullet[ibullet].turn==1)
  996. bullet[ibullet].x=role.x+10;
  997. else
  998. bullet[ibullet].x=role.x-26;
  999. }
  1000. int game::is_l_touch(int id)
  1001. {
  1002. int x,y;
  1003. int i;
  1004. if(id==1) //id==1表示主角是否碰到id为1的地图,及游戏中黄色的地图
  1005. {
  1006. x=-xmap+role.x;
  1007. y=role.y;
  1008. for(i=0;i<350;i++)
  1009. {
  1010. if(map[i].id!=0&&map[i].id<4)
  1011. {
  1012. POINT m[2];
  1013. m[0].x=map[i].x;
  1014. m[0].y=map[i].y;
  1015. m[1].x=map[i].x+role.w;
  1016. m[1].y=map[i].y;
  1017. if(map[i].id==3)
  1018. {
  1019. if(((y-m[1].y)/role.h==0||(y-m[1].y-role.h)/role.h==0)&&x>m[1].x&&x<m[1].x+role.w)
  1020. return 1;
  1021. }
  1022. else
  1023. {
  1024. if((y-m[1].y)/role.h==0&&x>m[0].x&&x<m[1].x)
  1025. return 1;
  1026. }
  1027. }
  1028. }
  1029. return 0;
  1030. }
  1031. else if(id==2) //id==2表示主角是否碰到敌人的左边
  1032. {
  1033. x=-xmap+role.x;
  1034. y=role.y;
  1035. for(i=0;i<20;i++)
  1036. {
  1037. if(enemy[i].id!=0)
  1038. {
  1039. POINT m[2];
  1040. m[0].x=enemy[i].x;
  1041. m[0].y=enemy[i].y;
  1042. m[1].x=enemy[i].x+role.w;
  1043. m[1].y=enemy[i].y;
  1044. if((y-m[1].y)/role.h==0&&x>m[0].x&&x<m[1].x)
  1045. return 1;
  1046. }
  1047. }
  1048. return 0;
  1049. }
  1050. else //id==3表示敌人是否碰到地图的左边
  1051. {
  1052. int j;
  1053. for(j=0;j<20;j++)
  1054. {
  1055. if(enemy[j].id!=0)
  1056. {
  1057. x=enemy[j].x;
  1058. y=enemy[j].y;
  1059. for(i=0;i<350;i++)
  1060. {
  1061. if(map[i].id!=0&&map[i].id<4)
  1062. {
  1063. POINT m[2];
  1064. m[0].x=map[i].x;
  1065. m[0].y=map[i].y;
  1066. m[1].x=map[i].x+role.w;
  1067. m[1].y=map[i].y;
  1068. if(map[i].id==3)
  1069. {
  1070. if(((y-m[1].y)/role.h==0||(y-m[1].y-role.h)/role.h==0)&&x>m[1].x&&x<m[1].x+role.w)
  1071. return 1;
  1072. }
  1073. else
  1074. {
  1075. if((y-m[1].y)/role.h==0&&x>m[0].x&&x<m[1].x)
  1076. return 1;
  1077. }
  1078. }
  1079. }
  1080. }
  1081. }
  1082. return 0;
  1083. }
  1084. }
  1085. int game::is_r_touch(int id)
  1086. {
  1087. int x,y;
  1088. int i;
  1089. if(id==1)
  1090. {
  1091. x=-xmap+role.x+role.w;
  1092. y=role.y;
  1093. for(i=0;i<350;i++)
  1094. {
  1095. if(map[i].id!=0&&map[i].id<4)
  1096. {
  1097. POINT m[2];
  1098. m[0].x=map[i].x;
  1099. m[0].y=map[i].y;
  1100. m[1].x=map[i].x+role.w;
  1101. m[1].y=map[i].y;
  1102. if(map[i].id==3)
  1103. {
  1104. if(((y-m[0].y)/role.h==0||(y-m[0].y-role.h)/role.h==0)&&x>m[0].x&&x<m[1].x)
  1105. return 1;
  1106. }
  1107. else
  1108. {
  1109. if((y-m[0].y)/role.h==0&&x>m[0].x&&x<m[1].x)
  1110. return 1;
  1111. }
  1112. }
  1113. }
  1114. return 0;
  1115. }
  1116. else if(id==2)
  1117. {
  1118. x=-xmap+role.x+role.w;
  1119. y=role.y;
  1120. for(i=0;i<20;i++)
  1121. {
  1122. if(enemy[i].id!=0)
  1123. {
  1124. POINT m[2];
  1125. m[0].x=enemy[i].x;
  1126. m[0].y=enemy[i].y;
  1127. m[1].x=enemy[i].x+role.w;
  1128. m[1].y=enemy[i].y;
  1129. if((y-m[0].y)/role.h==0&&x>m[0].x&&x<m[1].x)
  1130. return 1;
  1131. }
  1132. }
  1133. return 0;
  1134. }
  1135. else
  1136. {
  1137. int j;
  1138. for(j=0;j<20;j++)
  1139. {
  1140. if(enemy[j].id!=0)
  1141. {
  1142. x=enemy[j].x+role.w;
  1143. y=enemy[j].y;
  1144. for(i=0;i<350;i++)
  1145. {
  1146. if(map[i].id!=0&&map[i].id<4)
  1147. {
  1148. POINT m[2];
  1149. m[0].x=map[i].x;
  1150. m[0].y=map[i].y;
  1151. m[1].x=map[i].x+role.w;
  1152. m[1].y=map[i].y;
  1153. if(map[i].id==3)
  1154. {
  1155. if(((y-m[0].y)/role.h==0||(y-m[0].y-role.h)/role.h==0)&&x>m[0].x&&x<m[1].x)
  1156. return 1;
  1157. }
  1158. else
  1159. {
  1160. if((y-m[0].y)/role.h==0&&x>m[0].x&&x<m[1].x)
  1161. return 1;
  1162. }
  1163. }
  1164. }
  1165. }
  1166. }
  1167. return 0;
  1168. }
  1169. }
  1170. int game::is_t_touch()
  1171. {
  1172. int x,y;
  1173. x=-xmap+role.x;
  1174. y=role.y;
  1175. for(int i=0;i<350;i++)
  1176. {
  1177. if(map[i].id!=0&&map[i].id<4)
  1178. {
  1179. POINT m[2];
  1180. m[0].x=map[i].x;
  1181. m[0].y=map[i].y;
  1182. m[1].x=map[i].x;
  1183. m[1].y=map[i].y+role.h;
  1184. if((x-m[1].x)/role.w==0&&y>m[0].y&&y<m[1].y)
  1185. return 1;
  1186. }
  1187. }
  1188. return 0;
  1189. }
  1190. int game::is_b_touch(int id)
  1191. {
  1192. if(id==1)
  1193. {
  1194. int x,y;
  1195. x=-xmap+role.x;
  1196. y=role.y+role.h;
  1197. for(int i=0;i<350;i++)
  1198. {
  1199. if(map[i].id!=0&&map[i].id<4)
  1200. {
  1201. POINT m[2];
  1202. m[0].x=map[i].x;
  1203. m[0].y=map[i].y;
  1204. m[1].x=map[i].x;
  1205. m[1].y=map[i].y+role.h;
  1206. if(map[i].id==3)
  1207. {
  1208. if(((x-m[0].x)/role.w==0||(x+role.w-m[0].x-2*role.w)/role.w==0)&&y>=m[0].y&&y<m[1].y)
  1209. return 1;
  1210. }
  1211. else
  1212. {
  1213. if((x-m[0].x)/role.w==0&&y>=m[0].y&&y<m[1].y)
  1214. return 1;
  1215. }
  1216. }
  1217. }
  1218. return 0;
  1219. }
  1220. else if(id==2)
  1221. {
  1222. int x,y;
  1223. x=-xmap+role.x;
  1224. y=role.y+role.h;
  1225. for(int i=0;i<20;i++)
  1226. {
  1227. if(enemy[i].id!=0)
  1228. {
  1229. POINT m[2];
  1230. m[0].x=enemy[i].x;
  1231. m[0].y=enemy[i].y;
  1232. m[1].x=enemy[i].x;
  1233. m[1].y=enemy[i].y+role.h;
  1234. if((x-m[0].x)/role.w==0&&y>m[0].y&&y<m[1].y)
  1235. {
  1236. mciSendString("play mymusic7 from 0", NULL, 0, NULL);
  1237. score+=10;
  1238. bomb[i].x=enemy[i].x;
  1239. bomb[i].y=enemy[i].y;
  1240. enemy[i].id=0;
  1241. enemy[i].iframe=-1;
  1242. enemy[i].turn=1;
  1243. enemy[i].x=-1;
  1244. enemy[i].y=-1;
  1245. return 1;
  1246. }
  1247. }
  1248. }
  1249. return 0;
  1250. }
  1251. return 0;
  1252. }
  1253. int game::is_touch()
  1254. {
  1255. int i,j;
  1256. POINT r[2];
  1257. r[0].x=-xmap+role.x;
  1258. r[0].y=role.y;
  1259. r[1].x=-xmap+role.x+role.w;
  1260. r[1].y=role.y+role.h;
  1261. for(i=0;i<50;i++)
  1262. {
  1263. if(coint[i].x!=-1||coint[i].y!=-1)
  1264. {
  1265. POINT c[4];
  1266. c[0].x=coint[i].x;
  1267. c[0].y=coint[i].y;
  1268. c[1].x=coint[i].x+role.w;
  1269. c[1].y=coint[i].y;
  1270. c[2].x=coint[i].x;
  1271. c[2].y=coint[i].y+role.h;
  1272. c[3].x=coint[i].x+role.w;
  1273. c[3].y=coint[i].y+role.h;
  1274. for(j=0;j<4;j++)
  1275. {
  1276. if(c[j].x>=r[0].x&&c[j].y>=r[0].y&&c[j].x<=r[1].x&&c[j].y<=r[1].y)
  1277. {
  1278. mciSendString("play mymusic3 from 0", NULL, 0, NULL);
  1279. score+=20;
  1280. icoint.x=coint[i].x;
  1281. icoint.y=coint[i].y;
  1282. coint[i].x=-1;
  1283. coint[i].y=-1;
  1284. coint[i].iframe=1;
  1285. return 1;
  1286. }
  1287. }
  1288. }
  1289. }
  1290. return 0;
  1291. }
  1292. int game::is_land(ENEMY e)
  1293. {
  1294. POINT r[2];
  1295. r[0].x=e.x;
  1296. r[0].y=e.y+role.h;
  1297. r[1].x=e.x+role.h;
  1298. r[1].y=e.y+role.h;
  1299. for(int i=0;i<350;i++)
  1300. {
  1301. if(map[i].id!=0&&map[i].id<4)
  1302. {
  1303. POINT m[3];
  1304. m[0].x=map[i].x;
  1305. m[0].y=map[i].y;
  1306. m[1].x=map[i].x+role.w;
  1307. m[1].y=map[i].y;
  1308. m[2].x=map[i].x;
  1309. m[2].y=map[i].y+role.h;
  1310. if(e.turn==1)
  1311. {
  1312. if((r[1].x-m[0].x)/role.w==0&&r[1].y>=m[0].y&&r[1].y<m[2].y)
  1313. return 1;
  1314. }
  1315. else
  1316. {
  1317. if((r[0].x-m[1].x)/role.w==0&&r[0].y>=m[0].y&&r[0].y<m[2].y)
  1318. return 1;
  1319. }
  1320. }
  1321. }
  1322. return 0;
  1323. }
  1324. void game::getbullet()
  1325. {
  1326. int i;
  1327. POINT r[2];
  1328. r[0].x=-xmap+role.x;
  1329. r[0].y=role.y;
  1330. r[1].x=-xmap+role.x+role.w;
  1331. r[1].y=role.y+role.h;
  1332. POINT b[4];
  1333. b[0].x=xbullet;
  1334. b[0].y=ybullet;
  1335. b[1].x=xbullet+52;
  1336. b[1].y=ybullet;
  1337. b[2].x=xbullet;
  1338. b[2].y=ybullet+25;
  1339. b[3].x=xbullet+52;
  1340. b[3].y=ybullet+25;
  1341. for(i=0;i<4;i++)
  1342. {
  1343. if(b[i].x>=r[0].x&&b[i].y>=r[0].y&&b[i].x<=r[1].x&&b[i].y<=r[1].y)
  1344. {
  1345. mciSendString("play mymusic8 from 0", NULL, 0, NULL);
  1346. get_bullet=1;
  1347. xbullet=0;
  1348. ybullet=0;
  1349. }
  1350. }
  1351. }
  1352. void game::fall()
  1353. {
  1354. h=v0*t+G*pow(t,2)/2;
  1355. role.y+=(int)(h+0.5);
  1356. if(v0>=0) //自由落体
  1357. {
  1358. if(isdie()==1)
  1359. return;
  1360. if(is_b_touch(1)==1)
  1361. {
  1362. v0=0;
  1363. role.y=role.y/role.h*role.h;
  1364. role.jump=0;
  1365. }
  1366. }
  1367. else //向上跳跃
  1368. {
  1369. if(v0>=0)
  1370. h=0;
  1371. else
  1372. role.y+=(int)(h+0.5);
  1373. if(is_t_touch()==1)
  1374. {
  1375. v0=0;
  1376. h=0;
  1377. role.y=role.y/role.h*role.h+role.h;
  1378. }
  1379. }
  1380. v0=v0+G*t;
  1381. }
  1382. void game::shoot()
  1383. {
  1384. int i;
  1385. for(i=0;i<20;i++)
  1386. {
  1387. if(bullet[i].id==1)
  1388. {
  1389. if(bullet[i].turn==1)
  1390. {
  1391. bullet[i].x+=2*STEP;
  1392. }
  1393. else
  1394. {
  1395. bullet[i].x-=2*STEP;
  1396. }
  1397. if((bullet[i].x<(-3*role.w))||(bullet[i].x>XSIZE))
  1398. {
  1399. bullet[i].id=0;
  1400. bullet[i].x=-1;
  1401. bullet[i].y=-1;
  1402. bullet[i].iframe=1;
  1403. bullet[i].turn=1;
  1404. }
  1405. if(eat(bullet[i])==1)
  1406. {
  1407. bullet[i].id=0;
  1408. bullet[i].x=-1;
  1409. bullet[i].y=-1;
  1410. bullet[i].iframe=1;
  1411. bullet[i].turn=1;
  1412. bomb[i].x=temp.x;
  1413. bomb[i].y=temp.y;
  1414. }
  1415. bullet[i].iframe*=-1;
  1416. }
  1417. }
  1418. }
  1419. int game::eat(BULLET b)
  1420. {
  1421. POINT r[4];
  1422. r[0].x=-xmap+b.x+role.w/2;
  1423. r[0].y=b.y;
  1424. r[1].x=-xmap+b.x+role.w;
  1425. r[1].y=b.y;
  1426. r[2].x=-xmap+b.x+role.w/2;
  1427. r[2].y=b.y+role.h/2;
  1428. r[3].x=-xmap+b.x+role.w;
  1429. r[3].y=b.y+role.h/2;
  1430. int i;
  1431. for(i=0;i<350;i++)
  1432. {
  1433. if(map[i].id!=0&&map[i].id<4)
  1434. {
  1435. POINT m[2];
  1436. m[0].x=map[i].x;
  1437. m[0].y=map[i].y;
  1438. if(map[i].id==3)
  1439. {
  1440. m[1].x=map[i].x+2*role.w;
  1441. m[1].y=map[i].y+2*role.h;
  1442. }
  1443. else
  1444. {
  1445. m[1].x=map[i].x+role.w;
  1446. m[1].y=map[i].y+role.h;
  1447. }
  1448. for(int j=0;j<4;j++)
  1449. {
  1450. if(r[j].x>m[0].x&&r[j].x<m[1].x&&r[j].y>m[0].y&&r[j].y<m[1].y)
  1451. {
  1452. mciSendString("play mymusic6 from 0", NULL, 0, NULL);
  1453. temp.x=r[0].x-role.w/4;
  1454. temp.y=r[0].y-role.w/4;
  1455. return 1;
  1456. }
  1457. }
  1458. }
  1459. if(i<20)
  1460. {
  1461. if(enemy[i].id==1)
  1462. {
  1463. POINT e[2];
  1464. e[0].x=enemy[i].x;
  1465. e[0].y=enemy[i].y;
  1466. e[1].x=enemy[i].x+role.w;
  1467. e[1].y=enemy[i].y+role.h;
  1468. for(int j=0;j<4;j++)
  1469. {
  1470. if(r[j].x>e[0].x&&r[j].x<e[1].x&&r[j].y>e[0].y&&r[j].y<e[1].y)
  1471. {
  1472. mciSendString("play mymusic5 from 0", NULL, 0, NULL);
  1473. score+=10;
  1474. temp.x=enemy[i].x;
  1475. temp.y=enemy[i].y;
  1476. enemy[i].id=0;
  1477. enemy[i].iframe=-1;
  1478. enemy[i].turn=1;
  1479. enemy[i].x=-1;
  1480. enemy[i].y=-1;
  1481. return 1;
  1482. }
  1483. }
  1484. }
  1485. }
  1486. }
  1487. return 0;
  1488. }
  1489. void game::end()
  1490. {
  1491. MyTimer tt;
  1492. EndBatchDraw();
  1493. if(isdie()==1||win==1)
  1494. pause=0;
  1495. if(pause==1)
  1496. return;
  1497. if(win==1)
  1498. tt.Sleep(5000);
  1499. else
  1500. tt.Sleep(2700);
  1501. mciSendString("close all", NULL, 0, NULL);
  1502. tt.Sleep(1000);
  1503. if(win==1)
  1504. {
  1505. pause=0;
  1506. score=0;
  1507. life=0;
  1508. mciSendString("open 通关.mp3 alias mymusic13", NULL, 0, NULL);
  1509. mciSendString("play mymusic13", NULL, 0, NULL);
  1510. putimage(0,-3*YSIZE,&img_home);
  1511. tt.Sleep(7000);
  1512. mciSendString("close mymusic13", NULL, 0, NULL);
  1513. }
  1514. else
  1515. {
  1516. score=0;
  1517. if(life==0)
  1518. {
  1519. mciSendString("open 游戏结束.mp3 alias mymusic12", NULL, 0, NULL);
  1520. mciSendString("play mymusic12", NULL, 0, NULL);
  1521. putimage(0,-YSIZE,&img_home);
  1522. tt.Sleep(5500);
  1523. mciSendString("close mymusic12", NULL, 0, NULL);
  1524. }
  1525. else
  1526. {
  1527. cleardevice();
  1528. outtextxy(XSIZE/2-43,YSIZE/3,"生命还剩下:");
  1529. if(life==1)
  1530. outtextxy(XSIZE/2,YSIZE/2-20,"1");
  1531. else if(life==2)
  1532. outtextxy(XSIZE/2,YSIZE/2-20,"2");
  1533. tt.Sleep(2000);
  1534. }
  1535. }
  1536. cleardevice();
  1537. }
  1538. void main()
  1539. {
  1540. game g;
  1541. while(true)
  1542. {
  1543. g.start();
  1544. g.init();
  1545. mciSendString("play mymusic1 repeat", NULL, 0, NULL);
  1546. g.show();
  1547. g.move();
  1548. g.end();
  1549. }
  1550. }

资源包领取进下我们学习群【881577770】找管理员领取哦!这里是空弦的编程小屋,关注我,后续会持续更新更多游戏源码与教程,B站与知乎ID:蒸汽小毛,欢迎大家进群学习!

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号