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

📄 gamecore.cpp

📁 这是一个完全使用wxWidgets开发库开发的小游戏
💻 CPP
字号:
//---------------------------------------------------------------------------
//
// Name:        GameCore
// Author:      ejoyc
// Created:     2008-6-23 19:00:35
// Description: GameCore作为游戏引擎,为游戏提供最核心功能支持
//
//---------------------------------------------------------------------------

#include "GameCore.h"

int Brick_Cell[7][4][4][4]=
{
	{//id = 0
		{//state =0
			{1,1,0,0},
			{1,1,0,0},
			{0,0,0,0},
			{0,0,0,0}
		},
		{//state =1
			{1,1,0,0},
			{1,1,0,0},
			{0,0,0,0},
			{0,0,0,0}
		},
		{//state =2
			{1,1,0,0},
			{1,1,0,0},
			{0,0,0,0},
			{0,0,0,0}
		},
		{//state =3
			{1,1,0,0},
			{1,1,0,0},
			{0,0,0,0},
			{0,0,0,0}
		} 
	},
	{//id = 1
		{//state =0
			{1,1,1,1},
			{0,0,0,0},
			{0,0,0,0},
			{0,0,0,0}
		},
		{//state =1
			{0,0,0,1},
			{0,0,0,1},
			{0,0,0,1},
			{0,0,0,1}
		},
		{//state =2
			{0,0,0,0},
			{0,0,0,0},
			{0,0,0,0},
			{1,1,1,1}
		},
		{//state =3
			{1,0,0,0},
			{1,0,0,0},
			{1,0,0,0},
			{1,0,0,0}
		} 
	},
	{//id = 2
		{//state =0
			{0,1,0,0},
			{1,1,0,0},
			{1,0,0,0},
			{0,0,0,0},
		},
		{//state =1
			{1,1,0,0},
			{0,1,1,0},
			{0,0,0,0},
			{0,0,0,0},
		},
		{//state =2
			{0,1,0,0},
			{1,1,0,0},
			{1,0,0,0},
			{0,0,0,0},
		},
		{//state =3
			{1,1,0,0},
			{0,1,1,0},
			{0,0,0,0},
			{0,0,0,0},
		} 
	},
	{//id = 3
		{//state =0
			{1,0,0,0},
			{1,1,0,0},
			{0,1,0,0},
			{0,0,0,0},
		},
		{//state =1
			{0,1,1,0},
			{1,1,0,0},
			{0,0,0,0},
			{0,0,0,0},
		},
		{//state =2
			{1,0,0,0},
			{1,1,0,0},
			{0,1,0,0},
			{0,0,0,0},
		},
		{//state =3
			{0,1,1,0},
			{1,1,0,0},
			{0,0,0,0},
			{0,0,0,0},
		} 
	},
	{//id = 4
		{//state =0
			{1,0,0,0},
			{1,0,0,0},
			{1,1,0,0},
			{0,0,0,0},
		},
		{//state =1
			{1,1,1,0},
			{1,0,0,0},
			{0,0,0,0},
			{0,0,0,0},
		},
		{//state =2
			{1,1,0,0},
			{0,1,0,0},
			{0,1,0,0},
			{0,0,0,0},
		},
		{//state =3
			{0,0,0,0},
			{0,0,1,0},
			{1,1,1,0},
			{0,0,0,0},
		} 
	},
	{//id = 5
		{//state =0
			{1,1,0,0},
			{1,0,0,0},
			{1,0,0,0},
			{0,0,0,0},
		},
		{//state =1
			{1,1,1,0},
			{0,0,1,0},
			{0,0,0,0},
			{0,0,0,0},
		},
		{//state =2
			{0,0,1,0},
			{0,0,1,0},
			{0,1,1,0},
			{0,0,0,0},
		},
		{//state =3
			{0,0,0,0},
			{1,0,0,0},
			{1,1,1,0},
			{0,0,0,0},
		} 
	},
	{//id = 6
		{//state =0
			{1,0,0,0},
			{1,1,0,0},
			{1,0,0,0},
			{0,0,0,0},
		},
		{//state =1
			{1,1,1,0},
			{0,1,0,0},
			{0,0,0,0},
			{0,0,0,0},
		},
		{//state =2
			{0,0,1,0},
			{0,1,1,0},
			{0,0,1,0},
			{0,0,0,0},
		},
		{//state =3
			{0,0,0,0},
			{0,1,0,0},
			{1,1,1,0},
			{0,0,0,0},
		} 
	}
};

