当前位置:   article > 正文

C语言植物大战僵尸_植物大战僵尸能用c语言写吗

植物大战僵尸能用c语言写吗

说明:

本项目是在B站博主<程序员Rock>的视频指导下完成,项目实现需要资源包,如有需要点这里根据博主分享移步自取

vs2022编译环境下完成,主要使用c语法,夹杂部分c++语法

效果预览:

文件组成:

计两个头文件,三个源文件

源码:

tools.h

  1. #pragma once
  2. #include <graphics.h>
  3. void putimagePNG(int picture_x, int picture_y, IMAGE* picture);
  4. int getDelay();

vector2.h

  1. long long dis(vector2 x);
  2. //向量除法
  3. vector2 operator /(vector2 x, vector2 y);
  4. //向量膜
  5. vector2 operator %(vector2 x, vector2 y);
  6. //向量GCD
  7. vector2 gcd(vector2 x, vector2 y);
  8. //贝塞尔曲线
  9. vector2 calcBezierPoint(float t, vector2 p0, vector2 p1, vector2 p2, vector2 p3);

main.cpp

  1. #include<iostream>
  2. using namespace std;
  3. #include<graphics.h>
  4. #include"tools.h"
  5. #include<ctime>
  6. #include<time.h>
  7. #include<math.h>
  8. #include"vector2.h" //向量工具包
  9. #include<mmsystem.h> //音乐
  10. #pragma comment(lib,"winmm.lib")
  11. enum { WAN_DAO, XIANG_RI_KUI, SHI_REN_HUA, ZHI_WU_COUNT }; //植物枚举
  12. enum { SUNSHINE_DOWN, SUNSHINE_GROUND, SUNSHINE_COLLECT, SUNSHINE_PRODUCT }; //阳光球状态枚举
  13. enum { GOING, WIN, FAIL };
  14. IMAGE imgBg; //游戏背景图
  15. IMAGE imgBar; //状态栏,放植物的背景板
  16. IMAGE imgCards[ZHI_WU_COUNT]; //植物卡牌数组
  17. IMAGE* imgZhiWu[ZHI_WU_COUNT][20]; //植物数组
  18. int curX, curY; //当前选中植物在移动中的坐标
  19. int curZhiWu; //当前选中的植物 0-没有选中,1-选中第一种植物
  20. int killZmCount; //杀掉的僵尸总数
  21. int zmCount; //生成的僵尸数量
  22. int gameStatus; //游戏的状态
  23. //宏定义游戏窗口大小
  24. #define WIN_WIDTH 900
  25. #define WIN_HEIGHT 600
  26. #define ZM_MAX 10 //僵尸总数
  27. struct zhiWu { //植物结构体
  28. int type; //0-没有植物,1-第一种植物
  29. int frameIndex; //序列帧的序号
  30. int shootTimer; //植物攻击时间间隔
  31. bool catched; //植物是否被僵尸捕获
  32. int deadTimer; //植物被吃时的死亡倒计时
  33. int x, y;
  34. int timer; //用于向日葵生成阳光的计时器
  35. };
  36. struct sunShineBall { //阳光球结构体
  37. int x, y;//阳光球的x、y坐标
  38. int frameIndex; //阳光球序列帧的序号
  39. int destY; //阳光球停止的y坐标
  40. bool used; //阳光球是否在使用
  41. int timer; //计时器,用来限制阳光球最后的停留时间
  42. int xoff; //阳光球归位的x坐标
  43. int yoff; //阳光球归位的y坐标
  44. //优化
  45. float t; //贝塞尔曲线的时间点 01
  46. vector2 p1, p2, p3, p4;
  47. vector2 pCur; //当前时刻阳光球的位置
  48. float speed;
  49. int status; //阳光球的状态
  50. };
  51. struct sunShineBall balls[10]; //阳光球池,用来事先存储阳光
  52. IMAGE imgSunShineBall[29]; //阳光序列帧总数 - 可以定义一个宏,方便后期管理
  53. struct zm { //僵尸结构体 -后期还是需要像植物一样搞个枚举,方便创建不同类型的僵尸
  54. int x, y;
  55. int row;
  56. int frameIndex;
  57. bool used;
  58. int speed; //僵尸前行速度
  59. int blood; //僵尸血量
  60. bool dead; //僵尸是否死亡
  61. bool eating; //僵尸是否在吃植物
  62. };
  63. struct zm zms[10]; //僵尸池,用来事先存储僵尸
  64. IMAGE imgZm[22];
  65. IMAGE imgZmDead[20];
  66. IMAGE imgZmEat[21];
  67. IMAGE imgZmStand[11];
  68. struct bullet { //豌豆子弹结构体
  69. int x, y, row, speed;
  70. bool used;
  71. bool blast; //是否爆炸
  72. int frameIndex; //爆炸帧序号
  73. };
  74. struct bullet bullets[30]; //豌豆子弹池
  75. IMAGE imgBulletNormal;
  76. IMAGE imgBulletBlast[4];
  77. int ballMax = sizeof(balls) / sizeof(balls[0]); //阳光池中阳光的总数 -是不是很奇怪,明明上面已经定义了10
  78. //这里还要计算一遍,别问,问就是高可用(个人建议把10换成一个宏定义,更加高可用)
  79. int zmMax = sizeof(zms) / sizeof(zms[0]); //僵尸池中僵尸的总数
  80. int bulletMax = sizeof(bullets) / sizeof(bullets[0]); //豌豆子弹池的总数
  81. struct zhiWu map[3][9]; //地图数组,方便存储植物
  82. int sunShine; //阳光值
  83. //声明游戏初始化
  84. void gameInit();
  85. //声明游戏开始界面
  86. void startUI();
  87. //声明场景巡场
  88. void viewScence();
  89. //声明状态栏下滑
  90. void barsDown();
  91. //声明渲染游戏窗口(渲染图片到窗口上)
  92. void updateWindow();
  93. //声明用户点击(鼠标操作)
  94. void userClick();
  95. //声明判断文件是否存在
  96. bool fileExist(const char* name);
  97. //声明更新动作
  98. void updateGame();
  99. //声明检测游戏是否结束
  100. bool checkOver();
  101. //声明创建阳光
  102. void createSunShine();
  103. //声明更新阳光动作
  104. void updateSunShine();
  105. //声明收集阳光
  106. void collectSunshine(ExMessage* msg);
  107. //声明创建僵尸
  108. void createZm();
  109. //声明更新僵尸动作
  110. void updateZm();
  111. //声明创建豌豆子弹
  112. void createBullets();
  113. //声明更新豌豆子弹动作
  114. void updateBullets();
  115. //声明检测僵尸碰撞
  116. void collisionCheck();
  117. //声明豌豆子弹与僵尸的碰撞检测
  118. void checkBullet2Zm();
  119. //声明僵尸与植物的碰撞检测
  120. void checkZm2ZhiWu();
  121. int main() {
  122. gameInit(); //游戏初始化
  123. startUI(); //加载游戏开始界面
  124. viewScence(); //场景巡场
  125. barsDown(); //状态栏下滑
  126. //双缓冲,先将要绘制的内容一次性绘制在图片上,再把图片输出,避免不断从内存读取数据而导致的屏幕闪烁
  127. //主要由BeginBatchDraw()、EndBatchDraw()、FlushBatchDraw()组成
  128. //BeginBatchDraw(); 双缓冲加到外面会卡状态栏bug-现在的技术暂时解决不了
  129. int timer = 0;
  130. bool flag = true;
  131. while (1)
  132. {
  133. userClick(); //获取用户点击事件
  134. timer += getDelay(); //获取间隔时间
  135. if (timer > 20) { //用来限制植物渲染时间
  136. timer = 0;
  137. flag = true;
  138. }
  139. if (flag) {
  140. flag = false;
  141. updateWindow(); //更新游戏窗口(渲染)
  142. updateGame(); //更新动作
  143. if (checkOver())break; //检查游戏是否结束
  144. }
  145. //刷新图片,避免黑屏
  146. //FlushBatchDraw();
  147. }
  148. //EndBatchDraw();
  149. system("pause");
  150. return 0;
  151. }
  152. //游戏初始化实现
  153. void gameInit() {
  154. //设置随机种子
  155. srand(time(NULL));
  156. //加载游戏背景图片
  157. loadimage(&imgBg, "res/bg.jpg");
  158. //加载状态栏
  159. loadimage(&imgBar, "res/bar5.png");
  160. killZmCount = 0;
  161. zmCount = 0;
  162. gameStatus = GOING;
  163. memset(imgZhiWu, 0, sizeof(imgZhiWu)); //给指针赋空值
  164. memset(map, 0, sizeof(map)); //初始化地图数组
  165. memset(balls, 0, sizeof(balls)); //初始化阳光池
  166. memset(zms, 0, sizeof(zms)); //初始化僵尸池
  167. memset(bullets, 0, sizeof(bullets)); //初始化豌豆子弹池
  168. //加载植物卡牌
  169. char name[64];
  170. for (int i = 0; i < ZHI_WU_COUNT; i++) {
  171. //生成植物卡牌的文件名
  172. sprintf_s(name, sizeof(name), "res/Cards/card_%d.png", i + 1);
  173. loadimage(&imgCards[i], name);
  174. for (int j = 0; j < 20; j++) { //20是固定值,可以寻求更匹配的方式
  175. sprintf_s(name, sizeof(name), "res/zhiwu/%d/%d.png", i, j + 1);
  176. //判断文件是否存在
  177. if (fileExist(name)) {
  178. imgZhiWu[i][j] = new IMAGE;
  179. loadimage(imgZhiWu[i][j], name);
  180. }
  181. else {
  182. break;
  183. }
  184. }
  185. }
  186. //初始化选中植物
  187. curZhiWu = 0;
  188. //初始化阳光值
  189. sunShine = 50;
  190. //加载阳光
  191. for (int i = 0; i < 29; i++) { //29是固定值,可以寻求更匹配的方式
  192. sprintf_s(name, sizeof(name), "res/sunshine/%d.png", i + 1);
  193. loadimage(&imgSunShineBall[i], name);
  194. }
  195. //加载僵尸图片
  196. for (int i = 0; i < 22; i++) {
  197. sprintf_s(name, sizeof(name), "res/zm/%d.png", i + 1);
  198. loadimage(&imgZm[i], name);
  199. }
  200. //加载僵尸死亡图片
  201. for (int i = 0; i < 20; i++) {
  202. sprintf_s(name, sizeof(name), "res/zm_dead/%d.png", i + 1);
  203. loadimage(&imgZmDead[i], name);
  204. }
  205. //加载僵尸吃植物图片
  206. for (int i = 0; i < 21; i++) {
  207. sprintf_s(name, sizeof(name), "res/zm_eat/%d.png", i + 1);
  208. loadimage(&imgZmEat[i], name);
  209. }
  210. //加载巡场僵尸图片
  211. for (int i = 0; i < 11; i++) {
  212. sprintf_s(name, sizeof(name), "res/zm_stand/%d.png", i + 1);
  213. loadimage(&imgZmStand[i], name);
  214. }
  215. //加载豌豆子弹图片
  216. loadimage(&imgBulletNormal, "res/bullets/bullet_normal.png");
  217. //加载豌豆子弹爆炸图片
  218. loadimage(&imgBulletBlast[3], "res/bullets/bullet_blast.png");
  219. for (int i = 0; i < 3; i++) {
  220. float k = (i + 2) * 0.2;
  221. loadimage(&imgBulletBlast[i], "res/bullets/bullet_blast.png",
  222. imgBulletBlast[3].getwidth() * k, imgBulletBlast[3].getheight() * k, true);
  223. }
  224. //创建游戏窗口
  225. initgraph(WIN_WIDTH, WIN_HEIGHT, 1/*,1*/); //参数1表示再开一个控制台窗口
  226. //设置字体
  227. LOGFONT f;
  228. gettextstyle(&f);
  229. f.lfHeight = 30;
  230. f.lfWeight = 15;
  231. strcpy(f.lfFaceName, "Segoe UI Black"); //设置字体效果
  232. f.lfQuality = ANTIALIASED_QUALITY; //抗锯齿
  233. settextstyle(&f);
  234. setbkmode(TRANSPARENT); //字体模式:背景透明
  235. setcolor(BLACK); //字体颜色:黑色
  236. }
  237. //游戏开始界面实现
  238. void startUI() {
  239. IMAGE imgMenu, imgMenu1, imgMenu2;
  240. int flag = 0;
  241. loadimage(&imgMenu, "res/menu.png"); //加载开始背景图
  242. loadimage(&imgMenu1, "res/menu1.png");
  243. loadimage(&imgMenu2, "res/menu2.png");
  244. while (1) {
  245. BeginBatchDraw();
  246. putimage(0, 0, &imgMenu); //渲染开始背景图到窗口上
  247. putimagePNG(474, 75, flag == 0 ? &imgMenu1 : &imgMenu2);
  248. ExMessage msg;
  249. if (peekmessage(&msg)) {
  250. if (msg.message == WM_LBUTTONDOWN && //鼠标左键落下 扩展:当鼠标经过时也可以高亮
  251. msg.x > 474 && msg.x < 774 && msg.y>75 && msg.y < 215) {
  252. flag = 1;
  253. }
  254. }
  255. else if (msg.message == WM_LBUTTONUP && flag == 1) { //鼠标左键抬起
  256. EndBatchDraw();
  257. return;
  258. }
  259. EndBatchDraw();
  260. }
  261. }
  262. //声明场景巡场实现
  263. void viewScence() {
  264. int xMin = WIN_WIDTH - imgBg.getwidth();
  265. vector2 points[9] = { {550,80},{530,160},{630,170},{530,200},{525,270}, //9个僵尸站位
  266. {565,370},{605,340},{705,280},{690,340} };
  267. int index[9];
  268. for (int i = 0; i < 9; i++) {
  269. index[i] = rand() % 11;
  270. }
  271. int count = 0;
  272. for (int x = 0; x >= xMin; x -= 4) { //每帧移动2像素
  273. BeginBatchDraw();
  274. putimage(x, 0, &imgBg);
  275. count++;
  276. for (int k = 0; k < 9; k++) {
  277. putimagePNG(points[k].x - xMin + x, points[k].y, &imgZmStand[index[k]]);
  278. if (count >= 10) {
  279. index[k] = (index[k] + 1) % 11;
  280. }
  281. }
  282. if (count >= 10)count = 0;
  283. EndBatchDraw();
  284. Sleep(5);
  285. }
  286. //停留1s
  287. for (int i = 0; i < 100; i++) {
  288. BeginBatchDraw();
  289. putimage(xMin, 0, &imgBg);
  290. for (int j = 0; j < 9; j++) {
  291. putimagePNG(points[j].x, points[j].y, &imgZmStand[index[j]]);
  292. index[j] = (index[j] + 1) % 11;
  293. }
  294. EndBatchDraw();
  295. Sleep(20);
  296. }
  297. //往回移
  298. for (int x = xMin; x <= -112; x += 4) { //每帧移动2像素
  299. BeginBatchDraw();
  300. putimage(x, 0, &imgBg);
  301. count++;
  302. for (int k = 0; k < 9; k++) {
  303. putimagePNG(points[k].x - xMin + x, points[k].y, &imgZmStand[index[k]]);
  304. if (count >= 10) {
  305. index[k] = (index[k] + 1) % 11;
  306. }
  307. if (count >= 10)count = 0;
  308. }
  309. EndBatchDraw();
  310. Sleep(5);
  311. }
  312. }
  313. //状态栏下滑实现
  314. void barsDown() {
  315. int height = imgBar.getheight();
  316. for (int y = -height; y <= 0; y++) {
  317. BeginBatchDraw();
  318. putimage(-112, 0, &imgBg);
  319. putimagePNG(250, y, &imgBar);
  320. for (int i = 0; i < ZHI_WU_COUNT; i++) {
  321. int x = 338 + i * 65;
  322. putimagePNG(x, 6 + y, &imgCards[i]);
  323. }
  324. EndBatchDraw();
  325. Sleep(5);
  326. }
  327. }
  328. //把图片加载到窗口上(渲染)
  329. void updateWindow() {
  330. BeginBatchDraw();
  331. putimage(-112, 0, &imgBg); //加载(渲染)背景板
  332. putimagePNG(255, 0, &imgBar); //加载(渲染)状态栏
  333. for (int i = 0; i < ZHI_WU_COUNT; i++) { //加载(渲染)植物卡牌
  334. int x = 343 + i * 65;
  335. int y = 6;
  336. putimagePNG(x, y, &imgCards[i]);
  337. }
  338. //在地图上加载(渲染)植物
  339. for (int i = 0; i < 3; i++) {
  340. for (int j = 0; j < 9; j++) {
  341. if (map[i][j].type > 0) {
  342. //int x = 256 + j * 81;
  343. //int y = 179 + i * 102 + 14;
  344. int zhiWuType = map[i][j].type - 1;
  345. int index = map[i][j].frameIndex;
  346. //putimagePNG(x, y, imgZhiWu[zhiWuType][index]);
  347. putimagePNG(map[i][j].x, map[i][j].y, imgZhiWu[zhiWuType][index]);
  348. }
  349. }
  350. }
  351. //加载(渲染)拖动的植物
  352. if (curZhiWu > 0) {
  353. IMAGE* img = imgZhiWu[curZhiWu - 1][0];
  354. putimagePNG(curX - img->getwidth() / 2, curY - img->getheight() / 2, img);
  355. }
  356. //for (int i = 0; i < ballMax; i++) { //此时渲染会被僵尸挡住
  357. // if (balls[i].used || balls[i].xoff) { //加载(渲染)阳光
  358. // IMAGE* img = &imgSunShineBall[balls[i].frameIndex];
  359. // putimagePNG(balls[i].x, balls[i].y, img);
  360. // }
  361. //}
  362. //加载(渲染)阳光值
  363. char scoreText[8];
  364. sprintf_s(scoreText, sizeof(scoreText), "%d", sunShine); //把阳光值转换成字符类型
  365. outtextxy(283, 67, scoreText); //渲染输出 位置可调整成居中,而不使用固定值y
  366. //加载(渲染)僵尸
  367. for (int i = 0; i < zmMax; i++) {
  368. if (zms[i].used) {
  369. //IMAGE* img = &imgZm[zms[i].frameIndex];
  370. //IMAGE* img = (zms[i].dead) ? imgZmDead : imgZm;
  371. IMAGE* img = NULL;
  372. if (zms[i].eating) {
  373. img = imgZmEat;
  374. }
  375. else if (zms[i].dead) {
  376. img = imgZmDead;
  377. }
  378. else {
  379. img = imgZm;
  380. }
  381. img += zms[i].frameIndex;
  382. putimagePNG(zms[i].x, zms[i].y - img->getheight(), img);
  383. }
  384. }
  385. //加载(渲染)豌豆子弹
  386. for (int i = 0; i < bulletMax; i++) {
  387. if (bullets[i].used) {
  388. if (bullets[i].blast) { //豌豆子弹碰撞渲染
  389. IMAGE* img = &imgBulletBlast[bullets[i].frameIndex];
  390. //IMAGE* img = &imgBulletBlast[];
  391. putimagePNG(bullets[i].x, bullets[i].y, img);
  392. FlushBatchDraw();
  393. }
  394. else { //豌豆子弹普通形态渲染
  395. putimagePNG(bullets[i].x, bullets[i].y, &imgBulletNormal);
  396. }
  397. }
  398. }
  399. //加载(渲染)阳光
  400. for (int i = 0; i < ballMax; i++) {
  401. if (balls[i].used /* || balls[i].xoff*/) {
  402. IMAGE* img = &imgSunShineBall[balls[i].frameIndex];
  403. //putimagePNG(balls[i].x, balls[i].y, img);
  404. putimagePNG(balls[i].pCur.x, balls[i].pCur.y, img);
  405. }
  406. }
  407. EndBatchDraw();
  408. }
  409. //用户点击实现
  410. void userClick() {
  411. static int status = 0;
  412. ExMessage msg;
  413. if (peekmessage(&msg)) { //判断用户是否有操作
  414. if (msg.message == WM_LBUTTONDOWN) { //鼠标左键按下
  415. if (msg.x > 343 && msg.x < 343 + 65 * ZHI_WU_COUNT && msg.y < 96) { //点击卡牌的事件
  416. int index = (msg.x - 343) / 65;
  417. //判断阳光值是否足够购买植物
  418. if (index == XIANG_RI_KUI) {
  419. if (sunShine >= 50) {
  420. status = 1;
  421. curZhiWu = index + 1;
  422. //使植物显示在点击位置,避免了植物出现在上次消失位置的小bug
  423. curX = msg.x;
  424. curY = msg.y;
  425. sunShine -= 50;
  426. }
  427. }
  428. else if (index == WAN_DAO) {
  429. if (sunShine >= 100) {
  430. status = 1;
  431. curZhiWu = index + 1;
  432. //使植物显示在点击位置,避免了植物出现在上次消失位置的小bug
  433. curX = msg.x;
  434. curY = msg.y;
  435. sunShine -= 100;
  436. }
  437. }
  438. else if (index == SHI_REN_HUA) {
  439. if (sunShine >= 150) {
  440. status = 1;
  441. curZhiWu = index + 1;
  442. //使植物显示在点击位置,避免了植物出现在上次消失位置的小bug
  443. curX = msg.x;
  444. curY = msg.y;
  445. sunShine -= 150;
  446. }
  447. }
  448. //其他的还有待补充
  449. status = 1;
  450. //使植物显示在点击位置,避免了植物出现在上次消失位置的小bug
  451. curX = msg.x;
  452. curY = msg.y;
  453. }
  454. else { //收集阳光事件
  455. collectSunshine(&msg);
  456. }
  457. }
  458. else if (msg.message == WM_MOUSEMOVE && status == 1) { //鼠标移动
  459. curX = msg.x;
  460. curY = msg.y;
  461. }
  462. else if (msg.message == WM_RBUTTONDOWN && status == 1) { //鼠标右键按下
  463. if (msg.x > 256 - 112 && msg.x < 900 - 30 && msg.y > 179 && msg.y < 489) {
  464. int row = (msg.y - 179) / 102; //获取行
  465. int col = (msg.x - 256 + 112) / 81; //获取列
  466. if (map[row][col].type == 0) {
  467. map[row][col].type = curZhiWu; //给鼠标当前行种下植物
  468. map[row][col].frameIndex = 0; //渲染植物第一帧
  469. map[row][col].shootTimer = 0; //初始化发射时间
  470. map[row][col].x = 256 - 112 + col * 81; //植物坐标
  471. map[row][col].y = 179 + row * 102 + 14;
  472. }
  473. }
  474. //使植物释放消失
  475. curZhiWu = 0;
  476. status = 0;
  477. //重置植物的坐标
  478. curX = 1000;
  479. curY = 1000;
  480. }
  481. }
  482. }
  483. //判断文件是否存在实现
  484. bool fileExist(const char* name) {
  485. FILE* fp = fopen(name, "r");
  486. if (fp == NULL) {
  487. return false;
  488. }
  489. else {
  490. fclose(fp);
  491. return true;
  492. }
  493. }
  494. //更新动作实现
  495. void updateGame() {
  496. //更新植物动作
  497. for (int i = 0; i < 3; i++) {
  498. for (int j = 0; j < 9; j++) {
  499. if (map[i][j].type > 0) {
  500. map[i][j].frameIndex++;
  501. int zhiWuType = map[i][j].type - 1;
  502. int index = map[i][j].frameIndex;
  503. if (imgZhiWu[zhiWuType][index] == NULL) {
  504. map[i][j].frameIndex = 0;
  505. }
  506. }
  507. }
  508. }
  509. //创建僵尸
  510. createZm();
  511. //更新僵尸动作
  512. updateZm();
  513. //创建阳光
  514. createSunShine();
  515. //更新阳光动作
  516. updateSunShine();
  517. //创建豌豆子弹
  518. createBullets(); //豌豆多了之后频率不一样
  519. //更新豌豆子弹动作
  520. updateBullets();
  521. //豌豆子弹与僵尸碰撞
  522. collisionCheck();
  523. }
  524. //检测游戏是否结束实现
  525. bool checkOver() {
  526. BeginBatchDraw();
  527. bool ret = false;
  528. if (gameStatus == WIN) {
  529. Sleep(100);
  530. loadimage(0, "res/gameWin.png");
  531. mciSendString("play res/win.mp3", 0, 0, 0);
  532. ret = true;
  533. }
  534. else if (gameStatus == FAIL) {
  535. Sleep(100);
  536. loadimage(0, "res/gameFail.png");
  537. mciSendString("play res/lose.mp3", 0, 0, 0);
  538. ret = true;
  539. }
  540. EndBatchDraw();
  541. return ret;
  542. }
  543. //创建阳光实现
  544. void createSunShine() {
  545. static int count = 0;
  546. static int fre = 200;
  547. count++;
  548. if (count >= fre) { //限制阳光生成的速度
  549. fre = 100 + rand() % 150; //第二次生成阳光的时间随机
  550. count = 0;
  551. int i;
  552. //从阳光池中取出可用的阳光
  553. for (i = 0; i < ballMax && balls[i].used; i++); //别问,问就是一种新定义方式,跟{}一个样
  554. if (i >= ballMax)return;
  555. balls[i].used = true;
  556. balls[i].frameIndex = 0;
  557. //balls[i].x = 260 + rand() % (900 - 320); //随机落点
  558. //balls[i].y = 60;
  559. //balls[i].destY = 200 + (rand() % 4) * 90; //随机停止位置
  560. //balls[i].xoff = 0;
  561. //balls[i].yoff = 0;
  562. balls[i].timer = 0;
  563. //优化
  564. balls[i].status = SUNSHINE_DOWN;
  565. balls[i].t = 0;
  566. balls[i].p1 = vector2(260 - 112 + rand() % (900 - 320 + 112), 60);
  567. balls[i].p4 = vector2(balls[i].p1.x, 200 + (rand() % 4) * 90);
  568. int off = 2;
  569. float distance = balls[i].p4.y - balls[i].p1.y;
  570. balls[i].speed = 1.0 / (distance / off);
  571. }
  572. //向日葵生产阳光
  573. for (int i = 0; i < 3; i++) {
  574. for (int j = 0; j < 9; j++) {
  575. if (map[i][j].type == XIANG_RI_KUI + 1) {
  576. map[i][j].timer++;
  577. if (map[i][j].timer > 200) {
  578. map[i][j].timer = 0;
  579. int k;
  580. for (k = 0; k < ballMax && balls[k].used; k++);
  581. if (k >= ballMax)return;
  582. balls[k].used = true;
  583. balls[k].p1 = vector2(map[i][j].x, map[i][j].y); //设置贝塞尔曲线的参数
  584. int w = (50 + rand() % 51) * (rand() % 2 ? 1 : -1);
  585. balls[k].p4 = vector2(map[i][j].x + w, map[i][j].y + imgZhiWu[XIANG_RI_KUI][0]->getheight()
  586. - imgSunShineBall->getheight());
  587. balls[k].p2 = vector2(balls[k].p1.x + w * 0.3, balls[k].p1.y - 100);
  588. balls[k].p3 = vector2(balls[k].p1.x + w * 0.7, balls[k].p1.y - 150);
  589. balls[k].status = SUNSHINE_PRODUCT;
  590. balls[k].speed = 0.05;
  591. balls[k].t = 0;
  592. }
  593. }
  594. }
  595. }
  596. }
  597. //更新阳光动作实现
  598. void updateSunShine() {
  599. for (int i = 0; i < ballMax; i++) {
  600. if (balls[i].used) {
  601. balls[i].frameIndex = (balls[i].frameIndex + 1) % 29; //更新序列帧
  602. if (balls[i].status == SUNSHINE_DOWN) {
  603. struct sunShineBall* sun = &balls[i];
  604. sun->t += sun->speed;
  605. sun->pCur = sun->p1 + sun->t * (sun->p4 - sun->p1);
  606. if (sun->t >= 1) {
  607. sun->status = SUNSHINE_GROUND;
  608. sun->t = 0;
  609. sun->timer = 0;
  610. }
  611. }
  612. else if (balls[i].status == SUNSHINE_GROUND) {
  613. balls[i].timer++;
  614. if (balls[i].timer > 100) {
  615. balls[i].used = false;
  616. balls[i].timer = 0;
  617. }
  618. }
  619. else if (balls[i].status == SUNSHINE_COLLECT) {
  620. struct sunShineBall* sun = &balls[i];
  621. sun->t += sun->speed;
  622. sun->pCur = sun->p1 + sun->t * (sun->p4 - sun->p1);
  623. if (sun->t > 1) {
  624. sunShine += 25;
  625. sun->used = false;
  626. sun->t = 0;
  627. }
  628. }
  629. else if (balls[i].status == SUNSHINE_PRODUCT) {
  630. struct sunShineBall* sun = &balls[i];
  631. sun->t += sun->speed;
  632. sun->pCur = calcBezierPoint(sun->t, sun->p1, sun->p2, sun->p3, sun->p4);
  633. if (sun->t > 1) {
  634. sun->status = SUNSHINE_GROUND;
  635. sun->t = 0;
  636. sun->timer = 0;
  637. }
  638. }
  639. /*if (balls[i].y < balls[i].destY) {
  640. balls[i].y += 2;
  641. }
  642. if (balls[i].y >= balls[i].destY) {
  643. balls[i].timer++;
  644. //cout << "i=" << i << ":" << balls[i].timer << endl;
  645. if (balls[i].timer > 100) {
  646. balls[i].timer = 0; //重置定时器,避免下一次取出同样的阳光球一到达停止位置就消失
  647. balls[i].used = false;
  648. //printf((balls[i].timer));
  649. //cout << "i=" << i << ":" << balls[i].timer << endl;
  650. }
  651. }*/
  652. }
  653. /*else if (balls[i].xoff) { //移动阳光球
  654. //设置阳光球的偏移值
  655. float destY = 0;
  656. float destX = 262;
  657. float angle = atan((balls[i].y - destY) / (balls[i].x - destX)); //三角函数
  658. balls[i].xoff = 4 * cos(angle);
  659. balls[i].yoff = 4 * sin(angle);
  660. balls[i].x -= balls[i].xoff;
  661. balls[i].y -= balls[i].yoff;
  662. if (balls[i].x < 262 || balls[i].y < 0) {
  663. //sunShine += 25; //在这里加阳光值会出bug
  664. balls[i].xoff = 0;
  665. balls[i].yoff = 0;
  666. }
  667. }*/
  668. }
  669. }
  670. //收集阳光实现
  671. void collectSunshine(ExMessage* msg) {
  672. int w = imgSunShineBall[0].getwidth(); //单个阳光球的宽度
  673. int h = imgSunShineBall[0].getheight(); //单个阳光球的高度
  674. for (int i = 0; i < ballMax; i++) {
  675. if (balls[i].used) { //阳光球被使用了才进行操作
  676. int x = balls[i].pCur.x;
  677. int y = balls[i].pCur.y;
  678. if (msg->x > x && msg->x<x + w && //只有当光标在阳光范围内才进行操作
  679. msg->y>y && msg->y < y + h) {
  680. //balls[i].used = false; //阳光球消失
  681. balls[i].status = SUNSHINE_COLLECT;
  682. //sunShine += 25; //阳光值加25
  683. mciSendString("play res/sunshine.mp3", 0, 0, 0);
  684. //PlaySound("res/sunshine.wav", NULL, SND_FILENAME | SND_ASYNC); //解决点击过快没音效,要使用WAV格式的音乐
  685. //设置阳光球的偏移值
  686. //float destY = 0;
  687. //float destX = 262;
  688. //float angle = atan((balls[i].y - destY) / (balls[i].x - destX)); //三角函数
  689. //balls[i].xoff = 4 * cos(angle);
  690. //balls[i].yoff = 4 * sin(angle);
  691. //优化
  692. balls[i].p1 = balls[i].pCur;
  693. balls[i].p4 = vector2(262, 0);
  694. balls[i].t = 0;
  695. float distance = dis(balls[i].p1 - balls[i].p4);
  696. float off = 8;
  697. balls[i].speed = 1.0 / (distance / off);
  698. break;
  699. }
  700. }
  701. }
  702. }
  703. //创建僵尸实现
  704. void createZm() {
  705. if (zmCount >= ZM_MAX) {
  706. return;
  707. }
  708. static int count = 0;
  709. static int zmFre = 500;
  710. count++;
  711. if (count >= zmFre) { //限制僵尸生成的速度
  712. zmFre = 300 + rand() % 200; //第二次生成僵尸的时间随机
  713. count = 0;
  714. int i;
  715. //从僵尸池中取出可用的僵尸
  716. for (i = 0; i < zmMax && zms[i].used; i++); //别问,问就是一种新定义方式,跟{}一个样,就是&&!xx
  717. if (i >= zmMax)return;
  718. zms[i].used = true;
  719. //zms[i].frameIndex = 0;
  720. zms[i].x = WIN_WIDTH;
  721. zms[i].row = rand() % 3;
  722. zms[i].y = 172 + (zms[i].row + 1) * 100;
  723. zms[i].speed = 1;
  724. zms[i].blood = 100;
  725. zms[i].dead = false;
  726. zms[i].eating = false;
  727. zmCount++;
  728. }
  729. }
  730. //更新僵尸动作实现
  731. void updateZm() {
  732. //更新僵尸位置
  733. static int count = 0;
  734. count++;
  735. if (count > 2) { //限制僵尸前进速度
  736. count = 0;
  737. for (int i = 0; i < zmMax; i++) {
  738. if (zms[i].used) {
  739. zms[i].x -= zms[i].speed;
  740. if (zms[i].x < 48) {
  741. //结束游戏
  742. gameStatus = FAIL;
  743. }
  744. }
  745. }
  746. }
  747. //更新僵尸动作
  748. static int count2 = 0;
  749. count2++;
  750. if (count2 > 4) { //限制僵尸动作更新速度,避免鬼畜
  751. count2 = 0;
  752. for (int i = 0; i < zmMax; i++) {
  753. if (zms[i].used) {
  754. if (zms[i].dead) {
  755. zms[i].frameIndex++;
  756. if (zms[i].frameIndex >= 20) {
  757. zms[i].used = false;
  758. killZmCount++;
  759. if (killZmCount == ZM_MAX) {
  760. gameStatus = WIN;
  761. }
  762. }
  763. }
  764. else if (zms[i].eating) {
  765. zms[i].frameIndex = (zms[i].frameIndex + 1) % 20;
  766. }
  767. else
  768. {
  769. zms[i].frameIndex = (zms[i].frameIndex + 1) % 21; //僵尸会闪现 -未解决(发现问题,求余大于10就会闪现,不会解决)
  770. //cout << "i=" << i << zms[i].frameIndex << endl; //已破案,加载僵尸图片时,数量加载错了
  771. }
  772. }
  773. }
  774. }
  775. }
  776. //创建豌豆子弹实现
  777. void createBullets() {
  778. int lines[3] = { 0 };
  779. int dangerX = WIN_WIDTH - imgZm[0].getwidth() + 50; //定义开始射击距离
  780. for (int i = 0; i < zmMax; i++) {
  781. if (zms[i].used && zms[i].x < dangerX) {
  782. lines[zms[i].row] = 1; //僵尸所在行改标记
  783. //cout <<"僵尸所在行" << zms[i].row << endl;
  784. }
  785. }
  786. for (int i = 0; i < 3; i++) { //39固定值,扩展性差,还是建议宏定义
  787. for (int j = 0; j < 9; j++) {
  788. if (lines[i] && map[i][j].type == 1) { //有豌豆且僵尸走到打击范围
  789. map[i][j].shootTimer++;
  790. if (map[i][j].shootTimer > 20) {
  791. map[i][j].shootTimer = 0;
  792. int k;
  793. for (k = 0; k < bulletMax && bullets[k].used; k++);
  794. if (k >= bulletMax) return;
  795. bullets[k].used = true; //初始化豌豆子弹
  796. bullets[k].row = i;
  797. //cout << "i=" << i << endl;
  798. bullets[k].speed = 4;
  799. bullets[k].blast = false;
  800. bullets[k].frameIndex = 0;
  801. int zwX = 256 - 112 + j * 81;
  802. int zwY = 179 + i * 102 + 14;
  803. bullets[k].x = zwX + imgZhiWu[0][0]->getwidth() - 10;
  804. bullets[k].y = zwY + 5;
  805. lines[i] = 0;
  806. }
  807. }
  808. }
  809. }
  810. }
  811. //更新豌豆子弹动作实现
  812. void updateBullets() {
  813. for (int i = 0; i < bulletMax; i++) {
  814. if (bullets[i].used) {
  815. bullets[i].x += bullets[i].speed;
  816. if (bullets[i].x > WIN_WIDTH) {
  817. bullets[i].used = false;
  818. }
  819. //碰撞播放完就消失
  820. if (bullets[i].blast) {
  821. bullets[i].frameIndex++;
  822. if (bullets[i].frameIndex > 3) { //超出3会导致状态栏闪烁
  823. bullets[i].used = false;
  824. }
  825. }
  826. }
  827. }
  828. }
  829. //检查僵尸碰撞实现
  830. void collisionCheck() {
  831. //豌豆子弹与僵尸的碰撞检测
  832. checkBullet2Zm();
  833. //僵尸与植物的碰撞检测
  834. checkZm2ZhiWu();
  835. }
  836. //豌豆子弹与僵尸的碰撞检测实现
  837. void checkBullet2Zm() {
  838. for (int i = 0; i < bulletMax; i++) {
  839. if (bullets[i].used == false || bullets[i].blast)continue; //如果豌豆子弹没使用或者已经开始碰撞,就跳过
  840. for (int j = 0; j < zmMax; j++) {
  841. if (zms[j].used == false)continue;
  842. int x1 = zms[j].x + 80;
  843. int x2 = zms[j].x + 110;
  844. int x = bullets[i].x;
  845. if (zms[j].dead == false && bullets[i].row == zms[j].row && x > x1 && x < x2) { //豌豆子弹与僵尸碰撞后
  846. zms[j].blood -= 10;//5;//10;//20;
  847. bullets[i].blast = true;
  848. bullets[i].speed = 0;
  849. if (zms[j].blood <= 0) {
  850. zms[j].dead = true;
  851. zms[j].speed = 0;
  852. zms[j].frameIndex = 0;
  853. }
  854. break;
  855. }
  856. }
  857. }
  858. }
  859. //僵尸与植物的碰撞检测实现
  860. void checkZm2ZhiWu() {
  861. for (int i = 0; i < zmMax; i++) {
  862. if (zms[i].dead)continue;
  863. int row = zms[i].row;
  864. for (int k = 0; k < 9; k++) {
  865. if (map[row][k].type == 0)continue;
  866. int zhiWuX = 256 - 112 + k * 81; //定义僵尸开吃范围
  867. int x1 = zhiWuX + 10;
  868. int x2 = zhiWuX + 60;
  869. int x3 = zms[i].x + 80;
  870. if (x3 > x1 && x3 < x2) {
  871. if (map[row][k].catched == true) { //僵尸吃的过程中的一些配置
  872. map[row][k].deadTimer++;
  873. if (map[row][k].deadTimer > 100) { //僵尸吃完了-重置参数
  874. map[row][k].deadTimer = 0;
  875. map[row][k].type = 0;
  876. zms[i].eating = false;
  877. zms[i].frameIndex = 0;
  878. zms[i].speed = 1;
  879. }
  880. }
  881. else { //僵尸开吃-配置参数
  882. map[row][k].catched = true;
  883. map[row][k].deadTimer = 0;
  884. zms[i].eating = true;
  885. zms[i].speed = 0;
  886. zms[i].frameIndex = 0;
  887. }
  888. }
  889. }
  890. }
  891. }

