简单的俄罗斯方块

记得写这个的时候,那天晚上我们班同学正要到二期桃李园集体包饺子.在去的路上我还在边想这个应该怎么做.之前没有用C在win32上面做过程序,也还没有学mfc,好在知道可以用c直接调用win32API,方块下落就用画图的画出来就可.晚上回去实现后,发现闪得厉害,跟同学请教了下,接解决了.贴下代码,以示纪念:
/********************************************************************
*     文件名:elsfk.cpp
*
*    文件描述:
*	         此程序是本人写的第一个WINDOWS程序,第一个版本很乱,此后慢慢更新
*     
*     开发工具:Microsoft Visual C++ 6.0
* 
*     创建人:sylinx_yqg 2006年2月1日 
*
*     版本号:2.0 
**********************************************************************/
#include "stdafx.h"
#include <stdio.h>
#include "resource.h"
#include "mmsystem.h"
#define STILL       0
#define RIGHT       1
#define LEFT        -1
#define DOWN         1
#define MAX_LOADSTRING 100
#define BKCOLOR RGB(0,160,107)
#pragma comment(lib,"winmm.lib")
/**********************************************************
//全局变量
*************************************************************/
HINSTANCE hInst;
HWND hWnd;
HWND hwndEdit;								
TCHAR szTitle[MAX_LOADSTRING];								
TCHAR szWindowClass[MAX_LOADSTRING];
HBRUSH hFk[8];
HBRUSH hBkBrush,hNextBrush;
HBITMAP hbp[8];
HPEN hbkpen;
RECT   ShowBox;
HDC hMemoryDC;
HBITMAP hBitmap[8],hBkBitmap1,hBkBitmap;
int stopflag=1;  //1暂停标志
int idTimer=0,leveltime=700;
int left=0,right=0,top=0,bottom=0;
int room[14][22]={0};
int level=1;
long totalscore=0;
struct shapebar   //形容方块的结构体
{
    int xy[8];
    int color;
    int initshape;
    int nowshape;
    int style_num;
}sqbar,oldsqbar,nextsqbar;

int NextFk=1;
int OldFk=0;
int fkx=5,fky=0;//记录正在下落的方块的坐标

int allshape[19][8]=      //方块的形状
{
	    0,1,1,0,1,1,1,2,
		1,0,0,1,1,1,2,1,
		1,0,1,1,1,2,2,1,
		0,1,1,1,2,1,1,2,
		
		0,1,0,2,1,1,2,1,
		0,0,1,0,1,1,1,2,
		2,0,0,1,1,1,2,1,
		1,0,1,1,1,2,2,2,
		
		0,1,0,2,1,2,2,2,
		1,1,1,2,1,3,2,1,
		0,1,1,1,2,1,2,2,
		1,0,1,1,1,2,0,2,
		
		0,1,1,1,1,2,2,2,
		2,0,2,1,1,1,1,2,
		
		0,2,1,2,1,1,2,1,
		1,0,1,1,2,1,2,2,
		
		0,1,1,1,2,1,3,1,
		1,0,1,1,1,2,1,3,
		
		1,1,1,2,2,1,2,2
};

/**************************************************************
//函数声明
***************************************************************/
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
void command(HWND hwnd,int icmd,HWND hctl,UINT ucode);
BOOL create(HWND hwnd,LPCREATESTRUCT lpcreatestruct);
void paint(HWND hwnd);
void destroy(HWND hwnd);
void keydown(HWND hwnd, UINT vk, BOOL fDown, int cRepeat, UINT flags);
void ontime(HWND hwnd,UINT id);

int SetLevel(HWND hwnd);
void shownextfk(HDC hdc,int style);
int cancel(HWND hwnd);
int max_min(int*p);
void cls_back(int flag);
int updateroom();
int move(int dx,int dy);
int move_or_not(int dx,int dy);
int turn();
void initfk();
int InitSource(HINSTANCE hInstance);
void CreatechildWindow(HWND hwnd);
LRESULT CALLBACK Help(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
BOOL SaveInfor();
BOOL ReadInfor();
void InitRoom();
void UpMove();
/*************************************************************
//主函数入口
****************************************************************/
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;
	// Initialize global strings
	InitRoom();
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_ELSFK, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);
       
	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}
    InitSource(hInstance);
	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_ELSFK);
	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
	if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
	{		
		   TranslateMessage(&msg);
		   DispatchMessage(&msg);
	}
	}

	return msg.wParam;
}
/************************************************************************
//注册窗口
*************************************************************************/
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_ELSFK);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= CreateSolidBrush((LONG)0x00FF8000);//创建一个逻辑画笔 
	wcex.lpszMenuName	= (LPCSTR)IDR_ELSFK;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}
