C语言游戏实战(12):植物大战僵尸(坤版)

植物大战僵尸

前言:

本游戏使用C语言和easyx图形库编写,通过这个项目我们可以深度的掌握C语言的各种语言特性和高级开发技巧,以及锻炼我们独立的项目开发能力,

在开始编写代码之前,我们需要先了解一下游戏的基本规则和功能:

游戏界面:游戏界面是一个矩形区域,玩家可以在区域内进行植物的放置和铲除等操作。

僵尸:僵尸会从游戏界面的右侧向左测移动,靠近植物后会停下来吃植物。

植物:不同的植物有不同的功能,在这里我们可以将植物分为三大类:

1. 生产型植物(如太阳花):这种植物的特点是在一定的时间生产出太阳,以增加太阳的产量。

2.发射型植物(如豌豆射手):这种植物的特点是发射相应类型的子弹,对僵尸产生伤害和特定的效果。

3. 爆炸性植物(火爆辣椒):这种植物的特点是对一定区域的所有僵尸产生高额伤害。(一次性植物)

接下来,我们将通过以下几个步骤来实现这个游戏:

初始化游戏界面。

实现僵尸的创建、僵尸的更新、僵尸吃植物的检测。

实现的植物的放置。

实现植物的功能:

对于生产型植物我们需要写出阳光的生产,以及阳光的收集操作。

对于发射型植物我们需要判断植物是否发射子弹,发射子弹后还需更新子弹、检测子弹与僵尸的碰撞。

对于爆炸型植物,我们需要判断僵尸是否在爆炸范围内,然后杀死在爆炸范围内的僵尸。

1. 初始化游戏界面

我们需要先将游戏地图、卡牌和卡牌槽绘制出来。可以利用windows自带的画图工具测出游戏地图的位置,从而将这些卡牌和卡牌槽放置到合适的位置。

//地图
putimage(-150, 0, &img);
//卡牌槽
putimagePNG(80, -10, &imgBar);	
//植物卡牌for (int i = 0; i < PLANT_COUNT + 2; i++){if(i==PLANT_COUNT)putimagePNG(163 + i * 65 + 8, -5, &imgCards[i]);elseputimagePNG(163 + i * 65, 0, &imgCards[i]);}

2.  放置植物

因为需要在这个9*5的草地区域内放置植物,所以我们得计算出每个草方块的位置。我利用画图软件测出每个草方块大约是长81、宽100个像素点。 第一个草方块距离游戏窗口大约101个像素点。

这样就可以得出每个草方块的位置:256 - 150 + col * 81;100 + row * 100 - 15; 

每个植物都有相似的特性,所以我们需要写一个放置植物信息的结构体。然后创建一个5*9的结构体数组,用来表示每个草方块上的植物。

enum {WAN_DOU, TAI_YANG, LA_JIAO, KUN_KUN, JIAN_GUO,HAN_BING_WAN_DOU, YING_TAO, SAN_TOU_WAN_DOU, PLANT_COUNT
};
struct plant
{int type;//植物类型int frameIndex;//植物动作帧//bool catched;//是否被吃int blood;//血量int shootTime;//植物子弹的射速int timer;//阳光生产的时间int x, y;//植物坐标bool shoot;//判断植物是否处于发射状态
};
struct plant map[5][9];

 在 Windows 编程中,我们可以利用下面的变量获取鼠标信息。