tools.cpp

  1. #include "tools.h"
  2. // 载入PNG图并去透明部分
  3. void _putimagePNG(int picture_x, int picture_y, IMAGE* picture) //x为载入图片的X坐标,y为Y坐标
  4. {
  5. DWORD* dst = GetImageBuffer(); // GetImageBuffer()函数,用于获取绘图设备的显存指针,EASYX自带
  6. DWORD* draw = GetImageBuffer();
  7. DWORD* src = GetImageBuffer(picture); //获取picture的显存指针
  8. int picture_width = picture->getwidth(); //获取picture的宽度,EASYX自带
  9. int picture_height = picture->getheight(); //获取picture的高度,EASYX自带
  10. int graphWidth = getwidth(); //获取绘图区的宽度,EASYX自带
  11. int graphHeight = getheight(); //获取绘图区的高度,EASYX自带
  12. int dstX = 0; //在显存里像素的角标
  13. // 实现透明贴图 公式: Cp=αp*FP+(1-αp)*BP , 贝叶斯定理来进行点颜色的概率计算
  14. for (int iy = 0; iy < picture_height; iy++)
  15. {
  16. for (int ix = 0; ix < picture_width; ix++)
  17. {
  18. int srcX = ix + iy * picture_width; //在显存里像素的角标
  19. int sa = ((src[srcX] & 0xff000000) >> 24); //0xAArrggbb;AA是透明度
  20. int sr = ((src[srcX] & 0xff0000) >> 16); //获取RGB里的R
  21. int sg = ((src[srcX] & 0xff00) >> 8); //G
  22. int sb = src[srcX] & 0xff; //B
  23. if (ix >= 0 && ix <= graphWidth && iy >= 0 && iy <= graphHeight && dstX <= graphWidth * graphHeight)
  24. {
  25. dstX = (ix + picture_x) + (iy + picture_y) * graphWidth; //在显存里像素的角标
  26. int dr = ((dst[dstX] & 0xff0000) >> 16);
  27. int dg = ((dst[dstX] & 0xff00) >> 8);
  28. int db = dst[dstX] & 0xff;
  29. draw[dstX] = ((sr * sa / 255 + dr * (255 - sa) / 255) << 16)
  30. | ((sg * sa / 255 + dg * (255 - sa) / 255) << 8)
  31. | (sb * sa / 255 + db * (255 - sa) / 255);
  32. }
  33. }
  34. }
  35. }
  36. // 适用于 y <0 以及x<0的任何情况
  37. void putimagePNG(int x, int y, IMAGE* picture) {
  38. IMAGE imgTmp, imgTmp2, imgTmp3;
  39. int winWidth = getwidth();
  40. int winHeight = getheight();
  41. if (y < 0) {
  42. SetWorkingImage(picture);
  43. getimage(&imgTmp, 0, -y,
  44. picture->getwidth(), picture->getheight() + y);
  45. SetWorkingImage();
  46. y = 0;
  47. picture = &imgTmp;
  48. }
  49. else if (y >= getheight() || x >= getwidth()) {
  50. return;
  51. }
  52. else if (y + picture->getheight() > winHeight) {
  53. SetWorkingImage(picture);
  54. getimage(&imgTmp, x, y, picture->getwidth(), winHeight - y);
  55. SetWorkingImage();
  56. picture = &imgTmp;
  57. }
  58. if (x < 0) {
  59. SetWorkingImage(picture);
  60. getimage(&imgTmp2, -x, 0, picture->getwidth() + x, picture->getheight());
  61. SetWorkingImage();
  62. x = 0;
  63. picture = &imgTmp2;
  64. }
  65. if (x > getwidth() - picture->getwidth()) {
  66. SetWorkingImage(picture);
  67. getimage(&imgTmp3, 0, 0, getwidth() - x, picture->getheight());
  68. SetWorkingImage();
  69. picture = &imgTmp3;
  70. }
  71. _putimagePNG(x, y, picture);
  72. }
  73. int getDelay() {
  74. static unsigned long long lastTime = 0;
  75. unsigned long long currentTime = GetTickCount();
  76. if (lastTime == 0) {
  77. lastTime = currentTime;
  78. return 0;
  79. }
  80. else {
  81. int ret = currentTime - lastTime;
  82. lastTime = currentTime;
  83. return ret;
  84. }
  85. }

