⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamemain.cpp

📁 一个VC游戏源码,带运行程序,供大家学习之用
💻 CPP
📖 第 1 页 / 共 5 页
字号:
						GraphToBack(&c[j]->lpanimate->graph[c[j]->intvalue[CUR]],c[j]->lpanimate->keycolor);
						break;
					case ONE_POINTER_TO_ONE_ANIMATE:
						SetCharacterPos(c[j],c[j]->x,c[j]->y);
						GraphToBack(&c[j]->lpanimate->graph[c[j]->lpanimate->cur],c[j]->lpanimate->keycolor);
						break;
					}
				}
				else
				{
					SetCharacterPos(c[j],c[j]->x,c[j]->y);
					GraphToBack(c[j]->lpgraph,c[j]->keycolor);
				}
				//DrawRect(c[j]->colliderect);
				if(c[j]->intvalue[HP_MAX]!=0)
				{
					SetRectPos(graph[3].destrect,(int)c[j]->x-5,(int)c[j]->y-c[j]->height*3,
						(int)((double)c[j]->intvalue[HP]/(double)c[j]->intvalue[HP_MAX]*((double)c[j]->width+10)),5);
					GraphToBack(&graph[3]);
					SetRectPos(destrect,(int)c[j]->x-5,(int)c[j]->y-c[j]->height*3,(int)c[j]->width+10,5);
					DrawRect(destrect,&lpsurback,0x666666);
				}
			}
		}
	}

	//界面显示

	for(i=0;i<item[ITEM_CATE];i++)
	{
		SetGraphPos(&etcanim[8].graph[0],16+i*(etcanim[8].graph[0].srcrect.right+8),420);
		GraphToBack(&etcanim[8].graph[0],etcanim[8].keycolor);
	}

	SetGraphPos(&graph[7],400,430);
	GraphToBack(&graph[7]);
	SetRectPos(graph[3].destrect,500,430,
		(int)((double)enemynum/(double)enemynum_max*100),22);
	GraphToBack(&graph[3]);
	SetRectPos(destrect,498,428,103,25);
	DrawRect(destrect,&lpsurback,0x666666);

	if(enemynum==0&&timer3==0) //StageClean计时器
	{
		if(timer1==0)timer1=GetTickCount()+5000;
		GraphToBack(&graph[4]);
		if(GetTickCount()>timer1)
		{
			timer1=0;
			stagenum+=1;
			gamestate=INITGAME;
			return 0;
		}
	}

	if(!doraemon.visible) //GameOver计时器
	{
		if(timer3==0)timer3=GetTickCount()+5000;
		GraphToBack(&graph[5]);
		if(GetTickCount()>timer3)
		{
			timer3=0;
			enemynum=0;
			gamestate=MAINTITLE;
			return 0;
		}
	}
	return 1;
}

