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

📄 czdemo.cpp

📁 gamecode 很不错的小游戏源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
							GL_GameLoop();//这个必须放在timer消息处理以外,否则会引起不能
											//及时的killtimer而使人物多走一小步!!
					}
					//查看是否有玩家已经胜利。
					/*if(players[PlayerID].GetMoney() > 10000)
					{
					//	GL_GameOver();
					}*/
				}									
				break;
				
			case WM_DESTROY:
				FreeDDraw();//释放所有DirectDraw对象
				ReleaseDirectSound();//释放DirectSound
				FreeMidi();
				PostQuitMessage(0);
				break;
			default:
				return DefWindowProc(hWnd, message, wParam, lParam);			
				
   }
   return 0;
}


//用于简单判定是否在矩形范围内
BOOL InRegion(int x,int y,int topx,int topy,int butox,int butoy)
{
	if(x>=topx && y>=topy && x<=butox && y<=butoy)
		return true;
	else
		return false;
}


//*************************
//按下键后松开
//GetAsyncKeyState 's Return Values:
//If the function succeeds, the return value specifies whether the key
// was pressed since the last call to GetAsyncKeyState, and whether the
// key is currently up or down. 
//If the most significant bit is set, the 
//key is down, and if the least significant bit is set, the key was pressed 
//after the previous call to GetAsyncKeyState. The return value is zero if a 
//window in another thread or process currently has the keyboard focus.

void PressKey(const DWORD& Key,const int& x)
{
	if( x==WAIT )//WAIT=0
		while( !GetAsyncKeyState(Key) )//一直等待,直到按下Key后退出。
		{
//			WaitMessage();
			Sleep(100);			//这样可以减少CPU的负荷。
		}
	else
		while( GetAsyncKeyState(Key) )//按下Key时等待
		{
//			WaitMessage();
			Sleep(100);			//这样可以减少CPU的负荷。
		}
}