vector2.cpp

  1. //头文件要求
  2. #include <cmath>
  3. #include "vector2.h"
  4. //加法
  5. vector2 operator +(vector2 x, vector2 y) {
  6. return vector2(x.x + y.x, x.y + y.y);
  7. }
  8. //减法
  9. vector2 operator -(vector2 x, vector2 y) {
  10. return vector2(x.x - y.x, x.y - y.y);
  11. }
  12. // 乘法
  13. vector2 operator *(vector2 x, vector2 y) {
  14. return vector2(x.x * y.x - x.y * y.y, x.y * y.x + x.x * y.y);
  15. }
  16. // 乘法
  17. vector2 operator *(vector2 y, float x) {
  18. return vector2(x * y.x, x * y.y);
  19. }
  20. vector2 operator *(float x, vector2 y) {
  21. return vector2(x * y.x, x * y.y);
  22. }
  23. //叉积
  24. long long cross(vector2 x, vector2 y) { return x.y * y.x - x.x * y.y; }
  25. //数量积 点积
  26. long long dot(vector2 x, vector2 y) { return x.x * y.x + x.y * y.y; }
  27. //四舍五入除法
  28. long long dv(long long a, long long b) {//注意重名!!!
  29. return b < 0 ? dv(-a, -b)
  30. : (a < 0 ? -dv(-a, b)
  31. : (a + b / 2) / b);
  32. }
  33. //模长平方
  34. long long len(vector2 x) { return x.x * x.x + x.y * x.y; }
  35. //模长
  36. long long dis(vector2 x) { return sqrt(x.x * x.x + x.y * x.y); }
  37. //向量除法
  38. vector2 operator /(vector2 x, vector2 y) {
  39. long long l = len(y);
  40. return vector2(dv(dot(x, y), l), dv(cross(x, y), l));
  41. }
  42. //向量膜
  43. vector2 operator %(vector2 x, vector2 y) { return x - ((x / y) * y); }
  44. //向量GCD
  45. vector2 gcd(vector2 x, vector2 y) { return len(y) ? gcd(y, x % y) : x; }
  46. //贝塞尔曲线
  47. vector2 calcBezierPoint(float t, vector2 p0, vector2 p1, vector2 p2, vector2 p3) {
  48. float u = 1 - t;
  49. float tt = t * t;
  50. float uu = u * u;
  51. float uuu = uu * u;
  52. float ttt = tt * t;
  53. vector2 p = uuu * p0;
  54. p = p + 3 * uu * t * p1;
  55. p = p + 3 * u * tt * p2;
  56. p = p + ttt * p3;
  57. return p;
  58. }

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

闽ICP备14008679号