int DrawMap()
{
	//该地图制作模块完成后,编写游戏程序的结构变动并没有在地图制作模块同步改动,除非存在未知的错误
	if(doraemon.type!=ONE_POINTER_TO_ONE_ANIMATE) //游戏中多拉A梦的动画类型为多指针指向,作为区别,
													//可以在游戏后返回地图制作时初始化相关变动数据
	{
		doraemon.type=ONE_POINTER_TO_ONE_ANIMATE; //设置多拉A梦为单指针指向动画
		doraemon.X=doraemon.Xstart; //初始化多拉A梦位置
		doraemon.Y=doraemon.Ystart;
		X=0;
		Y=0;
	}
	WIDTH=MAXWIDTH-128; //初始化地图区
	HEIGHT=MAXHEIGHT-64;
	SetRectPos(maprect,0,0,WIDTH,HEIGHT);
	static int facing=0;

	/////////////////////////////////////////////////////////////////////////////////////////////

	//碰撞

	/////////////////////////////////////////////////////////////////////////////////////////////

	int i,j;
	for(i=(int)(doraemon.X/32);i<(doraemon.X+doraemon.width)/32;i++)
	{
		for(j=(int)(doraemon.Y/32);j<(doraemon.Y+doraemon.height)/32;j++)
		{
			SetRectPos(destrect,i*32-(int)X,j*32-(int)Y,32,32);
			if(Act(doraemon.colliderect,destrect)&&stage.collide[i][j]==TRUE)
			{
				doraemon.X=doraemon.Xhis;
				doraemon.Y=doraemon.Yhis;
			}
		}
	}
	if(doraemon.X<=0||doraemon.X+doraemon.width>=32*stage.maplength||doraemon.Y<=0||doraemon.Y+doraemon.height>=32*16)
	{
		if(doraemon.Xhis<=0||doraemon.Xhis+doraemon.width>=32*stage.maplength||doraemon.Yhis<=0||doraemon.Yhis+doraemon.height>=32*16)
		{
			doraemon.X=doraemon.Xstart;
			doraemon.Y=doraemon.Ystart;
		}
		else
		{
			doraemon.X=doraemon.Xhis;
			doraemon.Y=doraemon.Yhis;
		}
	}
	doraemon.Xhis=doraemon.X;
	doraemon.Yhis=doraemon.Y;


	/////////////////////////////////////////////////////////////////////////////////////////////

	//控制一切

	/////////////////////////////////////////////////////////////////////////////////////////////

	speed=normalspeed;
	if((key.left&&key.up)||(key.left&&key.down)||(key.right&&key.up)||(key.right&&key.down))speed=sqrtspeed;
	if(key.left&&key.right)
	{
		key.left=0;
		key.right=0;
	}
	if(key.up&&key.down)
	{
		key.up=0;
		key.down=0;
	}

	switch(ctrlobject)
	{
	case OBJ_TILE:
		selscenery=-1;
		selanimate=-1;
		break;
	case OBJ_SCENERY:
		selanimate=-1;
		if(selscenery!=-1)
		{
			if(key.left)stage.scenery[selscenery].X-=speed;
			if(key.right)stage.scenery[selscenery].X+=speed;
			if(key.up)stage.scenery[selscenery].Y-=speed;
			if(key.down)stage.scenery[selscenery].Y+=speed;
			Xfocus=stage.scenery[selscenery].X;
			Yfocus=stage.scenery[selscenery].Y;
		}
		break;
	case OBJ_ANIMATE:
		selscenery=-1;
		if(selanimate!=-1)
		{
			if(key.left&&(!test))stage.animate[selanimate].Xstart-=speed;
			if(key.right&&(!test))stage.animate[selanimate].Xstart+=speed;
			if(key.up&&(!test))stage.animate[selanimate].Ystart-=speed;
			if(key.down&&(!test))stage.animate[selanimate].Ystart+=speed;
			Xfocus=stage.animate[selanimate].X;
			Yfocus=stage.animate[selanimate].Y;
		}
		break;
	}

	if(selscenery==-1&&selanimate==-1)
	{
		if(facing==0)
		{
			if(key.left)
			{
				facing=1;
				doraemon.lpanimate=&dora_anim[0];
			}
			else if(key.right)
			{
				doraemon.X+=speed;
			}
		}
		else
		{
			if(key.right)
			{
				facing=0;
				doraemon.lpanimate=&dora_anim[1];
			}
			else if(key.left)
			{
				doraemon.X-=speed;
			}
		}
		if(key.down)
		{
			doraemon.Y+=speed;
		}
		if(key.up)
		{
			doraemon.Y-=speed;
		}

		if(key.down||key.up||key.right||key.left)
		{
			PlayAnimate(doraemon.lpanimate);
		}
		else
		{
			StopAnimate(doraemon.lpanimate);
		}

		Xfocus=doraemon.X;
		Yfocus=doraemon.Y;
	}

	if(Xfocus-X>WIDTH/2&&X+speed<=stage.maplength*32-WIDTH)
	{
		X+=speed;
	}
	if((Xfocus-X<WIDTH/2&&X-speed>=0)||X>stage.maplength*32-WIDTH)
	{
		X-=speed;
	}
	if(Yfocus-Y>HEIGHT/2&&Y+speed<=16*32-HEIGHT)
	{
		Y+=speed;
	}
	if((Yfocus-Y<HEIGHT/2&&Y-speed>=0)||Y>16*32-HEIGHT)
	{
		Y-=speed;
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	//画屏幕范围地图

	/////////////////////////////////////////////////////////////////////////////////////////////

	int Xmin=((int)X)/32,Xmax=(((int)X)+WIDTH)/32,Ymin=((int)Y)/32,Ymax=(((int)Y)+HEIGHT)/32,Xstart=-(((int)X)%32),Ystart=-(((int)Y)%32);
	for(i=Xmin;i<=Xmax;i++)
	{
		for(j=Ymin;j<=Ymax;j++)
		{
			SetGraphPos(&tile[stage.map[i][j]],Xstart+(i-Xmin)*32,Ystart+(j-Ymin)*32);
			destrect=tile[stage.map[i][j]].destrect;
			if(Act(mouserect,destrect)&&mouse_ld&&Act(mouserect,maprect)&&ctrlobject==OBJ_TILE)
			{
				stage.map[i][j]=seltile;
			}
			if(Act(mouserect,destrect)&&mouse_rd&&Act(mouserect,maprect)&&ctrlobject==OBJ_TILE)
			{
				stage.collide[i][j]=(stage.collide[i][j]==1?0:1);
			}
			GraphToBack(&tile[stage.map[i][j]]);
			if(viewlattice)
			{
				DrawLine(destrect.right-1,destrect.top-1,destrect.right-1,destrect.bottom-1);
				DrawLine(destrect.right-1,destrect.bottom-1,destrect.left-1,destrect.bottom-1);
			}
			if(stage.collide[i][j]==TRUE)
			{
				if(viewcollide)
				{
					DrawLine(destrect.right-1,destrect.top-1,destrect.left-1,destrect.bottom-1);
					DrawLine(destrect.left-1,destrect.top-1,destrect.right-1,destrect.bottom-1);
				}
			}
		}
	}

	/////////////////////////////////////////////////////////////////////////////////////////////

	//人生不就这样吗,总是这么痛苦不堪,唉,该来的还是要来,这就是人生啊.

	//物体被简单排序进行有序绘制,同Z平面总是按Y轴逐一呈现.

	//排序的原理是,如果一个物体总是比其它物体更后面,就有更多的机会被打上-1的标签,它的负值最大.
	//但这个负值不可能大于物体总数|-(n-1)|,同时存在一个不会大于总数|n-1|的最前面的物体.
	//如果有多个物体在同一水平线,谁先绘制都没有人觉得很奇怪,事实上同一水平线的物体都是按创建时的次序绘制的.
	//(当它还只是数组里的小数据的时候)

	/////////////////////////////////////////////////////////////////////////////////////////////

	int num=0; //统计排序对象总数
	CHARACTER* c[sizeof(stage.scenery)/sizeof(CHARACTER)+sizeof(stage.animate)/sizeof(CHARACTER)+1]; //用于排序的指针数组
	int clist[sizeof(stage.scenery)/sizeof(CHARACTER)+sizeof(stage.animate)/sizeof(CHARACTER)+1]; //用于排序序号的数组
	memset(clist,0,sizeof(clist)); //初始化排序序号为0
	BOOL hasgetindex=0; //目标点选状态
	int hoverindex=-1; //鼠标经过对象
	RECT hoverrect; //鼠标经过对象区域
	for(i=0;i<sizeof(stage.scenery)/sizeof(CHARACTER);i++) //统计对象总数并定位显示位置
	{
		if(stage.scenery[i].visible)
		{
			stage.scenery[i].x=stage.scenery[i].X-X; //象幽灵一样的东西
			stage.scenery[i].y=stage.scenery[i].Y-Y;
			SetCharacterPos(&stage.scenery[i],stage.scenery[i].x,stage.scenery[i].y);
			if(Act(stage.scenery[i].lpgraph->destrect,maprect)) //仅提取满足显示需要的对象
			{
				c[num]=&stage.scenery[i]; //获得对象的指针
				num+=1;
				if(i==selscenery)DrawRect(stage.scenery[i].lpgraph->destrect);
				if(Act(stage.scenery[i].lpgraph->destrect,mouserect)&&ctrlobject==OBJ_SCENERY&&Act(mouserect,maprect))
				{
					if((hasgetindex==1&&stage.scenery[hoverindex].Y<stage.scenery[i].Y)||hasgetindex==0) //用于点选最前端目标
					{
						hoverindex=i;
						hoverrect=stage.scenery[i].lpgraph->destrect;
						hasgetindex=1;
					}
				}
			}
		}
	}
	if(mouse_rd&&Act(mouserect,maprect)&&hoverindex!=-1)
	{
		stage.scenery[hoverindex].visible=0;
		sceneryused=GetSceneryUsed();
		selscenery=-1;
	}
	if(mouse_rd&&Act(mouserect,maprect)&&hoverindex==-1)selscenery=-1;
	if(Act(mouserect,maprect)&&ctrlobject==OBJ_SCENERY)
	{
		if(mouse_ld)selscenery=hoverindex;
		if(mouse_ld&&selscenery==-1)
		{
			int setindex=GetSceneryIndex();
			if(setindex!=-1)
			{
				stage.scenery[setindex].X=mouse_x+X;
				stage.scenery[setindex].Y=mouse_y+Y;
				stage.scenery[setindex].width=32;
				stage.scenery[setindex].height=32;
				stage.scenery[setindex].relation=CHARACTER_GRAPH;
				stage.scenery[setindex].keycolor=0xFFFFFF;
				stage.scenery[setindex].lpgraph=&scenerygraph[selgraph];
				stage.scenery[setindex].visible=1;
				selscenery=setindex;
				sceneryused=GetSceneryUsed();
			}
		}
	}

	doraemon.x=doraemon.X-X; //附加多拉A梦的对象信息
	doraemon.y=doraemon.Y-Y;
	SetCharacterPos(&doraemon,doraemon.x,doraemon.y);
	c[num]=&doraemon;
	num+=1;

	for(i=0;i<sizeof(stage.animate)/sizeof(CHARACTER);i++)
	{
		if(stage.animate[i].visible)
		{
			//碰撞
			int k,l;
			RECT colliderect;
			for(k=(int)(stage.animate[i].X/32);k<(stage.animate[i].X+stage.animate[i].width)/32;k++)
			{
				for(l=(int)(stage.animate[i].Y/32);l<(stage.animate[i].Y+stage.animate[i].height)/32;l++)
				{
					SetRectPos(destrect,k*32,l*32,32,32);
					SetRectPos(colliderect,(int)stage.animate[i].X,(int)stage.animate[i].Y,stage.animate[i].width,stage.animate[i].height);
					if(Act(colliderect,destrect)&&stage.collide[k][l]==TRUE)
					{
						stage.animate[i].X=stage.animate[i].Xhis;
						stage.animate[i].Y=stage.animate[i].Yhis;
						if(rand()%50==0)
						{
							stage.animate[i].Xtarget=stage.animate[i].X+(rand()%WIDTH-WIDTH/2);
							stage.animate[i].Ytarget=stage.animate[i].Y+(rand()%HEIGHT-HEIGHT/2);
						}
						SetRectPos(destrect,k*32-(int)X,l*32-(int)Y,32,32);
						DrawRect(destrect);
					}
				}
			}
			if(!test)
			{
				stage.animate[i].X=stage.animate[i].Xstart;
				stage.animate[i].Y=stage.animate[i].Ystart;
			}
			if(stage.animate[i].X<=0||stage.animate[i].X+stage.animate[i].width>=stage.maplength*32||stage.animate[i].Y<=0||stage.animate[i].Y+stage.animate[i].height>=16*32)
			{
				stage.animate[i].X=stage.animate[i].Xhis;
				stage.animate[i].Y=stage.animate[i].Yhis;
				if(rand()%50==0)
				{
					stage.animate[i].Xtarget=stage.animate[i].X+(rand()%WIDTH-WIDTH/2);
					stage.animate[i].Ytarget=stage.animate[i].Y+(rand()%HEIGHT-HEIGHT/2);
				}
			}
			if(stage.animate[i].Xstart<=0||stage.animate[i].Xstart+stage.animate[i].width>=stage.maplength*32||stage.animate[i].Ystart<=0||stage.animate[i].Ystart+stage.animate[i].height>=16*32)
			{
				stage.animate[i].visible=0;
				selanimate=-1;
				animateused=GetAnimateUsed();
			}
			//DrawLine(stage.animate[i].X-X,stage.animate[i].Y-Y,stage.animate[i].Xhis-X,stage.animate[i].Yhis-Y);
			stage.animate[i].Xhis=stage.animate[i].X;
			stage.animate[i].Yhis=stage.animate[i].Y;

			if(rand()%500==1)
			{
				stage.animate[i].Xtarget=stage.animate[i].X+(rand()%WIDTH-WIDTH/2);
				stage.animate[i].Ytarget=stage.animate[i].Y+(rand()%HEIGHT-HEIGHT/2);
			}
			if((int)stage.animate[i].X<(int)stage.animate[i].Xtarget&&(rand()%50==0))stage.animate[i].intvalue[ACTION]=1;
			if((int)stage.animate[i].X>(int)stage.animate[i].Xtarget&&(rand()%50==0))stage.animate[i].intvalue[ACTION]=2;
			if((int)stage.animate[i].Y<(int)stage.animate[i].Ytarget&&(rand()%50==0))stage.animate[i].intvalue[ACTION]=3;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -