c语言实现植物大战僵尸(未完成)

记录实现过程


#include<stdio.h>
#include<graphics.h>
#include"toos.h"
#include<time.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
#define sum 3
IMAGE imgBg;//图片
IMAGE imgBg1;
IMAGE imgBg2;
enum { wan, xiang, tu, zhiwi_count };
IMAGE *zhiwu[zhiwi_count][20];
IMAGE imgBg3[zhiwi_count];
struct zhiwu
{
	int tep;//0无植物,1有植物
	int fame;//播放位数
};//种植
struct zhiwu map[5][9];//地图
int curzhiwu;//移动时渲染选择的植物
int curx, cury;//移动过程坐标位置
int sunzhi;
struct sunshine
{
	int x, y;
	int xuhao;//图片序号
	int paceY;//目标位置
	bool use;
	int time;
};
struct sunshine ball[10];
IMAGE sunshine[29];//图片
struct zm
{
	int x, y;
	int form;
	bool use;
	int speed;
};
struct zm zms[10];
IMAGE imgzm[22];
int file(char* name)
{
	FILE* fp = NULL;
	int s =0;
	s=fopen_s(&fp, name, "r");
	if (s == 0)
	{
		fclose(fp);
		return 1;
	}
	else
	{
		return 0;
	}
}
void gamelint()
{
	//加载背景
	loadimage(&imgBg, "images/interface/background1.jpg");
	//卡槽
	loadimage(&imgBg2, "res/toolbar.png");
	//植物
	memset(zhiwu, 0, sizeof(zhiwu));
	memset(map, 0, sizeof(map));
	char name[64];
	for (int i = 0;i < zhiwi_count; i++)
	{
		//生成卡片文件名
		sprintf_s(name, sizeof(name), "res/Cards/card_%d.png",i+1);
		loadimage(&imgBg3[i], name);
		for (int j = 0; j < 20; j++)
		{
			sprintf_s(name, sizeof(name), "res/zhiwu/%d/%d.png",i,j + 1);
			if (file(name))
			{
				zhiwu[i][j] = new IMAGE;
				loadimage(zhiwu[i][j], name);
			}
			else break;
		}
	}
	curzhiwu = 0;
	sunzhi = 150;
	memset(ball, 0, sizeof(ball));
	for (int i = 0; i < 29; i++)
	{
		sprintf_s(name, sizeof(name), "res/sunshine/%d.png", i + 1);
		loadimage(&sunshine[i], name);
	}
	//随机数
	srand(time(NULL));
	//创建窗口
	initgraph(900, 600,1);
	LOGFONT f;
	gettextstyle(&f);
	f.lfHeight = 30;
	f.lfWeight = 20;
	strcpy(f.lfFaceName, "Segoe UL Black");
	f.lfQuality = ANTIALIASED_QUALITY;//抗锯齿
	settextstyle(&f);
	setbkmode(LPD_TRANSPARENT);//背景透明
	setcolor(BLACK);//颜色
	//僵尸
	memset(zms, 0, sizeof(zms));
	for (int i = 0; i < 22; i++)
	{
		sprintf_s(name, sizeof(name), "res/zm/%d.png", i + 1);
		loadimage(&imgzm[i], name);
	}

}
void drawzm()
{
	int zmmas = sizeof(zms) / sizeof(zms[0]);
	for (int i = 0; i < zmmas; i++)
	{
		if (zms[i].use)
		{
			putimagePNG(zms[i].x, zms[i].y - 60, &imgzm[zms[i].form]);
		}
	}
}
void update()//渲染函数
{
	BeginBatchDraw();//缓冲
	putimage(0, 0, &imgBg);
	putimagePNG(170, 0, &imgBg2);
	for (int i = 0; i < zhiwi_count; i++)
	{
		int x = 260+ i * 65, y = 5;
		putimage(x, y, &imgBg3[i]);
	}
	for (int i = 0;i < 5; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			if (map[i][j].tep > 0)
			{
				int x = j * 80 + 250;
				int y = i * 100 + 80;
				putimagePNG(x, y, zhiwu[map[i][j].tep-1][map[i][j].fame]);
			}
		}
	}
	//渲染拖到过程
	if (curzhiwu > 0)
	{
		IMAGE* img = zhiwu[curzhiwu - 1][0];
		putimagePNG(curx - img->getwidth() / 2, cury - img->getwidth() / 2, img);
	}
	int ballmas = sizeof(ball) / sizeof(ball[0]);
	for (int i = 0; i < ballmas; i++)
	{
		if (ball[i].use)
		{
			IMAGE* img = &sunshine[ball[i].xuhao];
			putimagePNG(ball[i].x, ball[i].y, img);
		}
	}
	char shutext[8];
	sprintf_s(shutext, sizeof(shutext), "%d", sunzhi);
	outtextxy(190, 70, shutext);
	drawzm();
	EndBatchDraw();//结束缓冲
}
void collectsun(ExMessage*msg)
{
	int size = sizeof(ball) / sizeof(ball[0]);
	int w = sunshine->getwidth();
	int h = sunshine->getheight();
	for (int i = 0; i < size; i++)
	{
		if (ball[i].use)
		{
			if (msg->x > ball[i].x && msg->x<ball[i].x + w && msg->y>ball[i].y && msg->y < ball[i].y + h)
			{
				ball[i].use = false;
				sunzhi += 25;
				mciSendString("play res/collectSunshine.wav",0,0,0);
			}
		}
	}
}
void user()
{
	ExMessage msg;
	static int zhuangtai=0;
	if(peekmessage(&msg));//判断是否有消息,有为真,没有为假
	{
		if (msg.message == WM_LBUTTONDOWN)//点击位置
		{
			if (msg.x > 260 && msg.x < 260 + 65 * zhiwi_count && msg.y < 96)
			{
				int leng = (msg.x - 260) / 65;
				//printf("%d\n",leng);
				zhuangtai = 1;
				curzhiwu = leng + 1;
			}
			else collectsun(&msg);
		}else
			if (msg.message == WM_MOUSEMOVE&& zhuangtai ==1)//拖动
			{
				//当下位置
				curx = msg.x;
				cury = msg.y;
			}else
				if (msg.message == WM_LBUTTONUP)//鼠标松开 
				{
					int row = (msg.y-80) / 100;
					int col= (msg.x - 250) / 80;
					//printf("%d %d\n", row, col);
					if (map[row][col].tep == 0)
					{
						map[row][col].tep = curzhiwu;
						map[row][col].fame = 0;
					}
					curzhiwu = 0;
					zhuangtai = 0;
				}
	}
}
void updatesun()
{
	int i;
	int bal = sizeof(ball) / sizeof(ball[0]);
	for (i = 0; i < bal; i++)
	{
		if (ball[i].use)
		{
			ball[i].xuhao = (ball[i].xuhao + 1) % 29;
			if (ball[i].time == 0)
			{
				ball[i].y += 8;
			}
			if (ball[i].y > ball[i].paceY)
			{
				ball[i].time++;
				if (ball[i].time > 100)
				{
					ball[i].use = false;
				}
			}
		}
	}
}
void creatsun()
{
	static int count = 0;
	int fe = 200;
	count++;
	if (count >= fe)
	{
		fe = rand() % 200 + 100;
		count = 0;
		int ballmas = sizeof(ball) / sizeof(ball[0]), i;
		for (i = 0; i < ballmas && ball[i].use; i++);
		if (i >= ballmas)return;
		ball[i].use = true;
		ball[i].xuhao = 0;
		ball[i].x = 260 + (rand() % (900 - 260));
		ball[i].y = 60;
		ball[i].paceY = (rand()) % 4 * 90 + 200;
		ball[i].time = 0;
	}
	//printf("jkdfrhnjsos\n");
}
void creatzm()
{
	static int count = 500;
	static int fe = 0;
	fe++;
	if (fe >= count)
	{
		fe = 0;
		count = rand() % 200 + 300;
		int i;
		int zmmas = sizeof(zms) / sizeof(zms[0]);
		for (i = 0; i < zmmas && zms[i].use; i++);
		if (i < zmmas)
		{
			zms[i].use = true;
			zms[i].x =900;
			zms[i].y = (1 + rand() % 5) * 100 + 50;
			zms[i].speed = 2;
		}
	}
}
void updatezm()
{
	int i;
	int zmmas = sizeof(zms) / sizeof(zms[0]);
	for (i = 0; i < zmmas; i++)
	{
		if (zms[i].use)
		{
			zms[i].x -= zms[i].speed;
			if (zms[i].x < 170)

			{
				printf("game over\n");
				MessageBox(NULL, "over", "over", 0);
				exit(0);
			}
		}
	}
}
void gaibiangame()
{
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			if (map[i][j].tep > 0)
			{
				map[i][j].fame++;
				if (zhiwu[map[i][j].tep - 1][map[i][j].fame]==NULL)
				{
					map[i][j].fame = 0;
				}
			}
		}
	}
	creatsun();
	updatesun();
	creatzm();
	updatezm();
}
void menu()
{
	IMAGE image,image1,image2;
	loadimage(&image, "res/menu.png");
	loadimage(&image1, "res/menu1.png");
	loadimage(&image2, "res/menu2.png");
	int flag = 0;
	while (1)
	{
		BeginBatchDraw();
		putimage(0, 0, &image);
		putimagePNG(474, 75, flag ? &image1 :& image2);
		ExMessage msg;
		if (peekmessage(&msg))
		{
			if (msg.message == WM_LBUTTONDOWN && msg.x > 474 && msg.x < 474 + 301 && msg.y>75 && msg.y < 145)
			{
				flag = 1;
			}
			else if (msg.message == WM_LBUTTONUP&&flag==1)
			{
				return;
			}
		}
		EndBatchDraw();
	}
}
int main(void)
{
	int time = 0;
	gamelint();
	menu();
	bool flag = true;
	while (1)
	{
		user();
		time += getDelay();
		if (time > 100)
		{
			flag = true;
			time = 0;
		}
		if (flag)
		{
			flag = false;
			update();
			gaibiangame();
		}
	}
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知名的网友.ᥫᩣ༠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值