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

📄 bmframe.cpp

📁 类似泡泡堂程序代码 类属游戏
💻 CPP
📖 第 1 页 / 共 2 页
字号:
					if (player[1].movetime == 20)
					{
						player[1].movetime = 0;
					}
				}
				else
				{
					player[1].movetime = 0;
					player[1].lastaction = 0;
				}		
				player[1].offsetX = 60 * (player[1].movetime/10+1);

				//位置移动	
				player[1].doMove();
			}
			else if (keybuffer[DIK_UP] & 0x80)	//按下 上 键
			{
				//贴图选择
				if (player[1].lastaction == 1)
				{
					player[1].movetime++;
					if (player[1].movetime == 20)
					{
						player[1].movetime = 0;
					}
				}
				else
				{
					player[1].movetime = 0;
					player[1].lastaction = 1;
				}		
				player[1].offsetX = 60 * (player[1].movetime/10+1);

				//位置移动	
				player[1].doMove();
			}
			else if (keybuffer[DIK_LEFT] & 0x80)	//按下 左 键
			{
				//贴图选择
				if (player[1].lastaction == 2)
				{
					player[1].movetime++;
					if (player[1].movetime == 20)
					{
						player[1].movetime = 0;
					}
				}
				else
				{
					player[1].movetime = 0;
					player[1].lastaction = 2;
				}		
				player[1].offsetX = 60 * (player[1].movetime/10+1);

				//位置移动	
				player[1].doMove();
			}
			else if (keybuffer[DIK_RIGHT] & 0x80)	//按下 右 键
			{
				//贴图选择
				if (player[1].lastaction == 3)
				{
					player[1].movetime++;
					if (player[1].movetime == 20)
					{
						player[1].movetime = 0;
					}
				}
				else
				{
					player[1].movetime = 0;
					player[1].lastaction = 3;
				}		
				player[1].offsetX = 60 * (player[1].movetime/10+1);

				//位置移动
				player[1].doMove();
			}
			else
			{
				player[1].movetime = 0;
				player[1].offsetX = 0;
			}
			player[1].offsetY = 60 * player[1].lastaction;

			if (keybuffer[DIK_NUMPADENTER] & 0x80)	//按下 小键盘回车 键
			{
				i = ((int)player[1].posY+40)/40-1;
				j = ((int)player[1].posX+30)/40-1;

				if (player[1].laytime==0 && player[1].bombcount<player[1].bombsum && map[i][j]>='`')
				{
					player[1].laytime++;
					bomb[bombtotal].index = 1;
					bomb[bombtotal].setStart();
					DSBuf[1]->Play(0,0,0);
					player[1].bombcount ++;
					bombtotal ++;
				}
			}
		}
	}
}