/////////////////////////////////////////////////////////////////////////////////
////游戏进入时的选择菜单。
int  ChooseMenu(MyGame& mygame)
{
	int bx=735;
	int by=340;
	int choose=0;
	showmmbu();
	Blt(pBBuf,bx-(choose)*85-60,by+(choose)*110,finger,GetRect(0,0,60,49),TRUE);	
	Blt(pBBuf,bx-(choose)*85,by+(choose)*110,button2,GetRect(0,choose*60,220,(choose+1)*60),TRUE);
	Flip();
	while(mygame._IsMenu()==true)
	{
		PressKey(VK_RETURN,1);
		PressKey(VK_CONTROL,1);//吸收Ctrl
		if( GetAsyncKeyState(VK_DOWN) )	//下一项
		{
			PressKey(VK_DOWN,1);
			choose=(choose+1)%4;
//			ShowMenuCus(x, y, choose, MainMenu, Num, step,RGB(0,0,0));//刷新菜单
			showmmbu();
			Blt(pBBuf,bx-(choose)*85-60,by+(choose)*110,finger,GetRect(0,0,60,49),TRUE);
			Blt(pBBuf,bx-(choose)*85,by+(choose)*110,button2,GetRect(0,choose*60,220,(choose+1)*60),TRUE);
			PlayDirectSound(selectsnd);
			Flip();
		}
		if( GetAsyncKeyState(VK_UP) )	//上一项
		{
			PressKey(VK_UP,1);
			if( choose==0 ) 
				choose=4-1;
			else 
				choose--;
//			ShowMenuCus(x, y, choose, MainMenu, Num,step, RGB(0,0,0));//刷新菜单
			showmmbu();
			Blt(pBBuf,bx-(choose)*85-60,by+(choose)*110,finger,GetRect(0,0,60,49),TRUE);
			Blt(pBBuf,bx-(choose)*85,by+(choose)*110,button2,GetRect(0,choose*60,220,(choose+1)*60),TRUE);
			PlayDirectSound(selectsnd);
			Flip();
		}
		if( GetAsyncKeyState(VK_RETURN) )	//确定
		{
			PressKey(VK_RETURN,1);	//防止连击。
			int choice=0;
			switch (choose)
			{
			case 0://新游戏
				choice=choosemap();
				switch (choice)
				{
				case 0:
					mapindex=1;
					actualpn=GL_InitGame(1);
					mygame._SetMenu(false);
					mygame._SetBegin(true);
					mygame.SetMode(actualpn);//单一玩家。
					return 0;
					break;
				case 1:
					mapindex=2;
					actualpn=GL_InitGame(2);
					mygame._SetMenu(false);
					mygame._SetBegin(true);
					mygame.SetMode(actualpn);//单一玩家。
					return 0;
					break;
				case 2:
					mapindex=3;
					actualpn=GL_InitGame(3);
					mygame._SetMenu(false);
					mygame._SetBegin(true);
					mygame.SetMode(actualpn);//单一玩家。
					return 0;
					break;
				case 3:
					{
						showmmbu();
						Blt(pBBuf,bx-(choose)*85-60,by+(choose)*110,finger,GetRect(0,0,60,49),TRUE);
						Blt(pBBuf,bx-(choose)*85,by+(choose)*110,button2,GetRect(0,choose*60,220,(choose+1)*60),TRUE);
						Flip();
					}
					break;
				}
				break;
			case 1://旧的回忆
				choice=choosemap();
				switch (choice)
				{
				case 0:
					mapindex=1;
					actualpn=GL_InitGame(1);
					mygame._SetMenu(false);
					mygame._SetBegin(true);
					mygame.SetMode(actualpn);
					maps.LoadMap(mapindex);
					maps.LoadBlock(mapindex);
					mygame.LoadGame(mybank.money,1);
					return 0;
					break;
				case 1:
					mapindex=2;
					actualpn=GL_InitGame(2);
					mygame._SetMenu(false);
					mygame._SetBegin(true);
					mygame.SetMode(actualpn);
					maps.LoadMap(mapindex);
					maps.LoadBlock(mapindex);
					mygame.LoadGame(mybank.money,2);
					return 0;
					break;
				case 2:
					mapindex=3;
					actualpn=GL_InitGame(3);
					mygame._SetMenu(false);
					mygame._SetBegin(true);
					mygame.SetMode(actualpn);
					maps.LoadMap(mapindex);
					maps.LoadBlock(mapindex);
					mygame.LoadGame(mybank.money,3);
					return 0;
					break;
				case 3:
					{
						showmmbu();
						Blt(pBBuf,bx-(choose)*85-60,by+(choose)*110,finger,GetRect(0,0,60,49),TRUE);
						Blt(pBBuf,bx-(choose)*85,by+(choose)*110,button2,GetRect(0,choose*60,220,(choose+1)*60),TRUE);
						Flip();
					}
					break;
				}
				break;
				break;
			case 2://退出
				PostMessage(hWnd, WM_DESTROY, 0, 0);
				return 0;
				break;
			case 3://制作小组
				PressKey(VK_RETURN,1);
				CreateBitmap(five_man,1024,768,"pics/five_man.fim",DDSCAPS_SYSTEMMEMORY);
				Blt(pBBuf,0,0,five_man,GetRect(0,0,1024,768),FALSE);
				PrintText(pBBuf,200,700,"请按回车键返回...");
				Flip();
				PressKey(VK_RETURN,0);
				showmmbu();
				Blt(pBBuf,bx-(choose)*85-60,by+(choose)*110,finger,GetRect(0,0,60,49),TRUE);
				Blt(pBBuf,bx-(choose)*85,by+(choose)*110,button2,GetRect(0,choose*60,220,(choose+1)*60),TRUE);
				Flip();
				break;
			}
		}
	}
	return 0;
}

void showmmbu()//main menu button
{
	Blt(pBBuf, 0,0, lpDDSBackGround, GetRect(0,0,ScreenW,ScreenH), FALSE);
	Blt(pBBuf,735,340,button1,GetRect(0,0,220,60),TRUE);
	Blt(pBBuf,650,450,button1,GetRect(0,60,220,60*2),TRUE);
	Blt(pBBuf,565,560,button1,GetRect(0,60*2,220,60*3),TRUE);
	Blt(pBBuf,480,670,button1,GetRect(0,60*3,220,60*4),TRUE);

}

