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

📄 game.h

📁 MiniWinOuterSM MiniWinOuterSM
💻 H
字号:
#include "osdwindows.h"
#include "stdio.h"
#define MAXX 12
#define MAXY 24
#define LEFT -1
#define RIGHT 1
#define IDC_NEXTOBJ 100 //next Oject
#define IDC_SPEED 101
#define IDC_SCORE 102
#define IDC_LEVEL 103
typedef struct struct_AppData
{
 HWND			hWnd;          //窗口的句柄
 char           bIsActive;     //游戏是否激活
}APPDATA,*LPAPPDATA;
APPDATA g_AppData;

typedef  struct struct_Object
{
  int      type;
  POINT   center;
  POINT    pos[4];
}OBJECT,*POBJECT;

int                      Ev[MAXX][MAXY];
OBJECT                   CurObj;
OBJECT                   NextObj;
#define    xCell 20
#define    yCell 20
int                      NextScore=10000;
static int               dtime;
static int               time[15]={800,700,650,600,500,450,400,350,300,250,200,150,120,100,90};

void EndGame(HWND);
//画
HWND hGame;
void GetCellRect(RECT*rc,int x,int y)
{
	rc->left=x*xCell;
	rc->top=y*yCell;
	rc->right=(x+1)*xCell;
	rc->bottom=(y+1)*yCell;
}
void GetObjectRect(RECT*rc,OBJECT*obj)
{
	int i;
	GetCellRect(rc,obj->pos[0].x,obj->pos[0].y);
	for(i=0;i<4;i++){
		RECT r;
		GetCellRect(&r,obj->pos[i].x,obj->pos[i].y);
		GetBoundRect(rc,rc,&r);
	}
}
static DrawObject(HDC hdc,OBJECT *obj)
{
	int i;
	COLORREF cp,cb;
	cp=SetDCPenColor(hdc,RGB(255,255,255));
	cb=SetDCBrushColor(hdc,RGB(255,0,0));
	for(i=0;i<4;i++){
		RECT rc;
		GetCellRect(&rc,obj->pos[i].x,obj->pos[i].y);
		OffsetRect(&rc,5,5);
		InflateRect(&rc,-1,-1);
		FillRect(hdc,&rc,(HBRUSH)(COLOR_BTNFACE+1));
		//DrawFrameControl(hdc,&rc,DFC_BUTTON,DFCS_FLAT);
	}
	SetDCPenColor(hdc,cp);
	SetDCBrushColor(hdc,cb);
}
void DrawSence(HDC hdc)
{
	int i,j;
	HBRUSH hbr,obr;
    for(i=0;i<MAXX;i++){
		for(j=0;j<MAXY;j++){
			RECT rc;
			GetCellRect(&rc,i,j);
			OffsetRect(&rc,5,5);
			if(!RectVisible(hdc,&rc))continue;
			hbr=CreateSolidBrush(Ev[i][j]==0?RGB(30,30,30):RGB(255,76,0));
			obr=(HBRUSH)SelectObject(hdc,(HGDIOBJ)hbr);
			if(Ev[i][j]==0)
				FillRect(hdc,&rc,hbr);
			else{
				//InflateRect(&rc,-1,-1);
				DrawFrameControl(hdc,&rc,DFC_BUTTON,DFCS_FLAT);
			}
			SelectObject(hdc,(HGDIOBJ)obr);
			DeleteObject((HGDIOBJ)hbr);
		}
	}
	DrawObject(hdc,&CurObj);  //画对象  
 }
