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

📄 game.cpp

📁 超级坦克大战游戏
💻 CPP
📖 第 1 页 / 共 4 页
字号:
#include "StdAfx.h"
#include "Game.h"
#include "ddutil.h"
#include <stdio.h>

//Global Variables:
CGame theGame;
CScreen theScreen;
CMap theMap;

//Defines of the class CMap:
CMap::CMap()
{
	//To do:
	int i;
	for(i=0;i<60;i++)
		STankMap[i]=new unsigned char[1024];
}

bool CMap::AddMap(int index)
{
	//To do:
	int i;
	if(numMap>=60||index>=60) return false;
	unsigned char *temp=STankMap[numMap++];
	if(numMap>0)
		for(i=numMap-1;i>index;i--)
			STankMap[i]=STankMap[i-1];
	STankMap[index]=temp;
	NewMap(STankMap[index]);
	return true;
}

bool CMap::DeleteMap(int index)
{
	//To do:
	if(index<=0) return false;
//	if(STankMap[index-1]!=NULL) delete[] STankMap[index-1];
	NewMap(STankMap[index-1]);
	unsigned char *temp=STankMap[index-1];
	int i;
	for(i=index-1;i<numMap-1;i++)
		STankMap[i]=STankMap[i+1];
	STankMap[numMap-1]=temp;
	numMap--;
	if(numMap<=0) numMap=1;
	return true;
}

void CMap::SetMap(int index,unsigned char *pnewmap)
{
	//To do:
	NewMap(STankMap[index-1]);
	for(int i=0;i<1024;i++)
		*(STankMap[index-1]+i)=*(pnewmap+i);
	return;
}

CMap::~CMap()
{
	//To do:
	int i;
	for(i=0;i<60;i++)
		if(STankMap[i]!=NULL) 
		{
			delete[] STankMap[i];
			STankMap[i]=NULL;
		}
}

bool CMap::LoadMap(char *str)
{
	//To do:
	FILE *fp;
	int i;
	
/*	for(i=0;i<60;i++)
		if(STankMap[i]!=NULL)
		{
			delete[] STankMap[i];
			STankMap[i]=NULL;
		}
*/	char strcheck[16];
	fp=fopen(str,"rb");
	if(fp==NULL) return false;
	fread(strcheck,15,1,fp);
	strcheck[15]=0;
	if(strcmp(strcheck,"STank Map File.")!=0) return false;
	fscanf(fp,"%d\n",&numMap);
	if(numMap>60) return false;
	for(i=0;i<numMap;i++)
	{
		STankMap[i]=new unsigned char[1024];
		fread(STankMap[i],1024,1,fp);
	}
	fclose(fp);
	if(numMap==0)
	{
		NewMap(STankMap[0]);
		numMap++;
	}
	return true;
}

void CMap::NewMap(unsigned char* temp)
{
	int i,j,t;
	for(i=0;i<32;i++)
		for(j=0;j<32;j++)
			*(temp+i+(j<<5))=30;
	t=0;
	for(i=29;i<32;i++)
	{
		*(temp+14+(i<<5))=25+t;
		*(temp+15+(i<<5))=0;
		t=1-t;
		*(temp+17+(i<<5))=25+t;
		*(temp+16+(i<<5))=0;
	}
	*(temp+943)=26;
	*(temp+944)=25;
	*(temp+975)=34;
}

void CMap::SaveMap(char *str)
{
	//To do:
	FILE *fp;
	int i;
	fp=fopen(str,"wb");
	fprintf(fp,"STank Map File.\n");
	fprintf(fp,"%d\n",numMap);
	for(i=0;i<numMap;i++)
		fwrite(STankMap[i],1024,1,fp);
	fclose(fp);
}

int CMap::GetNum(void)
{
	return numMap;
}

void CMap::GetSpt(int index,unsigned char* copy)
{
	if(index<0) return;
	for(int i=0;i<1024;i++)
		*(copy+i)=*(STankMap[index-1]+i);
	return;
}

//Defines of the class CGame:
CGame::CGame():	player1(NULL),player2(NULL),
				Occasion(17),finger(1),timer(0),
				Keyboard1(0),Mouse2(0),MenuItem(0),
				MouseX(0),MouseY(0),dx(0),dy(0),MouseStyle(0),LClick(0),MouseMove(false),
				KeyPGD(false),KeyPGU(false),KeyDel(false),OldX(0),OldY(0),
				KeyESC(false),Current(0),twoplayer(false),
				numEnemy(0),Appear(0),next(0),enemyBorn(false),Complete(false)
{
	//To do:
	srand((unsigned)time(NULL));
	player1=new CTank;
	player2=new CTank;
	pScreen=GetTheScreen();
	pMap=GetTheMap();
}