void BMFrame::renderFrame()
{
	if (gamestate == MAINMENU)
	{
		//绘制主菜单
		DDBuf->BltFast(0, 0, DDPla[5], CRect(0, 0, 800, 600), DDBLTFAST_WAIT);
		if (gametime > 25)
			gametime = 25;
		if (menudown)
		{
			DDBuf->BltFast(300, 70-gametime*2, DDPla[6],
							CRect(200*menuselect, 0, 200+200*menuselect, 100+gametime*2),
							DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
			DDBuf->BltFast(300, 20, DDPla[6],
							CRect(200*menuselect, 100+gametime*2, 200+200*menuselect, 150),
							DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
		}
		else
		{
			DDBuf->BltFast(300, 20, DDPla[6],
							CRect(200*menuselect, 50-gametime*2, 200+200*menuselect, 150),
							DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
			DDBuf->BltFast(300, 120+gametime*2, DDPla[6],
							CRect(200*menuselect, 0, 200+200*menuselect, 50-gametime*2),
							DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
		}

	}
	else
	{
		//绘制背景
		DDBuf->BltFast(0, 0, DDPla[0], CRect(0, 0, 800, 600), DDBLTFAST_WAIT);

		//搜索地图
		for (i = 0; i < 13; i++)
		{
			for (j = 17; j >= 0; j--)
			{
				isthing = false;
				isstate = false;

				//判断物体类型
				if (map[i][j] < '`')
					isthing =true;
				if (map[i][j] > '`')
					isstate =true;
				
				//绘制障碍
				if (isthing)
				{
					picnum = (map[i][j]-'A');
					DDBuf->BltFast(j*40+35, i*40+10, DDPla[2], 
						CRect(picnum%4*50, picnum/4*80, picnum%4*50+50, picnum/4*80+80),
						DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
				}
				//绘制效果
				if (isstate)
				{
					//道具浮动效果
					if (map[i][j]>='m' && map[i][j]<='x')
						map[i][j] = (map[i][j]-'m')/4*4 + 'm' + gametime/20%4;

					picnum = (map[i][j]-'a');
					DDBuf->BltFast(j*40+35, i*40+10, DDPla[3], 
									CRect(picnum%4*50, picnum/4*80, picnum%4*50+50, picnum/4*80+80),
									DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
				}
			}
			//Player1
			if (i*40<=(int)player[0].posY && i*40+40>(int)player[0].posY)
			{
				//绘制人物
				if (player[0].alive)
				{
					DDBuf->BltFast((int)player[0].posX, (int)player[0].posY, DDPla[1],
									CRect(player[0].offsetX, player[0].offsetY, player[0].offsetX+60, player[0].offsetY+60),
									DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
				}
				//绘制被炸效果
				else
				{
					if ((int)player[0].posY < 50)
					{
						DDBuf->BltFast((int)player[0].posX-10, 0, DDPla[4],
										CRect(player[0].offsetX, player[0].offsetY+50-(int)player[0].posY, player[0].offsetX+80, player[0].offsetY+120),
										DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
					}
					DDBuf->BltFast((int)player[0].posX-10, (int)player[0].posY-50, DDPla[4],
									CRect(player[0].offsetX, player[0].offsetY, player[0].offsetX+80, player[0].offsetY+120),
									DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
				}
			}
			//Player2
			if (gamestate == VERSUS 
					&& i*40<=(int)player[1].posY && i*40+40>(int)player[1].posY)
			{
				//绘制人物
				if (player[1].alive)
				{
					DDBuf->BltFast((int)player[1].posX, (int)player[1].posY, DDPla[1],
									CRect(player[1].offsetX+180, player[1].offsetY, player[1].offsetX+240, player[1].offsetY+60),
									DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
				}
				//绘制被炸效果
				else
				{
					if ((int)player[1].posY < 50)
					{
						DDBuf->BltFast((int)player[1].posX-10, 0, DDPla[4],
										CRect(player[1].offsetX+320, player[1].offsetY+50-(int)player[1].posY, player[1].offsetX+400, player[1].offsetY+120),
										DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
					}
					DDBuf->BltFast((int)player[1].posX-10, (int)player[1].posY-50, DDPla[4],
									CRect(player[1].offsetX+320, player[1].offsetY, player[1].offsetX+400, player[1].offsetY+120),
									DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
				}
			}
		}
	}
}

void BMFrame::playSingle()
{
	//炸弹进行
	for (i=0; i<bombtotal; i++)
	{
		if (bomb[i].doBlast())
			delBomb();
	}
	player[0].doPlay();
}

void BMFrame::playVersus()
{
	//炸弹进行
	for (i=0; i<bombtotal; i++)
	{
		if (bomb[i].doBlast())
			delBomb();
	}
	player[0].doPlay();
	player[1].doPlay();
	//判断被碰
	if (abs((int)player[0].posX-(int)player[1].posX)<30
			&& abs((int)player[0].posY-(int)player[1].posY)<30)
	{
		if(!player[0].alive && player[1].alive && player[0].traptime<420)
			player[0].traptime = 420;
		if(!player[1].alive && player[0].alive && player[1].traptime<420)
			player[1].traptime = 420;

	}
}

void BMFrame::delBomb()
{
	if (bombtotal-1 != i)
		bomb[i] = bomb[bombtotal-1];
	bombtotal --;
}


void BMFrame::ColorKey(int num)
{
	key.dwColorSpaceHighValue = 0;
	key.dwColorSpaceLowValue = 0;
	DDPla[num]->SetColorKey(DDCKEY_SRCBLT,&key);
}


void BMFrame::CreateDDPla(int width,int height,char* filename,int num)
{
//设定幕后内存区大小
	DDde.dwWidth = width;
	DDde.dwHeight = height; 
//建立幕后内存区
	result = DD->CreateSurface(&DDde, &DDPla[num], NULL); 
	if(result !=DD_OK)
	{
  		MessageBox("建立幕后内存区失败!");
		return;
	}
//载入位图		
	bitmap = (HBITMAP)::LoadImage(NULL,filename,IMAGE_BITMAP,width,height,LR_LOADFROMFILE); 
	if(bitmap==NULL)
	{
		MessageBox(filename);
		return;
	}
//复制位图到绘图中
	::SelectObject(hdc,bitmap);
	DDPla[num]->GetDC( &dhdc );
	::BitBlt( dhdc , 0 , 0 ,width,height, hdc , 0 , 0 , SRCCOPY );
	DDPla[num]->ReleaseDC(dhdc);
}

void BMFrame::CreateDSBuf(char* filename,int num)
{	
//开启与检查文件格式
	hmmio = mmioOpen(filename, NULL, MMIO_ALLOCBUF|MMIO_READ );
	if(hmmio == NULL)      
	{
		MessageBox("文件不存在!");
		return;
	}
	ckRiff.fccType = mmioFOURCC('W', 'A', 'V', 'E');
	mmresult = mmioDescend(hmmio,&ckRiff,NULL,MMIO_FINDRIFF);
	if(mmresult != MMSYSERR_NOERROR)
	{
		MessageBox("文件格式错误!");
		return;
	}
	ckInfo.ckid = mmioFOURCC('f','m','t',' '); 
	mmresult = mmioDescend(hmmio,&ckInfo,&ckRiff,MMIO_FINDCHUNK);
	if(mmresult != MMSYSERR_NOERROR)
	{
		MessageBox("文件格式错误!");
		return;
	}
//读取文件格式
	mmresult = mmioRead(hmmio,(HPSTR)&wfmt,sizeof(wfmt));
	if(mmresult == -1)
	{
		MessageBox("读取格式失败!");
		return;
	}
	mmresult = mmioAscend(hmmio,&ckInfo,0);     
	ckInfo.ckid = mmioFOURCC('d','a','t','a'); 
	mmresult = mmioDescend(hmmio,&ckInfo,&ckRiff,MMIO_FINDCHUNK);
	if(mmresult != MMSYSERR_NOERROR)
	{
		MessageBox("文件格式错误!");
		return;
	}
	wsize = ckInfo.cksize; 
//设定次缓冲区特性
	memset( &DSde,0,sizeof(DSde));     
    DSde.dwSize  = sizeof(DSde);      
    DSde.dwFlags = 	 DSBCAPS_STATIC | DSBCAPS_GLOBALFOCUS;
    DSde.dwBufferBytes = wsize;         
    DSde.lpwfxFormat   = &wfmt;  
//建立次缓冲区
    result = DS->CreateSoundBuffer( &DSde, &DSBuf[num], NULL );
	if(result != DS_OK)
	{
		MessageBox("建立次缓冲区失败!");
		return;
	}
//锁定次缓冲区并载入音文件
	result = DSBuf[num]->Lock(0,wsize,&pAudio,&bytesAudio,NULL,NULL,NULL);
	if(result != DS_OK)
	{
		MessageBox("锁定缓冲区失败!");
		return;
	}
	mmresult = mmioRead(hmmio,(HPSTR)pAudio,bytesAudio);
	if(mmresult == -1)
	{
		MessageBox("读取音文件数据失败!");
		return;
	}
	result = DSBuf[num]->Unlock(pAudio,bytesAudio,NULL,NULL);
	if(result != DS_OK)
	{
		MessageBox("解除锁定缓冲区失败!");
		return;
	}
	mmioClose(hmmio,0);
}

void BMFrame::InitDD()
{
//建立 DirectDraw 对象
	result = DirectDrawCreateEx(NULL, (VOID**)&DD, IID_IDirectDraw7, NULL);
    if (result != DD_OK)
	{
		MessageBox("建立DirectDraw对象失败!");
		return;
	}
//设定协调层级
	result = DD->SetCooperativeLevel(m_hWnd, DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN
							|DDSCL_ALLOWREBOOT);
	if(result !=DD_OK)
	{
		MessageBox("设定程序协调层级失败!");
		return;
	}
//设定显示模式
	result = DD->SetDisplayMode(800,600,16,0,DDSDM_STANDARDVGAMODE); 
	if(result !=DD_OK)
	{
		MessageBox("设定屏幕显示模式失败!");
		return;
	}
//建立主绘图页
	memset(&DDde,0,sizeof(DDde));       
	DDde.dwSize = sizeof(DDde);           
	DDde.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
	DDde.dwBackBufferCount = 1;        
	DDde.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
	result = DD->CreateSurface(&DDde,&DDSur,NULL);
	if(result !=DD_OK)
	{
	 	MessageBox("建立主绘图页失败!");
		return;
	}
//连结后缓冲区
	DDcaps.dwCaps = DDSCAPS_BACKBUFFER;  
	result = DDSur->GetAttachedSurface(&DDcaps,&DDBuf); 
	if(result !=DD_OK)
	{
		MessageBox("连接后缓冲区失败!");
		return;
	}
//声明幕后内存区的共同特性
	memset(&DDde,0,sizeof(DDde));        
	DDde.dwSize = sizeof(DDde);
	DDde.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; 
	DDde.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN ;
}

void BMFrame::InitDS()
{
//建立 DirectSound 对象
    result = DirectSoundCreate( NULL, &DS, NULL ); 
	if(result != DS_OK)
	{
		MessageBox("建立 DirectSound 对象失败!");
		return;
	}
//设定协调层级
    result = DS->SetCooperativeLevel( m_hWnd, DSSCL_PRIORITY );
	if(result != DS_OK)
	{
		MessageBox("设定协调层级失败!");
		return;
	}
//建立主缓冲区
    memset( &DSde,0, sizeof(DSde) );          
    DSde.dwSize        = sizeof(DSde);       
    DSde.dwFlags       = DSBCAPS_PRIMARYBUFFER;
    DSde.dwBufferBytes = 0;
    DSde.lpwfxFormat   = NULL;
    result = DS->CreateSoundBuffer( &DSde, &DSPri, NULL );
	if(result != DS_OK)
	{
		MessageBox("建立主缓冲区失败!");
		return;
	}
//设定声音播放格式
    memset( &DSfmt,0, sizeof(DSfmt) );
    DSfmt.wFormatTag      = WAVE_FORMAT_PCM;
    DSfmt.nChannels       = 2;               
    DSfmt.nSamplesPerSec  = 44100;           
    DSfmt.wBitsPerSample  = 16;          
    DSfmt.nBlockAlign     = DSfmt.wBitsPerSample / 8 * DSfmt.nChannels;
    DSfmt.nAvgBytesPerSec = DSfmt.nSamplesPerSec * DSfmt.nBlockAlign;
    result = DSPri->SetFormat(&DSfmt);     
	if(result != DS_OK)
	{
		MessageBox("设定播放格式失败!");
		return;
	}
}

void BMFrame::InitDI()
{
//建立 DirectInput 对象
	HINSTANCE hinst = AfxGetInstanceHandle(); 
	result = DirectInputCreateEx(hinst, DIRECTINPUT_VERSION,IID_IDirectInput7,
									(void**)&DI, NULL); 
	if(result != DI_OK)
	{
		MessageBox("建立 DirectInput 对象失败!");
		return;
	}
//建立输入装置对象
	result = DI->CreateDeviceEx(GUID_SysKeyboard, IID_IDirectInputDevice7,
									(void**)&DIkb, NULL); 
	if(result != DI_OK)
	{
		MessageBox("建立鼠标输入装置失败!");
		return;
	}
//设定数据格式
	result = DIkb->SetDataFormat(&c_dfDIKeyboard);
	if(result != DI_OK)
	{
		MessageBox("设定数据格式失败!");
		return;
	}
//设定协调层级
	result = DIkb->SetCooperativeLevel(m_hWnd,DISCL_BACKGROUND | DISCL_NONEXCLUSIVE); 
	if(result != DI_OK)
	{
		MessageBox("设定程序协调层级失败!");
		return;
	}
//取用输入装置
	result = DIkb->Acquire();
	if(result != DI_OK)
	{
		MessageBox("取用输入装置失败!");
		return;
	}
}


⌨️ 快捷键说明

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