//产生一个对象
void CreateObj(POBJECT pObj)
{
	int sx=MAXX/2;
	srand(PrTimeGetTicks());
	pObj->type=rand()%7;
	switch(pObj->type)
	{
	case 0:
    pObj->pos[0].x=sx;		pObj->pos[0].y=0;
	pObj->pos[1].x=sx+1;	pObj->pos[1].y=0;
	pObj->pos[2].x=sx;		pObj->pos[2].y=1;
	pObj->pos[3].x=sx+1;	pObj->pos[3].y=1;
	pObj->center.x=0.5;		pObj->center.y=0.5;
	break;
	case 1:
	pObj->pos[0].x=sx;		pObj->pos[0].y=0;
	pObj->pos[1].x=sx;		pObj->pos[1].y=1;
	pObj->pos[2].x=sx+1;	pObj->pos[2].y=1;
	pObj->pos[3].x=sx+1;	pObj->pos[3].y=2;
	pObj->center.x=sx+1;	pObj->center.y=1;
	break;
	case 2:
	pObj->pos[0].x=sx+1;	pObj->pos[0].y=0;
	pObj->pos[1].x=sx;		pObj->pos[1].y=1;
	pObj->pos[2].x=sx+1;	pObj->pos[2].y=1;
	pObj->pos[3].x=sx;		pObj->pos[3].y=2;
	pObj->center.x=sx;		pObj->center.y=1;
	break;
	case 3:
	pObj->pos[0].x=sx;		pObj->pos[0].y=0;
	pObj->pos[1].x=sx+1;	pObj->pos[1].y=0;
	pObj->pos[2].x=sx+1;	pObj->pos[2].y=1;
	pObj->pos[3].x=sx+1;	pObj->pos[3].y=2;
	pObj->center.x=sx+1;	pObj->center.y=1;
	break;
    case 4:
	pObj->pos[0].x=sx+1;	pObj->pos[0].y=0;
	pObj->pos[1].x=sx;		pObj->pos[1].y=0;
	pObj->pos[2].x=sx;		pObj->pos[2].y=1;
	pObj->pos[3].x=sx;		pObj->pos[3].y=2;
	pObj->center.x=sx;		pObj->center.y=1;
	break;
	case 5:
	pObj->pos[0].x=sx;		pObj->pos[0].y=0;
	pObj->pos[1].x=sx;		pObj->pos[1].y=1;
	pObj->pos[2].x=sx;		pObj->pos[2].y=2;
	pObj->pos[3].x=sx+1;	pObj->pos[3].y=1;
	pObj->center.x=sx;		pObj->center.y=1;
	break;
	case 6:
	pObj->pos[0].x=sx;		pObj->pos[0].y=0;
	pObj->pos[1].x=sx;		pObj->pos[1].y=1;
	pObj->pos[2].x=sx;		pObj->pos[2].y=2;
	pObj->pos[3].x=sx;		pObj->pos[3].y=3;
	pObj->center.x=sx;		pObj->center.y=2;
	break;
	}
}
//产生下一个对象
void GetNextObj()
{
	int i;//,sx=(int)MAXX/2;
	char isDead=0;
	srand(PrTimeGetTicks());
	memcpy( (void*)&CurObj,(void*)&NextObj,sizeof(OBJECT));
	
	//假设未结束
	for(i=0;i<4;i++){
		if(Ev[(int) CurObj.pos[i].x][ (int)CurObj.pos[i].y])
			isDead=1;
	}
	//游戏结束
	if(isDead)
		EndGame(hGame);
	CreateObj(&NextObj);	
	InvalidateRect(GetDlgItem(hGame,IDC_NEXTOBJ),NULL,FALSE);
}

//消行的动画
void DrawDieLine(HWND hwnd,int *DieLine)
{
	int i,j;
     for(i=0;i<MAXX;i++)
	 {
		for(j=0;j<4;j++)
		if(DieLine[j]>=0){
			RECT rc;
		  Ev[i][DieLine[j]]=0;
		  GetCellRect(&rc,i,j);
		  InvalidateRect(hwnd,&rc,FALSE);
		}
	 }
	return;
}

//把对象不可见
void SetCruObjZero()
{
	int i;
	for(i=0;i<4;i++)
		CurObj.pos[i].y=-6;
}
void InvalidateObj(HWND hwnd,OBJECT*obj)
{
	RECT rc;
	GetObjectRect(&rc,obj);
	InflateRect(&rc,10,10);
	InvalidateRect(hwnd,&rc,FALSE);
}
//=========翻转==========================
void Flip(HWND hwnd)
{
	float cx;
	float cy;
	int i;
	POINT temp[4];
	RECT rc0,rc1;
	if(CurObj.type==0)//当前为方块
		return;
	for(i=0;i<4;i++){
		cx=CurObj.pos[i].x-CurObj.center.x;
		cy=CurObj.center.y-CurObj.pos[i].y;
		temp[i].x=CurObj.center.x+cy;
		temp[i].y=CurObj.center.y+cx;
		if(Ev[ (temp[i].x) ][ (temp[i].y)] )//Can not Flip
			return;
		if(temp[i].x<0||temp[i].x>=MAXX)
			return;
		if(temp[i].y<0||temp[i].y>=MAXY)
			return;
	}
	GetObjectRect(&rc0,&CurObj);
	for(i=0;i<4;i++)//Can Flip
	{
		CurObj.pos[i].x=temp[i].x;
		CurObj.pos[i].y=temp[i].y;
	}
	GetObjectRect(&rc1,&CurObj);
	UnionRect(&rc0,&rc0,&rc1);
	InflateRect(&rc0,5,5);
	InvalidateRect(hwnd,&rc0,FALSE);
	return;
}//+++++++++++++++++++++++++++++++++

//测试是否有某一行可以消去===========
int GetDieLine(HWND hwnd,int*DieLine)
{
	int i,j,isDead=1;
	int DieCount=0;
	int DestLine;
	int minLine=MAXY;
	for( i=0;i<4;i++)
		DieLine[i]=-1;
	//Find the Die Line
	for(i=MAXY-1;i>=0;i--)
	{
		isDead=1;
		for(j=0;j<MAXX;j++){
			if(Ev[j][i]==0){
				isDead=0;  break;
			}
		}
		if(isDead){
			DieLine[DieCount]=i;
			DieCount++;
		}
	}
	if(GetDlgItemInt(hwnd,IDC_SCORE,NULL,FALSE)*100>=NextScore){
		int speed=GetDlgItemInt(hwnd,IDC_SPEED,NULL,FALSE);
		if(speed==16)
			speed=0;
		SetDlgItemInt(hwnd,IDC_SPEED,speed+1,FALSE);
		dtime=time[speed];
		NextScore+=10000;
	}
	for(i=DieCount-1;i>=0;i--){
		for(DestLine = DieLine[i];DestLine>0;DestLine--){
			for(j=0;j<MAXX;j++)
				Ev[j][DestLine]=Ev[j][DestLine-1];
		}
	}
	if(DieCount){
		RECT rc,rc0;
		GetCellRect(&rc0,0,0);
		GetCellRect(&rc,MAXX-1,DieLine[DieCount-1]);
		UnionRect(&rc,&rc0,&rc);
		InvalidateRect(hwnd,NULL,FALSE);
		//TRACE("GetDieLine Update from line %d to %d\r\n",0,DieLine[DieCount-1]);
	}
	return DieCount;
}

