C语言课设:植物大战僵尸
温馨提示:这篇文章已超过428天没有更新,请注意相关的内容是否还可用!
文章是作者C语言项目的开发日志,主要介绍与该项目有关的函数,代码实现以及在开发过程中遇到的问题。IDE:VS2022。
资源包以及工具文件已上传,需要的朋友可自取。
C语言:植物大战僵尸库函数解析-CSDN博客
目录
资源包以及工具文件已上传,需要的朋友可自取。
1.EasyX图形库下载(确保在开始之前已经安装)
1.2进入官网:EasyX Graphics Library for C++
1.3安装后打开文件,点下一步
1.4将EasyX安装到正在使用的编译器中,带点击安装即可
1.5为了确保安装成功,需要打开编译器创建新项目输入以下代码
2.源文件main.cpp
2.1判断文件能否打开:fileExist()
2.2游戏初始化:gameinit()
2.3渲染僵尸:drawzm()
2.4渲染阳光:drawsunshine()
2.5渲染卡牌:drawCard()
2.6渲染种植后的植物:drawPlantMap()
2.7渲染鼠标拖动的植物:drawPlantMove()
2.8渲染豌豆子弹:drawPea()
2.9显示左上角阳光数:outsunshine()
2.10渲染:updatewindow()
2.11收集阳光:collectsunshine()
2.12鼠标操作:userclick()
2.13创建阳光:creatsunshine()
2.14更新阳光参数:updatesunshine()
2.15创建僵尸:creatzomb()
2.16更新僵尸参数:updatezomb()
2.17发射豌豆:shoot()
2.18更新子弹参数:updatebullets()
2.19豌豆->僵尸:checkbullet()
2.20僵尸->植物:checkeat()
2.21碰撞检测:collisioncheck()
2.22更新植物参数:updatePlant()
2.23更改参数:updategame()
2.24起始菜单:startUI()
2.25画面巡视:viewScene()
2.26工具栏下降:barsDown()
2.27判定游戏结束:checkOver()
2.28主函数
3.头文件tools.h(工具文件)
4.源文件tools.cpp(工具文件)
5.源文件vector2.cpp(工具文件:实现贝塞尔曲线)
6.头文件vector2.h(工具文件:实现贝塞尔曲线)
1.EasyX图形库下载(确保在开始之前已经安装)
1.2进入官网:EasyX Graphics Library for C++
1.3安装后打开文件,点下一步
1.4将EasyX安装到正在使用的编译器中,带点击安装即可
注意:easyx只支持C++,所以有些编译器无法下载
1.5为了确保安装成功,需要打开编译器创建新项目输入以下代码
#include // 引用 EasyX 绘图库头文件
#include
int main(){
initgraph(640, 480);
circle(320, 240, 100);
_getch();
closegraph();
return 0;
}
运行结果证明安装成功
2.源文件main.cpp
#include
#include //easyx图形库头文件
#include
#include "tools.h"//图片透明函数接口
#include
#include //播放音乐头文件
#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 getheight(), img3);
}
}
}
void drawsunshine()//渲染阳光
{
for (int i = 0; i 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 x > x && msg->xy>y && msg->y 338 && msg.x 144 && msg.y > 179 && msg.y = fre)
{
fre = 100 + rand() % 200;//每200到399毫秒创建一个阳光
count = 0;
int i = 0;
for (i = 0; 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 100)//生产阳光的时间间隔
{
map[i][j].timer = 0;//归零
int k;//找到第k个可用阳光
for (k = 0; 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 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 = 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 2)
{
count = 0;
for (int i = 0; i = 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 15)//调节豌豆发射的频率(可调节)
{
map[i][j].shootTimer = 0;
int k;
for (k = 0; k getwidth() - 10;
bullets[k].y = peay + 5;
}
}
}
}
}
}
void updatebullets()//更新子弹的数据
{
int bulletMax = sizeof(bullets) / sizeof(bullets[0]);//子弹数目
for (int i = 0; i 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 x1 && x x1 && x3 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 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 75 &&
msg.y = xMax; i -= 2)
{
BeginBatchDraw();
putimage(i, 0, &imgBg);
scenecount++;
for (int k = 0; k = 10)
index[k] = (index[k] + 1) % 11;
}
if (scenecount >= 10)
scenecount = 0;
EndBatchDraw();
Sleep(5);
}
//画面停留时间(可修改)
for (int i = 0; i = 10)
scenecount = 0;
EndBatchDraw();
Sleep(5);
}
}
void barsDown()//工具栏下降
{
int hight = imgBar.getheight();//获取工具栏的高
for (int i = -hight; i 10)//运行时间间隔大于10毫秒
{
flag = true;
timer = 0;//再初始化
}
if (flag)
{
flag = false;
updateWindow();
updateGame();
if (checkOver())
break;
}
}
system("pause");//暂停
return 0;
}
2.1判断文件能否打开:fileExist()
bool fileExist(char* name)//判断文件能否打开
{
FILE* fp = fopen(name, "r");//以“读”的形式打开文件
//fopen函数能打开文件返回文件路径,打不开文件返回NULL,所以需要定义指针变量
if (fp == NULL)
return false;//表示文件打不开
else
fclose(fp);//关闭文件
return true;//表示文件能打开
}
2.2游戏初始化:gameinit()
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
2.3渲染僵尸:drawzm()
void drawzm()//渲染僵尸
{
int zmMax = sizeof(zms) / sizeof(zms[0]);
for (int i = 0; i getheight(), img3);
}
}
}
2.4渲染阳光:drawsunshine()
void drawsunshine()//渲染阳光
{
for (int i = 0; i
2.5渲染卡牌:drawCard()
void drawCard()//渲染卡牌
{
for (int i = 0; i
2.6渲染种植后的植物:drawPlantMap()
void drawPlantMap()//渲染种植后的植物
{
for (int i = 0; i 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]);
}
}
}
}
2.7渲染鼠标拖动的植物:drawPlantMove()
void drawPlantMove()//渲染鼠标拖动的植物
{
if (curPLANT)//渲染拖动时的植物模型
{
IMAGE* img1 = imgPLANT[curPLANT - 1][0];
//实现无背景贴图,让鼠标位于植物的正中间
putimagePNG(curX - img1->getwidth() / 2, curY - img1->getheight() / 2, img1);//取植物晃动的第一张图片
}
}
2.8渲染豌豆子弹:drawPea()
void drawPea()渲染豌豆
{
int bulletMax = sizeof(bullets) / sizeof(bullets[0]);
for (int i = 0; i
2.9显示左上角阳光数:outsunshine()
void outsunshine()//显示左上角阳光数
{
char scoreText[8];
sprintf_s(scoreText, sizeof(scoreText), "%d", sunshine);//将阳光格式化为字符串并加载进数组
outtextxy(279, 67, scoreText);//在指定位置输出字符串
}
2.10渲染:updatewindow()
void updateWindow()//渲染显示图片
{
BeginBatchDraw();//开始缓冲
putimage(-112, 0, &imgBg);//从最左上角显示草坪
putimagePNG(250, 0, &imgBar);//植物栏
drawCard();//渲染卡牌
drawPlantMap();//渲染种植后的植物
drawPlantMove();//渲染拖动时的植物
drawsunshine();//渲染阳光
drawzm();//渲染僵尸
drawPea();//渲染豌豆
outsunshine();//显示左上角阳光数
EndBatchDraw();//结束双缓冲
}
2.11收集阳光:collectsunshine()
void collectsunshine(ExMessage* msg)//实现收集阳光
{
int width = imgsunshine[0].getwidth();//获取阳光球的宽
int hight = imgsunshine[0].getheight();//获取阳光球的高
for (int i = 0; i x > x && msg->xy>y && msg->y
2.12鼠标操作:userclick()
void userclick()//实现鼠标相关操作
{
ExMessage msg;//结构体变量
static int status = 0;//判断状态
if (peekmessage(&msg))//判断msg的范围内是否有信息
{
if (msg.message == WM_LBUTTONDOWN)//鼠标左键按下
{
if (msg.x > 338 && msg.x 144 && msg.y > 179 && msg.y
2.13创建阳光:creatsunshine()
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)
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 100)//生产阳光的时间间隔
{
map[i][j].timer = 0;//归零
int k;//找到第k个可用阳光
for (k = 0; 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;
}
}
}
}
}
}
2.14更新阳光参数:updatesunshine()
void updatesunshine()//更新阳光状态:使用——>未使用,动画帧再初始化
{
for (int i = 0; i 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
2.15创建僵尸:creatzomb()
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
2.16更新僵尸参数:updatezomb()
void updatezomb()//更新僵尸的数据
{
int zmMax = sizeof(zms) / sizeof(zms[0]);//僵尸的数量
static int count = 0;
count++;
if (count > 2)
{
count = 0;
for (int i = 0; i = 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;//防止越界
}
}
}
}
}
2.17发射豌豆:shoot()
void shoot()//发射豌豆
{
static int count6 = 0;
if (++count6 15)//调节豌豆发射的频率(可调节)
{
map[i][j].shootTimer = 0;
int k;
for (k = 0; k getwidth() - 10;
bullets[k].y = peay + 5;
}
}
}
}
}
}
2.18更新子弹参数:updatebullets()
void updatebullets()//更新子弹的数据
{
int bulletMax = sizeof(bullets) / sizeof(bullets[0]);//子弹数目
for (int i = 0; i WIN_WIDTH)
{
bullets[i].used = false;//对超出边界的豌豆回收
}
if (bullets[i].blast)//子弹符合爆炸条件
{
bullets[i].frameindex++;
if (bullets[i].frameindex >= 4)
{
bullets[i].used = false;//爆炸结束
}
}
}
}
}
2.19豌豆->僵尸:checkbullet()
void checkbullet()//豌豆->僵尸
{
int bulletMax = sizeof(bullets) / sizeof(bullets[0]);
int zombMax = sizeof(zms) / sizeof(zms[0]);
for (int i = 0; i x1 && x
void checkeat()//僵尸->植物
{
int zmcount = sizeof(zms) / sizeof(zms[0]);
for (int i = 0; i x1 && x3 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;
}
}
}
}
}
2.21碰撞检测:collisioncheck()
void collisioncheck()//碰撞检测
{
checkbullet();//子弹碰撞
checkeat();//僵尸捕获植物
}
2.22更新植物参数:updatePlant()
void updatePlant()//更新植物
{
static int count = 0;//频度控制
if (++count > 2)
{
count = 0;
for (int i = 0; i 0)
{
map[i][j].frameindex++;//从第一张图片到最后一张图片
//判断能否打开文件,打不开证明是最后一帧
if (imgPLANT[map[i][j].type - 1][map[i][j].frameindex] == NULL)
{
map[i][j].frameindex = 0;//再初始化,从第一帧重新开始
}
}
}
}
}
}
2.23更改参数:updategame()
void updateGame()//在每次循环后更改相应的参数
{
updatePlant();//更新植物
creatsunshine();//创建阳光
updatesunshine();//更新阳光
creatzomb();//创建僵尸
updatezomb();//更新僵尸
shoot();//发射豌豆
updatebullets();//更新豌豆
collisioncheck();//碰撞检测
}
2.24起始菜单:startUI()
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 75 &&
msg.y
2.25画面巡视:viewScene()
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 = xMax; i -= 2)
{
BeginBatchDraw();
putimage(i, 0, &imgBg);
scenecount++;
for (int k = 0; k = 10)
index[k] = (index[k] + 1) % 11;
}
if (scenecount >= 10)
scenecount = 0;
EndBatchDraw();
Sleep(5);
}
//画面停留时间(可修改)
for (int i = 0; i = 10)
scenecount = 0;
EndBatchDraw();
Sleep(5);
}
}
2.26工具栏下降:barsDown()
void barsDown()//工具栏下降
{
int hight = imgBar.getheight();//获取工具栏的高
for (int i = -hight; i 10)//运行时间间隔大于10毫秒
{
flag = true;
timer = 0;//再初始化
}
if (flag)
{
flag = false;
updateWindow();
updateGame();
if (checkOver())
break;
}
}
system("pause");//暂停
return 0;
}
3.头文件tools.h(工具文件)
#pragma once
#include
void putimagePNG(int picture_x, int picture_y, IMAGE* picture);
int getDelay();
4.源文件tools.cpp(工具文件)
#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 > 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 = 0 && iy 16);
int dg = ((dst[dstX] & 0xff00) >> 8);
int db = dst[dstX] & 0xff;
draw[dstX] = ((sr * sa / 255 + dr * (255 - sa) / 255) = getwidth()) {
return;
}
else if (y + picture->getheight() > winHeight) {
SetWorkingImage(picture);
getimage(&imgTmp, x, y, picture->getwidth(), winHeight - y);
SetWorkingImage();
picture = &imgTmp;
}
if (x 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;//返回此次调用与上次调用的时间差
}
}
5.源文件vector2.cpp(工具文件:实现贝塞尔曲线)
//头文件要求
#include
#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
6.头文件vector2.h(工具文件:实现贝塞尔曲线)
#pragma once
//头文件要求
#include
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);