ExMessage msg;
msg.message
ExMessage msg;
static int status = 0;
if (peekmessage(&msg))//判断有没有消息
{if (msg.message == WM_LBUTTONDOWN)//左键按下{//鼠标是否在卡牌的位置按下if (msg.x > 163 && msg.x < 163 + 65 * (PLANT_COUNT+2) && msg.y < 96){//mciSendString("play res/audio/bleep.mp3 alias BGM4", NULL, NULL, NULL);PlaySound("res/audio/bleep.wav", NULL, SND_FILENAME | SND_ASYNC);//PlaySound("res/sunshine.wav", NULL, SND_FILENAME | SND_ASYNC);int index = (msg.x - 163) / 65;//坤坤/*	if (index + 1 == KUN_KUN && sunshine >= 100){curPlant = index + 1;status = 1;curX = msg.x;curY = msg.y;sunshine -= 100;}*/curPlant = index + 1;status = 1;curX = msg.x;curY = msg.y;}}else if (msg.message == WM_MOUSEMOVE && status == 1)//鼠标移动{curX = msg.x;curY = msg.y;}else if (msg.message == WM_LBUTTONUP)//鼠标放下{if (msg.x > 256 - 150 && msg.x < Wide - 70 && msg.y > 100 && msg.y < 590){int row = (msg.y - 100) / 102;int col = (msg.x - 256 + 150 ) / 81;if (map[row][col].type == 0 && curPlant != PLANT_COUNT + 1 && curPlant != PLANT_COUNT + 2){//printf("%d\n", map[col][row].type);map[row][col].type = curPlant;map[row][col].frameIndex = 0;if(curPlant!=0)PlaySound("res/audio/coffee.wav", NULL, SND_FILENAME | SND_ASYNC);if (curPlant == 5)map[row][col].blood = KUNKUN_BLOOD * 50;elsemap[row][col].blood = KUNKUN_BLOOD;map[row][col].shootTime = 0;map[row][col].shoot = false;map[row][col].x = 256 - 150 + col * 81;map[row][col].y = 100 + row * 100 - 15;}else if (map[row][col].type != 0 && curPlant == PLANT_COUNT + 1){PlaySound("res/audio/coffee.wav", NULL, SND_FILENAME | SND_ASYNC);map[row][col].type = 0;map[row][col].blood = 0;}}}else if (msg.message == WM_RBUTTONDOWN)//鼠标右键{curPlant = 0;status = 0;}

3. 僵尸

 3.1 创建僵尸

僵尸也是和植物一样需要创建一个结构体存放信息。然后创建一个结构体数组,作为一个僵尸池,当需要创建一个僵尸时,我们就从这个池里取一个未被使用的僵尸。

struct zm
{int x, y;//僵尸的坐标int frameIndex;//僵尸动作帧bool used;//僵尸是否被使用int speed;//僵尸每一次移动的位移int row;//僵尸所在行int blood;//僵尸血量bool dead;//僵尸是否死亡bool eating;//僵尸是否在吃植物bool boom;//僵尸是否被炸死int zmSpeed;//僵尸的移动快慢
};
struct zm zms[ZM_MAX];
//找一个可用的僵尸
int i;
for (i = 0; i < zmMax && zms[i].used && zms[i].dead == false; i++);

到一定的时间就创建一个僵尸。


void createZM()
{if (zmCount >= zmCount_max)return;static int zmFre = 500;static int count = 0;//控制僵尸的生成快慢count++;if (count > zmFre){count = 0;zmFre = 200;int i = 0;int zmMax = sizeof(zms) / sizeof(zms[0]);for (i = 0; i < zmMax && zms[i].used && zms[i].dead == false; i++);if (i < zmMax){memset(&zms[i], 0, sizeof(zms[i]));zms[i].used = true;zms[i].speed = 2;zms[i].row = rand() % 5;zms[i].y = 100 + (zms[i].row) * 100 + 70;zms[i].x = Wide;zms[i].blood = ORDINARY_ZM_BLOOD;zms[i].dead = false;zms[i].boom = false;zms[i].zmSpeed = 4;zmCount++;}}
}

3. 2 检测僵尸对植物的伤害检测

僵尸靠近植物就将僵尸置为吃东西的状态,减植物的血量,当植物的血量小于等于0时,就去除植物。

void checkZM2Zhiwu()
{char name[64];int zCount = sizeof(zms) / sizeof(zms[0]);for (int i = 0; i < zCount; i++){//killCount = 0;if (zms[i].dead)continue;//zms[i].chomptime = 0;int row = zms[i].row;for (int j = 0; j < 9; j++){//if (map[row][j].type == 0)continue;//int zhiwuX = 101 + j * 81;int x1 = zhiwuX;int x2 = zhiwuX + 81;int x3 = zms[i].x + 100;if (x3 >= x1 && x3 <= x2){if (map[row][j].blood <= 0 || (map[row][j].type == 0 && zms[i].eating != false)){map[row][j].blood = 0;map[row][j].type = 0;zms[i].eating = false;//zms[i].frameIndex = 0;zms[i].speed = 3;}else if (map[row][j].type != 0){//mciSendString("play ZM_BGM repeat", NULL, NULL, NULL);//mciSendString("play name repeat", NULL, 0, NULL);zms[i].eating = true;zms[i].speed = 0;if (map[row][j].type != 3 && map[row][j].type != 7)map[row][j].blood--;//zms[i].frameIndex = 0;}}else if (x3 > 830){zms[i].eating = false;zms[i].speed = 3;}}}
}

4. 植物子弹

4.1 发射子弹

当僵尸出现在游戏界面时,与该僵尸同一行的发射性植物就发射出子弹。

同样需要创建一个结构体存放子弹的信息,然后创建一个子弹池。

//子弹
struct bullet
{double x, y;//子弹的坐标bool used;//子弹是否被使用int row;//子弹所在行int speed;//子弹速度bool blast;//是否发生爆炸int frameIndex;//帧序号
};
//坤坤
struct bullet bullets[1000];

 以坤坤为例:

void shoot()
{int lines[5] = { 0 };int bulletMax = sizeof(bullets) / sizeof(bullets[0]);int zmCount = sizeof(zms) / sizeof(zms[0]);int dangerX = Wide - 80;for (int i = 0; i < zmCount; i++){if (zms[i].dead == false && zms[i].x < dangerX && zms[i].x>100){lines[zms[i].row] = 1;}}for (int i = 0; i < 5; i++){for (int j = 0; j < 9; j++){//坤坤if (map[i][j].type == KUN_KUN + 1 && lines[i]){map[i][j].shootTime++;if (map[i][j].shootTime > 20){map[i][j].shootTime = 0;//子弹池int k;for (k = 0; k < bulletMax && bullets[k].used; k++);if (k < bulletMax){map[i][j].frameIndex = 3;bullets[k].used = true;bullets[k].row = i;bullets[k].speed = 10;bullets[k].blast = false;bullets[k].frameIndex = 0;int zwX = 256 - 150 + j * 81;int zwY = 100 + i * 100 - 15;bullets[k].x = zwX + imgPlant[map[i][j].type - 1][0]->getwidth() - 10;bullets[k].y = zwY + 5;}}}}}
}

4. 2  更新子弹

不断的改变加子弹的横坐标,当子弹出游戏界面时,子弹就被置为未被使用。

以坤坤为例:

void updateBullets_kunkun()
{int countMax = sizeof(bullets) / sizeof(bullets[0]);for (int i = 0; i < countMax; i++){if (bullets[i].used){bullets[i].x += bullets[i].speed;if (bullets[i].x > Wide){bullets[i].used = false;}}}
}

 4.3 检测子弹对僵尸的伤害

当子弹靠近僵尸时减去僵尸的一定量血量,然后对僵尸造成一定的效果(例如,坤坤子弹的效果是击退僵尸,那么我们只需要更改僵尸的横坐标即可。),最后将子弹置为未被使用。

以坤坤为例:

void checkBullet2Zm_kunkun()
{int bCount = sizeof(bullets) / sizeof(bullets[0]);int zCount = sizeof(zms) / sizeof(zms[0]);for (int i = 0; i < bCount; i++){{for (int j = 0; j < zCount; 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].row == zms[j].row && x > x1 && x < x2 && bullets[i].used) {zms[j].blood -= 20;zms[j].x += 1;bullets[i].blast = true;bullets[i].speed = 0;//bullets[i].x = 0;if (zms[j].blood <= 0){killCount++;zms[j].dead = true;zms[j].speed = 0;zms[j].frameIndex = 0;}break;}}}}
}

 5. 爆炸性植物

爆炸性植物是一次性的,当植物的动作帧执行完最后一帧后植物就置为死亡,在爆炸范围内的僵尸也死亡。

以火爆辣椒为例:

void checkBoom2Zm()
{int zCount = sizeof(zms) / sizeof(zms[0]);for (int i = 0; i < 5; i++){for (int j = 0; j < 9; j++){//火爆辣椒else if (map[i][j].type == LA_JIAO + 1){if (map[i][j].frameIndex > 7){for (int k = 0; k < zCount; k++){if (zms[k].used == false)continue;if (zms[k].row == i && zms[k].x < Wide - 80 - 70 && zms[k].dead == false){killCount++;zms[k].boom = true;zms[k].dead = true;zms[k].speed = 0;zms[k].frameIndex = 0;zms[k].blood = 0;}}if (map[i][j].frameIndex > 14){map[i][j].type = 0;map[i][j].frameIndex = 0;}}}}}
}

6. 小推车

 创建存放小车信息的结构体数组,小推车一条路一辆共5辆,所以我们只需写一个大小为5的结构体数组即可。

//小推车
struct car
{bool move;//是否处于移动状态int x, y;//位置bool used;//是否被使用
};
struct car cars[5];

放置小推车 

	for (int i = 0; i < 5; i++){cars[i].x = 50;cars[i].y = 100 + i * 100 - 15;cars[i].used = true;cars[i].move = false;}

检测小推车对小车的伤害 

当僵尸的横坐标小于等于小推车最左端的坐标时,小车置为移动状态,处于小推车左边的僵尸死亡。

void checkcars2Zm()
{for (int i = 0; i < 5; i++){int carsX = cars[i].x + 70;for (int j = 0; j < ZM_MAX; j++){if (zms[j].used && zms[j].dead == false && zms[j].row == i){int zmX = zms[j].x + 80;if (carsX > zmX && cars[i].used){if (cars[i].move == false)cars[i].move = true;else{killCount++;zms[j].dead = true;zms[j].speed = 0;zms[j].frameIndex = 0;}}}}}
}

更新小推车的位置

当小车被置为移动时,小推车开始移动。

void updatecar()
{for (int i = 0; i < 5; i++){if (cars[i].move){cars[i].x += 20;}if (cars[i].x > Wide){cars[i].move = false;cars[i].used = false;}}
}

源码:

test.cpp文件: 

#include"game.h"//判断文件是否存在
bool fileExist(const char* name)
{FILE* pf = fopen(name, "r");if (pf == NULL){return false;}else{fclose(pf);return true;}
}//初始化豌豆子弹的帧图片数组
void bulletsInit()
{//坤坤篮球loadimage(&imgBulletNormal, "res/bullets/basketball.png", 40, 40);memset(bullets, 0, sizeof(bullets));//初始化豌豆子弹的帧图片数组loadimage(&imgBallBlast[3], "res/bullets/PeaNormalExplode/PeaNormalExplode_0.png");for (int i = 0; i < 3; i++){float k = (i + 1) * 0.2;loadimage(&imgBallBlast[i], "res/bullets/PeaNormalExplode/PeaNormalExplode_0.png",imgBallBlast[3].getwidth() * k,imgBallBlast[3].getheight() * k, true);}//豌豆子弹loadimage(&imgBulletNormal_wandou, "res/bullets/bullet_normal.png");memset(bullets_wandou, 0, sizeof(bullets_wandou));//初始化豌豆子弹的帧图片数组loadimage(&imgBallBlast_wandou[3], "res/bullets/PeaNormalExplode/PeaNormalExplode_0.png");for (int i = 0; i < 3; i++){float k = (i + 1) * 0.2;loadimage(&imgBallBlast_wandou[i], "res/bullets/PeaNormalExplode/PeaNormalExplode_0.png",imgBallBlast_wandou[3].getwidth() * k,imgBallBlast_wandou[3].getheight() * k, true);}//寒冰豌豆子弹loadimage(&imgBulletNormal_hanbing, "res/bullets/PeaIce/PeaIce_0.png");memset(bullets_hanbing, 0, sizeof(bullets_hanbing));//初始化豌豆子弹的帧图片数组loadimage(&imgBallBlast_hanbing[3], "res/bullets/PeaNormalExplode/PeaNormalExplode_0.png");for (int i = 0; i < 3; i++){float k = (i + 1) * 0.2;loadimage(&imgBallBlast_hanbing[i], "res/bullets/PeaNormalExplode/PeaNormalExplode_0.png",imgBallBlast_hanbing[3].getwidth() * k,imgBallBlast_hanbing[3].getheight() * k, true);}//三头豌豆子弹loadimage(&imgBulletNormal_santou, "res/bullets/bullet_normal.png");memset(bullets_santou, 0, sizeof(bullets_santou));//初始化豌豆子弹的帧图片数组loadimage(&imgBallBlast_santou[3], "res/bullets/PeaNormalExplode/PeaNormalExplode_0.png");for (int i = 0; i < 3; i++){float k = (i + 1) * 0.2;loadimage(&imgBallBlast_santou[i], "res/bullets/PeaNormalExplode/PeaNormalExplode_0.png",imgBallBlast_santou[3].getwidth() * k,imgBallBlast_santou[3].getheight() * k, true);}
}void gameInit()
{char name[64];//音效mciSendString("open res/bg.mp3 alias BGM", NULL, NULL, NULL);mciSendString("open res/audio/UraniwaNi.mp3 alias BGM2", NULL, NULL, NULL);mciSendString("open res/audio/WateryGraves.mp3 alias BGM3", NULL, NULL, NULL);mciSendString("open res/audio/readysetplant.mp3 alias BGM4", NULL, NULL, NULL);mciSendString("open res/audio/chomp.mp3 alias ZM_BGM", NULL, NULL, NULL);loadimage(&img, "res/map/map0.jpg");loadimage(&imgBar, "res/bar5.png");//loadimage(&imgnotify, "res/screen/PanelBackground.png");memset(imgPlant, 0, sizeof(imgPlant));memset(map, 0, sizeof(map));memset(cars, 0, sizeof(cars));//开场动画for (int i = 0; i < 29; i++){sprintf_s(name, sizeof(name), "res/yuanshen/%d.png", i + 1);loadimage(&imgopena[i], name, 1196, 670);}//植物卡牌for (int i = 0; i < PLANT_COUNT + 2; i++){sprintf_s(name, sizeof(name), "res/Cards/card_%d.png", i + 1);loadimage(&imgCards[i], name,64,89);for (int j = 0; j < 20; j++){sprintf_s(name, sizeof(name), "res/zhiwu/%d/%d.png",i , j + 1);//先判断文件是否存在if (fileExist(name)){imgPlant[i][j] = new IMAGE;if (i != 3){loadimage(imgPlant[i][j], name);}else{loadimage(imgPlant[i][j], name,65,100);}}else{break;}}}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);}curPlant = 0;sunshine = 50;initgraph(Wide, Hight);//设置字体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);//初始化子弹的帧图片数组bulletsInit();//初始化僵尸数据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);}killCount = 0;zmCount = 0;gameStatus = GOING;//初始化僵尸for (int i = 0; i < 38; i++){sprintf_s(name, sizeof(name), "res/zm_dead/%d.png",i + 1);loadimage(&imgZMDead[i], name);}for (int i = 0; i < 21; i++){sprintf_s(name, sizeof(name), "res/zm_eat/%d.png", i + 1);loadimage(&imgZMEat[i], name);}for (int i = 0; i < 11; i++){sprintf_s(name, sizeof(name), "res/zm_stand/%d.png", i + 1);loadimage(&imgZmStand[i], name);}for (int i = 0; i < 20; i++){sprintf_s(name, sizeof(name), "res/zm_dead2/%d.png", i + 1);loadimage(&imgzmboom[i], name);}//小推车loadimage(&imgcar, "res/Screen/car.png");for (int i = 0; i < 5; i++){cars[i].x = 50;cars[i].y = 100 + i * 100 - 15;cars[i].used = true;cars[i].move = false;}//StartButtonloadimage(&imgready, "res/Screen/Boom.png");
}void drawZm()
{int zmCount = sizeof(zms) / sizeof(zms[0]);for (int i = 0; i < zmCount; i++){if (zms[i].used){//IMAGE* img = (zms[i].dead) ? &imgZMDead[zms[i].frameIndex] : &imgZm[zms[i].frameIndex];IMAGE* img = NULL;if (zms[i].dead){if (zms[i].boom == true)img = &imgzmboom[zms[i].frameIndex];elseimg = &imgZMDead[zms[i].frameIndex];}else if (zms[i].eating) img = &imgZMEat[zms[i].frameIndex];else img = &imgZm[zms[i].frameIndex];putimagePNG(zms[i].x, zms[i].y - img->getheight(), img);}}
}void drawSunshine()
{int ballMax = sizeof(balls) / sizeof(balls[0]);for (int i = 0; i < ballMax; i++){//if (balls[i].used || balls[i].xoff)if(balls[i].used){IMAGE* img = &imgSunshineBall[balls->frameIndex];//putimagePNG(balls[i].x, balls[i].y, img);putimagePNG(balls[i].pCur.x, balls[i].pCur.y, img);}}
}void drawBullets_kunkun()
{int bulletsMax = sizeof(bullets) / sizeof(bullets[0]);for (int i = 0; i < bulletsMax; i++){if (bullets[i].used) {if (bullets[i].blast) {IMAGE* img = &imgBallBlast[bullets[i].frameIndex];putimagePNG(bullets[i].x, bullets[i].y - 10, img);}else {putimagePNG(bullets[i].x, bullets[i].y, &imgBulletNormal);}}}
}void drawBullets_wandou()
{int bulletsMax = sizeof(bullets_wandou) / sizeof(bullets_wandou[0]);for (int i = 0; i < bulletsMax; i++){if (bullets_wandou[i].used) {if (bullets_wandou[i].blast) {IMAGE* img = &imgBallBlast_wandou[bullets_wandou[i].frameIndex];putimagePNG(bullets_wandou[i].x, bullets_wandou[i].y, img);}else {putimagePNG(bullets_wandou[i].x, bullets_wandou[i].y, &imgBulletNormal_wandou);}}}
}void drawBullets_hanbing()
{int bulletsMax = sizeof(bullets_hanbing) / sizeof(bullets_hanbing[0]);for (int i = 0; i < bulletsMax; i++){if (bullets_hanbing[i].used) {if (bullets_hanbing[i].blast) {IMAGE* img = &imgBallBlast_hanbing[bullets_hanbing[i].frameIndex];putimagePNG(bullets_hanbing[i].x, bullets_hanbing[i].y, img);}else {putimagePNG(bullets_hanbing[i].x, bullets_hanbing[i].y, &imgBulletNormal_hanbing);}}}
}void drawBullets_santou()
{int bulletsMax = sizeof(bullets_santou) / sizeof(bullets_santou[0]);for (int i = 0; i < bulletsMax; i++){if (bullets_santou[i].used) {if (bullets_santou[i].blast) {IMAGE* img = &imgBallBlast_santou[bullets_santou[i].frameIndex];putimagePNG(bullets_santou[i].x, bullets_santou[i].y, img);}else {putimagePNG(bullets_santou[i].x, bullets_santou[i].y, &imgBulletNormal_santou);}}}
}void drawBullets()
{//坤坤drawBullets_kunkun();//豌豆drawBullets_wandou();//寒冰豌豆drawBullets_hanbing();//三头豌豆drawBullets_santou();
}void show()//渲染游戏画面
{BeginBatchDraw();putimage(-150, 0, &img);putimagePNG(80, -10, &imgBar);char scoreText[8];char scoreText1[16];sprintf_s(scoreText, sizeof(scoreText), "%d", sunshine);sprintf_s(scoreText1, sizeof(scoreText1), "Wave %d zombies", wava_count);//sprintf_s(name, sizeof(name), "res/yuanshen/%d.png", i + 1);outtextxy(105, 60, scoreText);outtextxy(700, 570, scoreText1);//outtextxy(700, 570, "s");//小推车for (int i = 0; i < 5; i++){if(cars[i].used)putimagePNG(cars[i].x, cars[i].y, &imgcar);}//植物卡牌for (int i = 0; i < PLANT_COUNT + 2; i++){if(i==PLANT_COUNT)putimagePNG(163 + i * 65 + 8, -5, &imgCards[i]);elseputimagePNG(163 + i * 65, 0, &imgCards[i]);}//植物for (int i = 0; i < 5; i++){for (int j = 0; j < 9; j++){if (map[i][j].type > 0){/*int x = 256 - 150 + j * 81;int y = 100 + i * 100 - 15;*/int PlantType = map[i][j].type - 1;int index = map[i][j].frameIndex;if (map[i][j].type != 4 && map[i][j].type != YING_TAO + 1 && map[i][j].type != LA_JIAO + 1){putimagePNG(map[i][j].x, map[i][j].y, imgPlant[PlantType][index]);}else if (map[i][j].type == YING_TAO + 1){if (index == 8)putimagePNG(map[i][j].x - 75, map[i][j].y-35, imgPlant[PlantType][index]);elseputimagePNG(map[i][j].x - 22, map[i][j].y, imgPlant[PlantType][index]);}else if (map[i][j].type == LA_JIAO + 1){if (index > 7)putimagePNG(100, map[i][j].y - 35, imgPlant[PlantType][index]);elseputimagePNG(map[i][j].x, map[i][j].y, imgPlant[PlantType][index]);}else{putimagePNG(map[i][j].x + 5, map[i][j].y - 20, imgPlant[PlantType][index]);}}}}//渲染子弹drawBullets();//铲子if (curPlant != PLANT_COUNT + 1){IMAGE* img = imgPlant[8][0];putimagePNG(163 + 8 * 65 + 8, 0, img);}//僵尸drawZm();//渲染拖动中的植物if (curPlant > 0){IMAGE* img = imgPlant[curPlant - 1][0];putimagePNG(curX - img->getwidth() / 2, curY - img->getheight() / 2, img);}//渲染阳光drawSunshine();EndBatchDraw();
}void collectSunshine(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;int x = balls[i].pCur.x;int y = balls[i].pCur.y;if (msg->x > x && msg->x<x + w&& msg->y>y && msg->y < y + h){sunshine += 25;//balls[i].used = false;balls[i].status = SUNSHINE_COLLECT;//音效//mciSendString("play res/sunshine.mp3", 0, 0, 0);//不支持MP3格式PlaySound("res/sunshine.wav", NULL, SND_FILENAME | SND_ASYNC);balls[i].p1 = balls[i].pCur;balls[i].p4 = vector2(100, 0);balls[i].t = 0;float distance = dis(balls[i].p1 - balls[i].p4);float off = 8;balls[i].speed = 1.0 / (distance / off);break;/*float destX = 0;float destY = 262;float angle = atan((y - destY) / (x - destX));balls[i].xoff = 4 * cos(angle);balls[i].yoff = 4 * sin(angle);*/}}}
}void userClick()
{ExMessage msg;static int status = 0;if (peekmessage(&msg))//判断有没有消息{if (msg.message == WM_LBUTTONDOWN)//左键按下{if (msg.x > 163 && msg.x < 163 + 65 * (PLANT_COUNT+2) && msg.y < 96){//mciSendString("play res/audio/bleep.mp3 alias BGM4", NULL, NULL, NULL);PlaySound("res/audio/bleep.wav", NULL, SND_FILENAME | SND_ASYNC);//PlaySound("res/sunshine.wav", NULL, SND_FILENAME | SND_ASYNC);int index = (msg.x - 163) / 65;//坤坤/*	if (index + 1 == KUN_KUN && sunshine >= 100){curPlant = index + 1;status = 1;curX = msg.x;curY = msg.y;sunshine -= 100;}*/curPlant = index + 1;status = 1;curX = msg.x;curY = msg.y;}else{collectSunshine(&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 - 150 && msg.x < Wide - 70 && msg.y > 100 && msg.y < 590){int row = (msg.y - 100) / 102;int col = (msg.x - 256 + 150 ) / 81;if (map[row][col].type == 0 && curPlant != PLANT_COUNT + 1 && curPlant != PLANT_COUNT + 2){//printf("%d\n", map[col][row].type);map[row][col].type = curPlant;map[row][col].frameIndex = 0;if(curPlant!=0)PlaySound("res/audio/coffee.wav", NULL, SND_FILENAME | SND_ASYNC);if (curPlant == 5)map[row][col].blood = KUNKUN_BLOOD * 50;elsemap[row][col].blood = KUNKUN_BLOOD;map[row][col].shootTime = 0;map[row][col].shoot = false;map[row][col].x = 256 - 150 + col * 81;map[row][col].y = 100 + row * 100 - 15;}else if (map[row][col].type != 0 && curPlant == PLANT_COUNT + 1){PlaySound("res/audio/coffee.wav", NULL, SND_FILENAME | SND_ASYNC);map[row][col].type = 0;map[row][col].blood = 0;}else if (curPlant == PLANT_COUNT + 2){if (zmCount >= ZM_MAX)return;int i = 0;int zmMax = sizeof(zms) / sizeof(zms[0]);for (i = 0; i < zmMax && zms[i].used; i++);if (i < zmMax){PlaySound("res/audio/coffee.wav", NULL, SND_FILENAME | SND_ASYNC);memset(&zms[i], 0, sizeof(zms[i]));zms[i].used = true;zms[i].speed = 2;zms[i].row = row;zms[i].y = 100 + (zms[i].row) * 100 + 70;zms[i].x = 256 - 150 + (col - 1) * 81;zms[i].blood = ORDINARY_ZM_BLOOD;zms[i].dead = false;zms[i].boom = false;zms[i].zmSpeed = 4;zmCount++;}}}}else if (msg.message == WM_RBUTTONDOWN){curPlant = 0;status = 0;}}
}void createSunshine()
{static int count = 0;static int fre = 200;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 = 160 + rand() % 600;balls[i].y = 60;balls[i].destY = (rand() % 4) * 90 + 160;*/balls[i].timer = 0;/*	balls[i].xoff = 0;balls[i].yoff = 0;*/balls[i].status = SUNSHINE_DOWN;balls[i].t = 0;balls[i].p1 = vector2(160 + rand() % 600, 60);balls[i].p4 = vector2(balls[i].p1.x, (rand() % 4) * 90 + 160);int off = 2;float distance = balls[i].p4.y - balls[i].p1.y;balls[i].speed = 1.0 / (distance / off);}int ballMax = sizeof(balls) / sizeof(balls[0]);//向日葵生产阳光for (int i = 0; i < 5; i++){for (int j = 0; j < 9; j++){if (map[i][j].type == TAI_YANG + 1){map[i][j].timer++;if (map[i][j].timer > 100){map[i][j].timer = 0;int 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 = (40 + rand() % 50) * (rand() % 2 ? 1 : -1);balls[k].p4 = vector2(map[i][j].x + w,map[i][j].y + imgPlant[TAI_YANG][0]->getheight() - imgSunshineBall[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_RPODUCT;balls[k].speed = 0.05;balls[k].t = 0;}}}}}void updatSunshine()
{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].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_RPODUCT){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;}}}}
}int mciZmTime = 0;
void createZM()
{if (zmCount >= zmCount_max)return;static int zmFre = 5000000;static int count = 0;count++;if (count > zmFre){if (mciZmTime == 0){mciSendString("play res/audio/awooga.mp3", 0, 0, 0);mciZmTime++;}count = 0;zmFre = 202 - 20 * wava_count;int i = 0;int zmMax = sizeof(zms) / sizeof(zms[0]);for (i = 0; i < zmMax && zms[i].used && zms[i].dead == false; i++);if (i < zmMax){memset(&zms[i], 0, sizeof(zms[i]));zms[i].used = true;zms[i].speed = 2;zms[i].row = rand() % 5;zms[i].y = 100 + (zms[i].row) * 100 + 70;zms[i].x = Wide;zms[i].blood = ORDINARY_ZM_BLOOD;zms[i].dead = false;zms[i].boom = false;zms[i].zmSpeed = 4;zmCount++;}}
}int zmSpeed = 6;
void updateZM()
{int zmMax = sizeof(zms) / sizeof(zms[0]);static int count[ZM_MAX] = { 0 };//更新僵尸的位置for (int i = 0; i < zmMax; i++){count[i]++;if (count[i] >= (gameStatus == GOING ? zms[i].zmSpeed : zmSpeed)){//printf("%d ", (gameStatus == GOING ? zms[i].zmSpeed : zmSpeed));count[i] = 0;if (zms[i].used){if (zms[i].dead){zms[i].frameIndex++;if (zms[i].boom == true){if (zms[i].frameIndex >= 20){//printf("%d ", killCount);zms[i].used = false;zms[i].row = 0;//killCount++;if (killCount >= ZM_MAX)gameStatus = WIN;else if (killCount >= zmCount_max){wava_count++;zmCount_max *= 2;killCount = 0;zmCount = 0;}}}else{if (zms[i].frameIndex >= 38){//printf("%d ", killCount);zms[i].used = false;zms[i].row = 0;if (killCount >= ZM_MAX)gameStatus = WIN;else if (killCount >= zmCount_max){wava_count++;zmCount_max *= 2;killCount = 0;zmCount = 0;}}}}else if (zms[i].eating){//mciSendString("play res/audio/chomp.mp3", 0, 0, 0);//mciSendString("play res/audio/chompsoft.mp3", 0, 0, 0);zms[i].frameIndex = (zms[i].frameIndex + 1) % 21;}else{zms[i].frameIndex++;if (zms[i].frameIndex >= 22){zms[i].frameIndex = 0;}}zms[i].x -= zms[i].speed;if (zms[i].x < 0){//printf("GAME OVER\n");//MessageBox(NULL, "over", "over", 0);//待优化//exit(0);//待优化gameStatus = FAIL;}}}}
}void shoot()
{int lines[5] = { 0 };int bulletMax = sizeof(bullets) / sizeof(bullets[0]);int bulletMax_wandou = sizeof(bullets_wandou) / sizeof(bullets_wandou[0]);int bulletMax_hanbing = sizeof(bullets_hanbing) / sizeof(bullets_hanbing[0]);int bulletMax_santou = sizeof(bullets_santou) / sizeof(bullets_santou[0]);int zmCount = sizeof(zms) / sizeof(zms[0]);int dangerX = Wide - 80;for (int i = 0; i < zmCount; i++){if (zms[i].dead == false && zms[i].x < dangerX && zms[i].x>100){lines[zms[i].row] = 1;}}for (int i = 0; i < 5; i++){for (int j = 0; j < 9; j++){//坤坤if (map[i][j].type == KUN_KUN + 1 && lines[i]){map[i][j].shootTime++;if (map[i][j].shootTime > 20){map[i][j].shootTime = 0;int k;for (k = 0; k < bulletMax && bullets[k].used; k++);if (k < bulletMax){map[i][j].frameIndex = 3;bullets[k].used = true;bullets[k].row = i;bullets[k].speed = 10;bullets[k].blast = false;bullets[k].frameIndex = 0;int zwX = 256 - 150 + j * 81;int zwY = 100 + i * 100 - 15;bullets[k].x = zwX + imgPlant[map[i][j].type - 1][0]->getwidth() - 10;bullets[k].y = zwY + 5;}}}//豌豆else if (map[i][j].type == WAN_DOU + 1 && lines[i]){map[i][j].shootTime++;if (map[i][j].shootTime > 35){map[i][j].shootTime = 0;int k;for (k = 0; k < bulletMax_wandou && bullets_wandou[k].used; k++);if (k < bulletMax_wandou){//map[i][j].shoot = true;//if(map[i][j].frameIndex > 1)map[i][j].frameIndex = 2;bullets_wandou[k].used = true;bullets_wandou[k].row = i;bullets_wandou[k].speed = 8;bullets_wandou[k].blast = false;bullets_wandou[k].frameIndex = 0;int zwX = 256 - 150 + j * 81;int zwY = 100 + i * 100 - 15;bullets_wandou[k].x = zwX + imgPlant[map[i][j].type - 1][0]->getwidth() - 10;bullets_wandou[k].y = zwY + 5;}}}//寒冰豌豆else if (map[i][j].type == HAN_BING_WAN_DOU + 1 && lines[i]){map[i][j].shootTime++;if (map[i][j].shootTime > 35){map[i][j].shootTime = 0;int k;for (k = 0; k < bulletMax_hanbing && bullets_hanbing[k].used; k++);if (k < bulletMax_hanbing){//map[i][j].shoot = true;//if(map[i][j].frameIndex > 1)map[i][j].frameIndex = 4;bullets_hanbing[k].used = true;bullets_hanbing[k].row = i;bullets_hanbing[k].speed = 10;bullets_hanbing[k].blast = false;bullets_hanbing[k].frameIndex = 0;int zwX = 256 - 150 + j * 81;int zwY = 100 + i * 100 - 15;bullets_hanbing[k].x = zwX + imgPlant[map[i][j].type - 1][0]->getwidth() - 10;bullets_hanbing[k].y = zwY + 5;}}}//三头豌豆else if (map[i][j].type == SAN_TOU_WAN_DOU + 1 && lines[i]){map[i][j].shootTime++;if (map[i][j].shootTime > 35){map[i][j].shootTime = 0;int k;for (int b = 0; b < 3; b++){for (k = 0; k < bulletMax_santou && bullets_santou[k].used; k++);if (k < bulletMax_santou){//map[i][j].shoot = true;//if(map[i][j].frameIndex > 1)//map[i][j].frameIndex = 2;bullets_santou[k].used = true;bullets_santou[k].row = i;bullets_santou[k].speed = 8;bullets_santou[k].blast = false;bullets_santou[k].frameIndex = 0;int zwX = 256 - 150 + j * 81;int zwY = 100 + i * 100 - 15;bullets_santou[k].x = zwX + imgPlant[map[i][j].type - 1][0]->getwidth() - 30;bullets_santou[k].y = zwY + 16;direction_santou[k] = b;row_santou[k] = i;}}}}}}
}void updateBullets_kunkun()
{int countMax = sizeof(bullets) / sizeof(bullets[0]);for (int i = 0; i < countMax; i++){if (bullets[i].used){//static double angle = 0;bullets[i].x += bullets[i].speed;//angle += bullets[i].speed;//bullets[i].y += 10;//bullets[i].x += bullets[i].speed;if (bullets[i].x > Wide){bullets[i].used = false;}//待完善if (bullets[i].blast){bullets[i].frameIndex++;if (bullets[i].frameIndex >= 4){bullets[i].used = false;}}}}
}void updateBullets_wandou()
{int countMax_wandou = sizeof(bullets_wandou) / sizeof(bullets_wandou[0]);for (int i = 0; i < countMax_wandou; i++){if (bullets_wandou[i].used){//static double angle = 0;bullets_wandou[i].x += bullets_wandou[i].speed;//angle += bullets[i].speed;//bullets[i].y += 10;//bullets[i].x += bullets[i].speed;if (bullets_wandou[i].x > Wide){bullets_wandou[i].used = false;}//待完善if (bullets_wandou[i].blast){bullets_wandou[i].frameIndex++;if (bullets_wandou[i].frameIndex >= 4){bullets_wandou[i].used = false;}}}}
}void updateBullets_hanbing()
{int countMax_hanbing = sizeof(bullets_hanbing) / sizeof(bullets_hanbing[0]);for (int i = 0; i < countMax_hanbing; i++){if (bullets_hanbing[i].used){//static double angle = 0;bullets_hanbing[i].x += bullets_hanbing[i].speed;//angle += bullets[i].speed;//bullets[i].y += 10;//bullets[i].x += bullets[i].speed;if (bullets_hanbing[i].x > Wide){bullets_hanbing[i].used = false;}//待完善if (bullets_hanbing[i].blast){bullets_hanbing[i].frameIndex++;if (bullets_hanbing[i].frameIndex >= 4){bullets_hanbing[i].used = false;}}}}
}void updateBullets_santou()
{int countMax_santou = sizeof(bullets_santou) / sizeof(bullets_santou[0]);for (int i = 0; i < countMax_santou; i++){if (bullets_santou[i].used){if (direction_santou[i] == MIDDLE){//static double angle = 0;bullets_santou[i].x += bullets_santou[i].speed;//bullets[i].x += bullets[i].speed;if (bullets_santou[i].x > Wide){bullets_santou[i].used = false;}	}else if (direction_santou[i] == UP){int destY = 85 + (row_santou[i] - 1) * 100;//int zwX = 256 - 150 + j * 81;float angle = atan(0.6);bullets_santou[i].x += bullets_santou[i].speed;bullets_santou[i].y -= bullets_santou[i].speed * tan(angle);//printf("%d\n", row_santou[i]);//printf("destY = %d\n", destY);//printf("bullets[i].y = %lf\n", bullets[i].y);if (bullets_santou[i].y <= destY + 16){direction_santou[i] = MIDDLE;}}else if (direction_santou[i] == DOWN){int destY = 85 + (row_santou[i] + 1) * 100;float angle = atan(0.6);bullets_santou[i].x += bullets_santou[i].speed;bullets_santou[i].y += bullets_santou[i].speed * tan(angle);if (bullets_santou[i].y >= destY + 16){direction_santou[i] = MIDDLE;}}//待完善if (bullets_santou[i].blast){bullets_santou[i].frameIndex++;if (bullets_santou[i].frameIndex >= 4){bullets_santou[i].used = false;}}}}
}void updateBullets()
{//坤坤updateBullets_kunkun();//豌豆射手updateBullets_wandou();//寒冰豌豆updateBullets_hanbing();//三头豌豆updateBullets_santou();
}void checkBullet2Zm_kunkun()
{int bCount = sizeof(bullets) / sizeof(bullets[0]);int zCount = sizeof(zms) / sizeof(zms[0]);for (int i = 0; i < bCount; i++){//if (bullets[i].used || bullets[i].blast == false){for (int j = 0; j < zCount; 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].row == zms[j].row && x > x1 && x < x2 && bullets[i].used) {zms[j].blood -= 20;zms[j].x += 1;bullets[i].blast = true;bullets[i].speed = 0;//bullets[i].x = 0;if (zms[j].blood <= 0){killCount++;zms[j].dead = true;zms[j].speed = 0;zms[j].frameIndex = 0;}break;}}}}
}void checkBullet2Zm_wandou()
{int bCount = sizeof(bullets_wandou) / sizeof(bullets_wandou[0]);int zCount = sizeof(zms) / sizeof(zms[0]);for (int i = 0; i < bCount; i++){//if (bullets[i].used || bullets[i].blast == false)for (int j = 0; j < zCount; j++){if (zms[j].used == false)continue;int x1 = zms[j].x + 80;int x2 = zms[j].x + 110;int x = bullets_wandou[i].x;if (zms[j].dead == false &&bullets_wandou[i].row == zms[j].row && x > x1 && x < x2 && bullets_wandou[i].used) {zms[j].blood -= 20;bullets_wandou[i].blast = true;bullets_wandou[i].speed = 0;//bullets_wandou[i].x = 0;if (zms[j].blood <= 0){killCount++;zms[j].dead = true;zms[j].speed = 0;zms[j].frameIndex = 0;}break;}}}
}void checkBullet2Zm_hanbing()
{int bCount = sizeof(bullets_hanbing) / sizeof(bullets_hanbing[0]);int zCount = sizeof(zms) / sizeof(zms[0]);for (int i = 0; i < bCount; i++){//if (bullets[i].used || bullets[i].blast == false)for (int j = 0; j < zCount; j++){if (zms[j].used == false)continue;int x1 = zms[j].x + 80;int x2 = zms[j].x + 110;int x = bullets_hanbing[i].x;if (zms[j].dead == false &&bullets_hanbing[i].row == zms[j].row && x > x1 && x < x2 && bullets_hanbing[i].used) {zms[j].blood -= 20;zms[j].zmSpeed = 7;bullets_hanbing[i].blast = true;bullets_hanbing[i].speed = 0;//bullets_hanbing[i].x = 0;if (zms[j].blood <= 0){killCount++;zms[j].dead = true;zms[j].speed = 0;zms[j].frameIndex = 0;zms[j].zmSpeed = 4;}break;}}}
}void checkBullet2Zm_santou()
{int bCount = sizeof(bullets_santou) / sizeof(bullets_santou[0]);int zCount = sizeof(zms) / sizeof(zms[0]);for (int i = 0; i < bCount; i++){bullets_santou[i].row = (bullets_santou[i].y - 85+5) / 100;//if (bullets[i].used || bullets[i].blast == false)for (int j = 0; j < zCount; j++){//100 + i * 100 - 15if (zms[j].used == false)continue;int x1 = zms[j].x + 80;int x2 = zms[j].x + 110;int x = bullets_santou[i].x;if (zms[j].dead == false &&bullets_santou[i].row == zms[j].row && x > x1 && x < x2 && bullets_santou[i].used) {zms[j].blood -= 20;bullets_santou[i].blast = true;bullets_santou[i].speed = 0;//bullets_santou[i].x = 0;if (zms[j].blood <= 0){killCount++;zms[j].dead = true;zms[j].speed = 0;zms[j].frameIndex = 0;zms[j].zmSpeed = 4;}break;}}}
}void checkBullet2Zm()
{//坤坤checkBullet2Zm_kunkun();//豌豆checkBullet2Zm_wandou();//寒冰豌豆checkBullet2Zm_hanbing();//三头豌豆checkBullet2Zm_santou();
}void checkBoom2Zm()
{int zCount = sizeof(zms) / sizeof(zms[0]);for (int i = 0; i < 5; i++){for (int j = 0; j < 9; j++){//樱桃if (map[i][j].type == YING_TAO + 1){if (map[i][j].frameIndex > 8){PlaySound("res/audio/cherrybomb.wav", NULL, SND_FILENAME | SND_ASYNC);//map[row][col].x = 256 - 150 + col * 81;//map[row][col].y = 100 + row * 100 - 15;map[i][j].type = 0;map[i][j].frameIndex = 0;int x1 = 100 + 81 * (j - 1);int x2 = 100 + 81 * (j + 2);int y1 = 85 + (i - 1) * 100;int y2 = 85 + (i + 2) * 100;for (int k = 0; k < zCount; k++){if (zms[k].used == false)continue;int zmX = zms[k].x + imgZm[0].getwidth() / 2;int zmY = zms[k].y;if (zmX <= x2 && zmX >= x1 && zmY >= y1 && zmY <= y2 && zms[k].dead == false){killCount++;zms[k].boom = true;zms[k].dead = true;zms[k].speed = 0;zms[k].frameIndex = 0;zms[k].blood = 0;}}}}//火爆辣椒else if (map[i][j].type == LA_JIAO + 1){if (map[i][j].frameIndex > 7){if (map[i][j].frameIndex == 8)PlaySound("res/audio/firepea.wav", NULL, SND_FILENAME | SND_ASYNC);for (int k = 0; k < zCount; k++){if (zms[k].used == false)continue;if (zms[k].row == i && zms[k].x < Wide - 80 - 70 && zms[k].dead == false){killCount++;zms[k].boom = true;zms[k].dead = true;zms[k].speed = 0;zms[k].frameIndex = 0;zms[k].blood = 0;}}if (map[i][j].frameIndex > 14){map[i][j].type = 0;map[i][j].frameIndex = 0;}}}}}
}void checkZM2Zhiwu()
{int chomp = 0;//mciSendString("play ZM_BGM repeat", NULL, NULL, NULL);char name[64];int bCount = sizeof(bullets) / sizeof(bullets[0]);int zCount = sizeof(zms) / sizeof(zms[0]);for (int i = 0; i < zCount; i++){//killCount = 0;if (zms[i].dead)continue;//zms[i].chomptime = 0;int row = zms[i].row;for (int j = 0; j < 9; j++){//if (map[row][j].type == 0)continue;//int zhiwuX = 101 + j * 81;int x1 = zhiwuX;int x2 = zhiwuX + 81;int x3 = zms[i].x + 100;if (x3 >= x1 && x3 <= x2){if (map[row][j].blood <= 0 || (map[row][j].type == 0 && zms[i].eating != false)){map[row][j].blood = 0;map[row][j].type = 0;zms[i].eating = false;//zms[i].frameIndex = 0;zms[i].speed = 3;}else if (map[row][j].type != 0){//mciSendString("play ZM_BGM repeat", NULL, NULL, NULL);//mciSendString("play name repeat", NULL, 0, NULL);zms[i].eating = true;zms[i].speed = 0;if (map[row][j].type != 3 && map[row][j].type != 7)map[row][j].blood--;//zms[i].frameIndex = 0;}if (zms[i].eating && chomp == 0)chomp = 1;}else if (x3 > 830){zms[i].eating = false;zms[i].speed = 3;}}}static int chomp_time = 0;chomp_time++;if (chomp&&chomp_time>20){chomp_time = 0;PlaySound("res/audio/chomp.wav", NULL, SND_FILENAME | SND_ASYNC);//mciSendString("play ZM_BGM", NULL, NULL, NULL);//printf("1 ");}
}void checkcars2Zm()
{for (int i = 0; i < 5; i++){int carsX = cars[i].x + 70;for (int j = 0; j < ZM_MAX; j++){if (zms[j].used && zms[j].dead == false && zms[j].row == i){int zmX = zms[j].x + 80;if (carsX > zmX && cars[i].used){if (cars[i].move == false)cars[i].move = true;else{killCount++;zms[j].dead = true;zms[j].speed = 0;zms[j].frameIndex = 0;}}}}}
}void collistionCheck()
{//子弹对僵尸的检测checkBullet2Zm();//僵尸对植物的检测checkZM2Zhiwu();//爆炸对植物的检测checkBoom2Zm();//小推车对僵尸的检测checkcars2Zm();
}void updatecar()
{for (int i = 0; i < 5; i++){if (cars[i].move){cars[i].x += 20;}if (cars[i].x > Wide){cars[i].move = false;cars[i].used = false;}}
}void updateGame()
{srand((unsigned)time(NULL));static int count = 0;count++;if (count > 4){count = 0;for (int i = 0; i < 5; i++){for (int j = 0; j < 9; j++){if (map[i][j].type > 0){map[i][j].frameIndex++;int PlantType = map[i][j].type - 1;int index = map[i][j].frameIndex;if (map[i][j].shoot){if (map[i][j].frameIndex > 1){map[i][j].shoot = false;}}else{if (imgPlant[PlantType][index] == NULL){map[i][j].frameIndex = 0;}}}}}}createSunshine();//创建阳光updatSunshine();//更新阳光状态createZM();//创建僵尸updateZM();//更新僵尸状态shoot();//发射豌豆子弹updateBullets();//更新子弹collistionCheck();//实现豌豆子弹的碰撞检测updatecar();//更新小推车
}void menu()
{mciSendString("play BGM", NULL, NULL, NULL);IMAGE imgBg, imgMenu1, imgMenu2;loadimage(&imgBg, "res/menu.png");loadimage(&imgMenu1, "res/menu2.png");loadimage(&imgMenu2, "res/menu1.png");int flag = 0;while (1){BeginBatchDraw();putimage(0, 0, &imgBg);putimagePNG(474, 75, flag ? &imgMenu1 : &imgMenu2);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;PlaySound("res/audio/bleep.wav", NULL, SND_FILENAME | SND_ASYNC);}else if (msg.message == WM_LBUTTONUP && flag == 1){EndBatchDraw();break;}}EndBatchDraw();}
}void viewScence()
{mciSendString("play BGM3", NULL, NULL, NULL);//开头场景时僵尸的位置vector2 points[9] = {{550,80},{530,160},{630,170},{530,200},{515,270},{565,370},{605,340},{705,280},{690,340}};int index[9];for (int i = 0; i < 9; i++){index[i] = rand() % 11;}int count = 0;for (int i = 0; i >= -500; i-=3){BeginBatchDraw();putimage(i, 0, &img);count++;for (int k = 0; k < 9; k++){putimagePNG(points[k].x + 500 + i,points[k].y, &imgZmStand[index[k]]);if (count >= 10){index[k] = (index[k] + 1) % 11;} }if (count >= 10)count = 0;EndBatchDraw();Sleep(10);}for (int i = 0; i < 60; i++){BeginBatchDraw();putimage(-500, 0, &img);for (int k = 0; k < 9; k++){putimagePNG(points[k].x, points[k].y, &imgZmStand[index[k]]);index[k] = (index[k] + 1) % 11;}EndBatchDraw();Sleep(50);}for (int i = -500; i <= -150; i += 2){BeginBatchDraw();putimage(i, 0, &img);count++;for (int k = 0; k < 9; k++){putimagePNG(points[k].x + 500 + i,points[k].y, &imgZmStand[index[k]]);if (count >= 10){index[k] = (index[k] + 1) % 11;}if (count >= 10) count = 0;}EndBatchDraw();Sleep(10);}}void barsDown()
{int height = imgBar.getheight();for (int y = -height; y <= -10; y++){BeginBatchDraw();putimagePNG(80, y, &imgBar);for (int i = 0; i < PLANT_COUNT + 2; i++){putimagePNG(163 + i * 65, y + 10, &imgCards[i]);}EndBatchDraw();Sleep(10);}for (int i = 0; i < 5; i++){putimagePNG(50, 100 + i * 100 - 15, &imgcar);}mciSendString("close BGM3", NULL, 0, NULL);mciSendString("play BGM4", NULL, NULL, NULL);Sleep(1000);putimagePNG(450 - imgready.getwidth()/2, 300 - imgready.getheight()/2, &imgready);Sleep(200);
}void failScence()
{for (int i = -150; i <= -100; i += 1){BeginBatchDraw();//zmSpeed = 6;putimage(i, 0, &img);//show();drawZm();//updateGame();createZM();//创建僵尸updateZM();//更新僵尸状态EndBatchDraw();Sleep(50);}
}bool checkOver()
{int ret = false;if (gameStatus == WIN){mciSendString("close BGM2", NULL, NULL, NULL);loadimage(0, "res/win2.png");mciSendString("play res/win.mp3", 0, 0, 0);ret = true;}else if (gameStatus == FAIL){mciSendString("close BGM2", NULL, NULL, NULL);mciSendString("play res/lose.mp3", 0, 0, 0);failScence();Sleep(500);loadimage(0, "res/fail2.png");ret = true;}return ret;
}void OpeningAnimation()
{mciSendString("play res/audio/yuanshen.mp3", 0, 0, 0);for (int i = 0; i < 29; i++){BeginBatchDraw();putimage(-148, -35, &imgopena[i]);EndBatchDraw();Sleep(50);}Sleep(4000);
}int main()
{//加载游戏画面gameInit();//开场动画OpeningAnimation();menu();mciSendString("close BGM", NULL, 0, NULL);//游戏开始时的场景切换viewScence();//游戏开始时卡牌和卡牌槽的下落barsDown();int timer = 0;bool flag = true;mciSendString("play BGM2 repeat", NULL, NULL, NULL);while (1){//用户操作userClick();//游戏更新时间timer += getDelay();if (timer > 20){flag = true;timer = 0;}if (flag){flag = false;//绘制游戏画面show();//更新游戏画面updateGame();if(checkOver()) break;}}closegraph;system("pause");return 0;
}

game.h文件

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1#include<easyx.h>
#include<stdio.h>
#include<time.h>
#include<math.h>
#include"tools.h"
#include<mmsystem.h>
#include"vector2.h"
#pragma comment(lib,"winmm.lib")#define Wide 900
#define Hight 600
#define KUNKUN_BLOOD 100//非坚果植物的血量
#define ORDINARY_ZM_BLOOD 1270*3//僵尸血量
#define ZM_MAX 1024//创建僵尸的最大数量enum {WAN_DOU, TAI_YANG, LA_JIAO, KUN_KUN, JIAN_GUO,HAN_BING_WAN_DOU, YING_TAO, SAN_TOU_WAN_DOU, PLANT_COUNT
};
IMAGE img;//地图
IMAGE imgBar;//卡牌槽
IMAGE imgCards[PLANT_COUNT + 2];//卡牌
IMAGE* imgPlant[PLANT_COUNT + 2][20];//植物动作
IMAGE imgZmStand[11];//开场僵尸的站立
IMAGE imgcar;//小推车
IMAGE imgopena[29];//开场动画
IMAGE imgready;
IMAGE imgnotify;int curX, curY;//当前选中植物,在移动过程中植物的坐标
int curPlant;// 0:没有选中,1:选中了第一种种植物
int sunshine;enum { GOING, WIN, FAIL };
int killCount;//已经杀掉的僵尸个数
int zmCount;//已经出现的僵尸个数
int zmCount_max = 1;//每波出现的僵尸数量
int gameStatus;//游戏的状态
int wava_count = 1;//第wava_count波僵尸struct plant
{int type;//植物类型int frameIndex;//植物动作帧//bool catched;//是否被吃int blood;//血量int shootTime;//植物子弹的射速int timer;//阳光生产的时间int x, y;//植物坐标bool shoot;//判断植物是否处于发射状态
};
struct plant map[5][9];enum { SUNSHINE_DOWN, SUNSHINE_GROUND, SUNSHINE_COLLECT, SUNSHINE_RPODUCT };struct sunshineBall
{int x, y;int frameIndex;//当前显示的图片帧序号int destY;//飘落的目标位置的y坐标bool used;//是否在使用int timer;float xoff;float yoff;float t;//贝塞尔曲线的时间点vector2 p1, p2, p3, p4;vector2 pCur;//当前时刻阳光球的位置float speed;int status;
};//10个阳光球
struct sunshineBall balls[10];
IMAGE imgSunshineBall[29];struct zm
{int x, y;//僵尸的坐标int frameIndex;//僵尸动作帧bool used;//僵尸是否被使用int speed;//僵尸每一次移动的位移int row;//僵尸所在行int blood;//僵尸血量bool dead;//僵尸是否死亡bool eating;//僵尸是否在吃植物bool boom;//僵尸是否被炸死int zmSpeed;//僵尸的移动快慢
};
struct zm zms[ZM_MAX];
IMAGE imgZm[22];
IMAGE imgZMDead[38];
IMAGE imgzmboom[20];
IMAGE imgZMEat[21];//子弹
struct bullet
{double x, y;//子弹的坐标bool used;//子弹是否被使用int row;//子弹所在行int speed;//子弹速度bool blast;//是否发生爆炸int frameIndex;//帧序号
};
//坤坤
struct bullet bullets[10000];
IMAGE imgBulletNormal;
IMAGE imgBallBlast[4];
//豌豆
struct bullet bullets_wandou[200];
IMAGE imgBallBlast_wandou[4];
IMAGE imgBulletNormal_wandou;
//寒冰豌豆
struct bullet bullets_hanbing[200];
IMAGE imgBallBlast_hanbing[4];
IMAGE imgBulletNormal_hanbing;
//三头豌豆
struct bullet bullets_santou[200];
IMAGE imgBallBlast_santou[4];
IMAGE imgBulletNormal_santou;
int direction_santou[200];
int row_santou[200];//三头豌豆发射子弹时所在的行数
enum { MIDDLE, UP, DOWN };//小推车
struct car
{bool move;//是否处于移动状态int x, y;//位置bool used;//是否被使用
};
struct car cars[5];
/*
* 增加植物的步骤:
* 1. 创建子弹的相关变量
* 2. 加载植物子弹图片
* 3. 渲染植物子弹
* 4. 发射植物子弹
* 5. 检查植物子弹与僵尸的碰撞
* 6. 更新植物子弹
*/

vector2.cpp文件(贝塞尔曲线)

//头文件要求
#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;
}

vector2.h文件

#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);

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 < 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里的Rint sg = ((src[srcX] & 0xff00) >> 8);   //Gint sb = src[srcX] & 0xff;              //Bif (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;}
}

tools.cpp文件 

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/13434.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基础2 JAVA图形编程桌面:探索图形程序的抽象实现

嘿&#xff0c;大家好&#xff01;我非常高兴又一次有机会与大家相聚&#xff0c;分享新的知识和经验。对于热爱编程和探索新技术的朋友们来说&#xff0c;今天的内容绝对不容错过。我为大家准备了一个详尽的视频教程&#xff1a;《基础2 JAVA 图形编程&#xff1a;主程序调用…

git拉取项目前需要操作哪些?

1.输入 $ ssh-keygen -t rsa -C "秘钥说明" 按enter键 2.出现 ssh/id_rsa&#xff1a;(输入也可以不输入也可以) 然后按enter键 3.出现empty for no passphrase&#xff1a;(输入也可以不输入也可以) 然后按enter键 4.出现same passphrase again: (输入也可以不输入也…

20240516-Flyme AIOS 特种兵发布会

目录 1 Flyme AIOS 2 路演功能 2.1 拖拽流转 2.2 任务剧本自定义 2.3 智能体商店 2.4 实况通知 2.5 AI壁纸 3 MYVU 3.1 翻译功能 3.2 AR导航-骑行 3.3 AI语音转文字-科技向善 3.4 Flyme AR-提词器增强 1 Flyme AIOS 1&#xff09;目标&#xff1a;All in AI&#…

AI绘图Stable Diffusion,如何无损高清放大图片,保姆级教程建议收藏!

前言 我们在用 stable diffusion 制作AI图片时&#xff0c;默认生成图片的尺寸为512*512&#xff0c;即使是竖图一般也就是512*768&#xff0c;如果再把尺寸设置大一些&#xff0c;就会因为硬件算力不够而造成系统崩溃&#xff0c;今天就来跟大家聊一聊&#xff0c;如何将制作…

RocketMQ-Dashboard 控制台使用详解

1 安装部署 具体部署启动请参考&#xff1a;RocketMQ从安装、压测到运维一站式文档_rocketmq benchmark压测-CSDN博客 RocketMq的dashboard&#xff0c;有运维页面&#xff0c;驾驶舱&#xff0c;集群页面&#xff0c;主题页面&#xff0c;消费者页面&#xff0c;生产者页面&…

【Kubenetes】边缘计算KubeEdge架构设计详解

文章目录 前言KubeEdge云边通信方式云端架构设计EdgeController:云到边&#xff1a;边到云 DeviceController:云到边边到云 边缘端架构设计EdgedPod的管理部分Pod的监控部分Pod的卷管理Pod的垃圾回收Pod同步管理 MetaMangger从云到边缘的更新 (Update From Cloud To Edge)从边缘…

Covalent长期数据设施,支持基于 “blob” 、总锁仓54亿美元的L2

Covalent Network&#xff08;CQT&#xff09;是领先的历史数据可用性网络&#xff0c;通过其在 Web3 中超过 225 个区块链上的结构化数据基础设施&#xff0c;为数千名客户和开发人员提供支持。Covalent Network&#xff08;CQT&#xff09;正在与未来以太坊的进步需求相匹配&…

SQL慢查询学习篇

https://www.cnblogs.com/isyues/p/17733015.html 1. 对扫到的SQL慢查询语句执行 explain explain select task_id, channel, count(task_id) as count from tablename where send_time > "2024-05-10 16:13:59" and send_time < "2024-05-14 16:13:59…

api接口、api文档、api调试、api测试

应用程序接口是一组定义、程序及协议的集合&#xff0c;通过 API 接口实现计算机软件之间的相互通信。API 的一个主要功能是提供通用功能集。程序员通过调用 API 函数对应用程序进行开发&#xff0c;可以减轻编程任务。 API 同时也是一种中间件&#xff0c;为各种不同平台提供数…

展馆展厅设计施工流程

1、需求分析和确定&#xff1a; 与客户沟通&#xff0c;了解客户需求&#xff0c;对展馆展厅的用途、面积、功能、展品特点等进行分析&#xff0c;并确定设计方案。 2、方案设计 根据需求确定设计方案&#xff0c;包括平面布局、展品陈列、展示方式、照明等。设计师需要提供设计…

如何在Spring启动的时候执行一些操作

如何在Spring启动的时候执行一些操作 在Spring启动的时候执行一些操作有多种方式。你可以通过实现ApplicationRunner或者CommandLineRunner接口&#xff0c;在Spring Boot应用程序启动后执行特定操作。另外&#xff0c;你也可以使用PostConstruct注解&#xff0c;在Spring Bea…

【考研数学】张宇《1000题》强化阶段正确率多少算合格?

张宇1000题真的很练人心态.... 基础不好&#xff0c;建议别碰1000题 基础好&#xff0c;1000题建议在两个月以内刷完 如果自己本身在基础阶段学的比较水&#xff0c;自己的薄弱点刷了一小部分题没有针对性完全解决&#xff0c;转身去刷1000题就会发现&#xff0c;会的题目刷…

shell脚本-重定向与管道符

一、重定向 因为shell脚本有着批量操作的特殊性&#xff0c;大部分操作处于后台执行&#xff0c;不需要用户进行干预&#xff0c;所以提取、过滤并执行信息十分需要重定向和管道。重定向的意思是不输出到默认设备上&#xff0c;而是输出到你指定的位置&#xff08;文件、其他输…

1.微信小程序开发之准备工作

1.微信小程序账号注册 小程序开发 与 网页开发不一样&#xff0c;在开始微信小程序开发之前&#xff0c;需要访问 微信公众平台&#xff0c;注册一个微信小程序账号。 在拥有了小程序的账号以后&#xff0c;我们才可以开发和管理小程序&#xff0c;后续可以通过该账号进行开发…

国网电力分公司、税务企业如何向央媒投稿?

税务、电力、银行等单位如果想要将稿件发布到中央媒体&#xff0c;可以遵循为大家整理的以下步骤和建议&#xff1a; 了解央媒的定位与要求&#xff1a;中央媒体&#xff0c;如新华社、人民日报、中央电视台等&#xff0c;都有其独特的报道风格和关注重点。在投稿前&#xff0…

【Web后端】会话跟踪技术及过滤器

1.会话跟踪技术 1.1 会话的概念 在web应用中&#xff0c;浏览器和服务器在一段时间内发送请求和响应的连续交互的全过程 1.2 会话跟踪概念 对同一个用户跟服务器的连续请求和接收响应的监视过程 1.3 会话跟踪作用 浏览器和服务器是以http协议进行通信&#xff0c;http协议是…

算法工程师面试问题 | YOLOv8面试考点原理全解析(一)

本文给大家带来的百面算法工程师是深度学习目标检测YOLOv8面试总结&#xff0c;文章内总结了常见的提问问题&#xff0c;旨在为广大学子模拟出更贴合实际的面试问答场景。在这篇文章中&#xff0c;我们还将介绍一些常见的深度学习目标检测面试问题&#xff0c;并提供参考的回答…

AWS RDS ElasticCache 监控可观测最佳实践

在当今的电子商务时代&#xff0c;一个高效、稳定的电商平台对于保持竞争力至关重要。数据库作为电商平台的核心支撑&#xff0c;其性能直接影响到用户体验和业务流畅度。本文将深入探讨如何在电商场景下通过观测云对亚马逊云科技 RDS&#xff08;MySQL&#xff09; 和 Elastic…

python+selenium - UI自动框架之封装log类

通过自定log类&#xff0c;能把执行过程记录到日志&#xff0c;方便检查和重现问题。 log类介绍&#xff1a; 每次调用log函数&#xff0c;会根据绝对路径生成日志文件在logs目录下面(在被调用的时候日志会输出到指定的文件&#xff09;&#xff0c;日志文件的格式是年月日.lo…

vue+element的表格(el-table)排班情况表(2024-05-09)

vueelement的表格&#xff08;el-table&#xff09;排班情况&#xff0c;增删查改等简单功能 代码&#xff1a; <template><!-- 表格 --><div class"sedules"><el-header><el-date-pickerv-model"monthValue2"type"month…