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

📄 fun.cpp

📁 一个用SDK编写的方块程序
💻 CPP
字号:
#include "stdafx.h"
#include "resource.h"
#include "SDKBlock.h"
#include "commctrl.h"
#include "mmsystem.h"


extern	int		Shape[7][4][4][4];
extern	UINT    nInterval[9];
extern	BOOL	bRunning;
extern	int		nShape;
extern  int		nState;
extern	int		nScore;
extern	int		nLevel;
extern	int		nNextShape;
extern	int		nCurrentX;
extern	int		nCurrentY;
extern	int		Map[20][12];
//-------------------------------------------------------------
extern	HBRUSH	hBrushInfo;
extern	HBRUSH	hBrushBack;
extern	HBRUSH	hBrushStatic;
//-------------------------------------------------------------
extern	HBITMAP		hEraseBmp;
extern	HBITMAP		hUnitSmallBmp;
extern	HBITMAP		hUnitBigBmp;
extern	HBITMAP		hBitmap;
extern	HBITMAP		hStartBmp;
extern	HBITMAP		hStopBmp;
extern	HBITMAP		hNewBmp;
extern	HBITMAP		hExitBmp;
extern	HBITMAP		hOkBmp;
extern	HBITMAP		hCancelBmp;

//-------------------------------------------------------------
extern	BOOL		bFirst;
extern	BOOL		bHide ;
extern	BOOL		bSoundOn ;
//-------------------------------------------------------------
extern	HWND		hLevelProgress;
extern	HWND		hScoreWnd;
extern	HWND		hTimeWnd;
extern	HWND		hMainWnd;
extern	HWND		hListWnd;

extern	HINSTANCE	hInst;

