鱼C论坛

 找回密码
 立即注册
查看: 482|回复: 0

[作品展示] 跟着B站Rock老师视频做植物大战僵尸项目(未完成)

[复制链接]
发表于 2023-10-7 17:28:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 六翻了 于 2023-10-7 17:34 编辑

第一天
先全部敲完,再慢慢研究
除了EasyX的函数不熟悉外,还有一些循环逻辑还待仔细研n究
比如sn[an[j][j] ] [bn[j][j] ]
论坛bug,二维数组an[n][j]中的n改成i直接不显示
一不注意就眼花,把作用给忘了
字体设置这方面我觉得直接照搬模仿使用就行


  1. #include <stdio.h>
  2. #include <graphics.h>
  3. #include "tools.h"
  4. #include <time.h>

  5. #include <mmsystem.h>
  6. #pragma comment(lib,"winmm.lib")

  7. #define WIN_WIDTH    960
  8. #define WIN_HEIGHT   600

  9. enum { WAN_DOU, XIANG_RI_KUI, ZHI_WU_COUNT };

  10. IMAGE imgBg;//表示背景图片
  11. IMAGE imgBar;
  12. IMAGE imgCards[ZHI_WU_COUNT];
  13. IMAGE* imgZhiWu[ZHI_WU_COUNT][20];

  14. int curX, curY;//当前选中植物坐标
  15. int curZhiWu;// 0:没有选中;1:选择了第一种植物

  16. struct zhiwu
  17. {
  18.         int type;//0:没有植物;1:第一种植物
  19.         int frameIndex;//序列帧的序号
  20. };
  21. struct zhiwu map[3][9];

  22. struct sunshineBall
  23. {
  24.         int x, y; //阳光球在飘落过程中的坐标位置(x保持不变)
  25.         int frameIndex; //当前显示图片帧的序号
  26.         int destY; //飘落的目标位置的y坐标
  27.         bool used; //是否在使用
  28.         int timer; //
  29. };
  30. struct sunshineBall balls[10];
  31. IMAGE imgSunshineBall[29];
  32. int sunshine;

  33. struct zm
  34. {
  35.         int x, y;
  36.         int frameIndex;
  37.         bool used;
  38.         int speed;
  39. };
  40. struct zm zms[10];//僵尸数量
  41. IMAGE imgZM[22];

  42. bool fileExist(const char* name)
  43. {
  44.         FILE* fp = fopen(name, "r");
  45.         if (fp == NULL)
  46.         {
  47.                 return false;
  48.         }
  49.         else
  50.         {
  51.                 fclose(fp);
  52.                 return true;
  53.         }
  54. }

  55. void gameInit()//将图片放入内存变量
  56. {
  57.         //加载背景图片,改成多字符集
  58.         loadimage(&imgBg, "res/bg.jpg");

  59.         loadimage(&imgBar, "res/bar5.png");

  60.         memset(imgZhiWu, 0, sizeof(imgZhiWu));
  61.         memset(map, 0, sizeof(map));

  62.         //初始化植物卡牌
  63.         char name[64];
  64.         for (int i = 0; i < ZHI_WU_COUNT; i++)
  65.         {
  66.                 //生成植物卡牌的文件名
  67.                 sprintf_s(name, sizeof(name), "res/Cards/card_%d.png", i + 1);
  68.                 loadimage(&imgCards[i], name);

  69.                 for (int j = 0; j < 20; j++)
  70.                 {
  71.                         sprintf_s(name, sizeof(name), "res/zhiwu/%d/%d.png", i, j + 1);
  72.                         //先判断文件是否存在

  73.                         if (fileExist(name))
  74.                         {
  75.                                 imgZhiWu[i][j] = new IMAGE;
  76.                                 loadimage(imgZhiWu[i][j], name);
  77.                         }
  78.                         else {
  79.                                 break;
  80.                         }
  81.                 }
  82.         }
  83.         curZhiWu = 0;
  84.         sunshine = 50;

  85.         memset(balls, 0, sizeof(balls));
  86.         for (int i = 0; i < 29; i++)
  87.         {
  88.                 sprintf_s(name, sizeof(name), "res/sunshine/%d.png", i + 1);
  89.                 loadimage(&imgSunshineBall[i], name);
  90.         }
  91.         //配制随机数种子
  92.         srand(time(NULL));


  93.         //创建游戏的图形化窗口
  94.         initgraph(WIN_WIDTH, WIN_HEIGHT, 1);

  95.         //设置字体
  96.         LOGFONT f;
  97.         gettextstyle(&f);
  98.         f.lfHeight = 30;
  99.         f.lfWeight = 15;
  100.         strcpy(f.lfFaceName, "Segoe UI Black");
  101.         f.lfQuality = ANTIALIASED_QUALITY;//抗锯齿
  102.         settextstyle(&f);
  103.         setbkmode(TRANSPARENT);
  104.         setcolor(BLACK);

  105.         //初始化僵尸
  106.         memset(zms, 0, sizeof(zms));
  107.         for (int i = 0; i < 22; i++) {
  108.                 sprintf_s(name, sizeof(name), "res/zm/%d.png", i + 1);
  109.                 loadimage(&imgZM[i], name);
  110.         }
  111. }

  112. void updateWindow()//显示图片
  113. {
  114.         BeginBatchDraw();//开始缓存,防止画面闪烁
  115.         putimage(0, 0, &imgBg);
  116.         //putimage(250, 0, &imgBar);
  117.         putimagePNG(250, 0, &imgBar);

  118.         for (int i = 0; i < ZHI_WU_COUNT; i++)
  119.         {
  120.                 int x = 338 + i * 65;
  121.                 int y = 6;
  122.                 putimage(x, y, &imgCards[i]);
  123.         }
  124.         for (int i = 0; i < 3; i++)
  125.         {
  126.                 for (int j = 0; j < 9; j++)
  127.                 {
  128.                         if (map[i][j].type > 0)
  129.                         {
  130.                                 int x = 256 + j * 81 - 2;
  131.                                 int y = 179 + i * 102 + 14;
  132.                                 int zhiWuType = map[i][j].type - 1;
  133.                                 int index = map[i][j].frameIndex;
  134.                                 putimagePNG(x, y, imgZhiWu[zhiWuType][index]);
  135.                         }
  136.                 }
  137.         }
  138.         //渲染 拖动 过程中的植物
  139.         if (curZhiWu > 0)
  140.         {
  141.                 IMAGE* img = imgZhiWu[curZhiWu - 1][0];
  142.                 putimagePNG(curX - img->getwidth() / 2, curY - img->getheight() / 2, img);
  143.         }

  144.         int ballMax = sizeof(balls) / sizeof(balls[0]);
  145.         for (int i = 0; i < ballMax; i++)
  146.         {
  147.                 if (balls[i].used)
  148.                 {
  149.                         IMAGE* img = &imgSunshineBall[balls[i].frameIndex];
  150.                         putimagePNG(balls[i].x, balls[i].y, img);
  151.                 }
  152.         }
  153.         //渲染数字
  154.         char scoreText[8];
  155.         sprintf_s(scoreText, sizeof(scoreText), "%d", sunshine);
  156.         outtextxy(276, 67, scoreText);

  157.         EndBatchDraw();//结束双缓冲
  158. }

  159. void cllectSunshine(ExMessage* msg)
  160. {
  161.         int count = sizeof(balls) / sizeof(balls[0]);
  162.         int w = imgSunshineBall[0].getwidth();
  163.         int h = imgSunshineBall[0].getheight();
  164.         for (int i = 0; i < count; i++){
  165.                 if (balls[i].used){
  166.                         int x = balls[i].x;
  167.                         int y = balls[i].y;
  168.                         if (msg->x > x && msg->x < x + w &&
  169.                                 msg->y > y && msg->y < y + h){
  170.                                 balls[i].used = false;
  171.                                 sunshine += 25;
  172.                                 mciSendString("play res/sunshine.mp3", 0, 0, 0);
  173.                         }
  174.                 }
  175.         }
  176. }

  177. void userClick()
  178. {
  179.         ExMessage msg;
  180.         static int status = 0;
  181.         if (peekmessage(&msg))//判断有没有消息,鼠标检测
  182.         {
  183.                 if (msg.message == WM_LBUTTONDOWN)
  184.                 {
  185.                         if (msg.x > 338 && msg.x < 338 + 65 * ZHI_WU_COUNT && msg.y < 96)
  186.                         {
  187.                                 int index = (msg.x - 338) / 65;
  188.                                 status = 1;
  189.                                 curZhiWu = index + 1;
  190.                         }
  191.                         else
  192.                         {
  193.                                 cllectSunshine(&msg);
  194.                         }
  195.                 }
  196.                 else if (msg.message == WM_MOUSEMOVE && status == 1)
  197.                 {
  198.                         curX = msg.x;
  199.                         curY = msg.y;
  200.                 }
  201.                 else if(msg.message==WM_LBUTTONUP)
  202.                 {
  203.                         if (msg.x > 256 && msg.y > 179 && msg.y < 489)
  204.                         {
  205.                                 int row = (msg.y - 179) / 102;
  206.                                 int col = (msg.x - 256) / 81;
  207.                                 if (map[row][col].type == 0)
  208.                                 {
  209.                                         map[row][col].type = curZhiWu;
  210.                                         map[row][col].frameIndex = 0;
  211.                                 }

  212.                         }
  213.                         curZhiWu = 0;
  214.                         status = 0;
  215.                 }
  216.         }
  217. }

  218. void creatSunshine(void)
  219. {
  220.         static int count = 0;
  221.         static int fre = 400;
  222.         count++;
  223.         if (count >= fre)
  224.         {
  225.                 fre = 200 + rand() % 200;
  226.                 count = 0;
  227.                 //从阳光池获取一个可以使用的
  228.                 int ballMax = sizeof(balls) / sizeof(balls[0]);
  229.                 int i;
  230.                 for (i = 0; i < ballMax && balls[i].used; i++);
  231.                 if (i >= ballMax)return;

  232.                 balls[i].used = true;
  233.                 balls[i].frameIndex = 0;
  234.                 balls[i].x = 260 + rand() % (900 - 260);  //260-900
  235.                 balls[i].y = 60;
  236.                 balls[i].destY = 200 + (rand() % 4) * 90;
  237.                 balls[i].timer = 0;
  238.         }
  239. }

  240. void updateSunshine()
  241. {
  242.         int ballMax = sizeof(balls) / sizeof(balls[0]);
  243.         for (int i = 0; i < ballMax; i++)
  244.         {
  245.                 if (balls[i].used)
  246.                 {
  247.                         balls[i].frameIndex = (balls[i].frameIndex + 1) % 29;
  248.                         if (balls[i].timer == 0)
  249.                         {
  250.                                 balls[i].y += 2;
  251.                         }
  252.                         if (balls[i].y >= balls[i].destY)
  253.                         {
  254.                                 balls[i].timer++;
  255.                                 if (balls[i].timer > 100)
  256.                                 {
  257.                                         balls[i].used = false;
  258.                                 }
  259.                         }
  260.                 }
  261.         }
  262. }

  263. void creatZM()
  264. {
  265.         static int zmFre = 500;
  266.         static int count = 0;
  267.         count++;
  268.         if (count >= zmFre) {
  269.                 count = 0;
  270.                 zmFre = rand() % 200 + 300;
  271.                 int i;
  272.                 int zmMax = sizeof(zms) / sizeof(zms[0]);
  273.                 for (i = 0; i < zmMax && zms[i].used; i++) {
  274.                         if (i < zmMax) {
  275.                                 zms[i].used = true;
  276.                                 zms[i].x = WIN_WIDTH;
  277.                                 zms[i].y = 172 + (1 + rand() % 3) * 100;
  278.                                 zms[i].speed = 1;//速度微调
  279.                         }
  280.                 }
  281.         }
  282. }

  283. void updateZM()
  284. {

  285. }

  286. void updateGame()
  287. {
  288.         for (int i = 0; i < 3; i++)
  289.         {
  290.                 for (int j = 0; j < 9; j++)
  291.                 {
  292.                         if (map[i][j].type > 0)
  293.                         {
  294.                                 map[i][j].frameIndex++;
  295.                                 int zhiWuType = map[i][j].type - 1;
  296.                                 int index = map[i][j].frameIndex;
  297.                                 if (imgZhiWu[zhiWuType][index] == NULL)
  298.                                 {
  299.                                         map[i][j].frameIndex = 0;
  300.                                 }
  301.                         }
  302.                 }
  303.         }
  304.         creatSunshine();//创建阳光
  305.         updateSunshine();//修改阳光状态
  306.         creatZM();//创建僵尸
  307.         updateZM();//更新僵尸状态
  308. }


  309. void startUI()
  310. {
  311.         IMAGE imgBg, imgMenu1, imgMenu2;
  312.         loadimage(&imgBg, "res/menu.png");
  313.         loadimage(&imgMenu1, "res/menu1.png");
  314.         loadimage(&imgMenu2, "res/menu2.png");
  315.         int flag = 0;

  316.         while (1)
  317.         {
  318.                 BeginBatchDraw();
  319.                 putimage(0, 0, &imgBg);
  320.                 putimagePNG(474, 75, flag ? &imgMenu2 : &imgMenu1);

  321.                 ExMessage msg;
  322.                 if (peekmessage(&msg))
  323.                 {
  324.                         if (msg.message == WM_LBUTTONDOWN &&
  325.                                 msg.x > 474 && msg.x < 474+300 &&
  326.                                 msg.y > 74 && msg.y < 75+140)
  327.                         {
  328.                                 flag = 1;
  329.                         }
  330.                         else if (msg.message == WM_LBUTTONUP && flag)
  331.                         {
  332.                                 return;
  333.                         }
  334.                 }
  335.                 EndBatchDraw();
  336.         }
  337. }

  338. int main(void)
  339. {
  340.         gameInit();
  341.         startUI();

  342.         int timer = 0;
  343.         bool flag = true;
  344.         while (1)
  345.         {
  346.                 userClick();
  347.                 timer += getDelay();
  348.                 if (timer > 20)
  349.                 {
  350.                         flag = true;
  351.                         timer = 0;
  352.                 }
  353.                 if(flag)
  354.                 {
  355.                         flag = false;
  356.                         updateWindow();
  357.                         updateGame();
  358.                 }
  359.         }

  360.         system("pause");

  361.         return 0;
  362. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-27 09:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表