当前位置:   article > 正文

植物大战僵尸源码1.0版

植物大战僵尸源码

基本上是rock老师讲的代码复现,有部分改动,较大改动文末附~

【程序员Rock】C语言项目:完整版植物大战僵尸!可能是B站最好的植物大战僵尸教程了!零基础手把手游戏开发_哔哩哔哩_bilibili

  小蒟蒻一枚~如有佬指正,不胜感激!

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <graphics.h> //easyx
  4. #include <iostream> //c++
  5. #include <time.h> //rand()
  6. #include <mmsystem.h> //play music
  7. #include <math.h> //贝塞尔函数
  8. #include "vector2.h"
  9. #pragma comment(lib, "winmm.lib")
  10. #define WIN_WIDTH 900
  11. #define WIN_HEIGHT 600
  12. #define ZM_MAX 10
  13. using namespace std;
  14. enum {wan_dou, xiang_ri_kui, zhi_wu_count};
  15. enum {GOING, WIN, FAIL};
  16. int killed_zm_count;
  17. int zm_appear_count;
  18. int game_status;
  19. IMAGE img_bg;
  20. IMAGE img_bar;
  21. IMAGE img_cards[zhi_wu_count];
  22. IMAGE* img_plants[zhi_wu_count][32];
  23. int curplant; // 0:没有选中;1:选择第一种植物
  24. int curx, cury;
  25. typedef struct plant {
  26. int type; // 0:没有植物;1:第一种植物
  27. int frame_index; //序列帧的序号
  28. bool catched;
  29. int deadtime;
  30. int x, y;
  31. int timer;
  32. int shoot_time;
  33. }plant;
  34. plant map[9][10];
  35. typedef struct sunshineball {
  36. int x, y;
  37. int frame_index;
  38. int dest_y;
  39. bool used;
  40. int timer;
  41. float xoff;
  42. float yoff;
  43. float t;//贝塞尔曲线时间
  44. vector2 p1, p2, p3, p4;
  45. vector2 pcur;//当前阳光位置
  46. float speed;
  47. int status;
  48. }sunshineball;
  49. sunshineball balls[10];
  50. IMAGE img_sunshineball[30];
  51. int sunshine;
  52. enum {sunshine_down, sunshine_ground, sunshine_collect, sunshine_product};
  53. typedef struct zm{
  54. int x, y;
  55. int frame_index;
  56. bool used;
  57. int speed;
  58. int row;
  59. int blood;
  60. bool dead;
  61. bool eating;
  62. }zm;
  63. zm zms[10];
  64. IMAGE img_zm[22];
  65. IMAGE img_zm_dead1[10];
  66. IMAGE img_zm_dead2[10];
  67. IMAGE img_zm_eat[21];
  68. IMAGE img_zm_stand[11];
  69. typedef struct bullet {
  70. int x, y;
  71. bool used;
  72. int speed;
  73. int row;
  74. bool blast; //是否爆炸
  75. int frame_index;
  76. }bullet;
  77. bullet bullets[50];
  78. IMAGE img_bullet_normal;
  79. IMAGE img_bullet_blast[4];
  80. IMAGE img_fail;
  81. IMAGE img_win;
  82. #pragma comment( lib, "MSIMG32.LIB")
  83. void TransparentImage(IMAGE* dstimg, int x, int y, IMAGE* srcimg) //新版png
  84. {
  85. HDC dstDC = GetImageHDC(dstimg);
  86. HDC srcDC = GetImageHDC(srcimg);
  87. int w = srcimg->getwidth();
  88. int h = srcimg->getheight();
  89. BLENDFUNCTION bf = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
  90. AlphaBlend(dstDC, x, y, w, h, srcDC, 0, 0, w, h, bf);
  91. }
  92. bool FileExist(const char* name) {
  93. FILE* fp = fopen(name, "r");
  94. if (fp == NULL) {
  95. return false;
  96. }
  97. else {
  98. fclose(fp);
  99. return true;
  100. }
  101. }
  102. void GameInit() {
  103. memset(img_plants, '\0', sizeof(img_plants));
  104. memset(map, '\0', sizeof map);
  105. killed_zm_count = 0;
  106. zm_appear_count = 0;
  107. game_status = GOING;
  108. curplant = 0;
  109. sunshine = 50;
  110. srand(time(NULL));
  111. initgraph(WIN_WIDTH, WIN_HEIGHT);
  112. loadimage(&img_bg, "res/map/map0.jpg");
  113. loadimage(&img_bar, "res/bar/bar5.png");
  114. char name[64];
  115. for (int i = 0; i < zhi_wu_count; i++) {
  116. sprintf_s(name, sizeof(name), "res/cards/card_%d.png", i + 1);
  117. loadimage(&img_cards[i], name);
  118. for (int j = 0; j < 20; j++) {
  119. sprintf_s(name, sizeof(name), "res/zhiwu/%d/%d.png", i + 1, j + 1);
  120. if (FileExist(name) == true) {//文件打开成功-->文件存在
  121. img_plants[i][j] = new IMAGE;
  122. loadimage(img_plants[i][j], name);
  123. }
  124. else {
  125. break;
  126. }
  127. }
  128. }
  129. memset(balls, '\0', sizeof(balls));
  130. for (int i = 1; i < 30; i++) {
  131. sprintf_s(name, sizeof(name), "res/sunshine/%d.png", i);
  132. if (FileExist(name) == true) {
  133. loadimage(&img_sunshineball[i - 1], name);
  134. }
  135. }
  136. LOGFONT f;
  137. gettextstyle(&f);
  138. f.lfHeight = 30;
  139. f.lfWeight = 15;
  140. strcpy(f.lfFaceName, "Segoe UI Black");
  141. f.lfQuality = ANTIALIASED_QUALITY;
  142. settextstyle(&f);
  143. setbkmode(TRANSPARENT);
  144. setcolor(BLACK);
  145. memset(zms, '\0', sizeof(zms));
  146. for (int i = 0; i < 22; i++) {
  147. sprintf_s(name, sizeof(name), "res/zm/%d.png", i + 1);
  148. loadimage(&img_zm[i], name);
  149. }
  150. loadimage(&img_bullet_normal, "res/bullets/bullet_normal.png");
  151. loadimage(&img_bullet_blast[3], "res/bullets/bullet_blast.png");
  152. for (int i = 0; i < 3; i++) {
  153. float k = 0.2 * i + 0.2;
  154. loadimage(&img_bullet_blast[i], "res/bullets/bullet_blast.png",
  155. img_bullet_blast[3].getwidth() * k, img_bullet_blast[3].getheight() * k, true);
  156. }
  157. memset(bullets, '\0', sizeof(bullets));
  158. for (int i = 0; i < 10; i++) {
  159. sprintf_s(name, "res/zm头掉了/%d.png", i + 1);
  160. loadimage(&img_zm_dead1[i], name);
  161. sprintf_s(name, "res/zm倒地/%d.png", i + 1);
  162. loadimage(&img_zm_dead2[i], name);
  163. }
  164. for (int i = 0; i < 21; i++) {
  165. sprintf_s(name, "res/zm_eat/%d.png", i + 1);
  166. loadimage(&img_zm_eat[i], name);
  167. }
  168. mciSendString("open res/sunshine.mp3", 0, 0, 0);
  169. for (int i = 0; i < 11; i++) {
  170. sprintf_s(name, "res/zm_stand/%d.png", i + 1);
  171. loadimage(&img_zm_stand[i], name);
  172. }
  173. loadimage(&img_fail, "res/fail2.png");
  174. loadimage(&img_win, "res/win2.png");
  175. }
  176. void DrawZm() {
  177. int zm_max = sizeof(zms) / sizeof(zms[0]);
  178. for (int i = 0; i < zm_max; i++) {
  179. if (zms[i].dead && zms[i].used) {
  180. IMAGE* img1 = &img_zm_dead1[zms[i].frame_index];
  181. IMAGE* img2 = &img_zm_dead2[zms[i].frame_index];
  182. TransparentImage(NULL, zms[i].x + 45, zms[i].y + 5, img1);
  183. TransparentImage(NULL, zms[i].x - 16, zms[i].y - 5, img2);
  184. }
  185. else if (zms[i].eating) {
  186. IMAGE* img = &img_zm_eat[zms[i].frame_index];
  187. TransparentImage(NULL, zms[i].x, zms[i].y, img);
  188. }
  189. else if (zms[i].used) {
  190. IMAGE* img = &img_zm[zms[i].frame_index];
  191. TransparentImage(NULL, zms[i].x, zms[i].y, img);
  192. }
  193. }
  194. }
  195. void DrawSunshineBall() {
  196. int ballmax = sizeof(balls) / sizeof(balls[0]);
  197. for (int i = 0; i < ballmax; i++) {
  198. if (balls[i].used ) {
  199. IMAGE img = img_sunshineball[balls[i].frame_index];
  200. TransparentImage(NULL, balls[i].pcur.x, balls[i].pcur.y, &img);
  201. }
  202. }
  203. }
  204. void DrawShoot() {
  205. int count_max = sizeof(bullets) / sizeof(bullets[0]);
  206. for (int i = 0; i < count_max; i++) {
  207. if (bullets[i].blast) {
  208. IMAGE* img = &img_bullet_blast[bullets[i].frame_index];
  209. TransparentImage(NULL, bullets[i].x, bullets[i].y, img);
  210. } else if (bullets[i].used) {
  211. TransparentImage(NULL, bullets[i].x, bullets[i].y, &img_bullet_normal);
  212. }
  213. }
  214. }
  215. void DrawCards() {
  216. for (int i = 0; i < zhi_wu_count; i++) {
  217. int x = 338 + i * 65;
  218. int y = 6;
  219. TransparentImage(NULL, x, y, &img_cards[i]);
  220. }
  221. }
  222. void DrawCurPlant() {
  223. if (curplant) {
  224. IMAGE* img = img_plants[curplant - 1][0];
  225. int x = curx - img->getwidth() / 2;
  226. int y = cury - img->getheight() / 2;
  227. TransparentImage(NULL, x, y, img);
  228. }
  229. }
  230. void DrawMap() {
  231. for (int i = 0; i < 5; i++) {
  232. for (int j = 0; j < 9; j++) {
  233. if (map[i][j].type != 0) {
  234. int index = map[i][j].frame_index;
  235. TransparentImage(NULL, map[i][j].x, map[i][j].y, img_plants[map[i][j].type - 1][index]);
  236. }
  237. }
  238. }
  239. }
  240. void DrawSunScore() {
  241. char score_text[8];
  242. sprintf_s(score_text, sizeof(score_text), "%d", sunshine);
  243. outtextxy(276, 67, score_text);
  244. }
  245. void UpdateWindow() {
  246. BeginBatchDraw();
  247. putimage(-112, 0, &img_bg);
  248. TransparentImage(NULL, 250, 0, &img_bar);
  249. DrawCards();
  250. DrawCurPlant();
  251. DrawMap();
  252. DrawSunshineBall();
  253. DrawSunScore();
  254. DrawZm();
  255. DrawShoot();
  256. EndBatchDraw();
  257. }
  258. void CollectSunshine(ExMessage* msg) {
  259. int count = sizeof(balls) / sizeof(balls[0]);
  260. int w = img_sunshineball[0].getwidth();
  261. int h = img_sunshineball[0].getheight();
  262. for (int i = 0; i < count; i++) {
  263. if (balls[i].used) {
  264. int x = balls[i].pcur.x;
  265. int y = balls[i].pcur.y;
  266. if (msg->x > x && msg->x < x + w && msg->y >y && msg->y < y + h) {
  267. //balls[i].used = false;
  268. //mciSendString("play res/sunshine.mp3", 0, 0, 0);
  269. balls[i].status = sunshine_collect;
  270. //通过角度控制阳光轨迹(*记笔记)
  271. /*float desty = 0;
  272. float destx = 262;
  273. float angle = atan((y - desty) / (x - destx));
  274. balls[i].xoff = 20 * cos(angle);
  275. balls[i].yoff = 20 * sin(angle);*/
  276. //贝塞尔曲线
  277. balls[i].p1 = balls[i].pcur;
  278. balls[i].p4 = vector2(262, 0);
  279. float distance = dis(balls[i].p1 - balls[i].p4);
  280. float off = 16;
  281. balls[i].speed = 1.0 / (distance / off);
  282. balls[i].t = 0;
  283. //总距离(distance) / 每次移动的像素(off)= 需要移动的次数
  284. break;//收集阳光不能一箭双雕
  285. }
  286. }
  287. }
  288. }
  289. void UserClick() {
  290. ExMessage msg;
  291. static int status = 0;
  292. if (peekmessage(&msg)) {
  293. if (msg.message == WM_LBUTTONDOWN) {
  294. if (status == 1) {
  295. status = 0;
  296. if (msg.y > 75 && msg.x < 1000 && msg.x > 256 - 112) {
  297. int row = (msg.y - 75) / 102; //x
  298. int col = (msg.x - 256 + 112) / 81; //y
  299. if (map[row][col].type == 0) {
  300. map[row][col].x = 256 - 112 + col * 81;
  301. map[row][col].y = 90 + row * 102;
  302. map[row][col].type = curplant;
  303. map[row][col].frame_index = 0;
  304. map[row][col].shoot_time = 0;
  305. }
  306. }
  307. curplant = 0;
  308. }
  309. if (msg.x > 338 && msg.x < 338 + 65 * zhi_wu_count && msg.y < 96) {
  310. if (status == 0) {
  311. int index = (msg.x - 338) / 65;
  312. curplant = index + 1;
  313. status = 1;
  314. curx = msg.x, cury = msg.y;
  315. }
  316. }
  317. else {
  318. CollectSunshine(&msg);
  319. }
  320. }
  321. else if (msg.message == WM_MOUSEMOVE && status == 1) {
  322. curx = msg.x, cury = msg.y;
  323. }
  324. }
  325. }
  326. void CreateSunshine() {
  327. int ball_max = sizeof(balls) / sizeof(balls[0]);
  328. static int count = 0;
  329. static int fre = 0;
  330. count++;
  331. if (count >= fre) {
  332. fre = rand() % 300 + 200;
  333. count = 0;
  334. int i;
  335. for (i = 0; i < ball_max && balls[i].used; i++);
  336. if (i >= ball_max) return;
  337. //原计算
  338. balls[i].used = true;
  339. balls[i].frame_index = 0;
  340. /*balls[i].x = rand() % (900 - 260 - img_sunshineball->getwidth()) + 260;
  341. balls[i].dest_y = (rand() % 6) * 90 + 100 + img_sunshineball->getheight();
  342. balls[i].y = 20;*/
  343. balls[i].timer = 0;
  344. /*balls[i].xoff = 0;
  345. balls[i].yoff = 0;*/
  346. //纳赛尔曲线
  347. balls[i].status = sunshine_down;
  348. balls[i].t = 0;
  349. balls[i].p1 = vector2(260 - 112 + rand() % (900 - 260 + 112), 60);
  350. balls[i].p4 = vector2(balls[i].p1.x, 200 + (rand() % 4) * 90);
  351. int off = 2;
  352. float distance = balls[i].p4.y - balls[i].p1.y;
  353. balls[i].speed = 1.0 / (distance / off);
  354. }
  355. //向日葵生产阳光
  356. for (int i = 0; i < 5; i++) {
  357. for (int j = 0; j < 9; j++) {
  358. if (map[i][j].type == xiang_ri_kui + 1) { //当初初始化的不好,如果==xiang_ri_kui实际是豌豆生产的太阳
  359. map[i][j].timer++;
  360. if (map[i][j].timer > 200) {
  361. map[i][j].timer = 0;
  362. int k;
  363. for (k = 0; k < ball_max && balls[k].used; k++);
  364. if (k >= ball_max) return;
  365. balls[k].used = true;
  366. balls[k].p1 = vector2(map[i][j].x, map[i][j].y);//起点就是向日葵的位置
  367. int w = 75;
  368. balls[k].p4 = vector2(map[i][j].x + w,
  369. map[i][j].y + img_plants[xiang_ri_kui][0]->getheight() - img_sunshineball[0].getheight());
  370. balls[k].p2 = vector2(balls[k].p1.x + w * 0.3, balls[k].p1.y - 100);
  371. balls[k].p3 = vector2(balls[k].p1.x + w * 0.7, balls[k].p1.y - 100);
  372. balls[k].status = sunshine_product;
  373. balls[k].speed = 0.05;
  374. balls[k].t = 0;
  375. }
  376. }
  377. }
  378. }
  379. }
  380. void UpdateSunshine() {
  381. int ballmax = sizeof(balls) / sizeof(balls[0]);
  382. for (int i = 0; i < ballmax; i++) {
  383. if (balls[i].used) {
  384. balls[i].frame_index = (balls[i].frame_index + 1) % 29;
  385. if (balls[i].status == sunshine_down) {
  386. sunshineball* sun = &balls[i];
  387. sun->t += sun->speed;
  388. sun->pcur = sun->p1 + sun->t * (sun->p4 - sun->p1);
  389. if (sun->t >= 1) {
  390. sun->status = sunshine_ground;
  391. sun->timer = 0;
  392. }
  393. }
  394. else if (balls[i].status == sunshine_ground) {
  395. balls[i].timer++;
  396. if (balls[i].timer > 100) {
  397. balls[i].used = false;
  398. balls[i].timer = 0;
  399. }
  400. }
  401. else if (balls[i].status == sunshine_collect) {
  402. sunshineball* sun = &balls[i];
  403. sun->t += sun->speed;
  404. sun->pcur = sun->p1 + sun->t * (sun->p4 - sun->p1);
  405. if (sun->t > 1) {
  406. sun->used = false;
  407. sunshine += 25;
  408. }
  409. }
  410. else if (balls[i].status == sunshine_product) {
  411. sunshineball* sun = &balls[i];
  412. sun->t += sun->speed;
  413. sun->pcur = calcBezierPoint(sun->t, sun->p1, sun->p2, sun->p3, sun->p4);
  414. if (sun->t > 1) {
  415. sun->status = sunshine_ground;
  416. sun->timer = 0;
  417. }
  418. }
  419. /*if (balls[i].timer == 0) {
  420. balls[i].y += 2;
  421. }
  422. if (balls[i].y >= balls[i].dest_y) {
  423. balls[i].timer++;
  424. if (balls[i].timer > 100) {
  425. balls[i].used = false;
  426. }
  427. }*/
  428. }
  429. /*else if (balls[i].xoff) {
  430. float desty = 0;
  431. float destx = 262;
  432. float angle = atan((balls[i].y - desty) / (balls[i].x - destx));
  433. balls[i].xoff = 20 * cos(angle);
  434. balls[i].yoff = 20 * sin(angle);
  435. balls[i].x -= balls[i].xoff;
  436. balls[i].y -= balls[i].yoff;
  437. if (balls[i].y < 0 || balls[i].x < 262) {
  438. balls[i].xoff = 0;
  439. balls[i].yoff = 0;
  440. }
  441. }*/
  442. }
  443. }
  444. void CreatZm() {
  445. static int zm_fre = 600;
  446. static int count = 0;
  447. count++;
  448. if (count > zm_fre && zm_appear_count <= ZM_MAX) {
  449. zm_appear_count++;
  450. count = 0;
  451. zm_fre = rand() % 200 + 300;
  452. int i;
  453. int zm_max = sizeof(zms) / sizeof(zms[0]);
  454. for (i = 0; i < zm_max && zms[i].used; i++);
  455. if (i < zm_max) {
  456. zms[i].eating = false;
  457. zms[i].used = true;
  458. zms[i].dead = false;
  459. zms[i].x = WIN_WIDTH;
  460. zms[i].speed = 1 ;
  461. zms[i].row = rand() % 5;
  462. zms[i].y = 35 + zms[i].row * 100;
  463. zms[i].blood = 5;
  464. }
  465. }
  466. }
  467. void UpdateZm() {
  468. int zm_max = sizeof(zms) / sizeof(zms[0]);
  469. for (int i = 0; i < zm_max; i++) {
  470. if (zms[i].used) {
  471. zms[i].x -= zms[i].speed ;
  472. if (zms[i].x < 56) {
  473. game_status = FAIL;
  474. }
  475. }
  476. }
  477. static int count = 0;
  478. count++;
  479. if (count > 4) {
  480. count = 0;
  481. for (int i = 0; i < zm_max; i++) {
  482. if (zms[i].dead && zms[i].used) {
  483. zms[i].frame_index++;
  484. if (zms[i].frame_index > 9) {
  485. zms[i].used = false;
  486. cout << i << ' ' << zms[i].frame_index << endl;
  487. if (killed_zm_count == ZM_MAX) {
  488. game_status = WIN;
  489. }
  490. }
  491. }
  492. else if (zms[i].eating == true) {
  493. zms[i].frame_index = (zms[i].frame_index + 1) % 21;
  494. }
  495. else if (zms[i].used) {
  496. zms[i].frame_index = (zms[i].frame_index + 1) % 22;
  497. }
  498. }
  499. }
  500. }
  501. void Shoot() {
  502. bool line_zm[5];
  503. memset(line_zm, false, sizeof(line_zm)); //如果第一行(line[0])没有僵尸,就是false
  504. int zm_count = sizeof(zms) / sizeof(zms[0]);
  505. int danger_x = WIN_WIDTH - img_zm[0].getwidth();
  506. int bullet_max = sizeof(bullets) / sizeof(bullets[0]);
  507. for (int i = 0; i < zm_count; i++) {
  508. if (zms[i].used && zms[i].x < danger_x) {
  509. line_zm[zms[i].row] = true;
  510. }
  511. }
  512. for (int i = 0; i < 5; i++) {
  513. for (int j = 0; j < 9; j++) {
  514. if (map[i][j].type == wan_dou + 1 && line_zm[i]) {
  515. map[i][j].shoot_time++;
  516. if (map[i][j].shoot_time > 40) {
  517. map[i][j].shoot_time = 0;
  518. int k;
  519. for (k = 0; k < bullet_max && bullets[k].used; k++);
  520. if (k < bullet_max) {
  521. bullets[k].used = true;
  522. bullets[k].row = i;
  523. bullets[k].speed = 4;
  524. bullets[k].blast = false;
  525. bullets[k].frame_index = 0;
  526. int plant_x = 256 - 112 + j * 81;
  527. int plant_y = 90 + i * 102;
  528. bullets[k].x = plant_x + img_plants[map[i][j].type - 1][0]->getwidth() - 10;
  529. bullets[k].y = plant_y + 5;
  530. }
  531. }
  532. }
  533. }
  534. }
  535. }
  536. void UpdateShoot() {
  537. int count_max = sizeof(bullets) / sizeof(bullets[0]);
  538. for (int i = 0; i < count_max; i++) {
  539. if (bullets[i].used) {
  540. bullets[i].x += bullets[i].speed;
  541. if (bullets[i].x > WIN_WIDTH) bullets[i].used = false;
  542. }
  543. if (bullets[i].blast) {
  544. bullets[i].frame_index++;
  545. if (bullets[i].frame_index >= 4) {
  546. bullets[i].used = false;
  547. bullets[i].blast = false;
  548. }
  549. }
  550. }
  551. }
  552. void Check_Plant_to_Zm() {
  553. int bullet_max = sizeof(bullets) / sizeof(bullets[0]);
  554. int zm_max = sizeof(zms) / sizeof(zms[0]);
  555. for (int i = 0; i < bullet_max; i++) {
  556. if (bullets[i].used == false || bullets[i].blast) continue;
  557. for (int j = 0; j < zm_max; j++) {
  558. if (zms[j].used == false) continue;
  559. int x1 = zms[j].x + 80;
  560. int x2 = zms[j].x + 110;
  561. int x = bullets[i].x;
  562. if (x > x1 && x < x2 && bullets[i].row == zms[j].row && zms[j].dead != true) {
  563. zms[j].blood--;
  564. bullets[i].blast = true;
  565. bullets[i].speed = 0;
  566. if (zms[j].blood <= 0) {
  567. zms[j].dead = true;
  568. zms[j].speed = 0;
  569. zms[j].frame_index = 0;
  570. }
  571. break; //不能一箭双雕
  572. }
  573. }
  574. }
  575. }
  576. void Check_Zm_to_Plant() {
  577. int zm_max = sizeof(zms) / sizeof(zms[0]);
  578. for (int i = 0; i < zm_max; i++) {
  579. if (zms[i].used == false) continue;
  580. int row = zms[i].row;
  581. for (int j = 0; j < 9; j++) {
  582. if (map[row][j].type == 0) continue;
  583. int plant_x = 256 - 112 + j * 81;
  584. int x1 = plant_x + 10;
  585. int x2 = plant_x + 60;
  586. int x3 = zms[i].x + 80;
  587. int x4 = zms[i].x + 150;
  588. if ((x1 < x3 && x4 < x2) || (x3 < x1 && x1 < x4) || (x3 < x2 && x2 < x4)) {
  589. if (map[row][j].catched) {
  590. map[row][j].deadtime++;
  591. if (map[row][j].deadtime >= 100) {
  592. map[row][j].deadtime = 0;
  593. map[row][j].type = 0;
  594. zms[i].eating = false;
  595. zms[i].frame_index = 0;
  596. zms[i].speed = 1;
  597. }
  598. }
  599. else {
  600. map[row][j].catched = true;
  601. map[row][j].deadtime = 0;
  602. zms[i].eating = true;
  603. zms[i].speed = 0;
  604. zms[i].frame_index = 0;
  605. }
  606. }
  607. }
  608. }
  609. }
  610. void CollisionCheck() {
  611. Check_Zm_to_Plant();
  612. Check_Plant_to_Zm();
  613. }
  614. void UpdateMap() {
  615. for (int i = 0; i < 5; i++) {
  616. for (int j = 0; j < 9; j++) {
  617. if (map[i][j].type != 0) {
  618. map[i][j].frame_index++;
  619. int plant_type = map[i][j].type - 1;
  620. int index = map[i][j].frame_index;
  621. if (img_plants[plant_type][index] == NULL) map[i][j].frame_index = 0;
  622. }
  623. }
  624. }
  625. }
  626. void UpdateGame() {
  627. UpdateMap();
  628. CreateSunshine();
  629. UpdateSunshine();
  630. CreatZm();
  631. UpdateZm();
  632. Shoot();
  633. UpdateShoot();
  634. CollisionCheck();
  635. }
  636. int GetDelay() {
  637. static unsigned long long lastTime = 0;
  638. unsigned long long currentTime = GetTickCount();
  639. if (lastTime == 0) {
  640. lastTime = currentTime;
  641. return 0;
  642. }
  643. else {
  644. int ret = currentTime - lastTime;
  645. lastTime = currentTime;
  646. return ret;
  647. }
  648. }
  649. void StartUI() {
  650. IMAGE img_bg, img_m1, img_m2;
  651. loadimage(&img_bg, "res/menu.png");
  652. loadimage(&img_m1, "res/menu1.png");
  653. loadimage(&img_m2, "res/menu2.png");
  654. int flag = 0;
  655. while (1) {
  656. BeginBatchDraw();
  657. TransparentImage(NULL, 0, 0, &img_bg);
  658. TransparentImage(NULL, 474, 75, flag ? &img_m1 : &img_m2);
  659. ExMessage msg;
  660. if (peekmessage(&msg)) {
  661. if (msg.message == WM_LBUTTONDOWN && msg.x > 474 && msg.x < 474 + 300 && msg.y > 75 && msg.y < 75 + 140) {
  662. flag = 1;
  663. }
  664. else if (msg.message == WM_LBUTTONUP && msg.x > 474 && msg.x < 474 + 300 && msg.y > 75 && msg.y < 75 + 140) {
  665. EndBatchDraw();
  666. break;
  667. }
  668. }
  669. EndBatchDraw();
  670. }
  671. }
  672. void ViewScence() {
  673. int xmin = WIN_WIDTH - img_bg.getwidth();
  674. vector2 points[9] = {
  675. {550, 80}, {530, 160}, {630, 170}, {530, 200}, {515, 270},
  676. {565, 370}, {605, 340}, {705, 280}, {690, 340}
  677. };
  678. int index[9];
  679. for (int i = 0; i < 9; i++) {
  680. index[i] = rand() % 11;
  681. }
  682. int count = 0;
  683. for (int x = 0; x >= xmin; x -= 2) {
  684. BeginBatchDraw();
  685. putimage(x, 0, &img_bg);
  686. count++;
  687. for (int k = 0; k < 9; k++) {
  688. TransparentImage(NULL, points[k].x - xmin + x, points[k].y, &img_zm_stand[index[k]]);
  689. if (count > 10) {
  690. index[k] = (index[k] + 1) % 11;
  691. }
  692. }
  693. if (count > 10) count = 0;
  694. EndBatchDraw();
  695. Sleep(5);
  696. }
  697. //停留1s左右
  698. for (int i = 0; i < 100; i++) {
  699. BeginBatchDraw();
  700. putimage(xmin, 0, &img_bg);
  701. for (int k = 0; k < 9; k++) {
  702. TransparentImage(NULL, points[k].x, points[k].y, &img_zm_stand[index[k]]);
  703. index[k] = (index[k] + 1) % 11;
  704. }
  705. EndBatchDraw();
  706. Sleep(30);
  707. }
  708. count = 0;
  709. for (int x = xmin; x <= -112; x += 2) {
  710. BeginBatchDraw();
  711. putimage(x, 0, &img_bg);
  712. count++;
  713. for (int k = 0; k < 9; k++) {
  714. TransparentImage(NULL, points[k].x + x - xmin, points[k].y, &img_zm_stand[index[k]]);
  715. if (count > 10) {
  716. index[k] = (index[k] + 1) % 11;
  717. }
  718. }
  719. if (count > 10) count = 0;
  720. EndBatchDraw();
  721. Sleep(5);
  722. }
  723. }
  724. void BarDown() {
  725. int height = img_bar.getheight();
  726. for (int y = -height; y <= 0; y++) {
  727. BeginBatchDraw();
  728. putimage(-112, 0, &img_bg);
  729. TransparentImage(NULL, 250, y, &img_bar);
  730. for (int i = 0; i < zhi_wu_count; i++) {
  731. int x = 338 + i * 65;
  732. TransparentImage(NULL, x, 6 + y, &img_cards[i]);
  733. }
  734. EndBatchDraw();
  735. Sleep(5);
  736. }
  737. }
  738. int CheckStatus() {
  739. if (game_status == FAIL) {
  740. Sleep(2000);
  741. putimage(0, 0, &img_fail);
  742. return -1;
  743. }
  744. else if (game_status == WIN) {
  745. Sleep(2000);
  746. putimage(0, 0, &img_win);
  747. return 1;
  748. }
  749. return 0;
  750. }
  751. int main() {
  752. GameInit();
  753. StartUI();
  754. ViewScence();
  755. BarDown();
  756. int timer = 0;
  757. bool time_flag = true;
  758. while (1) {
  759. UserClick();
  760. timer += GetDelay();
  761. if (timer > 40) {
  762. time_flag = true;
  763. timer = 0;
  764. }
  765. if (time_flag) {
  766. time_flag = false;
  767. UpdateGame();
  768. UpdateWindow();
  769. if (CheckStatus() != 0) goto P;
  770. }
  771. }
  772. P:
  773. Sleep(80);
  774. system("pause");
  775. }

与rock老师不同列表:
1.僵尸死亡图片(黑沙)
2.透明贴图
3.僵尸吃植物的碰撞检测,根据弹幕改,增加x4判断

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

闽ICP备14008679号