/******************************************************************
//初始化窗口实例
******************************************************************/
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}
/*******************************************************************************
//消息处理函数
*********************************************************************************/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
			HANDLE_MSG(hWnd,WM_COMMAND,command);
		    HANDLE_MSG(hWnd,WM_CREATE,create);
            HANDLE_MSG(hWnd,WM_PAINT,paint);
            HANDLE_MSG(hWnd,WM_DESTROY,destroy);
            HANDLE_MSG(hWnd,WM_KEYDOWN,keydown);
	        HANDLE_MSG(hWnd,WM_TIMER,ontime);
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}
/*****************************************************************
//加载各种资源
******************************************************************/
int InitSource(HINSTANCE hInstance)
{
    int i=0;
	hBitmap[7]=LoadBitmap(hInstance,(LPSTR)IDB_FK0);
    hBitmap[1]=LoadBitmap(hInstance,(LPSTR)IDB_FK1);
	hBitmap[2]=LoadBitmap(hInstance,(LPSTR)IDB_FK2);
	hBitmap[3]=LoadBitmap(hInstance,(LPSTR)IDB_FK3);
	hBitmap[4]=LoadBitmap(hInstance,(LPSTR)IDB_FK4);
	hBitmap[5]=LoadBitmap(hInstance,(LPSTR)IDB_FK5);
	hBitmap[6]=LoadBitmap(hInstance,(LPSTR)IDB_FK6);
	hbp[0]=LoadBitmap(hInstance,(LPSTR)IDB_FACE1);
	hbp[1]=LoadBitmap(hInstance,(LPSTR)IDB_FACE2);
	hbp[2]=LoadBitmap(hInstance,(LPSTR)IDB_FACE3);
	hbp[3]=LoadBitmap(hInstance,(LPSTR)IDB_FACE4);
	hbp[4]=LoadBitmap(hInstance,(LPSTR)IDB_FACE5);
	hbp[5]=LoadBitmap(hInstance,(LPSTR)IDB_FACE6);
	hbp[6]=LoadBitmap(hInstance,(LPSTR)IDB_FACE7);
	hbp[7]=LoadBitmap(hInstance,(LPSTR)IDB_FACE8);
	for(i=1;i<8;i++)
		hFk[i]=CreatePatternBrush(hBitmap[i]);
	return TRUE;
}
/************************************************************
//由WM_TIMER产生的消息映射
**************************************************************/
void ontime(HWND hwnd,UINT id)
{
  HDC hdcmem;
  int temp=0;
  BITMAP   bitmap ;
  HDC hdc=GetDC(hwnd);
  if(stopflag==0)
  {
  switch(id)
  {
  case IDT_TIME1:
       // if(stopflag!=0) break;//move(STILL,STILL);
	     if(!move(STILL,DOWN)&& (fky==0) )
			{	
				KillTimer(hwnd,IDT_TIME1);
				KillTimer(hwnd,IDT_TIME2);
		        MessageBox(hwnd,"继续加油哦!","game over!",0);
				PostQuitMessage(0);    
			}
			else if(!move(STILL,DOWN))
			{
				cancel(hwnd);
				SetLevel(hwnd);
				initfk();
				shownextfk(hdc,NextFk);
			}
		InvalidateRect(hwnd,&ShowBox,0);
		break;
  case IDT_TIME2:
	   UpMove();
	    break;
  case IDT_TIME3:
	   temp=rand()%7;
       hdcmem=CreateCompatibleDC(hdc);
       SelectObject(hdcmem,hbp[temp]);
	   GetObject (hbp[0],sizeof (BITMAP),&bitmap); 
       BitBlt(hdc,left-bitmap.bmWidth-20,top+150,bitmap.bmWidth,bitmap.bmHeight,hdcmem,0,0,SRCCOPY);
	   DeleteDC(hdcmem);//用ReleaseDC(hwnd,hdcmem)会出错;
	   break;
  default:break;

  }
  }
	ReleaseDC(hwnd,hdc);
}
/************************************************************
//由WM_KEYDOWN产生的消息映射
**************************************************************/
void keydown(HWND hwnd, UINT vk, BOOL fDown, int cRepeat, UINT flags)
{
	switch(vk){
		case VK_LEFT:
			if(stopflag==1)break;
			move(LEFT,STILL);
			InvalidateRect(hwnd,&ShowBox,0);
			break;
		case VK_DOWN:
			if(stopflag==1)break;
			move(STILL,DOWN);
			InvalidateRect(hwnd,&ShowBox,0);
			break;
		case VK_UP:
			if(stopflag==1)break;
			turn();
			InvalidateRect(hwnd,&ShowBox,0);
			break;
		case VK_RIGHT:
			if(stopflag==1)break;
			move(RIGHT,STILL);
			InvalidateRect(hwnd,&ShowBox,0);
			break;
		case VK_SPACE:
			if(stopflag==1)break;
			   while(move(STILL,DOWN))
			   {
				   //SetFocus(NULL);
			        InvalidateRect(hwnd,&ShowBox,0);
			   }
			//SetFocus(hwnd);
			break;
		case VK_ESCAPE:
			PostQuitMessage (0) ;
			break;
		case VK_PAUSE: //暂停
            stopflag=stopflag^1;
            break;
		default: break;
		}
}

