|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 六翻了 于 2023-10-7 17:34 编辑
第一天
先全部敲完,再慢慢研究
除了EasyX的函数不熟悉外,还有一些循环逻辑还待仔细研n究
比如sn[an[j][j] ] [bn[j][j] ]
论坛bug,二维数组an[n][j]中的n改成i直接不显示
一不注意就眼花,把作用给忘了
字体设置这方面我觉得直接照搬模仿使用就行
- #include <stdio.h>
- #include <graphics.h>
- #include "tools.h"
- #include <time.h>
- #include <mmsystem.h>
- #pragma comment(lib,"winmm.lib")
- #define WIN_WIDTH 960
- #define WIN_HEIGHT 600
- enum { WAN_DOU, XIANG_RI_KUI, ZHI_WU_COUNT };
- IMAGE imgBg;//表示背景图片
- IMAGE imgBar;
- IMAGE imgCards[ZHI_WU_COUNT];
- IMAGE* imgZhiWu[ZHI_WU_COUNT][20];
- int curX, curY;//当前选中植物坐标
- int curZhiWu;// 0:没有选中;1:选择了第一种植物
- struct zhiwu
- {
- int type;//0:没有植物;1:第一种植物
- int frameIndex;//序列帧的序号
- };
- struct zhiwu map[3][9];
- struct sunshineBall
- {
- int x, y; //阳光球在飘落过程中的坐标位置(x保持不变)
- int frameIndex; //当前显示图片帧的序号
- int destY; //飘落的目标位置的y坐标
- bool used; //是否在使用
- int timer; //
- };
- struct sunshineBall balls[10];
- IMAGE imgSunshineBall[29];
- int sunshine;
- struct zm
- {
- int x, y;
- int frameIndex;
- bool used;
- int speed;
- };
- struct zm zms[10];//僵尸数量
- IMAGE imgZM[22];
- bool fileExist(const char* name)
- {
- FILE* fp = fopen(name, "r");
- if (fp == NULL)
- {
- return false;
- }
- else
- {
- fclose(fp);
- return true;
- }
- }
- void gameInit()//将图片放入内存变量
- {
- //加载背景图片,改成多字符集
- loadimage(&imgBg, "res/bg.jpg");
- loadimage(&imgBar, "res/bar5.png");
- memset(imgZhiWu, 0, sizeof(imgZhiWu));
- memset(map, 0, sizeof(map));
- //初始化植物卡牌
- char name[64];
- for (int i = 0; i < ZHI_WU_COUNT; i++)
- {
- //生成植物卡牌的文件名
- sprintf_s(name, sizeof(name), "res/Cards/card_%d.png", i + 1);
- loadimage(&imgCards[i], name);
- for (int j = 0; j < 20; j++)
- {
- sprintf_s(name, sizeof(name), "res/zhiwu/%d/%d.png", i, j + 1);
- //先判断文件是否存在
- if (fileExist(name))
- {
- imgZhiWu[i][j] = new IMAGE;
- loadimage(imgZhiWu[i][j], name);
- }
- else {
- break;
- }
- }
- }
- curZhiWu = 0;
- sunshine = 50;
- memset(balls, 0, sizeof(balls));
- for (int i = 0; i < 29; i++)
- {
- sprintf_s(name, sizeof(name), "res/sunshine/%d.png", i + 1);
- loadimage(&imgSunshineBall[i], name);
- }
- //配制随机数种子
- srand(time(NULL));
- //创建游戏的图形化窗口
- initgraph(WIN_WIDTH, WIN_HEIGHT, 1);
- //设置字体
- LOGFONT f;
- gettextstyle(&f);
- f.lfHeight = 30;
- f.lfWeight = 15;
- strcpy(f.lfFaceName, "Segoe UI Black");
- f.lfQuality = ANTIALIASED_QUALITY;//抗锯齿
- settextstyle(&f);
- setbkmode(TRANSPARENT);
- setcolor(BLACK);
- //初始化僵尸
- memset(zms, 0, sizeof(zms));
- for (int i = 0; i < 22; i++) {
- sprintf_s(name, sizeof(name), "res/zm/%d.png", i + 1);
- loadimage(&imgZM[i], name);
- }
- }
- void updateWindow()//显示图片
- {
- BeginBatchDraw();//开始缓存,防止画面闪烁
- putimage(0, 0, &imgBg);
- //putimage(250, 0, &imgBar);
- putimagePNG(250, 0, &imgBar);
- for (int i = 0; i < ZHI_WU_COUNT; i++)
- {
- int x = 338 + i * 65;
- int y = 6;
- putimage(x, y, &imgCards[i]);
- }
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 9; j++)
- {
- if (map[i][j].type > 0)
- {
- int x = 256 + j * 81 - 2;
- int y = 179 + i * 102 + 14;
- int zhiWuType = map[i][j].type - 1;
- int index = map[i][j].frameIndex;
- putimagePNG(x, y, imgZhiWu[zhiWuType][index]);
- }
- }
- }
- //渲染 拖动 过程中的植物
- if (curZhiWu > 0)
- {
- IMAGE* img = imgZhiWu[curZhiWu - 1][0];
- putimagePNG(curX - img->getwidth() / 2, curY - img->getheight() / 2, img);
- }
- int ballMax = sizeof(balls) / sizeof(balls[0]);
- for (int i = 0; i < ballMax; i++)
- {
- if (balls[i].used)
- {
- IMAGE* img = &imgSunshineBall[balls[i].frameIndex];
- putimagePNG(balls[i].x, balls[i].y, img);
- }
- }
- //渲染数字
- char scoreText[8];
- sprintf_s(scoreText, sizeof(scoreText), "%d", sunshine);
- outtextxy(276, 67, scoreText);
- EndBatchDraw();//结束双缓冲
- }
- void cllectSunshine(ExMessage* msg)
- {
- int count = sizeof(balls) / sizeof(balls[0]);
- int w = imgSunshineBall[0].getwidth();
- int h = imgSunshineBall[0].getheight();
- for (int i = 0; i < count; i++){
- if (balls[i].used){
- int x = balls[i].x;
- int y = balls[i].y;
- if (msg->x > x && msg->x < x + w &&
- msg->y > y && msg->y < y + h){
- balls[i].used = false;
- sunshine += 25;
- mciSendString("play res/sunshine.mp3", 0, 0, 0);
- }
- }
- }
- }
- void userClick()
- {
- ExMessage msg;
- static int status = 0;
- if (peekmessage(&msg))//判断有没有消息,鼠标检测
- {
- if (msg.message == WM_LBUTTONDOWN)
- {
- if (msg.x > 338 && msg.x < 338 + 65 * ZHI_WU_COUNT && msg.y < 96)
- {
- int index = (msg.x - 338) / 65;
- status = 1;
- curZhiWu = index + 1;
- }
- else
- {
- cllectSunshine(&msg);
- }
- }
- else if (msg.message == WM_MOUSEMOVE && status == 1)
- {
- curX = msg.x;
- curY = msg.y;
- }
- else if(msg.message==WM_LBUTTONUP)
- {
- if (msg.x > 256 && msg.y > 179 && msg.y < 489)
- {
- int row = (msg.y - 179) / 102;
- int col = (msg.x - 256) / 81;
- if (map[row][col].type == 0)
- {
- map[row][col].type = curZhiWu;
- map[row][col].frameIndex = 0;
- }
- }
- curZhiWu = 0;
- status = 0;
- }
- }
- }
- void creatSunshine(void)
- {
- static int count = 0;
- static int fre = 400;
- count++;
- if (count >= fre)
- {
- fre = 200 + rand() % 200;
- count = 0;
- //从阳光池获取一个可以使用的
- int ballMax = sizeof(balls) / sizeof(balls[0]);
- int i;
- for (i = 0; i < ballMax && balls[i].used; i++);
- if (i >= ballMax)return;
- balls[i].used = true;
- balls[i].frameIndex = 0;
- balls[i].x = 260 + rand() % (900 - 260); //260-900
- balls[i].y = 60;
- balls[i].destY = 200 + (rand() % 4) * 90;
- balls[i].timer = 0;
- }
- }
- void updateSunshine()
- {
- int ballMax = sizeof(balls) / sizeof(balls[0]);
- for (int i = 0; i < ballMax; i++)
- {
- if (balls[i].used)
- {
- balls[i].frameIndex = (balls[i].frameIndex + 1) % 29;
- if (balls[i].timer == 0)
- {
- balls[i].y += 2;
- }
- if (balls[i].y >= balls[i].destY)
- {
- balls[i].timer++;
- if (balls[i].timer > 100)
- {
- balls[i].used = false;
- }
- }
- }
- }
- }
- void creatZM()
- {
- static int zmFre = 500;
- static int count = 0;
- count++;
- if (count >= zmFre) {
- count = 0;
- zmFre = rand() % 200 + 300;
- int i;
- int zmMax = sizeof(zms) / sizeof(zms[0]);
- for (i = 0; i < zmMax && zms[i].used; i++) {
- if (i < zmMax) {
- zms[i].used = true;
- zms[i].x = WIN_WIDTH;
- zms[i].y = 172 + (1 + rand() % 3) * 100;
- zms[i].speed = 1;//速度微调
- }
- }
- }
- }
- void updateZM()
- {
- }
- void updateGame()
- {
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 9; j++)
- {
- if (map[i][j].type > 0)
- {
- map[i][j].frameIndex++;
- int zhiWuType = map[i][j].type - 1;
- int index = map[i][j].frameIndex;
- if (imgZhiWu[zhiWuType][index] == NULL)
- {
- map[i][j].frameIndex = 0;
- }
- }
- }
- }
- creatSunshine();//创建阳光
- updateSunshine();//修改阳光状态
- creatZM();//创建僵尸
- updateZM();//更新僵尸状态
- }
- void startUI()
- {
- IMAGE imgBg, imgMenu1, imgMenu2;
- loadimage(&imgBg, "res/menu.png");
- loadimage(&imgMenu1, "res/menu1.png");
- loadimage(&imgMenu2, "res/menu2.png");
- int flag = 0;
- while (1)
- {
- BeginBatchDraw();
- putimage(0, 0, &imgBg);
- putimagePNG(474, 75, flag ? &imgMenu2 : &imgMenu1);
- ExMessage msg;
- if (peekmessage(&msg))
- {
- if (msg.message == WM_LBUTTONDOWN &&
- msg.x > 474 && msg.x < 474+300 &&
- msg.y > 74 && msg.y < 75+140)
- {
- flag = 1;
- }
- else if (msg.message == WM_LBUTTONUP && flag)
- {
- return;
- }
- }
- EndBatchDraw();
- }
- }
- int main(void)
- {
- gameInit();
- startUI();
- int timer = 0;
- bool flag = true;
- while (1)
- {
- userClick();
- timer += getDelay();
- if (timer > 20)
- {
- flag = true;
- timer = 0;
- }
- if(flag)
- {
- flag = false;
- updateWindow();
- updateGame();
- }
- }
- system("pause");
- return 0;
- }
复制代码 |
|