extern	WNDPROC		SysOldProc;
//-------------------------------------------------------------
extern	char		pszPlayerName[80];
//-------------------------------------------------------------
void ShowBlock(HDC hdc)
{
	RECT	rect;
	HDC		hdcMem;
	HBITMAP	hOldBmp;
	hdcMem = CreateCompatibleDC(hdc);
	hOldBmp = (HBITMAP)SelectObject(hdcMem,hUnitSmallBmp);
	int i,j;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
		{
			if(Shape[nShape][nState][i][j]==1)
			{
				rect.left=(nCurrentX+j)*20;
				rect.top=(i+nCurrentY)*20;
				rect.right=(nCurrentX+j+1)*20;
				rect.bottom=(i+nCurrentY+1)*20;
				InvalidateRect(hMainWnd,&rect,TRUE);
			}
		}
	SelectObject(hdcMem,hOldBmp);
	DeleteDC(hdcMem);
}
void HideBlock(HDC hdc)
{
	RECT	rect;
	int i,j;
	bHide	= TRUE;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
		{
			if(Shape[nShape][nState][i][j]==1)
			{
				rect.left=(nCurrentX+j)*20;
				rect.top=(i+nCurrentY)*20;
				rect.right=(nCurrentX+j+1)*20;
				rect.bottom=(i+nCurrentY+1)*20;
				InvalidateRect(hMainWnd,&rect,TRUE);
			}
		}
}
void OnFileNew(HWND hWnd)
{
	nScore = 0;
	nLevel = 0;
	srand(GetTickCount());
	nShape = rand()%7;
	nNextShape = rand()%7;
	nState = 0;
	nCurrentX = 5;
	nCurrentY = 0;
	if(bRunning)
	{
		KillTimer(hWnd,ID_TIMER);
		bRunning = FALSE;
	}
	memset(Map,0,sizeof(Map));
	InvalidateRect(hWnd,NULL,TRUE);
	char s[20];
	wsprintf(s,"%d",nScore);
	SetWindowText(hScoreWnd,s);
	SendMessage(hLevelProgress,PBM_SETPOS,(WPARAM)nLevel,0);
}
void OnStart(HWND hWnd)
{
	if(!bRunning)
	{
		SetTimer(hWnd,ID_TIMER,nInterval[nLevel],(TIMERPROC)TimerProc);
		bRunning = TRUE;
	}
}
void OnPause(HWND hWnd)
{
	if(bRunning)
	{
		KillTimer(hWnd,ID_TIMER);
		bRunning=FALSE;
	}
}
VOID CALLBACK TimerProc(HWND hWnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
	
	DWORD dwPt=GetMessagePos();
	POINT pt;
	pt.x=LOWORD(dwPt);
	pt.y=HIWORD(dwPt);
	if(WindowFromPoint(pt)==hWnd)
		SetFocus(hWnd);
	if(Test(nState,nCurrentX,nCurrentY)==FALSE)
	{
		OnPause(hWnd);
		if(bSoundOn)
		{
			PlaySound((LPCTSTR)IDR_WIN,NULL ,SND_RESOURCE  |SND_ASYNC  );
		}
		SaveToFile();
		OnFileNew(hWnd);
		return;
	}
	if(Test(nState,nCurrentX,nCurrentY+1)==FALSE)
	{
		if(bSoundOn)
		{
			PlaySound((LPCTSTR)IDR_BOTTOM,NULL ,SND_RESOURCE  |SND_ASYNC  );
		}
		for(int i=0;i<4;i++)
			for(int j=0;j<4;j++)
			{
				if(Shape[nShape][nState][i][j]==1)
				Map[nCurrentY+i][nCurrentX+j]=1;
			}
		UINT uDelInfo=GetDelInfo();
		if(uDelInfo!=0)
		{
			ProcessDeleteLine(hWnd,uDelInfo);
		}
		CreateBlock();
		ShowPreview(hWnd);
	}
	else
	{
		GoFastDown(hWnd);
	}
}
void CreateBlock()
{
	nShape = nNextShape;
	srand(GetTickCount());
	nNextShape = rand()%7;
	nState = 0;
	nCurrentX = 5;
	nCurrentY=0;

}
void GoLeft(HWND hWnd)
{
	if(!bRunning)
		return;
	if(Test(nState,nCurrentX-1,nCurrentY)==TRUE)
	{
		HDC hdc=GetDC(hWnd);
		HideBlock(hdc);
		nCurrentX = nCurrentX-1;
		ShowBlock(hdc);
		ReleaseDC(hWnd,hdc);
		if(bSoundOn)
		{
			PlaySound((LPCTSTR)IDR_LEFT,NULL ,SND_RESOURCE  |SND_ASYNC  );
		}
	}
}
void GoRight(HWND hWnd)
{
	if(!bRunning)
		return;
	if(Test(nState,nCurrentX+1,nCurrentY)==TRUE)
	{
		HDC hdc=GetDC(hWnd);
		HideBlock(hdc);
		nCurrentX = nCurrentX+1;
		ShowBlock(hdc);
		ReleaseDC(hWnd,hdc);
		if(bSoundOn)
		{
			PlaySound((LPCTSTR)IDR_LEFT,NULL ,SND_RESOURCE  |SND_ASYNC  );
		}
	}
}
void GoFastDown(HWND hWnd)
{
	if(!bRunning)
		return;
	if(Test(nState,nCurrentX,nCurrentY+1)==TRUE)
	{
		HDC hdc=GetDC(hWnd);
		HideBlock(hdc);
		nCurrentY = nCurrentY+1;
		ShowBlock(hdc);
		ReleaseDC(hWnd,hdc);
	}
}
void ChangeState(HWND hWnd)
{
	if(!bRunning)
		return;
	if(Test((nState+1)%4,nCurrentX,nCurrentY)==TRUE)
	{
		HDC hdc=GetDC(hWnd);
		HideBlock(hdc);
		nState = (nState+1)%4;
		ShowBlock(hdc);
		ReleaseDC(hWnd,hdc);
		if(bSoundOn)
		{
			PlaySound((LPCTSTR)IDR_CHANGE,NULL ,SND_RESOURCE  |SND_ASYNC  );
		}
	}
}
BOOL Test(int state,int x,int y)
{
	int i,j;
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
		{
			if((x+j<0)&&(Shape[nShape][state][i][j]==1))
				return FALSE;
			if((x+j>=12)&&(Shape[nShape][state][i][j]==1))
				return FALSE;
			
			if((y+i>=20)&&(Shape[nShape][state][i][j]==1))
				return FALSE;
			
			if((Map[y+i][x+j] & Shape[nShape][state][i][j]) ==1)
				return FALSE;
		}
	return TRUE;
}
void DeleteLine(HWND hWnd,int iIndex)
{
	RECT rect;
	int i;
	for(i=iIndex;i>=1;i--)
		memcpy(Map[i],Map[i-1],12*sizeof(int));
	memset(Map,0,12*sizeof(int));
	SetRect(&rect,0,0,240,400);
	rect.bottom=iIndex*20+20;
	InvalidateRect(hWnd,&rect,TRUE);
	if(bSoundOn)
	{
		PlaySound((LPCTSTR)IDR_DEL,NULL ,SND_RESOURCE |SND_ASYNC  );
	}
	Sleep(200);
	
	
	
}
UINT GetDelInfo()
{
	UINT uDeleteLineInfo=0;
	int i,j;
	for(i=0;i<20;i++)
		{
			for(j=0;j<12;j++)
			{
				if(Map[i][j]==0)
					break;
			}
			if(j==12)             // has one line to delete
				uDeleteLineInfo=uDeleteLineInfo|(1L<<i);
		}
	return uDeleteLineInfo;
}
void ProcessDeleteLine(HWND hWnd,UINT uDeleteLineInfo)
{
	int i;
	int	iLineNumInfo=0;
	for(i=0;i<20;i++)
	{
		if( ( (uDeleteLineInfo>>i) & 1L)!=0)
			iLineNumInfo++;
	}
	
	for(i=0;i<20;i++)
	{
		if( ( (uDeleteLineInfo>>i) & 1L)!=0)
			DeleteLine(hWnd,i);
	}
	if(iLineNumInfo==1)
		nScore+=100;
	else if(iLineNumInfo==2)
		nScore+=300;
	else if(iLineNumInfo==3)
		nScore+=500;
	else
		nScore+=1000;
	char s[20];
	wsprintf(s,"%d",nScore);
	SetWindowText(hScoreWnd,s);
	if( (nScore/2000) >nLevel )
	{
		nLevel=( (nLevel+1) > 8) ? 8 : (nLevel+1);
		OnPause(hWnd);
		OnStart(hWnd);
	}
	SendMessage(hLevelProgress,PBM_SETPOS,(WPARAM)nLevel,0);
}
void ShowPreview(HWND hWnd)
{
	RECT	rect;
	HDC		hdc;
	HDC		hdcMem;
	HBITMAP	hOldBmp;
	int		i,j;
	int		nBaseX = 300;
	int		nBaseY = 150;
	SetRect(&rect,nBaseX,nBaseY,nBaseX+82,nBaseY+45);
	hdc = GetDC(hWnd);
	hdcMem = CreateCompatibleDC(hdc);
	hOldBmp = (HBITMAP)SelectObject(hdcMem,hEraseBmp);
	BitBlt(hdc,nBaseX,nBaseY,80,40,hdcMem,0,0,SRCCOPY);
	for(i=0;i<4;i++)
	{
		for(j=0;j<4;j++)
		{
			if(Shape[nNextShape][0][i][j]==1)
			{
				SelectObject(hdcMem,hUnitBigBmp);
				BitBlt(hdc,nBaseX+j*20,nBaseY+i*20,20,20,hdcMem,0,0,SRCCOPY);
				SetRect(&rect,nBaseX+j*20,nBaseY+i*20,nBaseX+j*20+20,nBaseY+i*20+20);
				DrawEdge(hdc,&rect,EDGE_RAISED,BF_RECT);
			}
		}
	}
	SelectObject(hdcMem,hOldBmp);
	DeleteDC(hdcMem);
	ReleaseDC(hWnd,hdc);
}
void GetBackBmp(HWND hWnd)
{
	HDC	hdc;
	HDC	dcMem;
	HBITMAP hOldBmp;
	hdc = GetDC(hWnd);
	dcMem = CreateCompatibleDC(hdc);	
	hEraseBmp = CreateCompatibleBitmap(hdc,80,40);
	hOldBmp = (HBITMAP)SelectObject(dcMem,hEraseBmp);
	BitBlt(dcMem,0,0,80,40,hdc,300,150,SRCCOPY);
	SelectObject(dcMem,hOldBmp);
	DeleteDC(dcMem);
	ReleaseDC(hWnd,hdc);
}
LRESULT CALLBACK InputProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:
		SendMessage(GetDlgItem(hDlg,IDOK),BM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)hOkBmp);
		SendMessage(GetDlgItem(hDlg,IDCANCEL),BM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)hCancelBmp);
		return TRUE;
	case WM_CTLCOLORDLG :
		return (LONG)hBrushInfo;
	case WM_CTLCOLORSTATIC :
		SetBkMode((HDC) wParam,TRANSPARENT);
		SetTextColor((HDC) wParam,RGB(0,0,255));
		return (LONG)hBrushInfo;	
	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK )
		{

			GetDlgItemText(hDlg,IDC_NAME,pszPlayerName,80);
			if(strcmp(pszPlayerName,"") == 0)
			{
				strcpy(pszPlayerName,"匿名");
			}
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		else if(LOWORD(wParam) == IDCANCEL) 
		{
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
			break;
	}
    return FALSE;
}

BOOL SaveToFile()
{
	FILE *pFile;
	HEROS heros[10];
	HEROS tempHero;
	SYSTEMTIME SysTime;
	char pszTime[40]="";
	int	i;
	memset(heros,0,sizeof(heros));
	memset(&tempHero,0,sizeof(HEROS));
	GetLocalTime(&SysTime);
	wsprintf(pszTime,"%4d/%02d/%02d--%02d:%02d:%02d",SysTime.wYear,SysTime.wMonth,SysTime.wDay,SysTime.wHour,SysTime.wMinute,SysTime.wSecond);
	if(_access("BlockData.bsn",0)== -1)
	{
		if(IDCANCEL==DialogBox(hInst, (LPCTSTR)IDD_DIALOG1, hMainWnd, (DLGPROC)InputProc))
		{
			strcpy(pszPlayerName,"匿名");
		}
		pFile=fopen("BlockData.bsn","w+");
		if(pFile == NULL)
		{
			MessageBox(NULL,"Can't create file for write!","warning",0);
			return FALSE;
		}
		wsprintf(tempHero.level,"%d",nLevel);
		wsprintf(tempHero.score,"%d",nScore);
		strcpy(tempHero.name,pszPlayerName);
		strcpy(tempHero.playtime,pszTime);
		fwrite(&tempHero,sizeof(HEROS),1,pFile);
		for(i=0;i<9;i++)
		{
			strcpy(heros[i].level,"0");
			strcpy(heros[i].score,"0");
			strcpy(heros[i].name,"匿名");
			strcpy(heros[i].playtime,"0000/00/00--00:00:00");
			fwrite(&heros[i],sizeof(HEROS),1,pFile);
		}
		fclose(pFile);
		return TRUE;

	}
	else
	{
		pFile=fopen("BlockData.bsn","r+");
		if(pFile == NULL)
		{
			MessageBox(NULL,"Can't open file for write!","warning",0);
			return FALSE;
		}
		for(i=0;i<10;i++)
		{
			fread(&heros[i],sizeof(HEROS),1,pFile);
		}
		for(i=0;i<10;i++)
		{
			if(atoi(heros[i].score) < nScore)
			{
				if(IDCANCEL==DialogBox(hInst, (LPCTSTR)IDD_DIALOG1, hMainWnd, (DLGPROC)InputProc))
				{
					strcpy(pszPlayerName,"匿名");
				}
				strcpy(tempHero.name,pszPlayerName);
				wsprintf(tempHero.score,"%d",nScore);
				wsprintf(tempHero.level,"%d",nLevel);
				strcpy(tempHero.playtime,pszTime);
				memmove(heros+i+1,heros+i,(9-i)*sizeof(HEROS));
				heros[i]=tempHero;
				fseek(pFile,0L,SEEK_SET);
				fwrite(heros,sizeof(HEROS),10,pFile);
				fclose(pFile);
				ShowWindow(hMainWnd,SW_HIDE);
				DialogBox(hInst, (LPCTSTR)IDD_DIALOG2, hMainWnd, (DLGPROC)HeroProc);
				ShowWindow(hMainWnd,SW_SHOW);
				return TRUE;
			}
		}
		fclose(pFile);
		return FALSE;
	}
}

LRESULT CALLBACK SysHeroProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	RECT	rect;
	PAINTSTRUCT ps;
	switch(message)
	{
	case WM_ERASEBKGND:
		GetClientRect(hWnd,&rect);
		FillRect((HDC) wParam,&rect,hBrushBack);
		return TRUE;
	case WM_SETCURSOR:
		return 0;
	case WM_LBUTTONDOWN:
		return TRUE;
	case WM_MOUSEMOVE:
		return TRUE;
	case WM_PAINT:
		RECT	rect1,rect2,rect3,rect4;
		SetRect(&rect1,0,0,95,20);
		SetRect(&rect2,96,0,185,20);
		SetRect(&rect3,186,0,275,20);
		SetRect(&rect4,276,0,446,20);
		hdc = BeginPaint(hWnd,&ps);
		DrawEdge(hdc,&rect1,EDGE_RAISED,BF_RECT);
		DrawEdge(hdc,&rect2,EDGE_RAISED,BF_RECT);
		DrawEdge(hdc,&rect3,EDGE_RAISED,BF_RECT);
		DrawEdge(hdc,&rect4,EDGE_RAISED,BF_RECT);
		SetBkMode(hdc,TRANSPARENT);
		SetTextColor(hdc,RGB(200,0,0));
		TextOut(hdc,5,1,"姓名",strlen("姓名"));
		TextOut(hdc,101,1,"得分",strlen("姓名"));
		TextOut(hdc,191,1,"级别",strlen("姓名"));
		TextOut(hdc,280,1,"记录时间",strlen("记录时间"));
		EndPaint(hWnd,&ps);
		return 0;
	}
	return CallWindowProc(SysOldProc,hWnd,message,wParam,lParam);
}