//Move Left or right====================
void Move(HWND hwnd,int step)
{
   int i;
   POINT temp[4];
   for(i=0;i<4;i++) {
     temp[i].x=CurObj.pos[i].x+step;
	 temp[i].y=CurObj.pos[i].y;
	 if(Ev[(temp[i].x) ][(temp[i].y)] )   return;
	 if(temp[i].x<0||temp[i].x>=MAXX)     return;
	 if(temp[i].y<0||temp[i].y>=MAXY)     return;
   }
   InvalidateObj(hwnd,&CurObj);
   for(i=0;i<4;i++){
  	CurObj.pos[i].x=temp[i].x;
	CurObj.pos[i].y=temp[i].y;
   }
   CurObj.center.x+=step;
   InvalidateObj(hwnd,&CurObj);
   return;
}



//下落
void Fall(HWND hwnd)
{
	int i;
    POINT temp[4];
    for( i=0;i<4;i++){
	 int DieLine[4];
	 int DieCount,Score;
     temp[i].x=CurObj.pos[i].x;
	 temp[i].y=CurObj.pos[i].y+1;
	 if(Ev[(temp[i].x)][(temp[i].y)]==1 )//Can not Fall
	 { 
		 int n;
		 for(n=0;n<4;n++)
			 Ev[ (int)CurObj.pos[n].x][(int)CurObj.pos[n].y]=1;
		 SetCruObjZero();
		 DieCount=GetDieLine(hwnd,DieLine);
		 if(DieCount){
			 //DrawDieLine(hwnd,DieLine);
			 Score=GetDlgItemInt(hwnd,IDC_SCORE,NULL,FALSE)+DieCount*100;
			 SetDlgItemInt(hwnd,IDC_SCORE,Score,FALSE);
		 }
		 GetNextObj();
		 //DrawSence(hdc);
		 //DrawNext(hdc);
		 return;
	 }
	 if(temp[i].y>=MAXY){ 
		 int n;
		 for( n=0;n<4;n++)
			 Ev[ (int)CurObj.pos[n].x][(int)CurObj.pos[n].y]=1;
		 SetCruObjZero();
		 DieCount=GetDieLine(hwnd,DieLine);
		 if(DieCount){
			 //DrawDieLine(hwnd,DieLine);
			 Score=GetDlgItemInt(hwnd,IDC_SCORE,NULL,FALSE)+DieCount*100;
			 SetDlgItemInt(hwnd,IDC_SCORE,Score,FALSE);
		 }
		 GetNextObj();
		 //DrawSence(hdc);
		 //DrawNext(hdc);
		 return;
	 }
	}
   for(i=0;i<4;i++)//Can Fall
   {
    CurObj.pos[i].x=temp[i].x;
	CurObj.pos[i].y=temp[i].y;
   }
   CurObj.center.y+=1;
   {
	   RECT rc;
	   GetObjectRect(&rc,&CurObj);
	   InflateRect(&rc,15,15);
	   InvalidateRect(hwnd,&rc,FALSE);
   }
   return;
}

void EndGame(HWND hwnd)
{
	int i,j;
	g_AppData.bIsActive=0;
	SetDlgItemInt(hGame,IDC_SPEED,0,FALSE);
	SetDlgItemInt(hGame,IDC_SCORE,0,FALSE);
	SetCruObjZero();
	for(i=0;i<MAXY;i++){
		for(j=0;j<MAXX;j++)
			Ev[j][i]=0;
	}
    InvalidateRect(hwnd,NULL,FALSE);
}


void BeginGame(HWND hwnd)
{
	int i,j;
	int level,speed;
	g_AppData.bIsActive=1;
   	CreateObj(&NextObj);
    CreateObj(&CurObj);
	NextScore=10000;
	SetDlgItemInt(hGame,IDC_SCORE,0,FALSE);
	speed=GetDlgItemInt(hGame,IDC_SPEED,NULL,FALSE);
	level=GetDlgItemInt(hGame,IDC_LEVEL,NULL,FALSE);
	dtime=time[speed];
	for(i=0;i<MAXX;i++)
  	 for(j=0;j<MAXY-level;j++)
	    Ev[i][j]=0;
	for(i=0;i<MAXX;i++)
	 for(j=MAXY-level;j<MAXY;j++)
	    Ev[i][j]=rand()%2;
    InvalidateRect(hwnd,NULL,FALSE);
}

⌨️ 快捷键说明

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