/************************************************************
//由WM_COMMAND产生的消息映射
**************************************************************/
void command(HWND hwnd,int icmd,HWND hctl,UINT ucode)
{
      switch(icmd)
	  {
	  case IDR_START:
		     stopflag=0;
			 break;
	  case IDR_HELP:
		     DialogBox(hInst,(LPCTSTR)IDD_HELPBOX, hwnd, (DLGPROC)Help);
			 break;
	  case IDR_SAVE:
		     SaveInfor();
			 break;
	  case IDR_CONTINUE:
		    ReadInfor();
			 break;
	  case IDR_STOP:
		    stopflag=1;
			break;
	   case IDR_UPMOVE:
		    SetTimer(hwnd,IDT_TIME2,12000,NULL);
			break;
	  default:break;
	  }
}

/************************************************************
//由WM_CREATE产生的消息映射
**************************************************************/
BOOL create(HWND hwnd,CREATESTRUCT FAR*  lpCreateStruct)
{
        RECT  rectClient;
	    SetWindowText(hwnd,"俄罗斯方块增强版(湖大计科SDK版)");
		hBkBrush=CreateSolidBrush(BKCOLOR);
		hNextBrush=CreateSolidBrush(RGB(236,233,216));
		hbkpen=CreatePen(PS_SOLID,0,BKCOLOR);
		idTimer=SetTimer(hwnd,IDT_TIME1,leveltime,NULL);
		SetTimer(hwnd,IDT_TIME3,2000,NULL);
		GetClientRect(hwnd,&rectClient);
		left=(rectClient.right-rectClient.left)/2-121;
		right=left+241;
		top=(rectClient.bottom-rectClient.top)/2-201;
		bottom=top+401;
		SetRect(&ShowBox,left,top,right,bottom);
		initfk();
		updateroom();
		CreatechildWindow(hwnd);
	    return TRUE;	
}
/************************************************************
//由WM_PAINT产生的消息映射
**************************************************************/
void paint(HWND hwnd)
{
  HDC hdc;
  PAINTSTRUCT   ps;
  int x,y;
  hdc = BeginPaint(hwnd, &ps);
  updateroom();
				for(y=1;y<=20;y++)
				{
					for(x=1;x<=12;x++)
					{
						if(room[x][y]!=0 )
						{
							UnrealizeObject(hFk[room[x][y]]);
							SelectObject(hdc,hFk[room[x][y]]);
						}
						else
					  SelectObject(hdc,hBkBrush);
                      SelectObject(hdc,hbkpen);
					  Rectangle(hdc,left+(x-1)*20+1,top+(y-1)*20+1,left+x*20+1,top+y*20+1);
					}
				}
  EndPaint(hwnd, &ps);
}
/************************************************************
//由WM_DESTROY产生的消息映射
**************************************************************/
void destroy(HWND hwnd)
{
  int i=0;
  DeleteObject(hBkBrush);
  DeleteObject(hNextBrush);
  DeleteObject(hBitmap);
  PostQuitMessage (0) ;
			while(i<7)
			{
				DeleteObject(hFk[i]);
				i++;
			}	
}

