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

📄 roleplane.cpp

📁 飞机小游戏(Demo),有npc和主角
💻 CPP
字号:
#include "stdafx.h"
#include "RolePlane.h"

CRolePlane::CRolePlane(HDC kdc){
          x=400;
	      y=500;
	      step=64;
		  py=0;
		  py1=-660;
		  count=0;
		  ncount=0;
		  
		  memset(bullet,0,0);
		  
          
		  hdc=kdc;
		  dc=CreateCompatibleDC(hdc);
		  bmp=CreateCompatibleBitmap(hdc,950,660);
		  SelectObject(dc,bmp);

	      bitmap=(HBITMAP)LoadImage(NULL,"beach.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
	      dcBack=CreateCompatibleDC(hdc);
		  SelectObject(dcBack,bitmap);

		  rolebitmap=(HBITMAP)LoadImage(NULL,"myfly.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
		  dcRole=CreateCompatibleDC(hdc);
		  SelectObject(dcRole,rolebitmap);

		  bmpMask=CreateBitmap(64,64,1,1,NULL);
		  dcMask=CreateCompatibleDC(hdc);
		  SelectObject(dcMask,bmpMask);
          
		  bulletbitmap=(HBITMAP)LoadImage(NULL,"fire0.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
		  dcBullet=CreateCompatibleDC(hdc);
		  SelectObject(dcBullet,bulletbitmap);

          buMask=CreateBitmap(12,12,1,1,NULL);
		  dcBuMask=CreateCompatibleDC(hdc);
		  SelectObject(dcBuMask,buMask);
		  
}

CRolePlane::~CRolePlane(){
          DeleteDC(dc);
	      DeleteDC(dcBack);
		  DeleteDC(dcMask);
		  DeleteDC(dcRole);
		  DeleteDC(hdc);
		  DeleteDC(dcBullet);
          DeleteDC(dcBuMask);
	      DeleteObject(bitmap);
	      DeleteObject(bmp);
		  DeleteObject(rolebitmap);
		  DeleteObject(bmpMask);
          DeleteObject(bulletbitmap);
		  DeleteObject(buMask);
		  for(int i=0;i<ncount;i++)
			   delete pNplane[i];
}

void CRolePlane::Draw(){

          BitBlt(dc,0,py,950,660,dcBack,0,0,SRCCOPY);
		  BitBlt(dc,0,py1,950,660,dcBack,0,0,SRCCOPY);

          BitBlt(dcMask,0,0,64,64,dcRole,step,0,SRCCOPY);
		  BitBlt(dc,x,y,64,64,dcRole,step,0,SRCINVERT);
		  BitBlt(dc,x,y,64,64,dcMask,0,0,SRCAND);
		  BitBlt(dc,x,y,64,64,dcRole,step,0,SRCINVERT);

		  for(int i=0;i<count;i++){
			     BitBlt(dcBuMask,0,0,12,12,dcBullet,0,0,SRCCOPY);
				 BitBlt(dc,bullet[i].bx,bullet[i].by,12,12,dcBullet,0,0,SRCINVERT);
                 BitBlt(dc,bullet[i].bx,bullet[i].by,12,12,dcBuMask,0,0,SRCAND);
				 BitBlt(dc,bullet[i].bx,bullet[i].by,12,12,dcBullet,0,0,SRCINVERT);
	      }

		  
		  for(int i=0;i<ncount;i++){
			     BitBlt(pNplane[i]->dcNpcMask,0,0,64,64,pNplane[i]->dcNpc,pNplane[i]->step,0,SRCCOPY);
				 BitBlt(dc,pNplane[i]->nx,pNplane[i]->ny,64,64,pNplane[i]->dcNpc,pNplane[i]->step,0,SRCINVERT);
				 BitBlt(dc,pNplane[i]->nx,pNplane[i]->ny,64,64,pNplane[i]->dcNpcMask,0,0,SRCAND);
				 BitBlt(dc,pNplane[i]->nx,pNplane[i]->ny,64,64,pNplane[i]->dcNpc,pNplane[i]->step,0,SRCINVERT);
		  }

		  for(int i=0;i<ncount;i++){
			  for(int j=0;j<pNplane[i]->nbcount;j++){
				 BitBlt(pNplane[i]->dcNbuMask,0,0,12,12,pNplane[i]->dcNbu,0,0,SRCCOPY);
				 BitBlt(dc,pNplane[i]->nBullet[j].nbx,pNplane[i]->nBullet[j].nby,12,12,pNplane[i]->dcNbu,0,0,SRCINVERT);
				 BitBlt(dc,pNplane[i]->nBullet[j].nbx,pNplane[i]->nBullet[j].nby,12,12,pNplane[i]->dcNbuMask,0,0,SRCAND);
				 BitBlt(dc,pNplane[i]->nBullet[j].nbx,pNplane[i]->nBullet[j].nby,12,12,pNplane[i]->dcNbu,0,0,SRCINVERT);
			  }
		  }

          BitBlt(hdc,0,0,950,660,dc,0,0,SRCCOPY);
		  
}


void CRolePlane::CreateBullet(){
          bullet[count].bx=x+22;
		  bullet[count].by=y;
		  count++;
}

void CRolePlane::CreateNPC(HDC hdc){
          int x=rand()%900;
		  int y;
		  if(x==0||x==900)
			  y=rand()%300;
		  else
			  y=0;
		  int j;
          char *str[4];
		  str[0]="army0.bmp";
		  str[1]="army1.bmp";
		  str[2]="army2.bmp";
		  str[3]="army3.bmp";
		  int i=rand()%4;

		  int step[3];
		  step[0]=0;
		  step[1]=64;
		  step[2]=128;
		  if(x>=0&&x<300)
			   j=rand()%2+1;
		  else if(x>=300&&x<600)
			   j=1;
		  else if(x>=600&&x<950)
			   j=rand()%2;
		  pNplane[ncount]=new CNpcPlane(hdc,str[i],x,y,step[j]);
		  ncount++;
}

void CRolePlane::Move(){
	      for(int i=0;i<ncount;i++){
			   switch(pNplane[i]->step){
					case 0:
						  if(pNplane[i]->nx>0&&pNplane[i]->ny<660){
							      pNplane[i]->ny+=3;
								  pNplane[i]->nx-=3;
						  }
						break;
					case 64:
                          if(pNplane[i]->ny<660){
						          pNplane[i]->ny+=3;
				          }
						break;
					case 128:
						  if(pNplane[i]->nx<950&&pNplane[i]->ny<660){
							      pNplane[i]->ny+=3;
								  pNplane[i]->nx+=3;
						  }
						break;
			  }
			          
			    if(pNplane[i]->ny>=660||pNplane[i]->nx<=0||pNplane[i]->nx>=950){
                    for(int j=i;j<ncount;j++){
				          pNplane[j]=pNplane[j+1];
					}
					ncount--;				   
				}	
		  }
		  for(int i=0;i<ncount;i++){
		       for(int j=0;j<pNplane[i]->nbcount;j++){
				    if(pNplane[i]->nx>x){
						  pNplane[i]->nBullet[j].nbx-=4;
						  pNplane[i]->nBullet[j].nby+=4;
					}
					else if(pNplane[i]->nx<x){
					      pNplane[i]->nBullet[j].nbx+=4;
						  pNplane[i]->nBullet[j].nby+=4;
					}
					else if(pNplane[i]->nx==x){
						  pNplane[i]->nBullet[j].nby+=4;
					}
			  }
		  }
}

void CRolePlane::CollidesWithNpc(){
	      for(int i=0;i<ncount;i++){
			  if(pNplane[i]->nx+64>=x&&pNplane[i]->ny+64>=y&&pNplane[i]->nx<=x+64&&pNplane[i]->ny<=y){
				  for(int j=i;j<ncount;j++){
				       pNplane[j]=pNplane[j+1];
				  }
				  ncount--;
			  }
		  }
}

void CRolePlane::CollidesWithBullet(){
	      for(int i=0;i<count;i++){
			  for(int l=0;l<ncount;l++){
			      if(bullet[i].bx+12>=pNplane[l]->nx&&bullet[i].by+12>=pNplane[l]->ny&&bullet[i].bx<=pNplane[l]->nx+64&&bullet[i].by<=pNplane[l]->ny){
			         for(int j=l;j<ncount;j++){
				           pNplane[j]=pNplane[j+1];
				     }
				  ncount--;
			      }
			  }
		  }
}

void CRolePlane::CreateNpcBullet(){
	      for(int i=0;i<ncount;i++){
			  pNplane[i]->CreateNpcBullet();
	      } 
}

⌨️ 快捷键说明

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