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

📄 russia.cpp

📁 用vc实现德罗斯放块游戏 用vc实现德罗斯放块游戏
💻 CPP
字号:
// Russia.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include "Cube.h"

#define MAX_LOADSTRING 100
#define IDT_SPEED 1
#define IDT_MOVE 2
#define WAIT_TIME 1000

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

RECT cubepos;
CCube Russia;
int cube[4][4];
int s = 20;
int GameStatic = 0;


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

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_RUSSIA, szWindowClass, MAX_LOADSTRING);
	MyRegsterClass(hInstance);

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

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

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}



//
//  FUNCTION: MyRegsterClass()
//
//  PURPOSE: Regsters the window class.
//
//  COMMENTS:
//
//    Ths function and its usage s only necessary if you want ths code
//    to be compatible with Win32 systems prior to the 'RegsterClassEx'
//    function that was added to Windows 95. It s important to call ths function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegsterClass(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_RUSSIA);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_RUSSIA;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In ths function, we save the instance handle in a global variable and
//        create and dsplay the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

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

   hWnd = CreateWindow(szWindowClass, szTitle, WS_SYSMENU|WS_MINIMIZEBOX,
      CW_USEDEFAULT, 0, s*16, s*26, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

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

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	HBRUSH hBrush=NULL;
	static int x = 0, y = 0;

	switch (message) 
	{
		case WM_CREATE:
			break;

		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		
		case WM_TIMER:
			switch(wParam)
			{
			case IDT_SPEED:
				if(Russia.Move(0, 1))
				{
					KillTimer(hWnd,IDT_MOVE);
					Russia.m_yMove++;
					SetTimer(hWnd,IDT_MOVE,WAIT_TIME,NULL);
				}
				else if(Russia.IsLevelUp())
				{
					SetTimer(hWnd,IDT_SPEED,Russia.m_speed,NULL);
				}
				else if(Russia.IsLost())
				{
					GameStatic = 2;
					KillTimer(hWnd,IDT_SPEED);
					SendMessage(hWnd, WM_PAINT, NULL, NULL);
				}	
				break;
			case IDT_MOVE:;
				if(!Russia.Move(0, 1))
				{
					Russia.m_xMove = 0;
					Russia.m_yMove = 0;
					Russia.GetCube();
				}
			}

		case WM_KEYDOWN:
			if(GameStatic!=1 && wParam!=0x20) break;

			switch(wParam)
			{
			case 0x20:	//Stop
				if(GameStatic!=1)
				{	
					SetTimer(hWnd,IDT_SPEED,Russia.m_speed,NULL);
					SetTimer(hWnd,IDT_MOVE,WAIT_TIME,NULL);
					GameStatic = 1;
					break;
				}
				if(GameStatic==1)
				{
					GameStatic=0;
					KillTimer(hWnd,IDT_SPEED);
					KillTimer(hWnd,IDT_MOVE);
				}
				else
				{
					GameStatic=1;
					SetTimer(hWnd,IDT_SPEED,Russia.m_speed,NULL);
					SetTimer(hWnd,IDT_MOVE,WAIT_TIME,NULL);
				}
				break;
			case 0x26:	//Transform
				Russia.Transform();
				break;
			case 0x25:	//left
				if(Russia.Move(1, 0)) Russia.m_xMove--;
				break;
			case 0x27:	//right
				if(Russia.Move(-1, 0)) Russia.m_xMove++;
				break;
			case 0x28:	//down
				if(Russia.Move(0, 1)) Russia.m_yMove++;
				break;
			default:
				break;
			}

		case WM_PAINT:
			hdc = GetDC(hWnd);
			BeginPaint(hWnd, &ps);
			// TODO: Add any drawing code here...

			for(y=0; y<22; y++)
			{
				for(x=0; x<11; x++)
				{
					cubepos.top = y*s-s*2;
					cubepos.left = x*s-s;
					cubepos.bottom = cubepos.top+s-1;
					cubepos.right = cubepos.left+s-1;

					switch(Russia.m_Map[y][x])
					{
					case 0:
						cubepos.bottom = cubepos.top+s;
						cubepos.right = cubepos.left+s;
						FillRect(hdc, &cubepos, NULL);
						break;
					case 1:
						hBrush=CreateSolidBrush(RGB(0,0,0));
						FillRect(hdc, &cubepos, hBrush);
						break;
					default:break;
					}
					DeleteObject(hBrush);
				}
			}

			for(y=0; y<4; y++)
			{
				for(x=0; x<4; x++)
				{
					cubepos.top = y*s+20;
					cubepos.left = x*s+s*11;
					cubepos.bottom = cubepos.top+s-1;
					cubepos.right = cubepos.left+s-1;

					switch(Russia.m_TempCube[y][x])
					{
					case 0:
						FillRect(hdc, &cubepos, NULL);
						break;
					case 1:
						hBrush=CreateSolidBrush(RGB(0,0,0));
						FillRect(hdc, &cubepos, hBrush);
						break;
					default:break;
					}
					DeleteObject(hBrush);
				}
			}

			if(GameStatic!=1) TextOut(hdc, s*11, s*13, "Pause", 5);
			else  TextOut(hdc, s*11, s*13, "           ", 10);

			if(GameStatic==2) TextOut(hdc, s*3, s*7, "You Lost !!!", 12);

			sprintf(szString,"Level :    %d   ",Russia.m_level);
			TextOut(hdc, s*11, s*16, szString, strlen(szString));

			sprintf(szString,"Score :    %d   ",Russia.m_score);
			TextOut(hdc, s*11, s*18, szString, strlen(szString));

			sprintf(szString,"Space :  Start / Pause    Up : Transform");
			TextOut(hdc, 0, s*21, szString, strlen(szString));

			sprintf(szString,"Left / Right : Move    Down : Speed Up");
			TextOut(hdc, 0, s*22, szString, strlen(szString));

			MoveToEx(hdc, s*10, 0, NULL);
			LineTo(hdc, s*10, s*20);
			MoveToEx(hdc, s*10, s*20, NULL); 
			LineTo(hdc, 0, s*20);

			EndPaint(hWnd, &ps);
			ReleaseDC(hWnd,hdc);
			break;

		case WM_DESTROY:
			KillTimer(hWnd,IDT_SPEED);
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}

⌨️ 快捷键说明

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