bool CGame::InitGame(void)
{
	pScreen->SetWnd(hGameWnd);
	if(!pMap->LoadMap(MAPFILE))
	{
		MessageBox(hGameWnd,"Cannot open the file \"STank.map\"!","Error!",MB_OK);
		return(false);
	}
	FILE *fp;
	fp=fopen(DATFILE,"rb");
	if(fp==NULL)
	{
		MessageBox(hGameWnd,"Cannot open the file \"STank.dat\"!","Error!",MB_OK);
		return(false);
	}
	fclose(fp);
	fp=fopen(BMPFILE,"rb");
	if(fp==NULL)
	{
		MessageBox(hGameWnd,"Cannot open the file \"STank.bmp\"!","Error!",MB_OK);
		return(false);
	}
	fclose(fp);
	if(!pScreen->InitDirectDraw())
	{
		MessageBox(hGameWnd,"Cannot initialize DirectDraw!","Error!",MB_OK);
		return(false);
	}
	pScreen->ClearDDSurface(NULL,0);
	pScreen->Flip();
	pScreen->ClearDDSurface(NULL,0);
	return true;
}

void CGame::GameOver(void)
{
	pScreen->PutSpt(225,450,82);
	Occasion=15;
	timer=0;
}

void CGame::Win(void)
{
	Occasion=16;
	timer=0;
}

void CGame::SetPoint(int x,int y)
{
//	OldX=MouseX;
//	OldY=MouseY;
	MouseX=x;
	MouseY=y;
	SetModify(true);
}

void CGame::SetMove(bool b)
{
	MouseMove=b;
}

unsigned char* CGame::GetCurrMap(void)
{
	return CurrMap;
}

unsigned char* CGame::GetCurrImage(void)
{
	return CurrImage;
}

void CGame::SetOccasion(int n)
{
	Occasion=n;
}

void CGame::SetModify(bool m=true)
{
	pScreen->SetModify(m);
}

bool CGame::BeModified(void)
{
	return pScreen->BeModified();
}

void CGame::ReduceEnemy(void)
{
	numEnemy--;
	if(Appear>0)Appear--;
	if(numEnemy<=0)
	{
		Current++;
		Complete=true;
		timer2=0;
	}
//	if(numEnemy<=0&&Current>=pMap->GetNum())
//	{
//		Complete=true;
//		Win();
//	}
}

void CGame::OnTimer(void)
{
	//To do:
	if(pScreen->BeReady()==false)
	{
		pScreen->RestoreDD();
		return;
	}

	switch(Occasion)
	{
	case 0:
	case 4:
	case 7:
	case 11:
		OnMenu();
		break;
	case 2:
	case 6:
	case 9:
		OnMapEdit();
		break;
	case 1:
	case 5:
	case 8:
	case 12:
	case 13:
	case 14:
	case 15:
	case 16:
		OnPlaying();
		break;
	case 10:
		PALETTEENTRY temp[256];
		int i,j;
		pScreen->lpDDPalette->GetEntries(0,0,256,temp);
		for(i=0;i<64;i++)
		{
			for(j=0;j<256;j++)
			{
				if(temp[j].peBlue<4) temp[j].peBlue=0;
				else temp[j].peBlue-=4;
				if(temp[j].peGreen<4) temp[j].peGreen=0;
				else temp[j].peGreen-=4;
				if(temp[j].peRed<4) temp[j].peRed=0;
				else temp[j].peRed-=4;
			}
			pScreen->lpDD->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN,NULL);
			pScreen->lpDDPalette->SetEntries(0,0,256,temp);
		}
		Occasion=3;
		break;
	case 17:
		for(i=0;i<256;i++)
			temp[i].peBlue=temp[i].peFlags=temp[i].peGreen=temp[i].peRed=0;
		pScreen->lpDDPalette->SetEntries(0,0,256,temp);
		for(i=0;i<32;i++)
		{
			pScreen->ClearDDSurface(NULL,0);
			pScreen->PutSpt(264,217,86+(i/2)%3);
			for(j=0;j<256;j++)
			{
				if(temp[j].peBlue>pScreen->DefaultPE[j].peBlue-8) temp[j].peBlue=pScreen->DefaultPE[j].peBlue;
				else temp[j].peBlue+=8;
				if(temp[j].peGreen>pScreen->DefaultPE[j].peGreen-8) temp[j].peGreen=pScreen->DefaultPE[j].peGreen;
				else temp[j].peGreen+=8;
				if(temp[j].peRed>pScreen->DefaultPE[j].peRed-8) temp[j].peRed=pScreen->DefaultPE[j].peRed;
				else temp[j].peRed+=8;

/*				if(temp[j].peBlue>251)temp[j].peBlue=255;
				else temp[j].peBlue+=4;
				if(temp[j].peGreen>251)temp[j].peGreen=255;
				else temp[j].peGreen+=4;
				if(temp[j].peRed>251)temp[j].peRed=255;
				else temp[j].peRed+=4;
*/
			}
			pScreen->lpDD->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN,NULL);
			pScreen->lpDDPalette->SetEntries(0,0,256,temp);
			pScreen->Flip();
			Sleep(50);
		}