/**************************************************************
//随机初始化方块
*****************************************************************/
void initfk()
{
	int i=0;
	OldFk=NextFk;
    fkx=5;
	fky=0;
	srand(time(NULL));
	NextFk=rand()%7+1;
	sqbar.color=OldFk;
	switch(OldFk)
    {
	case 1:
		sqbar.initshape=0;
		sqbar.nowshape=0;
		sqbar.style_num=4;
		break;
	case 2:
		sqbar.initshape=4;
		sqbar.nowshape=4;
		sqbar.style_num=4;
		break;
	case 3:
		sqbar.initshape=8;
		sqbar.nowshape=8;
		sqbar.style_num=4;
		break;
	case 4:
		sqbar.initshape=12;
		sqbar.nowshape=12;
		sqbar.style_num=2;
		break;
	case 5:
		sqbar.initshape=14;
		sqbar.nowshape=14;
		sqbar.style_num=2;
		break;
	case 6:
		sqbar.initshape=16;
		sqbar.nowshape=16;
		sqbar.style_num=2;
		break;
	case 7:
		sqbar.initshape=18;
		sqbar.nowshape=18;
		sqbar.style_num=1;
		break;
	default:break;
    }
	for(i=0;i<=7;i++)
		sqbar.xy[i]=allshape[sqbar.initshape][i];
	
}

