赞
踩
文章是作者C语言项目的开发日志,主要介绍与该项目有关的函数,代码实现以及在开发过程中遇到的问题。IDE:VS2022。
目录
1.2进入官网:EasyX Graphics Library for C++
1.5为了确保安装成功,需要打开编译器创建新项目输入以下代码
5.源文件vector2.cpp(工具文件:实现贝塞尔曲线)
注意:easyx只支持C++,所以有些编译器无法下载
- #include <graphics.h> // 引用 EasyX 绘图库头文件
- #include <conio.h>
- int main(){
- initgraph(640, 480);
- circle(320, 240, 100);
- _getch();
- closegraph();
- return 0;
- }
运行结果证明安装成功
- #include <stdio.h>
- #include <graphics.h>//easyx图形库头文件
- #include <math.h>
- #include "tools.h"//图片透明函数接口
- #include <time.h>
- #include <mmsystem.h>//播放音乐头文件
- #include "vector2.h"//实现产阳光抛物线
-
- #define WIN_WIDTH 900
- #define WIN_HIGHT 600
- #define ZM_MAX 10
- #define _CRT_SECURE_NO_WARNINGS
-
- #pragma warning(disable:4996)//忽略不安全函数
- #pragma comment(lib,"winmm.lib")//播放音乐并加载库
-
- //植物类型
- enum { PEA, SUN_FLOWER, PLANT_COUNTS };//方便添加植物卡牌
-
- //阳光的状态
- enum { SUNSHINE_DOWN, SUNSHINE_GROUND, SUNSHINE_COLLECT, SUNSHINE_PRODUCT };//下降,落地,收集,生产
-
- //游戏状态
- enum{GOING,WIN,FAIL};//进行,胜利,失败
-
- IMAGE imgBg;//背景图片
- IMAGE imgBar;//工具栏
- IMAGE imgCard[PLANT_COUNTS];//植物卡牌
- IMAGE* imgPLANT[PLANT_COUNTS][20];//实现植物晃动的二维指针数组,20是晃动牌组
- IMAGE imgsunshine[29];//存放阳光球图片帧
- IMAGE imgzombie[22];//僵尸行走的图片帧
- IMAGE imgBulletNormal;//子弹正常形态
- IMAGE imgballblast[4];//爆炸图片帧
- IMAGE imgzmDead[20];//僵尸死亡帧
- IMAGE imgzmEat[21];//僵尸吃植物帧
- IMAGE imgzmStand[11];//僵尸站立帧
-
- int curX, curY;//表示选中植物卡牌的位置
- int curPLANT;//表示选中植物的类型,0:没有选中,1:选中第一种植物
- int sunshine = 50;//初始阳光值
- int killcount;//已经杀掉的僵尸个数
- int zmCount1;//已经出现的僵尸个数
- int gameStatus;//游戏状态
-
- struct PLANT//植物相关的结构体
- {
- int type;//一块草坪种植的植物类型,0:没有种植,1:种植第一种植物
- int frameindex;//表示植物晃动时图片的序号
- int shootTimer;//发射计数器
- bool catched;//是否被僵尸捕获
- int deadtime;//死亡计数器
- int timer;//生产阳光的计时器
- int x, y;//植物的位置
- };
- struct PLANT map[3][9];//草坪有3行9列
-
- struct sunshineBALL //阳光球结构体
- {
- int x, y;//阳光掉落时的坐标
- int frameINDEX;//阳光旋转的图片帧
- int destY;//阳光下落的最终坐标
- bool used;//判断阳光是否在使用
- int timer;//阳光在场上停留的时间
- float xoff;//阳光飞跃时x轴偏移量
- float yoff;//阳光飞跃时y轴偏移量
- float t;//贝塞尔曲线的时间点
- vector2 p1, p2, p3, p4;//贝塞尔曲线的起始点(p1,p4)和控制点(p2,p3)
- vector2 pCur;//当前时刻阳光球的位置
- float speed;
- int status;//当前状态
- };
- struct sunshineBALL balls[10];//10个阳光球
- int ballMax = sizeof(balls) / sizeof(balls[0]);//阳光数量
-
- struct zomb//僵尸结构体
- {
- int x, y;//僵尸的坐标
- int frameIndex;//图片帧
- bool used;//使用情况
- int speed;//行走速度
- int zmrow;//僵尸在第(zmrow+1)行
- int blood;//僵尸的血量
- bool dead;//死亡
- bool eating;//正在吃植物的状态
- };
- struct zomb zms[10];//10个僵尸
-
- struct bullet//豌豆射手的子弹
- {
- int x, y;
- int pearow;//子弹所处的行
- int speed;//速度
- bool used;//使用情况
- bool blast;//子弹爆炸情况
- int frameindex;//爆炸帧
- };
- struct bullet bullets[30];//30个子弹
-
- bool fileExist(char* name)//判断文件能否打开
- {
- FILE* fp = fopen(name, "r");//以“读”的形式打开文件
- //fopen函数能打开文件返回文件路径,打不开文件返回NULL,所以需要定义指针变量
- if (fp == NULL)
- return false;//表示文件打不开
- else
- fclose(fp);//关闭文件
- return true;//表示文件能打开
- }
-
- void gameInit()//初始化函数
- {
- //加载背景图片
- loadimage(&imgBg, "res/bg.jpg");
- loadimage(&imgBar, "res/bar5.png");
-
- killcount = 0;
- zmCount1 = 0;
- gameStatus = GOING;
-
- //对数组整体初始化
- memset(imgPLANT, 0, sizeof(imgPLANT));
- memset(map, 0, sizeof(map));
- memset(balls, 0, sizeof(balls));
- memset(zms, 0, sizeof(zms));
- memset(bullets, 0, sizeof(bullets));
-
- char plant_name[64];
- for (int i = 0; i < PLANT_COUNTS; i++)//初始化植物卡牌
- {
- sprintf_s(plant_name, sizeof(plant_name), "res/Cards/card_%d.png", i + 1);
- loadimage(&imgCard[i], plant_name);
-
- for (int j = 0; j < 20; j++)
- {
- //将植物晃动的图片格式化到数组plant_name中
- sprintf_s(plant_name, sizeof(plant_name), "res/zhiwu/%d/%d.png", i, j + 1);
- if (fileExist(plant_name))//判断文件是否存在
- {
- imgPLANT[i][j] = new IMAGE;//分配新的内存
- loadimage(imgPLANT[i][j], plant_name);//将图片加载到实现晃动的数组里
- }
- else//打不开跳出循环
- break;
- }
- }
-
- curPLANT = 0;//初始化
-
- for (int i = 0; i < 29; i++)//初始化阳光球
- {
- sprintf_s(plant_name, sizeof(plant_name), "res/sunshine/%d.png", i + 1);
- loadimage(&imgsunshine[i], plant_name);
- }
-
- srand(time(NULL));//配置随机种子
-
- initgraph(WIN_WIDTH, WIN_HIGHT, 1);//创建游戏窗口,显示图片
-
- LOGFONT f;//设置显示阳光的字体
- gettextstyle(&f);//获取字体的文本格式
- f.lfHeight = 30;//字体高度30
- f.lfWeight = 15;//设置字体的粗细
- strcpy(f.lfFaceName, "Segoe UI Black");//设置字体格式
- f.lfQuality = ANTIALIASED_QUALITY;//抗锯齿
- settextstyle(&f);
- setbkmode(TRANSPARENT);//设置字体背景透明
- setcolor(BLACK);//设置字体颜色
-
- for (int i = 0; i < 22; i++)//初始化僵尸
- {
- sprintf_s(plant_name, sizeof(plant_name), "res/zm/%d.png", i + 1);
- loadimage(&imgzombie[i], plant_name);
- }
-
- //初始化豌豆normal
- loadimage(&imgBulletNormal, "res/bullets/bullet_normal.png");
-
- //初始化豌豆blast
- //爆炸的实质是一张爆炸帧图片放大四次
- loadimage(&imgballblast[3], "res/bullets/bullet_blast.png");
- for (int i = 0; i < 3; i++)//放大次数(可修改)
- {
- float k = (i + 1) * 0.2;//0.2是放大倍数(可修改)
- loadimage(&imgballblast[i], "res/bullets/bullet_blast.png",
- imgballblast[3].getwidth() * k, imgballblast[3].getheight() * k, true);
- //true表示按照如上的大小放大该图片,false相反
- }
-
- //初始化僵尸dead
- for (int i = 0; i < 20; i++)
- {
- sprintf_s(plant_name, sizeof(plant_name), "res/zm_dead/%d.png", i + 1);
- loadimage(&imgzmDead[i], plant_name);
- }
-
- //初始化僵尸eat
- for (int i = 0; i < 21; i++)
- {
- sprintf_s(plant_name, sizeof(plant_name), "res/zm_eat/%d.png", i + 1);
- loadimage(&imgzmEat[i], plant_name);
- }
-
- //初始化僵尸stand
- for (int i = 0; i < 11; i++)
- {
- sprintf_s(plant_name, sizeof(plant_name), "res/zm_stand/%d.png", i + 1);
- loadimage(&imgzmStand[i], plant_name);
- }
- }
-
- void drawzm()//渲染僵尸
- {
- int zmMax = sizeof(zms) / sizeof(zms[0]);
- for (int i = 0; i < zmMax; i++)
- {
- if (zms[i].used)
- {
- //根据僵尸的情况而调用不同的数组
- IMAGE* img3 = NULL;
- if (zms[i].dead)
- img3 = imgzmDead;
- else if (zms[i].eating)
- img3 = imgzmEat;
- else
- img3 = imgzombie;
- img3 += zms[i].frameIndex;
- putimagePNG(zms[i].x, zms[i].y - img3->getheight(), img3);
- }
- }
- }
-
- void drawsunshine()//渲染阳光
- {
- for (int i = 0; i < ballMax; i++)
- {
- //if (balls[i].used || balls[i].xoff)
- if(balls[i].used)
- {
- IMAGE* img2 = &imgsunshine[balls[i].frameINDEX];
- //putimagePNG(balls[i].x, balls[i].y, img2);
- putimagePNG(balls[i].pCur.x, balls[i].pCur.y, img2);
- }
- }
- }
-
- void drawCard()//渲染卡牌
- {
- for (int i = 0; i < PLANT_COUNTS; i++)//加载植物卡牌
- {
- int x = 338 + i * 65;//将植物加载到卡槽里
- int y = 6;
- putimage(x, y, &imgCard[i]);
- }
- }
-
- void drawPlantMap()//渲染种植后的植物
- {
- 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;//植物种植的横坐标
- //int y = 179 + i * 102 + 14;//植物种植的纵坐标,位置可自行调整
- putimagePNG(map[i][j].x, map[i][j].y, imgPLANT[map[i][j].type - 1][map[i][j].frameindex]);
- }
- }
- }
- }
-
- void drawPlantMove()//渲染鼠标拖动的植物
- {
- if (curPLANT)//渲染拖动时的植物模型
- {
- IMAGE* img1 = imgPLANT[curPLANT - 1][0];
- //实现无背景贴图,让鼠标位于植物的正中间
- putimagePNG(curX - img1->getwidth() / 2, curY - img1->getheight() / 2, img1);//取植物晃动的第一张图片
- }
- }
-
- void drawPea()渲染豌豆
- {
- int bulletMax = sizeof(bullets) / sizeof(bullets[0]);
- for (int i = 0; i < bulletMax; i++)
- {
- if (bullets[i].used)
- {
- if (bullets[i].blast)//爆炸
- {
- IMAGE* img5 = &imgballblast[bullets[i].frameindex];
- putimagePNG(bullets[i].x, bullets[i].y, img5);
- }
- else//正常
- {
- putimagePNG(bullets[i].x, bullets[i].y, &imgBulletNormal);
- }
- }
- }
- }
-
- void outsunshine()//显示左上角阳光数
- {
- char scoreText[8];
- sprintf_s(scoreText, sizeof(scoreText), "%d", sunshine);//将阳光格式化为字符串并加载进数组
- outtextxy(279, 67, scoreText);//在指定位置输出字符串
- }
-
- void updateWindow()//渲染显示图片
- {
- BeginBatchDraw();//开始缓冲
- putimage(-112, 0, &imgBg);//从最左上角显示草坪
- putimagePNG(250, 0, &imgBar);//植物栏
-
- drawCard();//渲染卡牌
-
- drawPlantMap();//渲染种植后的植物
-
- drawPlantMove();//渲染拖动时的植物
-
- drawsunshine();//渲染阳光
-
- drawzm();//渲染僵尸
-
- drawPea();//渲染豌豆
-
- outsunshine();//显示左上角阳光数
-
- EndBatchDraw();//结束双缓冲
- }
-
- void collectsunshine(ExMessage* msg)//实现收集阳光
- {
- int width = imgsunshine[0].getwidth();//获取阳光球的宽
- int hight = imgsunshine[0].getheight();//获取阳光球的高
- for (int i = 0; i < ballMax; i++)
- {
- if (balls[i].used)//遍历每一个用过的阳光
- {
- //int x = balls[i].x;//此阳光球左上角的x坐标
- //int y = balls[i].y;//此阳光球左上角的y坐标
- int x = balls[i].pCur.x;
- int y = balls[i].pCur.y;
- if (msg->x > x && msg->x<x + width && msg->y>y && msg->y < y + hight)//判断鼠标位置与阳光球的位置
- {
- //选中范围近似为矩形
- //balls[i].used = false;//选中后消失
- balls[i].status = SUNSHINE_COLLECT;//收集状态
- PlaySound("res/sunshine.wav", NULL, SND_FILENAME | SND_ASYNC);
- //mciSendString("play res/sunshine.mp3", 0, 0, NULL);//音乐播放(有延迟)
-
- //贝塞尔曲线法渲染阳光球飞跃路线
- balls[i].p1 = balls[i].pCur;//起点:被收集时的位置
- balls[i].p4 = vector2( 262,0 );//终点
- balls[i].t = 0;
- float distance = dis(balls[i].p1 - balls[i].p2);//两点之间的距离
- float off = 8;//每次移动的像素(可修改)
- balls[i].speed = 1.0 / (distance / off);//一次移动完成
- break;
-
- //获取阳光球与目的地中心连线与水平方向的夹角
- //float destY = 0;
- //float destX = 262;//植物栏阳光收集位置的坐标
- //float angle = atan((y - destY) / (x - destX));//通过反正切函数获取夹角
- //balls[i].xoff = 40 * cos(angle);
- //balls[i].yoff = 40 * sin(angle);//4是指定的飞跃速度
- }
- }
- }
- }
-
- void userclick()//实现鼠标相关操作
- {
- ExMessage msg;//结构体变量
- static int status = 0;//判断状态
- if (peekmessage(&msg))//判断msg的范围内是否有信息
- {
- if (msg.message == WM_LBUTTONDOWN)//鼠标左键按下
- {
- if (msg.x > 338 && msg.x < 338 + 65 * PLANT_COUNTS && msg.y < 96)//选择植物
- {
- int index = (msg.x - 338) / 65;//判断植物卡牌,这是下标
- status = 1;//表示鼠标左键已经按下
- //printf("%d\n", index);//测试
- curPLANT = index + 1;//选择植物
- }
- else //收集阳光
- {
- collectsunshine(&msg);
- }
- }
- else if (msg.message == WM_MOUSEMOVE && status == 1)//鼠标移动
- {
- curX = msg.x;//鼠标移动时图片的x轴位置
- curY = msg.y;//鼠标移动时图片的y轴位置
- }
- else if (msg.message == WM_LBUTTONUP)//鼠标左键抬起
- {
- if (msg.x > 144 && msg.y > 179 && msg.y < 489)//草坪的范围
- {
- int row = (msg.y - 179) / 102;//植物种植的行,102是行宽
- int col = (msg.x - 144) / 81;//植物种植的列,81是列宽
- //printf("%d,%d\n", row, col);//测试
- if (map[row][col].type == 0)//草坪只有空状态才能种植
- {
- map[row][col].type = curPLANT;//表示row行col列的植物类型为curPLANT
- map[row][col].frameindex = 0;
- map[row][col].shootTimer = 0;
- map[row][col].x = 144 + col * 81;
- map[row][col].y = 179 + row * 102 + 14;
- }
- }
- status = 0;
- curPLANT = 0;//鼠标左键抬起植物种下
- }
- }
- }
-
- void creatsunshine()//选取阳光,阳光结构体成员的初始化
- {
- static int count = 0;
- static int fre = 200;
- count++;
- if (count >= fre)
- {
- fre = 100 + rand() % 200;//每200到399毫秒创建一个阳光
- count = 0;
- int i = 0;
- for (i = 0; i < 29 && balls[i].used; i++);//空循环查询可使用的阳光
- //used初始为0表示该阳光可以被使用,循环停止
- if (i >= 29)
- return;
- balls[i].used = true;//第i个阳光被使用
- balls[i].frameINDEX = 0;//动画帧
- balls[i].timer = 0;
- //balls[i].x = 260 + rand() % (900 - 260);//阳光球随机在场上刷新
- //balls[i].y = 60;//初始下落坐标
- //balls[i].destY = 200 + (rand() % 4 * 90);
- //balls[i].xoff = 0;
- //balls[i].yoff = 0;
- balls[i].status = SUNSHINE_DOWN;//下落状态
- balls[i].t = 0;
- balls[i].p1 = vector2( 260 + rand() % (900 - 148) ,60 );//起点
- balls[i].p4 = vector2( balls[i].p1.x,200 + (rand() % 4 * 90) );//终点
- int off = 2;
- float distance = balls[i].p4.y - balls[i].p1.y;
- balls[i].speed = 1.0 / (distance / off);
- }
- //向日葵生产阳光
- static int sunshinecount = 0;
- if (++sunshinecount > 2)
- {
- sunshinecount = 0;
- for (int i = 0; i < 3; i++)//遍历3行9列
- {
- for (int j = 0; j < 9; j++)
- {
- if (map[i][j].type == SUN_FLOWER + 1)
- {
- map[i][j].timer++;
- if (map[i][j].timer > 100)//生产阳光的时间间隔
- {
- map[i][j].timer = 0;//归零
- int k;//找到第k个可用阳光
- for (k = 0; k < ballMax && balls[k].used; k++);
- if (k >= ballMax)
- return;
- balls[k].used = true;
- balls[k].p1 = vector2(map[i][j].x, map[i][j].y);//起点
- int w = (100 + rand() % 50) * (rand() % 2 ? 1 : -1);//阳光掉落位置与向日葵的距离(可在左也可在右)
- balls[k].p4 = vector2(map[i][j].x + w, map[i][j].y + imgPLANT[SUN_FLOWER][0]->getheight() - imgsunshine[0].getheight());
- balls[k].p2 = vector2(balls[k].p1.x + w * 0.3, balls[k].p1.y - 100);
- balls[k].p3 = vector2(balls[k].p1.x + w * 0.7, balls[k].p1.y - 100);
- balls[k].status = SUNSHINE_PRODUCT;
- balls[k].speed = 0.05;
- balls[k].t = 0;
- }
- }
- }
- }
- }
- }
-
- void updatesunshine()//更新阳光状态:使用——>未使用,动画帧再初始化
- {
- for (int i = 0; i < ballMax; i++)
- {
- if (balls[i].used)//如果阳光球处于使用状态,则更新
- {
- balls[i].frameINDEX = (balls[i].frameINDEX + 1) % 29;//防止动画帧越界
- if (balls[i].status == SUNSHINE_DOWN)//下落
- {
- struct sunshineBALL* sun = &balls[i];
- sun->t += sun->speed;
- sun->pCur = sun->p1 + sun->t * (sun->p4 - sun->p1);//直线运动
- if (sun->t >= 1)//到达终点
- {
- sun->status = SUNSHINE_GROUND;//更改状态
- sun->timer = 0;
- }
- }
- else if (balls[i].status == SUNSHINE_GROUND)//落地
- {
- balls[i].timer++;
- if (balls[i].timer > 100)
- {
- balls[i].used = false;
- balls[i].timer = 0;
- }
- }
- else if (balls[i].status == SUNSHINE_COLLECT)//收集
- {
- struct sunshineBALL* sun = &balls[i];
- sun->t += sun->speed;
- sun->pCur = sun->p1 + sun->t * (sun->p4 - sun->p1);//直线运动
- if (sun->t > 1)
- {
- sun->used = false;
- sunshine += 25;
- }
- }
- else if (balls[i].status == SUNSHINE_PRODUCT)//生产
- {
- struct sunshineBALL* sun = &balls[i];
- sun->t += sun->speed;
- //调用贝塞尔曲线函数
- sun->pCur = calcBezierPoint(sun->t, sun->p1, sun->p2, sun->p3, sun->p4);//曲线运动
- if (sun->t > 1)
- {
- sun->status = SUNSHINE_GROUND;
- sun->timer = 0;
- }
- }
- //if (balls[i].timer == 0)//下落过程中停留时间不变为0
- //{
- // balls[i].y += 3;//阳光下落
- //}
- //if (balls[i].y >= balls[i].destY)
- //{
- // balls[i].timer++;
- // if (balls[i].timer > 100)//停留100帧后消失
- // {
- // balls[i].used = false;//更新阳光的使用状态
- // }
- //}
- }
- //else if (balls[i].xoff)
- //{
- // float destY = 0;
- // float destX = 262;//植物栏阳光收集位置的坐标
- // float angle = atan((balls[i].y - destY) / (balls[i].x - destX));//通过反正切函数获取夹角
- // balls[i].xoff = 40 * cos(angle);
- // balls[i].yoff = 40 * sin(angle);//4是指定的飞跃速度
- // //每次飞跃时消除误差
-
- // balls[i].x -= balls[i].xoff;
- // balls[i].y -= balls[i].yoff;
- // if (balls[i].x < 262 || balls[i].y < 0)
- // {
- // balls[i].xoff = 0;
- // balls[i].yoff = 0;//偏移量归零
- // sunshine += 25;
- // balls[i].used = false;//到达目的地后模型消失
- // }
- //}
- }
- }
-
- void creatzomb()//创建僵尸
- {
- if (zmCount1 >= 10)
- return;
- int i;
- int zmMax = sizeof(zms) / sizeof(zms[0]);//僵尸的数量
- static int zmcount = 0;
- static int zmfre = 40;
- zmcount++;
- if (zmcount >= zmfre)
- {
- zmfre = 100 + rand() % 100;
- zmcount = 0;
- for (i = 0; i < zmMax && zms[i].used; i++);//查询可用的僵尸
- if (i < zmMax)
- {
- memset(&zms[i], 0, sizeof(zms[i]));
- zms[i].used = true;//表示该僵尸已经用过
- zms[i].x = WIN_WIDTH;//僵尸的横坐标
- zms[i].zmrow = rand() % 3;
- zms[i].y = 173 + (zms[i].zmrow + 1) * 100;//行高近似为100
- zms[i].speed = 1;//初始速度
- zms[i].blood = 150;//10个豌豆
- zms[i].dead = false;
- zmCount1++;
- }
- else
- {
- printf("创建僵尸失败\n");
- }
- }
- }
-
- void updatezomb()//更新僵尸的数据
- {
- int zmMax = sizeof(zms) / sizeof(zms[0]);//僵尸的数量
- static int count = 0;
- count++;
- if (count > 2)
- {
- count = 0;
- for (int i = 0; i < zmMax; i++)
- {
- if (zms[i].used)//被使用则更新
- {
- zms[i].x -= zms[i].speed;
- if (zms[i].x <= 28)
- {
- //printf("GAME OVER\n");
- //MessageBox(NULL, "over", "over", 0);//显示对话框
- //exit(0);//退出程序(待优化)
- gameStatus = FAIL;//游戏失败
- }
- }
- }
- }
-
- static int count2 = 0;
- count2++;
- if (count2 > 8)
- {
- count2 = 0;
- for (int i = 0; i < zmMax; i++)//更新僵尸的图片帧
- {
- if (zms[i].used)
- {
- if (zms[i].dead)//判断僵尸死亡
- {
- zms[i].frameIndex++;
- if (zms[i].frameIndex >= 20)
- {
- zms[i].used = false;
- killcount++;
- if (killcount == ZM_MAX)
- gameStatus = WIN;//游戏胜利
- }
- }
- else if (zms[i].eating)
- {
- zms[i].frameIndex = (zms[i].frameIndex + 1) % 21;
- }
- else
- {
- zms[i].frameIndex = (zms[i].frameIndex + 1) % 22;//防止越界
- }
- }
- }
- }
- }
-
- void shoot()//发射豌豆
- {
- static int count6 = 0;
- if (++count6 < 5)
- return;
- count6 = 0;
- int lines[3] = { 0 };//表示3行草坪
- int zmMax = sizeof(zms) / sizeof(zms[0]);//僵尸数量
- int peaMax = sizeof(bullets) / sizeof(bullets[0]);
- int dangerX = WIN_WIDTH;//- imgzombie[0].getwidth();//发射豌豆的距离(可调节)
- for (int i = 0; i < zmMax; i++)
- {
- if (zms[i].used && zms[i].x < dangerX)//僵尸出现在草坪上
- {
- lines[zms[i].zmrow] = 1;//僵尸出现的行表示为1
- }
- }
-
- for (int i = 0; i < 3; i++)//遍历草坪
- {
- for (int j = 0; j < 9; j++)
- {
- if (map[i][j].type == PEA + 1 && lines[i])//植物类型是1代表豌豆射手,可以发射子弹
- {
- map[i][j].shootTimer++;
- if (map[i][j].shootTimer > 15)//调节豌豆发射的频率(可调节)
- {
- map[i][j].shootTimer = 0;
- int k;
- for (k = 0; k < peaMax && bullets[k].used; k++);//查找可用子弹
- if (k < peaMax)
- {
- bullets[k].used = true;
- bullets[k].pearow = i;//第i+1行
- bullets[k].speed = 3;
- bullets[k].blast = false;//最开始没有发生爆炸
- bullets[k].frameindex = 0;
-
- int peax = 144 + j * 81;//植物种植的横坐标
- int peay = 179 + i * 102 + 14;//植物种植的纵坐标,位置可自行调整
- //发射位置是豌豆左上角坐标加上一个身位的嘴部,-10可自行调整,只是为了更接近原版
- bullets[k].x = peax + imgPLANT[map[i][j].type - 1][0]->getwidth() - 10;
- bullets[k].y = peay + 5;
- }
- }
- }
- }
- }
- }
-
- void updatebullets()//更新子弹的数据
- {
- int bulletMax = sizeof(bullets) / sizeof(bullets[0]);//子弹数目
- for (int i = 0; i < bulletMax; i++)
- {
- if (bullets[i].used)
- {
- bullets[i].x += bullets[i].speed;//更新x坐标
- if (bullets[i].x > WIN_WIDTH)
- {
- bullets[i].used = false;//对超出边界的豌豆回收
- }
- if (bullets[i].blast)//子弹符合爆炸条件
- {
- bullets[i].frameindex++;
- if (bullets[i].frameindex >= 4)
- {
- bullets[i].used = false;//爆炸结束
- }
- }
- }
- }
- }
-
- void checkbullet()//豌豆->僵尸
- {
- int bulletMax = sizeof(bullets) / sizeof(bullets[0]);
- int zombMax = sizeof(zms) / sizeof(zms[0]);
- for (int i = 0; i < bulletMax; i++)//遍历子弹
- {
- if (bullets[i].used == false || bullets[i].blast)
- continue;//如果子弹还未出现或已经爆炸则无需做爆炸检测
- for (int j = 0; j < zombMax; j++)//遍历僵尸
- {
- if (zms[j].used == false)
- continue;//僵尸还未出现无需检测
- int x1 = zms[j].x + 80;
- int x2 = zms[j].x + 110;//僵尸模型的范围
- int x = bullets[i].x;//子弹的坐标
- if (zms[j].dead == false && bullets[i].pearow == zms[j].zmrow && x > x1 && x < x2)//保证僵尸和子弹在同一行
- {
- //僵尸死亡不用做碰撞检测
- zms[j].blood -= 20;//扣20滴血
- bullets[i].blast = true;//子弹爆炸
- bullets[i].speed = 0;
- if (zms[j].blood <= 0)
- {
- zms[j].dead = true;//僵尸死亡
- zms[j].speed = 0;
- zms[j].frameIndex = 0;
- }
- break;
- }
- }
- }
- }
-
- void checkeat()//僵尸->植物
- {
- int zmcount = sizeof(zms) / sizeof(zms[0]);
- for (int i = 0; i < zmcount; i++)//遍历僵尸
- {
- if (zms[i].dead)
- continue;
- int samerow = zms[i].zmrow;
- for (int k = 0; k < 9; k++)//只遍历相同行的9个格子
- {
- if (map[samerow][k].type == 0)//该格子没有种植植物
- continue;
- int plantX = 144 + k * 81;//植物图片左上角的坐标
- int x1 = plantX + 10;//植物的左边界
- int x2 = plantX + 60;//植物的右边界
- int x3 = zms[i].x + 80;//僵尸的左边界
- if (x3 > x1 && x3 < x2)
- {
- if (map[samerow][k].catched)//植物被捕获
- {
- map[samerow][k].deadtime ++;
- if (map[samerow][k].deadtime > 100)
- {
- map[samerow][k].deadtime = 0;
- map[samerow][k].type = 0;
- zms[i].eating = false;
- zms[i].frameIndex = 0;
- zms[i].speed = 2;
- map[samerow][k].catched = false;
- }
- }
- else
- {
- map[samerow][k].catched = true;
- map[samerow][k].deadtime = 0;//死亡倒计时
- zms[i].eating = true;//僵尸开吃
- zms[i].speed = 0;
- zms[i].frameIndex = 0;
- }
- }
- }
- }
- }
-
- void collisioncheck()//碰撞检测
- {
- checkbullet();//子弹碰撞
- checkeat();//僵尸捕获植物
- }
-
- void updatePlant()//更新植物
- {
- static int count = 0;//频度控制
- if (++count > 2)
- {
- count = 0;
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 9; j++)
- {
- if (map[i][j].type > 0)
- {
- map[i][j].frameindex++;//从第一张图片到最后一张图片
- //判断能否打开文件,打不开证明是最后一帧
- if (imgPLANT[map[i][j].type - 1][map[i][j].frameindex] == NULL)
- {
- map[i][j].frameindex = 0;//再初始化,从第一帧重新开始
- }
- }
- }
- }
- }
- }
-
- void updateGame()//在每次循环后更改相应的参数
- {
- updatePlant();//更新植物
-
- creatsunshine();//创建阳光
- updatesunshine();//更新阳光
-
- creatzomb();//创建僵尸
- updatezomb();//更新僵尸
-
- shoot();//发射豌豆
- updatebullets();//更新豌豆
-
- collisioncheck();//碰撞检测
- }
-
- void startUI()//起始菜单
- {
- mciSendString("play res/bg.mp3", 0, 0, NULL);
- IMAGE imgBG, imgMenu1, imgMenu2;
- loadimage(&imgBG, "res/menu.png");
- loadimage(&imgMenu1, "res/menu1.png");//选中选项卡
- loadimage(&imgMenu2, "res/menu2.png");//未选中选项卡
- int flag = 0;//表示是否选中,选中则为1
-
- 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>75 &&
- msg.y < 75 + 140)
- {
- flag = 1;//表示选中
- }
- else if (msg.message == WM_LBUTTONUP && flag)
- {
- EndBatchDraw();
- break;
- }
- }
- EndBatchDraw();
- }
- mciSendString("close res/bg.mp3", 0, 0, 0);
- }
-
- void viewScene()//视角移动
- {
- int xMax = WIN_WIDTH - imgBg.getwidth();//窗口宽-图片宽(坐标为负数)
- vector2 point[9] = { {550,80},{530,160},{630,170},{530,200},{515,270},
- {565,370},{605,340},{705,280},{690,340} };//阅览僵尸站位(可修改)
-
- int index[9];
- for (int j = 0; j < 9; j++)
- {
- index[j] = rand() % 11;//僵尸起始帧序号
- }
-
- int scenecount = 0;
-
- //画面巡视
- for (int i = 0; i >= xMax; i -= 2)
- {
- BeginBatchDraw();
-
- putimage(i, 0, &imgBg);
- scenecount++;
- for (int k = 0; k < 9; k++)//绘制僵尸站位
- {
- putimagePNG(point[k].x - xMax + i, point[k].y, &imgzmStand[index[k]]);
- if (scenecount >= 10)
- index[k] = (index[k] + 1) % 11;
- }
- if (scenecount >= 10)
- scenecount = 0;
-
- EndBatchDraw();
- Sleep(5);
- }
-
- //画面停留时间(可修改)
- for (int i = 0; i < 50; i++)
- {
- BeginBatchDraw();
-
- putimage(xMax, 0, &imgBg);
- for (int k = 0; k < 9; k++)
- {
- putimagePNG(point[k].x, point[k].y, &imgzmStand[index[k]]);
- index[k] = (index[k] + 1) % 11;
- }
-
- EndBatchDraw();
- Sleep(30);
- }
-
- //画面返回
- for (int k = xMax; k <= -112; k += 2)
- {
- BeginBatchDraw();
-
- putimage(k, 0, &imgBg);
- scenecount++;
- for (int i = 0; i < 9; i++)
- {
- putimagePNG(point[i].x - xMax + k, point[i].y, &imgzmStand[index[i]]);
- if (scenecount >= 10)
- index[i] = (index[i] + 1) % 11;
- }
- if (scenecount >= 10)
- scenecount = 0;
-
- EndBatchDraw();
- Sleep(5);
- }
- }
-
- void barsDown()//工具栏下降
- {
- int hight = imgBar.getheight();//获取工具栏的高
- for (int i = -hight; i <= 0; i++)
- {
- BeginBatchDraw();
-
- putimage(-112, 0, &imgBg);
- putimagePNG(250, i, &imgBar);
-
- for (int k = 0; k < PLANT_COUNTS; k++)
- {
- int x = 338 + k * 65;
- int y = 6 + i;
- putimage(x, y, &imgCard[k]);
- }
-
- EndBatchDraw();
- Sleep(10);
- }
- }
-
- bool checkOver()//游戏结束画面
- {
- bool ret = false;
- if (gameStatus == WIN)//游戏胜利
- {
- mciSendString("play res/win.mp3", 0, 0, NULL);
- Sleep(2000);
- mciSendString("close res/win.mp3", 0, 0, NULL);
- IMAGE* img7 = NULL;
- loadimage(img7, "res/gameWin.png");
- putimage(0, 0, img7);
- ret = true;
- }
- else if (gameStatus == FAIL)
- {
- mciSendString("play res/lose.mp3", 0, 0, NULL);
- Sleep(2000);
- mciSendString("close res/lose.mp3", 0, 0, NULL);
- IMAGE* img9 = NULL;
- loadimage(img9, "res/gameFail.png");
- putimage(0, 0, img9);
- ret = true;
- }
- return ret;
- }
-
- int main()
- {
- gameInit();
- startUI();
- viewScene();
- barsDown();
-
- int timer = 0;//实现帧等待
- bool flag = true;
-
- while (1)//循环退出条件:用户单击
- {
- userclick();
-
- timer += getDelay();//运行时间间隔
- if (timer > 10)//运行时间间隔大于10毫秒
- {
- flag = true;
- timer = 0;//再初始化
- }
- if (flag)
- {
- flag = false;
- updateWindow();
- updateGame();
- if (checkOver())
- break;
- }
- }
-
- system("pause");//暂停
- return 0;
- }
- bool fileExist(char* name)//判断文件能否打开
- {
- FILE* fp = fopen(name, "r");//以“读”的形式打开文件
- //fopen函数能打开文件返回文件路径,打不开文件返回NULL,所以需要定义指针变量
- if (fp == NULL)
- return false;//表示文件打不开
- else
- fclose(fp);//关闭文件
- return true;//表示文件能打开
- }
- void gameInit()//初始化函数
- {
- //加载背景图片
- loadimage(&imgBg, "res/bg.jpg");
- loadimage(&imgBar, "res/bar5.png");
-
- killcount = 0;
- zmCount1 = 0;
- gameStatus = GOING;
-
- //对数组整体初始化
- memset(imgPLANT, 0, sizeof(imgPLANT));
- memset(map, 0, sizeof(map));
- memset(balls, 0, sizeof(balls));
- memset(zms, 0, sizeof(zms));
- memset(bullets, 0, sizeof(bullets));
-
- char plant_name[64];
- for (int i = 0; i < PLANT_COUNTS; i++)//初始化植物卡牌
- {
- sprintf_s(plant_name, sizeof(plant_name), "res/Cards/card_%d.png", i + 1);
- loadimage(&imgCard[i], plant_name);
-
- for (int j = 0; j < 20; j++)
- {
- //将植物晃动的图片格式化到数组plant_name中
- sprintf_s(plant_name, sizeof(plant_name), "res/zhiwu/%d/%d.png", i, j + 1);
- if (fileExist(plant_name))//判断文件是否存在
- {
- imgPLANT[i][j] = new IMAGE;//分配新的内存
- loadimage(imgPLANT[i][j], plant_name);//将图片加载到实现晃动的数组里
- }
- else//打不开跳出循环
- break;
- }
- }
-
- curPLANT = 0;//初始化
-
- for (int i = 0; i < 29; i++)//初始化阳光球
- {
- sprintf_s(plant_name, sizeof(plant_name), "res/sunshine/%d.png", i + 1);
- loadimage(&imgsunshine[i], plant_name);
- }
-
- srand(time(NULL));//配置随机种子
-
- initgraph(WIN_WIDTH, WIN_HIGHT, 1);//创建游戏窗口,显示图片
-
- LOGFONT f;//设置显示阳光的字体
- gettextstyle(&f);//获取字体的文本格式
- f.lfHeight = 30;//字体高度30
- f.lfWeight = 15;//设置字体的粗细
- strcpy(f.lfFaceName, "Segoe UI Black");//设置字体格式
- f.lfQuality = ANTIALIASED_QUALITY;//抗锯齿
- settextstyle(&f);
- setbkmode(TRANSPARENT);//设置字体背景透明
- setcolor(BLACK);//设置字体颜色
-
- for (int i = 0; i < 22; i++)//初始化僵尸
- {
- sprintf_s(plant_name, sizeof(plant_name), "res/zm/%d.png", i + 1);
- loadimage(&imgzombie[i], plant_name);
- }
-
- //初始化豌豆normal
- loadimage(&imgBulletNormal, "res/bullets/bullet_normal.png");
-
- //初始化豌豆blast
- //爆炸的实质是一张爆炸帧图片放大四次
- loadimage(&imgballblast[3], "res/bullets/bullet_blast.png");
- for (int i = 0; i < 3; i++)//放大次数(可修改)
- {
- float k = (i + 1) * 0.2;//0.2是放大倍数(可修改)
- loadimage(&imgballblast[i], "res/bullets/bullet_blast.png",
- imgballblast[3].getwidth() * k, imgballblast[3].getheight() * k, true);
- //true表示按照如上的大小放大该图片,false相反
- }
-
- //初始化僵尸dead
- for (int i = 0; i < 20; i++)
- {
- sprintf_s(plant_name, sizeof(plant_name), "res/zm_dead/%d.png", i + 1);
- loadimage(&imgzmDead[i], plant_name);
- }
-
- //初始化僵尸eat
- for (int i = 0; i < 21; i++)
- {
- sprintf_s(plant_name, sizeof(plant_name), "res/zm_eat/%d.png", i + 1);
- loadimage(&imgzmEat[i], plant_name);
- }
-
- //初始化僵尸stand
- for (int i = 0; i < 11; i++)
- {
- sprintf_s(plant_name, sizeof(plant_name), "res/zm_stand/%d.png", i + 1);
- loadimage(&imgzmStand[i], plant_name);
- }
- }
- void drawzm()//渲染僵尸
- {
- int zmMax = sizeof(zms) / sizeof(zms[0]);
- for (int i = 0; i < zmMax; i++)
- {
- if (zms[i].used)
- {
- //根据僵尸的情况而调用不同的数组
- IMAGE* img3 = NULL;
- if (zms[i].dead)
- img3 = imgzmDead;
- else if (zms[i].eating)
- img3 = imgzmEat;
- else
- img3 = imgzombie;
- img3 += zms[i].frameIndex;
- putimagePNG(zms[i].x, zms[i].y - img3->getheight(), img3);
- }
- }
- }
- void drawsunshine()//渲染阳光
- {
- for (int i = 0; i < ballMax; i++)
- {
- //if (balls[i].used || balls[i].xoff)
- if(balls[i].used)
- {
- IMAGE* img2 = &imgsunshine[balls[i].frameINDEX];
- //putimagePNG(balls[i].x, balls[i].y, img2);
- putimagePNG(balls[i].pCur.x, balls[i].pCur.y, img2);
- }
- }
- }
- void drawCard()//渲染卡牌
- {
- for (int i = 0; i < PLANT_COUNTS; i++)//加载植物卡牌
- {
- int x = 338 + i * 65;//将植物加载到卡槽里
- int y = 6;
- putimage(x, y, &imgCard[i]);
- }
- }
- void drawPlantMap()//渲染种植后的植物
- {
- 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;//植物种植的横坐标
- //int y = 179 + i * 102 + 14;//植物种植的纵坐标,位置可自行调整
- putimagePNG(map[i][j].x, map[i][j].y, imgPLANT[map[i][j].type - 1][map[i][j].frameindex]);
- }
- }
- }
- }
- void drawPlantMove()//渲染鼠标拖动的植物
- {
- if (curPLANT)//渲染拖动时的植物模型
- {
- IMAGE* img1 = imgPLANT[curPLANT - 1][0];
- //实现无背景贴图,让鼠标位于植物的正中间
- putimagePNG(curX - img1->getwidth() / 2, curY - img1->getheight() / 2, img1);//取植物晃动的第一张图片
- }
- }
- void drawPea()渲染豌豆
- {
- int bulletMax = sizeof(bullets) / sizeof(bullets[0]);
- for (int i = 0; i < bulletMax; i++)
- {
- if (bullets[i].used)
- {
- if (bullets[i].blast)//爆炸
- {
- IMAGE* img5 = &imgballblast[bullets[i].frameindex];
- putimagePNG(bullets[i].x, bullets[i].y, img5);
- }
- else//正常
- {
- putimagePNG(bullets[i].x, bullets[i].y, &imgBulletNormal);
- }
- }
- }
- }
- void outsunshine()//显示左上角阳光数
- {
- char scoreText[8];
- sprintf_s(scoreText, sizeof(scoreText), "%d", sunshine);//将阳光格式化为字符串并加载进数组
- outtextxy(279, 67, scoreText);//在指定位置输出字符串
- }
- void updateWindow()//渲染显示图片
- {
- BeginBatchDraw();//开始缓冲
- putimage(-112, 0, &imgBg);//从最左上角显示草坪
- putimagePNG(250, 0, &imgBar);//植物栏
-
- drawCard();//渲染卡牌
-
- drawPlantMap();//渲染种植后的植物
-
- drawPlantMove();//渲染拖动时的植物
-
- drawsunshine();//渲染阳光
-
- drawzm();//渲染僵尸
-
- drawPea();//渲染豌豆
-
- outsunshine();//显示左上角阳光数
-
- EndBatchDraw();//结束双缓冲
- }
- void collectsunshine(ExMessage* msg)//实现收集阳光
- {
- int width = imgsunshine[0].getwidth();//获取阳光球的宽
- int hight = imgsunshine[0].getheight();//获取阳光球的高
- for (int i = 0; i < ballMax; i++)
- {
- if (balls[i].used)//遍历每一个用过的阳光
- {
- //int x = balls[i].x;//此阳光球左上角的x坐标
- //int y = balls[i].y;//此阳光球左上角的y坐标
- int x = balls[i].pCur.x;
- int y = balls[i].pCur.y;
- if (msg->x > x && msg->x<x + width && msg->y>y && msg->y < y + hight)//判断鼠标位置与阳光球的位置
- {
- //选中范围近似为矩形
- //balls[i].used = false;//选中后消失
- balls[i].status = SUNSHINE_COLLECT;//收集状态
- PlaySound("res/sunshine.wav", NULL, SND_FILENAME | SND_ASYNC);
- //mciSendString("play res/sunshine.mp3", 0, 0, NULL);//音乐播放(有延迟)
-
- //贝塞尔曲线法渲染阳光球飞跃路线
- balls[i].p1 = balls[i].pCur;//起点:被收集时的位置
- balls[i].p4 = vector2( 262,0 );//终点
- balls[i].t = 0;
- float distance = dis(balls[i].p1 - balls[i].p2);//两点之间的距离
- float off = 8;//每次移动的像素(可修改)
- balls[i].speed = 1.0 / (distance / off);//一次移动完成
- break;
-
- //获取阳光球与目的地中心连线与水平方向的夹角
- //float destY = 0;
- //float destX = 262;//植物栏阳光收集位置的坐标
- //float angle = atan((y - destY) / (x - destX));//通过反正切函数获取夹角
- //balls[i].xoff = 40 * cos(angle);
- //balls[i].yoff = 40 * sin(angle);//4是指定的飞跃速度
- }
- }
- }
- }
- void userclick()//实现鼠标相关操作
- {
- ExMessage msg;//结构体变量
- static int status = 0;//判断状态
- if (peekmessage(&msg))//判断msg的范围内是否有信息
- {
- if (msg.message == WM_LBUTTONDOWN)//鼠标左键按下
- {
- if (msg.x > 338 && msg.x < 338 + 65 * PLANT_COUNTS && msg.y < 96)//选择植物
- {
- int index = (msg.x - 338) / 65;//判断植物卡牌,这是下标
- status = 1;//表示鼠标左键已经按下
- //printf("%d\n", index);//测试
- curPLANT = index + 1;//选择植物
- }
- else //收集阳光
- {
- collectsunshine(&msg);
- }
- }
- else if (msg.message == WM_MOUSEMOVE && status == 1)//鼠标移动
- {
- curX = msg.x;//鼠标移动时图片的x轴位置
- curY = msg.y;//鼠标移动时图片的y轴位置
- }
- else if (msg.message == WM_LBUTTONUP)//鼠标左键抬起
- {
- if (msg.x > 144 && msg.y > 179 && msg.y < 489)//草坪的范围
- {
- int row = (msg.y - 179) / 102;//植物种植的行,102是行宽
- int col = (msg.x - 144) / 81;//植物种植的列,81是列宽
- //printf("%d,%d\n", row, col);//测试
- if (map[row][col].type == 0)//草坪只有空状态才能种植
- {
- map[row][col].type = curPLANT;//表示row行col列的植物类型为curPLANT
- map[row][col].frameindex = 0;
- map[row][col].shootTimer = 0;
- map[row][col].x = 144 + col * 81;
- map[row][col].y = 179 + row * 102 + 14;
- }
- }
- status = 0;
- curPLANT = 0;//鼠标左键抬起植物种下
- }
- }
- }
- void creatsunshine()//选取阳光,阳光结构体成员的初始化
- {
- static int count = 0;
- static int fre = 200;
- count++;
- if (count >= fre)
- {
- fre = 100 + rand() % 200;//每200到399毫秒创建一个阳光
- count = 0;
- int i = 0;
- for (i = 0; i < 29 && balls[i].used; i++);//空循环查询可使用的阳光
- //used初始为0表示该阳光可以被使用,循环停止
- if (i >= 29)
- return;
- balls[i].used = true;//第i个阳光被使用
- balls[i].frameINDEX = 0;//动画帧
- balls[i].timer = 0;
- //balls[i].x = 260 + rand() % (900 - 260);//阳光球随机在场上刷新
- //balls[i].y = 60;//初始下落坐标
- //balls[i].destY = 200 + (rand() % 4 * 90);
- //balls[i].xoff = 0;
- //balls[i].yoff = 0;
- balls[i].status = SUNSHINE_DOWN;//下落状态
- balls[i].t = 0;
- balls[i].p1 = vector2( 260 + rand() % (900 - 148) ,60 );//起点
- balls[i].p4 = vector2( balls[i].p1.x,200 + (rand() % 4 * 90) );//终点
- int off = 2;
- float distance = balls[i].p4.y - balls[i].p1.y;
- balls[i].speed = 1.0 / (distance / off);
- }
- //向日葵生产阳光
- static int sunshinecount = 0;
- if (++sunshinecount > 2)
- {
- sunshinecount = 0;
- for (int i = 0; i < 3; i++)//遍历3行9列
- {
- for (int j = 0; j < 9; j++)
- {
- if (map[i][j].type == SUN_FLOWER + 1)
- {
- map[i][j].timer++;
- if (map[i][j].timer > 100)//生产阳光的时间间隔
- {
- map[i][j].timer = 0;//归零
- int k;//找到第k个可用阳光
- for (k = 0; k < ballMax && balls[k].used; k++);
- if (k >= ballMax)
- return;
- balls[k].used = true;
- balls[k].p1 = vector2(map[i][j].x, map[i][j].y);//起点
- int w = (100 + rand() % 50) * (rand() % 2 ? 1 : -1);//阳光掉落位置与向日葵的距离(可在左也可在右)
- balls[k].p4 = vector2(map[i][j].x + w, map[i][j].y + imgPLANT[SUN_FLOWER][0]->getheight() - imgsunshine[0].getheight());
- balls[k].p2 = vector2(balls[k].p1.x + w * 0.3, balls[k].p1.y - 100);
- balls[k].p3 = vector2(balls[k].p1.x + w * 0.7, balls[k].p1.y - 100);
- balls[k].status = SUNSHINE_PRODUCT;
- balls[k].speed = 0.05;
- balls[k].t = 0;
- }
- }
- }
- }
- }
- }
- void updatesunshine()//更新阳光状态:使用——>未使用,动画帧再初始化
- {
- for (int i = 0; i < ballMax; i++)
- {
- if (balls[i].used)//如果阳光球处于使用状态,则更新
- {
- balls[i].frameINDEX = (balls[i].frameINDEX + 1) % 29;//防止动画帧越界
- if (balls[i].status == SUNSHINE_DOWN)//下落
- {
- struct sunshineBALL* sun = &balls[i];
- sun->t += sun->speed;
- sun->pCur = sun->p1 + sun->t * (sun->p4 - sun->p1);//直线运动
- if (sun->t >= 1)//到达终点
- {
- sun->status = SUNSHINE_GROUND;//更改状态
- sun->timer = 0;
- }
- }
- else if (balls[i].status == SUNSHINE_GROUND)//落地
- {
- balls[i].timer++;
- if (balls[i].timer > 100)
- {
- balls[i].used = false;
- balls[i].timer = 0;
- }
- }
- else if (balls[i].status == SUNSHINE_COLLECT)//收集
- {
- struct sunshineBALL* sun = &balls[i];
- sun->t += sun->speed;
- sun->pCur = sun->p1 + sun->t * (sun->p4 - sun->p1);//直线运动
- if (sun->t > 1)
- {
- sun->used = false;
- sunshine += 25;
- }
- }
- else if (balls[i].status == SUNSHINE_PRODUCT)//生产
- {
- struct sunshineBALL* sun = &balls[i];
- sun->t += sun->speed;
- //调用贝塞尔曲线函数
- sun->pCur = calcBezierPoint(sun->t, sun->p1, sun->p2, sun->p3, sun->p4);//曲线运动
- if (sun->t > 1)
- {
- sun->status = SUNSHINE_GROUND;
- sun->timer = 0;
- }
- }
- //if (balls[i].timer == 0)//下落过程中停留时间不变为0
- //{
- // balls[i].y += 3;//阳光下落
- //}
- //if (balls[i].y >= balls[i].destY)
- //{
- // balls[i].timer++;
- // if (balls[i].timer > 100)//停留100帧后消失
- // {
- // balls[i].used = false;//更新阳光的使用状态
- // }
- //}
- }
- //else if (balls[i].xoff)
- //{
- // float destY = 0;
- // float destX = 262;//植物栏阳光收集位置的坐标
- // float angle = atan((balls[i].y - destY) / (balls[i].x - destX));//通过反正切函数获取夹角
- // balls[i].xoff = 40 * cos(angle);
- // balls[i].yoff = 40 * sin(angle);//4是指定的飞跃速度
- // //每次飞跃时消除误差
-
- // balls[i].x -= balls[i].xoff;
- // balls[i].y -= balls[i].yoff;
- // if (balls[i].x < 262 || balls[i].y < 0)
- // {
- // balls[i].xoff = 0;
- // balls[i].yoff = 0;//偏移量归零
- // sunshine += 25;
- // balls[i].used = false;//到达目的地后模型消失
- // }
- //}
- }
- }
- void creatzomb()//创建僵尸
- {
- if (zmCount1 >= 10)
- return;
- int i;
- int zmMax = sizeof(zms) / sizeof(zms[0]);//僵尸的数量
- static int zmcount = 0;
- static int zmfre = 40;
- zmcount++;
- if (zmcount >= zmfre)
- {
- zmfre = 100 + rand() % 100;
- zmcount = 0;
- for (i = 0; i < zmMax && zms[i].used; i++);//查询可用的僵尸
- if (i < zmMax)
- {
- memset(&zms[i], 0, sizeof(zms[i]));
- zms[i].used = true;//表示该僵尸已经用过
- zms[i].x = WIN_WIDTH;//僵尸的横坐标
- zms[i].zmrow = rand() % 3;
- zms[i].y = 173 + (zms[i].zmrow + 1) * 100;//行高近似为100
- zms[i].speed = 1;//初始速度
- zms[i].blood = 150;//10个豌豆
- zms[i].dead = false;
- zmCount1++;
- }
- else
- {
- printf("创建僵尸失败\n");
- }
- }
- }
- void updatezomb()//更新僵尸的数据
- {
- int zmMax = sizeof(zms) / sizeof(zms[0]);//僵尸的数量
- static int count = 0;
- count++;
- if (count > 2)
- {
- count = 0;
- for (int i = 0; i < zmMax; i++)
- {
- if (zms[i].used)//被使用则更新
- {
- zms[i].x -= zms[i].speed;
- if (zms[i].x <= 28)
- {
- //printf("GAME OVER\n");
- //MessageBox(NULL, "over", "over", 0);//显示对话框
- //exit(0);//退出程序(待优化)
- gameStatus = FAIL;//游戏失败
- }
- }
- }
- }
-
- static int count2 = 0;
- count2++;
- if (count2 > 8)
- {
- count2 = 0;
- for (int i = 0; i < zmMax; i++)//更新僵尸的图片帧
- {
- if (zms[i].used)
- {
- if (zms[i].dead)//判断僵尸死亡
- {
- zms[i].frameIndex++;
- if (zms[i].frameIndex >= 20)
- {
- zms[i].used = false;
- killcount++;
- if (killcount == ZM_MAX)
- gameStatus = WIN;//游戏胜利
- }
- }
- else if (zms[i].eating)
- {
- zms[i].frameIndex = (zms[i].frameIndex + 1) % 21;
- }
- else
- {
- zms[i].frameIndex = (zms[i].frameIndex + 1) % 22;//防止越界
- }
- }
- }
- }
- }
- void shoot()//发射豌豆
- {
- static int count6 = 0;
- if (++count6 < 5)
- return;
- count6 = 0;
- int lines[3] = { 0 };//表示3行草坪
- int zmMax = sizeof(zms) / sizeof(zms[0]);//僵尸数量
- int peaMax = sizeof(bullets) / sizeof(bullets[0]);
- int dangerX = WIN_WIDTH;//- imgzombie[0].getwidth();//发射豌豆的距离(可调节)
- for (int i = 0; i < zmMax; i++)
- {
- if (zms[i].used && zms[i].x < dangerX)//僵尸出现在草坪上
- {
- lines[zms[i].zmrow] = 1;//僵尸出现的行表示为1
- }
- }
-
- for (int i = 0; i < 3; i++)//遍历草坪
- {
- for (int j = 0; j < 9; j++)
- {
- if (map[i][j].type == PEA + 1 && lines[i])//植物类型是1代表豌豆射手,可以发射子弹
- {
- map[i][j].shootTimer++;
- if (map[i][j].shootTimer > 15)//调节豌豆发射的频率(可调节)
- {
- map[i][j].shootTimer = 0;
- int k;
- for (k = 0; k < peaMax && bullets[k].used; k++);//查找可用子弹
- if (k < peaMax)
- {
- bullets[k].used = true;
- bullets[k].pearow = i;//第i+1行
- bullets[k].speed = 3;
- bullets[k].blast = false;//最开始没有发生爆炸
- bullets[k].frameindex = 0;
-
- int peax = 144 + j * 81;//植物种植的横坐标
- int peay = 179 + i * 102 + 14;//植物种植的纵坐标,位置可自行调整
- //发射位置是豌豆左上角坐标加上一个身位的嘴部,-10可自行调整,只是为了更接近原版
- bullets[k].x = peax + imgPLANT[map[i][j].type - 1][0]->getwidth() - 10;
- bullets[k].y = peay + 5;
- }
- }
- }
- }
- }
- }
- void updatebullets()//更新子弹的数据
- {
- int bulletMax = sizeof(bullets) / sizeof(bullets[0]);//子弹数目
- for (int i = 0; i < bulletMax; i++)
- {
- if (bullets[i].used)
- {
- bullets[i].x += bullets[i].speed;//更新x坐标
- if (bullets[i].x > WIN_WIDTH)
- {
- bullets[i].used = false;//对超出边界的豌豆回收
- }
- if (bullets[i].blast)//子弹符合爆炸条件
- {
- bullets[i].frameindex++;
- if (bullets[i].frameindex >= 4)
- {
- bullets[i].used = false;//爆炸结束
- }
- }
- }
- }
- }
- void checkbullet()//豌豆->僵尸
- {
- int bulletMax = sizeof(bullets) / sizeof(bullets[0]);
- int zombMax = sizeof(zms) / sizeof(zms[0]);
- for (int i = 0; i < bulletMax; i++)//遍历子弹
- {
- if (bullets[i].used == false || bullets[i].blast)
- continue;//如果子弹还未出现或已经爆炸则无需做爆炸检测
- for (int j = 0; j < zombMax; j++)//遍历僵尸
- {
- if (zms[j].used == false)
- continue;//僵尸还未出现无需检测
- int x1 = zms[j].x + 80;
- int x2 = zms[j].x + 110;//僵尸模型的范围
- int x = bullets[i].x;//子弹的坐标
- if (zms[j].dead == false && bullets[i].pearow == zms[j].zmrow && x > x1 && x < x2)//保证僵尸和子弹在同一行
- {
- //僵尸死亡不用做碰撞检测
- zms[j].blood -= 20;//扣20滴血
- bullets[i].blast = true;//子弹爆炸
- bullets[i].speed = 0;
- if (zms[j].blood <= 0)
- {
- zms[j].dead = true;//僵尸死亡
- zms[j].speed = 0;
- zms[j].frameIndex = 0;
- }
- break;
- }
- }
- }
- }
- void checkeat()//僵尸->植物
- {
- int zmcount = sizeof(zms) / sizeof(zms[0]);
- for (int i = 0; i < zmcount; i++)//遍历僵尸
- {
- if (zms[i].dead)
- continue;
- int samerow = zms[i].zmrow;
- for (int k = 0; k < 9; k++)//只遍历相同行的9个格子
- {
- if (map[samerow][k].type == 0)//该格子没有种植植物
- continue;
- int plantX = 144 + k * 81;//植物图片左上角的坐标
- int x1 = plantX + 10;//植物的左边界
- int x2 = plantX + 60;//植物的右边界
- int x3 = zms[i].x + 80;//僵尸的左边界
- if (x3 > x1 && x3 < x2)
- {
- if (map[samerow][k].catched)//植物被捕获
- {
- map[samerow][k].deadtime ++;
- if (map[samerow][k].deadtime > 100)
- {
- map[samerow][k].deadtime = 0;
- map[samerow][k].type = 0;
- zms[i].eating = false;
- zms[i].frameIndex = 0;
- zms[i].speed = 2;
- map[samerow][k].catched = false;
- }
- }
- else
- {
- map[samerow][k].catched = true;
- map[samerow][k].deadtime = 0;//死亡倒计时
- zms[i].eating = true;//僵尸开吃
- zms[i].speed = 0;
- zms[i].frameIndex = 0;
- }
- }
- }
- }
- }
- void collisioncheck()//碰撞检测
- {
- checkbullet();//子弹碰撞
- checkeat();//僵尸捕获植物
- }
- void updatePlant()//更新植物
- {
- static int count = 0;//频度控制
- if (++count > 2)
- {
- count = 0;
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 9; j++)
- {
- if (map[i][j].type > 0)
- {
- map[i][j].frameindex++;//从第一张图片到最后一张图片
- //判断能否打开文件,打不开证明是最后一帧
- if (imgPLANT[map[i][j].type - 1][map[i][j].frameindex] == NULL)
- {
- map[i][j].frameindex = 0;//再初始化,从第一帧重新开始
- }
- }
- }
- }
- }
- }
- void updateGame()//在每次循环后更改相应的参数
- {
- updatePlant();//更新植物
-
- creatsunshine();//创建阳光
- updatesunshine();//更新阳光
-
- creatzomb();//创建僵尸
- updatezomb();//更新僵尸
-
- shoot();//发射豌豆
- updatebullets();//更新豌豆
-
- collisioncheck();//碰撞检测
- }
- void startUI()//起始菜单
- {
- mciSendString("play res/bg.mp3", 0, 0, NULL);
- IMAGE imgBG, imgMenu1, imgMenu2;
- loadimage(&imgBG, "res/menu.png");
- loadimage(&imgMenu1, "res/menu1.png");//选中选项卡
- loadimage(&imgMenu2, "res/menu2.png");//未选中选项卡
- int flag = 0;//表示是否选中,选中则为1
-
- 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>75 &&
- msg.y < 75 + 140)
- {
- flag = 1;//表示选中
- }
- else if (msg.message == WM_LBUTTONUP && flag)
- {
- EndBatchDraw();
- break;
- }
- }
- EndBatchDraw();
- }
- mciSendString("close res/bg.mp3", 0, 0, 0);
- }
- void viewScene()//视角移动
- {
- int xMax = WIN_WIDTH - imgBg.getwidth();//窗口宽-图片宽(坐标为负数)
- vector2 point[9] = { {550,80},{530,160},{630,170},{530,200},{515,270},
- {565,370},{605,340},{705,280},{690,340} };//阅览僵尸站位(可修改)
-
- int index[9];
- for (int j = 0; j < 9; j++)
- {
- index[j] = rand() % 11;//僵尸起始帧序号
- }
-
- int scenecount = 0;
-
- //画面巡视
- for (int i = 0; i >= xMax; i -= 2)
- {
- BeginBatchDraw();
-
- putimage(i, 0, &imgBg);
- scenecount++;
- for (int k = 0; k < 9; k++)//绘制僵尸站位
- {
- putimagePNG(point[k].x - xMax + i, point[k].y, &imgzmStand[index[k]]);
- if (scenecount >= 10)
- index[k] = (index[k] + 1) % 11;
- }
- if (scenecount >= 10)
- scenecount = 0;
-
- EndBatchDraw();
- Sleep(5);
- }
-
- //画面停留时间(可修改)
- for (int i = 0; i < 50; i++)
- {
- BeginBatchDraw();
-
- putimage(xMax, 0, &imgBg);
- for (int k = 0; k < 9; k++)
- {
- putimagePNG(point[k].x, point[k].y, &imgzmStand[index[k]]);
- index[k] = (index[k] + 1) % 11;
- }
-
- EndBatchDraw();
- Sleep(30);
- }
-
- //画面返回
- for (int k = xMax; k <= -112; k += 2)
- {
- BeginBatchDraw();
-
- putimage(k, 0, &imgBg);
- scenecount++;
- for (int i = 0; i < 9; i++)
- {
- putimagePNG(point[i].x - xMax + k, point[i].y, &imgzmStand[index[i]]);
- if (scenecount >= 10)
- index[i] = (index[i] + 1) % 11;
- }
- if (scenecount >= 10)
- scenecount = 0;
-
- EndBatchDraw();
- Sleep(5);
- }
- }
- void barsDown()//工具栏下降
- {
- int hight = imgBar.getheight();//获取工具栏的高
- for (int i = -hight; i <= 0; i++)
- {
- BeginBatchDraw();
-
- putimage(-112, 0, &imgBg);
- putimagePNG(250, i, &imgBar);
-
- for (int k = 0; k < PLANT_COUNTS; k++)
- {
- int x = 338 + k * 65;
- int y = 6 + i;
- putimage(x, y, &imgCard[k]);
- }
-
- EndBatchDraw();
- Sleep(10);
- }
- }
- bool checkOver()//游戏结束画面
- {
- bool ret = false;
- if (gameStatus == WIN)//游戏胜利
- {
- mciSendString("play res/win.mp3", 0, 0, NULL);
- Sleep(2000);
- mciSendString("close res/win.mp3", 0, 0, NULL);
- IMAGE* img7 = NULL;
- loadimage(img7, "res/gameWin.png");
- putimage(0, 0, img7);
- ret = true;
- }
- else if (gameStatus == FAIL)
- {
- mciSendString("play res/lose.mp3", 0, 0, NULL);
- Sleep(2000);
- mciSendString("close res/lose.mp3", 0, 0, NULL);
- IMAGE* img9 = NULL;
- loadimage(img9, "res/gameFail.png");
- putimage(0, 0, img9);
- ret = true;
- }
- return ret;
- }
- int main()
- {
- gameInit();
- startUI();
- viewScene();
- barsDown();
-
- int timer = 0;//实现帧等待
- bool flag = true;
-
- while (1)//循环退出条件:用户单击
- {
- userclick();
-
- timer += getDelay();//运行时间间隔
- if (timer > 10)//运行时间间隔大于10毫秒
- {
- flag = true;
- timer = 0;//再初始化
- }
- if (flag)
- {
- flag = false;
- updateWindow();
- updateGame();
- if (checkOver())
- break;
- }
- }
-
- system("pause");//暂停
- return 0;
- }
- #pragma once
- #include <graphics.h>
-
- void putimagePNG(int picture_x, int picture_y, IMAGE* picture);
- int getDelay();
- #include "tools.h"
-
- // 载入PNG图并去透明部分
- void _putimagePNG(int picture_x, int picture_y, IMAGE* picture) //x为载入图片的X坐标,y为Y坐标
- {
- DWORD* dst = GetImageBuffer(); // GetImageBuffer()函数,用于获取绘图设备的显存指针,EASYX自带
- DWORD* draw = GetImageBuffer();
- DWORD* src = GetImageBuffer(picture); //获取picture的显存指针
- int picture_width = picture->getwidth(); //获取picture的宽度,EASYX自带
- int picture_height = picture->getheight(); //获取picture的高度,EASYX自带
- int graphWidth = getwidth(); //获取绘图区的宽度,EASYX自带
- int graphHeight = getheight(); //获取绘图区的高度,EASYX自带
- int dstX = 0; //在显存里像素的角标
-
- // 实现透明贴图 公式: Cp=αp*FP+(1-αp)*BP , 贝叶斯定理来进行点颜色的概率计算
- for (int iy = 0; iy < picture_height; iy++)
- {
- for (int ix = 0; ix < picture_width; ix++)
- {
- int srcX = ix + iy * picture_width; //在显存里像素的角标
- int sa = ((src[srcX] & 0xff000000) >> 24); //0xAArrggbb;AA是透明度
- int sr = ((src[srcX] & 0xff0000) >> 16); //获取RGB里的R
- int sg = ((src[srcX] & 0xff00) >> 8); //G
- int sb = src[srcX] & 0xff; //B
- if (ix >= 0 && ix <= graphWidth && iy >= 0 && iy <= graphHeight && dstX <= graphWidth * graphHeight)
- {
- dstX = (ix + picture_x) + (iy + picture_y) * graphWidth; //在显存里像素的角标
- int dr = ((dst[dstX] & 0xff0000) >> 16);
- int dg = ((dst[dstX] & 0xff00) >> 8);
- int db = dst[dstX] & 0xff;
- draw[dstX] = ((sr * sa / 255 + dr * (255 - sa) / 255) << 16)
- | ((sg * sa / 255 + dg * (255 - sa) / 255) << 8)
- | (sb * sa / 255 + db * (255 - sa) / 255);
- }
- }
- }
- }
-
- // 适用于 y <0 以及x<0的任何情况
- void putimagePNG(int x, int y, IMAGE* picture) {
-
- IMAGE imgTmp, imgTmp2, imgTmp3;
- int winWidth = getwidth();
- int winHeight = getheight();
- if (y < 0) {
- SetWorkingImage(picture);
- getimage(&imgTmp, 0, -y,
- picture->getwidth(), picture->getheight() + y);
- SetWorkingImage();
- y = 0;
- picture = &imgTmp;
- }
- else if (y >= getheight() || x >= getwidth()) {
- return;
- }
- else if (y + picture->getheight() > winHeight) {
- SetWorkingImage(picture);
- getimage(&imgTmp, x, y, picture->getwidth(), winHeight - y);
- SetWorkingImage();
- picture = &imgTmp;
- }
-
- if (x < 0) {
- SetWorkingImage(picture);
- getimage(&imgTmp2, -x, 0, picture->getwidth() + x, picture->getheight());
- SetWorkingImage();
- x = 0;
- picture = &imgTmp2;
- }
-
- if (x > winWidth - picture->getwidth()) {
- SetWorkingImage(picture);
- getimage(&imgTmp3, 0, 0, winWidth - x, picture->getheight());
- SetWorkingImage();
- picture = &imgTmp3;
- }
-
-
- _putimagePNG(x, y, picture);
- }
-
- int getDelay() {//时间差函数
- static unsigned long long lastTime = 0;
- unsigned long long currentTime = GetTickCount();//获取游戏开始到现在的时间
- if (lastTime == 0) {
- lastTime = currentTime;
- return 0;
- }
- else {
- int ret = currentTime - lastTime;
- lastTime = currentTime;
- return ret;//返回此次调用与上次调用的时间差
- }
- }
- //头文件要求
- #include <cmath>
- #include "vector2.h"
-
- //加法
- vector2 operator +(vector2 x, vector2 y) {
- return vector2(x.x + y.x, x.y + y.y );
- }
-
- //减法
- vector2 operator -(vector2 x, vector2 y) {
- return vector2(x.x - y.x, x.y - y.y);
- }
-
- // 乘法
- vector2 operator *(vector2 x, vector2 y) {
- return vector2(x.x * y.x - x.y * y.y, x.y * y.x + x.x * y.y);
- }
-
- // 乘法
- vector2 operator *(vector2 y, float x) {
- return vector2(x*y.x, x*y.y);
- }
-
- vector2 operator *(float x, vector2 y) {
- return vector2(x * y.x, x * y.y);
- }
-
- //叉积
- long long cross(vector2 x, vector2 y) { return x.y * y.x - x.x * y.y; }
-
- //数量积 点积
- long long dot(vector2 x, vector2 y) { return x.x * y.x + x.y * y.y; }
-
- //四舍五入除法
- long long dv(long long a, long long b) {//注意重名!!!
- return b < 0 ? dv(-a, -b)
- : (a < 0 ? -dv(-a, b)
- : (a + b / 2) / b);
- }
-
- //模长平方
- long long len(vector2 x) { return x.x * x.x + x.y * x.y; }
-
- //模长
- long long dis(vector2 x) { return sqrt(x.x * x.x + x.y * x.y); }
-
- //向量除法
- vector2 operator /(vector2 x, vector2 y) {
- long long l = len(y);
- return vector2(dv(dot(x, y), l), dv(cross(x, y), l));
- }
-
- //向量膜
- vector2 operator %(vector2 x, vector2 y) { return x - ((x / y) * y); }
-
- //向量GCD
- vector2 gcd(vector2 x, vector2 y) { return len(y) ? gcd(y, x % y) : x; }
-
-
- vector2 calcBezierPoint(float t, vector2 p0, vector2 p1, vector2 p2, vector2 p3) {
- float u = 1 - t;
- float tt = t * t;
- float uu = u * u;
- float uuu = uu * u;
- float ttt = tt * t;
-
- vector2 p = uuu * p0;
- p = p + 3 * uu * t * p1;
- p = p + 3 * u * tt * p2;
- p = p + ttt * p3;
-
- return p;
- }
- #pragma once
-
- //头文件要求
- #include <cmath>
-
- struct vector2 {
- vector2(int _x=0, int _y=0) :x(_x), y(_y) {}
- vector2(int* data) :x(data[0]), y(data[1]){}
- long long x, y;
- };
-
- //加法
- vector2 operator +(vector2 x, vector2 y);
-
- //减法
- vector2 operator -(vector2 x, vector2 y);
-
- // 乘法
- vector2 operator *(vector2 x, vector2 y);
- vector2 operator *(vector2, float);
- vector2 operator *(float, vector2);
-
- //叉积
- long long cross(vector2 x, vector2 y);
-
- //数量积 点积
- long long dot(vector2 x, vector2 y);
-
- //四舍五入除法
- long long dv(long long a, long long b);
-
-
- //模长平方
- long long len(vector2 x);
-
- //模长
- long long dis(vector2 x);
-
- //向量除法
- vector2 operator /(vector2 x, vector2 y);
-
- //向量膜
- vector2 operator %(vector2 x, vector2 y);
-
- //向量GCD
- vector2 gcd(vector2 x, vector2 y);
-
- //贝塞尔曲线
- vector2 calcBezierPoint(float t, vector2 p0, vector2 p1, vector2 p2, vector2 p3);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。