GameCell::GameCell()
{
	snd_award=new wxSound("IDW_Award",true);
	snd_start=new wxSound("IDW_Start",true);
	snd_lost =new wxSound("IDW_Lost",true);
	snd_move =new wxSound("IDW_Move",true);
	snd_fixup=new wxSound("IDW_Fixup",true);
	//-----------------------------------------
	srand((unsigned   int)wxGetUTCTime());   
	//-----------------------------------------
	for(int Y=0;Y<20;Y++)
	{
		for(int X=1;X<9;X++)
		{
			ShowWall[Y][X]=1;
			TrueWall[Y][X]=1;				
		}			
	}
	Brick_Y=0;
	Brick_X=3;
	Brick_ID=0;
	Brick_Type=0;
	Brick_Next_Type=rand()%4;
	Brick_Next_ID=rand()%7;

	sum_score=0;
}

int GameCell::GetCurrentScore()
{
	return sum_score*100;
}

wxBitmap GameCell::GetMsgBitmap()
{
	wxBitmap bmp(220,100);
	wxBitmap brick(wxBITMAP(IDB_Brick));
	wxMemoryDC memDC;
	memDC.SelectObject(bmp);
	memDC.SetBackground(*wxBLACK);
	memDC.SetTextBackground(*wxBLACK);
	memDC.SetTextForeground(*wxBLUE);
	//------------------------------------
	wxFont font(12, wxFONTFAMILY_SWISS, wxNORMAL, wxFONTWEIGHT_BOLD );
	memDC.SetFont(font);
	//-------------------------------------
	wxString info;
	wxDateTime now=wxDateTime::Now();
	info.Printf(wxT("时间[%02d:%02d:%02d]   分数[%06d]"),now.GetHour(),now.GetMinute(),now.GetSecond(),GetCurrentScore());
	memDC.DrawText(info,wxPoint(0,0));
	memDC.DrawText(wxT("空格键:暂停/开始"),wxPoint(0,20));
	memDC.DrawText(wxT("方向键:方向/变形"),wxPoint(0,40));
	memDC.DrawText(wxT("ESC 键:退出游戏"),wxPoint(0,60));
	memDC.DrawText(wxT("[Powered by ejoyc]"),wxPoint(0,80));	
	//-------------------------------------
	for(int Y=0;Y<4;Y++)
	{
		for(int X=0;X<4;X++)
		{
			if(Brick_Cell[Brick_Next_ID][Brick_Next_Type][Y][X]==1)
			{
				memDC.DrawBitmap(brick,20*X+140,20*Y+20);
			};
		}
	}
	memDC.SelectObject(wxNullBitmap);
	return bmp;
}

wxBitmap GameCell::GetCurrentWall()
{
	wxBitmap bmp(wxBITMAP(IDB_Wall));
	wxBitmap brick(wxBITMAP(IDB_Brick));
	wxMemoryDC memDC;
	memDC.SelectObject(bmp);
	//-------------------------------------
	for(int Y=0;Y<20;Y++)
	{
		for(int X=0;X<10;X++)
		{
			if(ShowWall[Y][X]==1)memDC.DrawBitmap(brick,20*X,20*Y,true);
			if(TrueWall[Y][X]==1)memDC.DrawBitmap(brick,20*X,20*Y,true);
		}
	}
	memDC.SelectObject(wxNullBitmap);
	return bmp;
}

void GameCell::InitWall()
{
	sum_score=0;
	for(int Y=0;Y<20;Y++)
	{
		for(int X=0;X<10;X++)
		{
			ShowWall[Y][X]=0;
			TrueWall[Y][X]=0;				
		}
	}
	PlayMusic(Game_Start);
	wxMilliSleep(500);
}