/**************************************************************
//用来给方块变形,其中用到全局变量allshape来记录各种状态
***************************************************************/
int turn()
{
	int i=0,j=0;
	oldsqbar=sqbar;
	sqbar.nowshape++;
	if((sqbar.nowshape-sqbar.initshape)==sqbar.style_num)
		sqbar.nowshape=sqbar.initshape;
    cls_back(0);
	for(i=0;i<8;i=i+2)
	{ 
	 if((room[fkx+allshape[sqbar.nowshape][i+1]][fky+allshape[sqbar.nowshape][i]]!=0)) return 0;
	}

	if(move_or_not(STILL,STILL)&&(fkx>0)&&(fky<16))
	{
		cls_back(0);
		for(i=0;i<=7;i++)
			sqbar.xy[i]=allshape[sqbar.nowshape][i];
		return 1;
	}
	else
	{	
		sqbar=oldsqbar;
		cls_back(1);
		return 0;
	}
}
/*********************************************************************
//判断是否能够移动,能就返回非0
**********************************************************************/
int move_or_not(int dx,int dy)
{
	int i=0,j=0,max[4]={0};
    max_min(max);
	if(((fkx+dx+max[3])>12)||((fkx+dx+max[2])<1)||((fky+dy+max[1])>20)) return 0;
	for(i=0;i<8;i=i+2)
	{ j=i+1;
	if((fkx+dx+sqbar.xy[j]<=0)||(room[fkx+dx+sqbar.xy[j]][fky+dy+sqbar.xy[i]]!=0)) return 0;
	}
	return 1;
}
/**********************************************************************
//移动方块,上下左右用DX,DY表示
***********************************************************************/
int move(int dx,int dy)
{
	int i=0,j=0;
	oldsqbar=sqbar;
	cls_back(0);
	if(move_or_not(dx,dy))
	{
		fkx=fkx+dx;
		fky=fky+dy;
        return 1;
	}
	else cls_back(1);
    return 0;
}
/**************************************************************
//用来更新ROOM
/**************************************************************/
int updateroom()
{
	int i=0,j=0;
	for(i=7;i>=0;i=i-2)
	{
		j=i-1;
		room[fkx+sqbar.xy[i]][fky+sqbar.xy[j]]=sqbar.color;
	}
	return 1;
}
/****************************************************************
//清除ROOM中的方块
*******************************************************************/
void cls_back(int flag)
{ 
	int i=0,j=0,temp=0;
	if(flag==0) temp=0;
	else temp=oldsqbar.color;
	for(i=7;i>=0;i=i-2)
	{
		j=i-1;
		room[fkx+oldsqbar.xy[i]][fky+oldsqbar.xy[j]]=temp;
	}
}
/**************************************************************
//判断在一个正下落的方块中,其有方块的最值,用来判断方块是否越界
***************************************************************/
int max_min(int*p)
{
	int i=0;
	p[0]=sqbar.xy[0];
	p[1]=sqbar.xy[0];
	p[2]=sqbar.xy[1];
	p[3]=sqbar.xy[1];
	for(i=0;i<8;i=i+2)
	{
		if(sqbar.xy[i]<p[0]) p[0]=sqbar.xy[i];
		if(sqbar.xy[i]>p[1]) p[1]=sqbar.xy[i];
	}
	for(i=1;i<8;i=i+2)
	{
		if(sqbar.xy[i]<p[2]) p[2]=sqbar.xy[i];
		if(sqbar.xy[i]>p[3]) p[3]=sqbar.xy[i];
	}
	return 1;
}
/********************************************************************
//消除满行并输出分数
********************************************************************/
int cancel(HWND hwnd)
{
	int i=0,j=0,k=0,flag=0;
	char score[20]; 
	HDC hdc=GetDC(hwnd);
	for(i=fky+4;i>=fky;i--)
	{
		flag=0;
		for(j=1;j<=12;j++)
			if(room[j][i]!=0) flag++;
			if(flag==12) /*FLAG=12表示有消去的行*/
			{
				PlaySound("sound",hInst,SND_ASYNC);
				totalscore=totalscore+100;
				for(k=i;k>=1;k--)    /*消去一行后,下移*/
					for(j=1;j<=12;j++)
						room[j][k]=room[j][k-1];
					i++;
			}
	}
	wsprintf(score,"%d   第%d关",totalscore,level);/*输出分数和关数*/
	SendMessage(hwndEdit, WM_SETTEXT, 0, (long)score);
	return 1;
}
/*****************************************************************
//显示下一个方块
******************************************************************/
void shownextfk(HDC hdc,int style)
{
	int nextfkx=left-122,nextfky=top+20;
    SelectObject(hdc,hNextBrush);
	Rectangle(hdc,nextfkx,nextfky,nextfkx+80,nextfky+81);
	UnrealizeObject(hFk[NextFk]);//恢复图刷的原点
	SelectObject(hdc,hFk[NextFk]);
    SelectObject(hdc,hbkpen);
	switch(NextFk)
    {
	case 1:
		Rectangle(hdc,nextfkx+10,nextfky+20,nextfkx+70,nextfky+40);
        Rectangle(hdc,nextfkx+30,nextfky+40,nextfkx+50,nextfky+60);
		break;
	case 2:
		Rectangle(hdc,nextfkx+20,nextfky+10,nextfkx+40,nextfky+70);
		Rectangle(hdc,nextfkx+40,nextfky+10,nextfkx+60,nextfky+30);
		break;
	case 3:
		Rectangle(hdc,nextfkx+30,nextfky+10,nextfkx+50,nextfky+70);
		Rectangle(hdc,nextfkx+10,nextfky+10,nextfkx+30,nextfky+30);
		break;
	case 4:
		Rectangle(hdc,nextfkx+20,nextfky+30,nextfkx+60,nextfky+50);
		Rectangle(hdc,nextfkx+20,nextfky+10,nextfkx+40,nextfky+30);
		Rectangle(hdc,nextfkx+40,nextfky+50,nextfkx+60,nextfky+70);
		break;
	case 5:
		Rectangle(hdc,nextfkx+20,nextfky+30,nextfkx+60,nextfky+50);
		Rectangle(hdc,nextfkx+20,nextfky+50,nextfkx+40,nextfky+70);
		Rectangle(hdc,nextfkx+40,nextfky+10,nextfkx+60,nextfky+30);
		break;
    case 6:
		Rectangle(hdc,nextfkx+20,nextfky,nextfkx+40,nextfky+80);
		break;
	case 7:
		Rectangle(hdc,nextfkx+20,nextfky+20,nextfkx+60,nextfky+60);
		break;
	default:break;
    }
	
}
/****************************************************************
//设置关数
*****************************************************************/
int SetLevel(HWND hwnd)
{
	
	if( totalscore<5000) return 0;
	else if(totalscore<10000)
	{level=2;
	leveltime=500;
	idTimer=SetTimer(hwnd,IDT_TIME1,leveltime,NULL);		
    }
	else if(totalscore<15000)
	{level=3;
	leveltime=400;
	idTimer=SetTimer(hwnd,IDT_TIME1,leveltime,NULL);		
    }
	else if(totalscore<20000)
	{level=4;
	leveltime=300;
	idTimer=SetTimer(hwnd,IDT_TIME1,leveltime,NULL);		
    }
	else if(totalscore<30000)
	{level=5;
	leveltime=200;
	idTimer=SetTimer(hwnd,IDT_TIME1,leveltime,NULL);		
    }
	return 1;
}
/***************************************************************
//创建子窗口
****************************************************************/
void CreatechildWindow(HWND hwnd)
{
	hwndEdit = CreateWindowEx(WS_EX_WINDOWEDGE,"edit", "0",
         WS_CHILD | ES_READONLY|WS_VISIBLE|WS_BORDER|WS_DISABLED,
             135, 150, 100,25, hwnd,(HMENU)ID_EDIT,hInst,NULL);
}
/******************************************************************************
//HELP对话框的消息处理
******************************************************************************/
LRESULT CALLBACK Help(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:
		return TRUE;		
	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK) 
		{
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		break;
	}
    return FALSE;
}
/****************************************************************************
//保存玩家的信息
*****************************************************************************/
BOOL SaveInfor()
{
	FILE *hFile;
	char buff[8];
	int i=0,j=0;
	if((hFile=fopen("infor.loy","w+"))==NULL)
		MessageBox(NULL,NULL,"error",NULL);
	cls_back(FALSE);                         //清除当前正下落的方块对应的ROOM值
	wsprintf(buff,"%ld",totalscore);
	fwrite(buff,sizeof(buff),1,hFile);//写入分数和关数
	wsprintf(buff,"%2d",level);
	fwrite(buff,sizeof(buff),1,hFile);//写入分数和关数
	for(i=0;i<8;i++)
	{
		wsprintf(buff,"%2d",sqbar.xy[i]);
		fwrite(buff,sizeof(buff),1,hFile);//写入当前正下落的方块的信息
	}
    wsprintf(buff,"%2d",sqbar.color);
	fwrite(buff,sizeof(buff),1,hFile);//写入当前正下落的方块的信息
	wsprintf(buff,"%2d",sqbar.initshape);
	fwrite(buff,sizeof(buff),1,hFile);
    wsprintf(buff,"%2d",sqbar.nowshape);
	fwrite(buff,sizeof(buff),1,hFile);
	wsprintf(buff,"%2d",sqbar.style_num);
	fwrite(buff,sizeof(buff),1,hFile);
	for(i=1;i<=12;i++)
		for(j=1;j<=20;j++)
		{
          wsprintf(buff,"%2d",room[i][j]);
		  fwrite(buff,sizeof(buff),1,hFile);
		}
	fclose(hFile);
	return TRUE;
}
/****************************************************************************
//读取玩家的信息
*****************************************************************************/
BOOL ReadInfor()
{
	FILE *hFile;
	char buff[8];
	int i=0,j=0,temp=0;
	if((hFile=fopen("infor.loy","r"))==NULL)
		MessageBox(NULL,NULL,"error",NULL);
	fread(buff,sizeof(buff),1,hFile);
	totalscore=(long)atol(buff);
	fread(buff,sizeof(buff),1,hFile);
	level=atoi(buff);
    for(i=0;i<8;i++)
	{
		fread(buff,sizeof(buff),1,hFile);
		sqbar.xy[i]=atoi(buff);
	}
    fread(buff,sizeof(buff),1,hFile);
	sqbar.color=atoi(buff);
	fread(buff,sizeof(buff),1,hFile);
	sqbar.initshape=atoi(buff);
	fread(buff,sizeof(buff),1,hFile);
	sqbar.nowshape=atoi(buff);
	fread(buff,sizeof(buff),1,hFile);
	sqbar.style_num=atoi(buff);
    for(i=1;i<=12;i++)
		for(j=1;j<=20;j++)
		{
		  fread(buff,sizeof(buff),1,hFile);
		  room[i][j]=atoi(buff);
		}
	fclose(hFile);
	return TRUE;
}
/*****************************************************************************
//初始化边界,用来判断是否越界
******************************************************************************/ 
void InitRoom()
{
	int i=0;
	for(i=0;i<=21;i++)
		room[13][i]=-1;
}
/*****************************************************************************
//增加游戏的难度,使方块定时上移
******************************************************************************/
void UpMove()
{
  int i=0,j=0;
	for(i=1;i<=12;i++)
		 for(j=1;j<=20;j++)
		   room[i][j]=room[i][j+1];
    srand(time(NULL));
	for(i=1;i<=12;i++)
		room[i][20]=rand()%7+0;
	fky--;
}


你可能感兴趣的:(游戏,windows,Microsoft,J#,mfc)