LRESULT CALLBACK HeroProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	FILE	*pFile;
	HEROS	heros[10];
	int		i;
	LVCOLUMN	lvcolumn;
	pFile=fopen("BlockData.bsn","r");
	fread(heros,sizeof(heros),1,pFile);
	fclose(pFile);
	switch (message)
	{
	case WM_INITDIALOG:
		ListView_SetExtendedListViewStyle(GetDlgItem(hDlg,IDC_LIST1),LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES|LVS_EX_TRACKSELECT);
		ListView_SetBkColor(GetDlgItem(hDlg,IDC_LIST1),RGB(0,0,0));
		ListView_SetTextColor(GetDlgItem(hDlg,IDC_LIST1),RGB(0,255,0));
		ListView_SetTextBkColor(GetDlgItem(hDlg,IDC_LIST1),RGB(0,0,0));
		lvcolumn.mask = LVCF_TEXT |LVCF_WIDTH |LVCF_FMT;
		lvcolumn.fmt = LVCFMT_LEFT ;
		lvcolumn.cx = 95;
		lvcolumn.pszText="姓名";
		lvcolumn.cchTextMax=20;
		ListView_InsertColumn(GetDlgItem(hDlg,IDC_LIST1),0,&lvcolumn);
		lvcolumn.cx = 90;
		lvcolumn.pszText="分数";
		ListView_InsertColumn(GetDlgItem(hDlg,IDC_LIST1),1,&lvcolumn);
		lvcolumn.pszText="到达等级";
		ListView_InsertColumn(GetDlgItem(hDlg,IDC_LIST1),2,&lvcolumn);
		lvcolumn.pszText="记录创建时间";
		lvcolumn.cx = 170;
		lvcolumn.cchTextMax=30;
		ListView_InsertColumn(GetDlgItem(hDlg,IDC_LIST1),3,&lvcolumn);
		SendMessage(GetDlgItem(hDlg,IDOK),BM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM)hOkBmp);
		for(i=0;i<10;i++)
		{
			LVITEM lvitem;
			memset(&lvitem,0,sizeof(LVITEM));
			lvitem.mask = LVIF_TEXT ;
			lvitem.iItem = i;
			lvitem.pszText = heros[i].name;
			lvitem.cchTextMax = 20;
			ListView_InsertItem(GetDlgItem(hDlg,IDC_LIST1),&lvitem);
			ListView_SetItemText(GetDlgItem(hDlg,IDC_LIST1),i,1,heros[i].score);
			ListView_SetItemText(GetDlgItem(hDlg,IDC_LIST1),i,2,heros[i].level);
			ListView_SetItemText(GetDlgItem(hDlg,IDC_LIST1),i,3,heros[i].playtime);
		}
		hListWnd=	GetWindow(GetDlgItem(hDlg,IDC_LIST1),GW_CHILD);
		SysOldProc=(WNDPROC)GetWindowLong(hListWnd,GWL_WNDPROC);
		SetWindowLong(hListWnd,GWL_WNDPROC,(LONG)SysHeroProc);
		return TRUE;
	case WM_CTLCOLORDLG :
		return (LONG)hBrushInfo;
	case WM_CTLCOLORSTATIC :
		SetBkMode((HDC) wParam,TRANSPARENT);
		SetTextColor((HDC) wParam,RGB(0,0,255));
		return (LONG)hBrushInfo;
	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK ||LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		break;
	}
	
    return FALSE;
}
void GoToBottom(HWND hWnd)
{
	HDC hdc = GetDC(hWnd);
	HideBlock(hdc);
	while(Test(nState,nCurrentX,nCurrentY+1)==TRUE)
	{
		nCurrentY++;
	}
	ShowBlock(hdc);
	ReleaseDC(hWnd,hdc);

}

⌨️ 快捷键说明

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