void GameCell::NewBrick()
{
	for(int Y=0;Y<20;Y++)
	{
		for(int X=0;X<10;X++)
		{
			if(ShowWall[Y][X]==1)
			{
				TrueWall[Y][X]=1;
			}
		}
	}	
	CleanupBrick();
	//------------------------------------------
	Brick_Y=0;
	Brick_X=3;
	Brick_ID=Brick_Next_ID;
	Brick_Type=Brick_Next_Type;
	Brick_Next_ID=rand()%7;
	Brick_Next_Type=rand()%4;
	if (GameOverTest())
	{
		for(int Y=0;Y<20;Y++)
		{
			for(int X=0;X<10;X++)
			{
				ShowWall[Y][X]=0;
				TrueWall[Y][X]=0;
			}
		}
		for(int Y=0;Y<20;Y++)
		{
			for(int X=1;X<9;X++)
			{
				ShowWall[Y][X]=1;
				TrueWall[Y][X]=1;				
			}			
		}
		PlayMusic(Game_Lost);	
	}	
	CleanupBrick();
}

void GameCell::UpdateWall()
{
	//--------------------------------------------
	//检测Brick与Truewall存在重叠	
	for(int Y=0;Y<4;Y++)
	{
		for(int X=0;X<4;X++)
		{
			if(Brick_Cell[Brick_ID][Brick_Type][Y][X]==1&&TrueWall[Brick_Y+Y][Brick_X+X]==1)
			{
				NewBrick();
			}
		}
	}
	//--------------------------------------------
	//复位ShowWall
	for(int Y=0;Y<20;Y++)
	{
		for(int X=0;X<10;X++)
		{
			ShowWall[Y][X]=0;	
		}
	}
	//--------------------------------------------
	//映射Brick的矩阵坐标到ShowWall矩阵坐标
	for(int Y=0;Y<4;Y++)
	{
		for(int X=0;X<4;X++)
		{
			if(Brick_Cell[Brick_ID][Brick_Type][Y][X]==1)
			{
				ShowWall[Brick_Y+Y][Brick_X+X]=1;
			};
		}
	}
}

void GameCell::CleanupBrick()
{
	int old_score=sum_score;

	for(int Y=19;Y>-1;Y--)
	{
		for(int X=0;X<10;X++)
		{
			if (TrueWall[Y][X]==0)
			{
				X=11;
			}
		}
		if(X<11)
		{
			for(int X=0;X<10;X++)
			{
				TrueWall[Y][X]=0;
			}
			sum_score++;
		}
	}
	//--------------------------------------------------
	if(sum_score==old_score)return;
	PlayMusic(Game_Fixup);
	if(sum_score-old_score>=4)
	{
		sum_score+=10;
		PlayMusic(Game_Award);
		wxMilliSleep(2500);
	}
	if(sum_score==100)
	{
		for(int Y=0;Y<20;Y++)
		{
			for(int X=0;X<10;X++)
			{
				TrueWall[Y][X]=0;
			}
		}
		PlayMusic(Game_Award);
		wxMilliSleep(2500);
	}
	
	//--------------------------------------------------
	//--------------------------------------------------
	int tempWall[20][10];
	for(int Y=0;Y<20;Y++)
	{
		for(int X=0;X<10;X++)
		{
			tempWall[Y][X]=TrueWall[Y][X];
			TrueWall[Y][X]=0;
		}
	}
	//--------------------------
	for(int Y=19,tY=19;tY>-1;tY--)
	{
		for(int X=0;X<10;X++)
		{
			if (tempWall[tY][X]==1)
			{
				X=11;
			}
		}
		if(X>10)
		{
			for(int X=0;X<10;X++)
			{
				TrueWall[Y][X]=tempWall[tY][X];
			}
			Y--;
		}
	}
}

bool GameCell::GameOverTest()
{
	for(int Y=0;Y<4;Y++)
	{
		for(int X=0;X<4;X++)
		{
			if(Brick_Cell[Brick_ID][Brick_Type][Y][X]==1&&TrueWall[Y+Brick_Y][X+Brick_X]==1)
			{
				return true;
			};
		}
	}
	return false;
}

