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