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

📄 i_gov1.cpp

📁 一个用SDK实现的俄罗斯方块程序,比较简单地实现基本应用功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// I_goV1.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include <time.h>

#include "resource.h"

#define MAX_LOADSTRING 100
#define UNIT_SIZE 15//模块单元大小
#define SQUARE_AMOUNT 17//窗体中模块单元的数目
#define WM_NEWSQUARE WM_USER+1//自定义消息
#define MYTIMER 1//自定义时钟标示
#define DIFLEVEL 500//难易度,即时钟间隔

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];								// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];								// The title bar text


// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

typedef struct _Square
{
	POINT ps[4];
	HWND hUnitWnd[4];
	int type;
}Square,*pSquare;

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_I_GOV1, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_I_GOV1);

	// 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(hInstance, (LPCTSTR)IDI_I_GOV1);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_I_GOV1;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}


BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_CAPTION|WS_SYSMENU,
      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)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
	static RECT rc;
	static BOOL IsFilled[SQUARE_AMOUNT][SQUARE_AMOUNT];
	static HWND hWndSquare[SQUARE_AMOUNT][SQUARE_AMOUNT];
	static Square sq;
	int i,j;
	int xFirstUnitPos = SQUARE_AMOUNT/2;
	static HMENU hMenu;

	switch (message) 
	{
		case WM_CREATE:
			rc.left = rc.top = 0;
			rc.right = rc.bottom = UNIT_SIZE*SQUARE_AMOUNT;
			AdjustWindowRect(&rc, WS_CAPTION|WS_SYSMENU,TRUE);
			SetWindowPos(hWnd,NULL,0,0,rc.right-rc.left,rc.bottom-rc.top,SWP_NOMOVE);
			break;
		case WM_TIMER:
			
			//是否不能再往下落了
			for ( i=0;i<4;i++)
			{
				if (IsFilled[sq.ps[i].x][sq.ps[i].y] == 1 )
				{
					KillTimer(hWnd,MYTIMER);
					MessageBox(hWnd,"你已失败","注意",MB_OK) ;
					PostMessage(hWnd,WM_DESTROY,0,0);
					return 0;
				}
				if (sq.ps[i].y == SQUARE_AMOUNT-1 || IsFilled[sq.ps[i].x][sq.ps[i].y+1] == TRUE )
				{
					for (j=0;j<4;j++)//填充方块所占的区域
					{
						IsFilled[sq.ps[j].x][sq.ps[j].y] = TRUE;
						hWndSquare[sq.ps[j].x][sq.ps[j].y] = sq.hUnitWnd[j];
					}
					KillTimer(hWnd,MYTIMER);
					//消去方块
					//浏览,产生记录条
					{//procedure
						unsigned int flag = 0;
						for(i=SQUARE_AMOUNT-1;i>=0;i--)//y shaft
						{
							for(j=0;j<SQUARE_AMOUNT;j++)//x shaft
							 if  ( !IsFilled[j][i] )
								 break;
							if ( j == SQUARE_AMOUNT )
								flag |=1l<<i;
						}
						if (flag) //有行要消掉
						{
							for( i=SQUARE_AMOUNT-1;i>=0;i-- )
								if (flag&1l<<i) //消去这一行
									for(j=0;j<SQUARE_AMOUNT;j++)
									{
										DestroyWindow(hWndSquare[j][i]);
										IsFilled[j][i] = FALSE;
									}

							for (i=SQUARE_AMOUNT-1;i>=0;i--)
								if ( !(flag&1l<<i) )//这行没有消去,进行下落处理
								{
									int temp,offset;
									temp=i+1;
									offset=0;
									while(temp<SQUARE_AMOUNT)
									{
										if ( flag&1l<<temp )
											offset++;
										temp++;
									}

									for (j=0;j<SQUARE_AMOUNT;j++)
									{
										RECT rcUnit;
										if ( IsFilled[j][i] )
										{
											GetWindowRect(hWndSquare[j][i],&rcUnit);
											POINT pt;
											pt.x = rcUnit.left;
											pt.y = rcUnit.top;
											ScreenToClient(hWnd,&pt);
										    MoveWindow(hWndSquare[j][i],pt.x,pt.y+offset*UNIT_SIZE,
												 UNIT_SIZE,UNIT_SIZE,TRUE);
										    IsFilled[j][i] = FALSE;
										    IsFilled[j][i+offset] = TRUE;
										    hWndSquare[j][i+offset] = hWndSquare[j][i];
										}

									}
								}
						}
					}//procedure
			
					SendMessage(hWnd,WM_NEWSQUARE,NULL,NULL);
					return 0;
				}
			}
		
			//移动方块
			for ( i= 0;i<4;i++)
			{
				//_beginthread();
				MoveWindow(sq.hUnitWnd[i],sq.ps[i].x*UNIT_SIZE,
				        (++sq.ps[i].y)*UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,TRUE);
			}
			
			break;
		
		case WM_NEWSQUARE://生成方块
			srand( (unsigned)time( NULL ) );

			switch ( rand()%6 )
			{
			/* @@@@ */
			case 0:
				sq.type = 0;
				xFirstUnitPos = 8;
				for( i=0;i<4;i++)
				{
					sq.ps[i].x = xFirstUnitPos+i;
					sq.ps[i].y = 0;
					sq.hUnitWnd[i] = CreateWindow("BUTTON",NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,0,
						UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
				}
				break;

			/* @
			   @@@ */
			case 1:
				sq.type = 1;
				for( i=0;i<4;i++)
				{
					if ( 0==i)
					{
						sq.ps[i].x = xFirstUnitPos;
					    sq.ps[i].y = 0;
						sq.hUnitWnd[i] = CreateWindow("BUTTON",NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,sq.ps[i].y,
							UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
					}
					else
					{
						sq.ps[i].x = xFirstUnitPos + i-1;
					    sq.ps[i].y = 1;
						sq.hUnitWnd[i] = CreateWindow("BUTTON",NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,sq.ps[i].y,
							UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
					}
				}
				break;


			/* @@
			   @@ */
			case 2:
				sq.type = 2;
				for( i=0;i<4;i++)
				{
					if (i==0 || i==1 )
					{
						sq.ps[i].x = xFirstUnitPos+i;
						sq.ps[i].y = 0;
						sq.hUnitWnd[i] = CreateWindow("BUTTON",NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,0,
							UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
					}
					else 
					{
						sq.ps[i].x = xFirstUnitPos+i-2;
						sq.ps[i].y = 1;
						sq.hUnitWnd[i] = CreateWindow("BUTTON",NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,0,
							UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
					}
				}

				break;

			/* @
			  @@@*/
			case 3:
				sq.type = 3;
				for( i=0;i<4;i++)
				{
					if (i==0  )
					{

⌨️ 快捷键说明

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