void GameCell::ShapeBrick()
{
	Brick_Type=(Brick_Type+1)%4;
	//--------------------------------------------
	//检测Brick与Truewall存在重叠	
	for(int Y=0;Y<4;Y++)
	{
		for(int X=0;X<4;X++)
		{
			if(Brick_Cell[Brick_ID][Brick_Type][Y][X]==1&&TrueWall[Brick_Y+Y][Brick_X+X]==1)
			{
				Brick_Type=(Brick_Type+3)%4;
			}
		}
	}

	for(int Y=0;Y<4;Y++)
	{
		for(int X=0;X<4;X++)
		{
			if(Brick_Cell[Brick_ID][Brick_Type][Y][X]==1&&(Brick_X+X>9))
			{
				Brick_Type=(Brick_Type+3)%4;
				X=4,Y=4;
			}
		}
	}

	for(int Y=0;Y<4;Y++)
	{
		for(int X=0;X<4;X++)
		{
			if(Brick_Cell[Brick_ID][Brick_Type][Y][X]==1&&(Brick_X+X<0))
			{
				Brick_Type=(Brick_Type+3)%4;
				X=4,Y=4;
			}
		}
	}
	PlayMusic(Game_Move);
}
void GameCell::MoveToLeft()
{
	Brick_X--;
	if(Brick_X<4)
	{
		for(int Y=0;Y<4;Y++)
		{
			for(int X=0;X<4;X++)
			{
				if(Brick_Cell[Brick_ID][Brick_Type][Y][X]==1&&(Brick_X+X)<0)
				{
					Brick_X++;
					X=4,Y=4;
				};
			}
		}
	}
	for(int Y=0;Y<4;Y++)
	{
		for(int X=0;X<4;X++)
		{
			if(Brick_Cell[Brick_ID][Brick_Type][Y][X]==1&&TrueWall[Brick_Y+Y][Brick_X+X]==1)
			{
				Brick_X++;
				X=4,Y=4;
			}
		}
	}
}
void GameCell::MoveToRight()
{
	Brick_X++;
	if(Brick_X>5)
	{
		for(int X=3;X>-1;X--)
		{
			for(int Y=0;Y<4;Y++)
			{
				if(Brick_Cell[Brick_ID][Brick_Type][Y][X]==1&&(Brick_X+X)>9)
				{

					Brick_X=9-X;
					X=-2,Y=5;
				};
			}
		}		
	}
	for(int Y=0;Y<4;Y++)
	{
		for(int X=0;X<4;X++)
		{
			if(Brick_Cell[Brick_ID][Brick_Type][Y][X]==1&&TrueWall[Brick_Y+Y][Brick_X+X]==1)
			{
				Brick_X--;
				X=4,Y=4;
			}
		}
	}

}

void GameCell::MoveToDown()
{
	int tBrick_Y=(++Brick_Y);
	if(Brick_Y>15)
	{
		for(int Y=0;Y<4;Y++)
		{
			for(int X=0;X<4;X++)
			{
				if(Brick_Cell[Brick_ID][Brick_Type][Y][X]==1&&(tBrick_Y+Y)==20)
				{
					X=4,Y=4;
					NewBrick();
					return ;
				};
			}
		}
	}
	//--------------------------------------------
	//检测Brick与Truewall存在重叠	
	for(int Y=0;Y<4;Y++)
	{
		for(int X=0;X<4;X++)
		{
			if(Brick_Cell[Brick_ID][Brick_Type][Y][X]==1&&TrueWall[tBrick_Y+Y][Brick_X+X]==1)
			{
				NewBrick();
				return;
			}
		}
	}
	PlayMusic(Game_Move);
}

void GameCell::PlayMusic(int music_type)//播放声音
{
	switch (music_type)
	{
	case 0:
		if (snd_move->IsOk())
			snd_move->Play(wxSOUND_ASYNC);
		break;
	case 1:
		if (snd_fixup->IsOk())
			snd_fixup->Play(wxSOUND_ASYNC);
		break;
	case 2:
		if (snd_award->IsOk())
			snd_award->Play(wxSOUND_ASYNC);
		break;
	case 3:
		if (snd_start->IsOk())
			snd_start->Play(wxSOUND_ASYNC);
		break;
	case 4:
		if (snd_lost->IsOk())
			snd_lost->Play(wxSOUND_ASYNC);

	}
}

⌨️ 快捷键说明

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