void showmapch()//choose map
{
	Blt(pBBuf, 0,0, lpDDSBackGround, GetRect(0,0,ScreenW,ScreenH), FALSE);
	PrintText(pBBuf,650,650,"请选择所要到达的开发区!");
	Blt(pBBuf,475,300,button3,GetRect(0,0,150,50),FALSE);
	Blt(pBBuf,475,400,button3,GetRect(0,50,150,50*2),FALSE);
	Blt(pBBuf,475,500,button3,GetRect(0,50*2,150,50*3),FALSE);
	Blt(pBBuf,475,600,button3,GetRect(0,50*3,150,50*4),FALSE);
}

int choosemap()
{
	showmapch();
	int bx=475;
	int by=300;
	Blt(pBBuf,bx,by,button3,GetRect(0,200,150,250),FALSE);
	Flip();
	int Num=4;//选项个数
	int thechoice=0;//选项
	bool dochoose=true;//在选择之中
	while(dochoose)
	{
		PressKey(VK_CONTROL,1);//吸收Ctrl
		PressKey(VK_RETURN,1);
		PressKey(VK_LEFT,1);
		PressKey(VK_RIGHT,1);
		if( GetAsyncKeyState(VK_DOWN) )	//下一项
		{
			PressKey(VK_DOWN,1);
			thechoice=(thechoice+1)%Num;
			showmapch();						
			Blt(pBBuf,475,by+thechoice*100,button3,\
				GetRect(0,200+thechoice*50,150,200+(thechoice+1)*50),FALSE);
			PlayDirectSound(selectsnd);
			Flip();
		}
		if( GetAsyncKeyState(VK_UP) )	//上一项
		{
			PressKey(VK_UP,1);
			if( thechoice==0 ) 
			{
				thechoice=Num-1;
			}
			else 
			{
				thechoice--;
			}
			showmapch();						
			Blt(pBBuf,475,by+thechoice*100,button3,\
				GetRect(0,200+thechoice*50,150,200+(thechoice+1)*50),FALSE);
			PlayDirectSound(selectsnd);
			Flip();
		}
		if( GetAsyncKeyState(VK_RETURN) )	//确定
		{
			PressKey(VK_RETURN,1);
			switch (thechoice)
			{
			case 0:
				{
					return 0;
				}
				break;
			case 1:
				{
					return 1;
				}
				break;
			case 2:
				{
					return 2;
				}
				break;
			case 3:
				{
					dochoose=false;
					return 3;
				}
				break;
			}
			dochoose=false;
		}
	}
				
	return 0;
}
//判断playerid玩家是否已over
void checkover(int playerid)
{
	if(players[PlayerID].GetMoney()+mybank.GetStore(PlayerID)<0)
	{
		players[PlayerID].existence=false;
		char* info="已经没有一点钱了,作为一个失败的投资者,我感到无比遗憾,既然这样,我只能被遣送回地球了,希望有一天还能再来。再见!";
		MyMessage::PutMSG(SHOWMSG,info);
		mymessage.RunMSG();
		
		//处理该玩家拥有的地产及工厂:
		//...
		int playerBuinum=players[PlayerID].buildingnum;
		int playerFacnum=players[PlayerID].factorynum;
		if(playerBuinum==0 && playerFacnum==0)
			return;//没有建筑
		else
		{
			if(playerBuinum!=0)
			{
				for(int i=0;i<playerBuinum;i++)//取消所有地产所有权
				{
					MyBuilding tempb;
					tempb=maps.GetBuilding(PlayerID);//取得建筑
					//BEGOVT设置为无人管理
					maps.ChangeOwnerB(tempb.x,tempb.y,PlayerID,BEGOVT);
				}
			}
			if(playerFacnum!=0)
			{
				for(int j=0;j<playerFacnum;j++)//取消所有工厂所有权
				{
					MyFactory tempf;
					tempf=maps.GetFactory(PlayerID);//取得工厂
					maps.ChangeOwnerF(tempf.x,tempf.y,PlayerID,BEGOVT);
					//BEGOVT设置为无人管理
				}
			}			
			MyMenus mymenu(360,260,"pics/menu.fim",280,120,GetRect(0,0,280,120));
			char* tchoice[]={"确定"};	
			PressKey(VK_RETURN,1);
			mymenu.ShowtMenu("该玩家房产已归政府!",tchoice,1);	
			GL_GamePicShowSilence();
			Flip();
		}
	}
	return ;
}