//		Sleep(500);
//		LPDIRECTDRAWPALETTE lpDDPal;
//		pScreen->lpDD->CreatePalette(DDPCAPS_8BIT|DDPCAPS_ALLOW256,temp,&lpDDPal,NULL);
//		lpDDPal->SetEntries(0,0,256,temp);
//		pScreen->lpDDSPrimary->SetPalette(lpDDPal);
/*		for(i=0;i<256;i++)
		{
			temp[i].peBlue=pScreen->DefaultPE[i].peBlue;
			temp[i].peFlags=pScreen->DefaultPE[i].peFlags;
			temp[i].peGreen=pScreen->DefaultPE[i].peGreen;
			temp[i].peRed=pScreen->DefaultPE[i].peRed;
		}
*/
 //		pScreen->lpDDPalette->SetEntries(0,0,256,temp);
		for(i=0;i<32;i++)
		{
			pScreen->ClearDDSurface(NULL,0);
			pScreen->PutSpt(264,217,86+(i/2)%3);
			for(j=0;j<256;j++)
			{
				if(temp[j].peBlue>247) temp[j].peBlue=255;
				else temp[j].peBlue+=8;
				if(temp[j].peFlags>251) temp[j].peFlags=255;
				else temp[j].peFlags+=8;
				if(temp[j].peGreen>247) temp[j].peGreen=255;
				else temp[j].peGreen+=8;
				if(temp[j].peRed>247) temp[j].peRed=255;
				else temp[j].peRed+=8;
//				temp[j].peBlue=temp[j].peFlags=temp[j].peGreen=temp[j].peRed=255;
			}
			pScreen->lpDD->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN,NULL);
			pScreen->lpDDPalette->SetEntries(0,0,256,temp);
			pScreen->Flip();
			Sleep(50);
//			lpDDPal->SetEntries(0,0,256,temp);
		}

//		pScreen->ClearDDSurface(NULL,250);
//		pScreen->Flip();
		Occasion=7;
		break;
	case 3:
		PostQuitMessage(0);
		break;
	}
}