//游戏主循环:
void GL_GameLoop()
{
	if( steps>6 && players[PlayerID].doubspeed!=true || steps>12 && players[PlayerID].doubspeed==true )//已经行走完步数。
	{
		steps=0;
		KillTimer(hWnd,1);
		//还原更改的属性
		if(players[PlayerID].doubspeed==true)//高级汽车
			players[PlayerID].doubspeed=false;
		if(players[PlayerID].choosestep!=0)//摇控色子
			players[PlayerID].choosestep=0;
		if(players[PlayerID].onestep==true)//乌龟卡
			players[PlayerID].onestep=false;
		if(players[PlayerID].stop==true)//停留卡
			players[PlayerID].stop=false;

		GL_CheckEvent();//每次结束行走消息,在这里判断人物所在当前格是否有事件。

		checkover(PlayerID);
		PressKey(VK_RETURN,1);
		GL_GamePicShowSilence();
		Flip();

	
	//其它事件:
	{
		//实验://///////////////////////////////////////////
		gamegovt.JudgePlayerHonour(PlayerID);//判断玩家封号
		gamegovt.ShowEvent();//显示消息
		///////////////////////////////////////////////////////		
	}
	//经过一个回合,即过一天。
	if(playercount>=mygame.GetMode())		
	{
		gametime.TimeGoes();	//更新游戏时间
		playercount=0;				//计数清0.
	}
	if(gametime.WeekPassed()==true)	//经过一周。
	{
		if(maps.pbuilding.listlength()>0 || maps.pfactory.listlength()>0)
		{
			char* info="本周投资收入了!";
			MyMessage::PutMSG(SHOWMSG,info);
			mymessage.RunMSG();

			//……处理-所有-玩家的所有收入:
			long tmoneyb=0l;
			long tmoneyf=0l;
			if(maps.pbuilding.listlength()>0 )
			{
				for(int i=0;i<mygame.GetMode();i++)
				{
					maps.GetProfitofB(i,tmoneyb);
					players[i].ChangeMoney(tmoneyb);//玩家得到利润。
				}
			}
			if(maps.pfactory.listlength()>0 )
			{
				for(int i=0;i<mygame.GetMode();i++)
				{
					maps.GetProfitofF(i,tmoneyf);
					players[i].ChangeMoney(tmoneyf);
				}
			}	
		}	
//		gameenvcons.MakeDecision();
	}
	if(gametime.MonthPassed()==true)
	{
		char* info="上月银行红利发放!";
		MyMessage::PutMSG(SHOWMSG,info);
		mymessage.RunMSG();
		for(int i=0;i<playernum;i++)
		{
			mybank.MonProfit(i);//i为PlayerID;
		}
		///////////////////////////////////////////////
		gameworld.SomethingHappen();
		mymessage.RunMSG(PlayerID);
	}	

		GL_SysMenu();//每回合后的系统选单
		
		//PressKey(VK_RETURN,WAIT);
		PlayerID++;//切换玩家
		if(PlayerID>=mygame.GetMode())
		{
			PlayerID=0;
		}
		while(players[PlayerID].IsExist()==false)
		{
			PlayerID++;
			//暂时这样:
			if( !players[0].IsExist() && !players[1].IsExist() && !players[2].IsExist() && !players[3].IsExist() )//所有玩家都已退出
			{
				GL_GamePicShowSilence();
				Flip();
				MyMenus mymenu(360,260,"pics/menu.fim",280,120,GetRect(0,0,280,120));
				char* tchoice[]={"确定"};	
				int sure;
				PressKey(VK_RETURN,1);
				sure=mymenu.ShowtMenu("所有人已经撤退!",tchoice,1);
				
				PostMessage(hWnd, WM_DESTROY, 0, 0);//测试成功,可以。
				FreeDDraw();//释放所有DirectDraw对象
				ReleaseDirectSound();//释放DirectSound
				FreeMidi();
				PostQuitMessage(0);
				return;

⌨️ 快捷键说明

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