void CGame::OnMenu(void)
{
	//To do:
	PALETTEENTRY temp[256];
	int i,j;

	if(pScreen->BeReady()==false)
	{
		pScreen->RestoreDD();
		return;
	}
	switch(Occasion)
	{
	case 0:	
		timer++;
		if(timer>=18) timer=0;
		if(timer%2==0)
		{
			if(Keyboard1&KEY_UP)
			{
				MenuItem=(MenuItem+2)%3;
				Keyboard1&=254;
				SetModify();
			}
			if(Keyboard1&KEY_DOWN)
			{
				MenuItem=(MenuItem+1)%3;
				Keyboard1&=253;
				SetModify();
			}
			if(keyENTER)
			{
				keyENTER=false;
				SetModify();
				switch(MenuItem)
				{
				case 0:
					Occasion=11;
					timer=0;
					MenuItem=0;
					SetModify(true);
					break;
				case 1:
					Occasion=9;
					SetModify(true);
					break;
				case 2:
					Occasion=10;
					SetModify(true);
					break;
				}
			}
		}
		if(timer%3==0)
		{
			finger=-finger;
			SetModify();
		}
		if(BeModified())
		{
			pScreen->ClearDDSurface(NULL,0);
			pScreen->PutSpt(158,30,0);
			pScreen->PutSpt(275,300,36);
			pScreen->PutSpt(275,325,37);
			pScreen->PutSpt(275,350,38);
			pScreen->PutSpt(250,302-finger+MenuItem*25,35);
			pScreen->Flip();
			pScreen->SetModify(false);
		}
		break;
	case 4:
		timer++;
		if(timer>=32)
		{
			timer=0;
			Occasion=0;
			MenuItem=0;
			break;
		}
		pScreen->lpDDPalette->SetEntries(0,(timer-1)<<3,8,pScreen->DefaultPE+((timer-1)<<3));
		break;
	case 7:
		pScreen->lpDDPalette->GetEntries(0,0,256,temp);
		for(i=0;i<64;i++)
		{
			for(j=0;j<256;j++)
			{
				if(temp[j].peBlue<4) temp[j].peBlue=0;
				else temp[j].peBlue-=4;
				if(temp[j].peGreen<4) temp[j].peGreen=0;
				else temp[j].peGreen-=4;
				if(temp[j].peRed<4) temp[j].peRed=0;
				else temp[j].peRed-=4;
			}
			pScreen->lpDD->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN,NULL);
			pScreen->lpDDPalette->SetEntries(0,0,256,temp);
		}

		for(i=0;i<256;i++)
		{
			temp[i].peBlue=0;
			temp[i].peFlags=0;
			temp[i].peGreen=0;
			temp[i].peRed=0;
		}
		pScreen->lpDDPalette->SetEntries(0,0,256,temp);
		pScreen->ClearDDSurface(NULL,0);
		pScreen->PutSpt(158,30,0);
		pScreen->Flip();
		Occasion=4;
		timer=0;
		break;
	case 11:
		timer++;
		if(timer>=18) timer=0;
		if(timer%2==0)
		{
			if(Keyboard1&KEY_UP)
			{
				MenuItem=(MenuItem+2)%3;
				SetModify();
			}
			if(Keyboard1&KEY_DOWN)
			{
				MenuItem=(MenuItem+1)%3;
				SetModify();
			}
			if(keyENTER)
			{
				keyENTER=false;
				SetModify();
				switch(MenuItem)
				{
				case 0:
					twoplayer=false;
					pMap->LoadMap(MAPFILE);
					player1->SetLife(TANKLIFE);
					player1->SetTotal(0);
					Current=1;
					Occasion=14;
					break;
				case 1:
					twoplayer=true;
					pMap->LoadMap(MAPFILE);
					player1->SetLife(TANKLIFE);
					player1->SetTotal(0);
					player2->SetLife(TANKLIFE);
					player2->SetTotal(0);
					Current=1;
					Occasion=14;
					break;
				case 2:
					Occasion=0;
					MenuItem=0;
					timer=0;
					SetModify(true);
					return;
				}
			}
		}
		if(timer%3==0)
		{
			finger=-finger;
			SetModify();
		}
		if(BeModified())
		{
			pScreen->ClearDDSurface(NULL,0);
			pScreen->PutSpt(158,30,0);
			pScreen->PutSpt(275,300,59);
			pScreen->PutSpt(275,325,60);
			pScreen->PutSpt(275,350,61);
			pScreen->PutSpt(250,302-finger+MenuItem*25,35);
			pScreen->Flip();
			pScreen->SetModify(false);
		}
		break;
	}
}

void CGame::OnPlaying(void)
{
	//To do:
	PALETTEENTRY temp[256];
	int i,j,s;

	if(pScreen->BeReady()==false)
	{
		pScreen->RestoreDD();
		return;
	}
	switch(Occasion)
	{
	case 1:
		if(KeyESC) Occasion=7;
		timer++;
		if(timer>=9) timer=0;
		if(player1->GetStatus()==ST_BORN) player1->SetDirection(0);
		if(player2->GetStatus()==ST_BORN)
		{
			player2->SetDirection(0);
			Mouse2=0;
		}
//		if(BeModified())
//		{
//			pScreen->ClearDDSurface(NULL,0);
//		player1->Action();
//		if(twoplayer)
//			player2->Action();
		if(timer%2==0)
		{
			if(player1->GetLife()<=0&&(!twoplayer||player2->GetLife()<=0))
				GameOver();
			if(!OnKey()) break;
			for(i=0;i<32;i++)

⌨️ 快捷键说明

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