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

📄 gamepaper.cpp

📁 纸牌源码
💻 CPP
字号:
// gamepaper.cpp : Defines the entry point for the application.
//

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

#include "bitmaptool.h"
#include "mybutton.h"
#include "gamemap.h"
#include "myclock.h"


#define MAX_LOADSTRING 100

#define GAME_STATE_MENU	0
#define GAME_STATE_MAIN	1
#define GAME_STATE_WIN	2

//游戏状态
int iGameState=GAME_STATE_MENU;

//BITMAP
MYBITMAP bBack(GAMEWIDTH,GAMEHEIGHT);
MYBITMAP bWin(GAMEWIDTH,GAMEHEIGHT);

//HANDLE
HDC hdcwindow,hdcscreen,hdcmem;
HBITMAP hback;

//FONT
HFONT hf[5];

//BUTTON
MYBUTTON btLevel1;
MYBUTTON btLevel2;
MYBUTTON btRestart;
MYBUTTON btBegin;

//MAIN LOGIC
GAMEMAP gmap;

//clock
MYCLOCK clock1;

//
void InitFont()
{
	char temp[10]={0};
	int i;

	for(i=0;i<=sizeof(hf)/sizeof(HFONT);i++)
	{
		sprintf(temp,"black%d",i);
		hf[i]=CreateFont(10*(i+2),0,0,0,FW_HEAVY,0,0,0,GB2312_CHARSET,OUT_DEFAULT_PRECIS,
				CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,temp);
	}
}


// 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);

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_GAMEPAPER, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

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

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

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

	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
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_GAMEPAPER);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
//	wcex.lpszMenuName	= (LPCSTR)IDC_GAMEPAPER;
	wcex.lpszMenuName	= NULL;
	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 this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;
   int x,y;

   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;
   }

	//窗口居中, 高度包含标题栏的30
	x=GetSystemMetrics(SM_CXSCREEN);
	y=GetSystemMetrics(SM_CYSCREEN);
	MoveWindow(hWnd,(x-GAMEWIDTH)/2,(y-GAMEHEIGHT-30)/2,GAMEWIDTH,GAMEHEIGHT+30,false);
	
	//DC
	hdcwindow=GetDC(hWnd);
	hdcmem=CreateCompatibleDC(hdcwindow);
	hdcscreen=CreateCompatibleDC(hdcwindow);
	
	//gmap.hdc=hdcscreen;
	
	ReleaseDC(hWnd,hdcwindow);
	//设置字体
	InitFont();

	//设置图像
	hback=LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP_NULL));
	bBack.Init(hInst,IDB_BITMAP_MENU,1,1);
	bBack.SetDC(hdcscreen,hdcmem);
	bBack.SetPos(BM_CENTER,0,0);

	bWin.Init(hInst,IDB_BITMAP_WIN,1,1);
	bWin.SetDC(hdcscreen,hdcmem);
	bWin.SetPos(BM_CENTER,0,250);

		//按钮
	btLevel1.Init("初级:两种花色",RGB(0,0,255),RGB(0,128,255),
		RGB(255,255,0),RGB(255,255,128),hf[1]);
	btLevel1.SetDC(hdcscreen,hdcmem);
	btLevel1.SetPos(BT_CENTER,0,200);

	btLevel2.Init("高级:四种花色",RGB(0,0,255),RGB(0,128,255),
		RGB(255,255,0),RGB(255,255,128),hf[1]);
	btLevel2.SetDC(hdcscreen,hdcmem);
	btLevel2.SetPos(BT_CENTER,0,250);

	btBegin.Init("重新发牌",RGB(0,0,255),RGB(0,128,255),
		RGB(255,255,0),RGB(255,255,128),hf[0]);
	btBegin.SetDC(hdcscreen,hdcmem);
	btBegin.SetPos(BT_BOTTOM_LEFT,100,0);

	btRestart.Init("回主菜单",RGB(0,0,255),RGB(0,128,255),
		RGB(255,255,0),RGB(255,255,128),hf[0]);
	btRestart.SetDC(hdcscreen,hdcmem);
	btRestart.SetPos(BT_BOTTOM_LEFT,0,0);

	

	//main logic
	gmap.SetDC(hdcscreen,hdcmem);
	gmap.Init(hInstance);

   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;
	TCHAR szHello[MAX_LOADSTRING];
	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

	switch (message) 
	{
		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_PAINT:
			hdcwindow = BeginPaint(hWnd, &ps);
			switch(iGameState)
			{
			case GAME_STATE_MENU:
				SelectObject(hdcscreen,hback);
				bBack.Draw(SRCCOPY);
				btLevel1.Draw();
				btLevel2.Draw();
				break;
			case GAME_STATE_MAIN:
				gmap.Show();
				gmap.DrawCatchPaper();
				if(gmap.CheckWin())
				{
					iGameState=GAME_STATE_WIN;
					gmap.ShowWinStart();					
					clock1.Begin(hWnd,25,180);//时长60*3秒
				}
				gmap.ShowBack();
				btRestart.Draw();
				btBegin.Draw();
				break;
			case GAME_STATE_WIN:
				gmap.ShowWin();				
				btRestart.Draw();
				btBegin.Draw();
				break;
			}
			//REFRESH SCREEN
   			BitBlt(hdcwindow,0,0,GAMEWIDTH,GAMEHEIGHT,hdcscreen,0,0,SRCCOPY);
			EndPaint(hWnd, &ps);
			break;
		case WM_MOUSEMOVE:
			switch(iGameState)
			{
			case GAME_STATE_MENU:
				btLevel1.DetectMouseMove(LOWORD(lParam),HIWORD(lParam));
				btLevel2.DetectMouseMove(LOWORD(lParam),HIWORD(lParam));
				break;
			case GAME_STATE_MAIN:
				//defect01
				if(wParam & MK_LBUTTON)
				{
					gmap.LMOVEproc(LOWORD(lParam),HIWORD(lParam));
				}
				else
				{
					//移动过程中,左键松开
					gmap.ClearCatchPaper();
				}
				btRestart.DetectMouseMove(LOWORD(lParam),HIWORD(lParam));
				btBegin.DetectMouseMove(LOWORD(lParam),HIWORD(lParam));
				break;

			case GAME_STATE_WIN:
				btRestart.DetectMouseMove(LOWORD(lParam),HIWORD(lParam));
				btBegin.DetectMouseMove(LOWORD(lParam),HIWORD(lParam));
				break;
			}
			InvalidateRect(hWnd,NULL,false);
			break;

		case WM_LBUTTONUP:
			switch(iGameState)
			{
			case GAME_STATE_MENU:
				if(btLevel1.DetectMouseUp(LOWORD(lParam),HIWORD(lParam)))
				{
					iGameState=GAME_STATE_MAIN;
					gmap.SetPaper();
					gmap.SetLevel(0);
				}
				if(btLevel2.DetectMouseUp(LOWORD(lParam),HIWORD(lParam)))
				{
					iGameState=GAME_STATE_MAIN;
					gmap.SetPaper();
					gmap.SetLevel(1);
				}
				
				break;

			case GAME_STATE_MAIN:
				gmap.LUPproc(LOWORD(lParam),HIWORD(lParam));
				if(btRestart.DetectMouseUp(LOWORD(lParam),HIWORD(lParam)))
				{
					iGameState=GAME_STATE_MENU;				
				}
				if(btBegin.DetectMouseUp(LOWORD(lParam),HIWORD(lParam)))
				{
					iGameState=GAME_STATE_MAIN;
					gmap.SetPaper();		
				}
				//test
				break;

			case GAME_STATE_WIN:
				if(btRestart.DetectMouseUp(LOWORD(lParam),HIWORD(lParam)))
				{
					iGameState=GAME_STATE_MENU;				
				}
				if(btBegin.DetectMouseUp(LOWORD(lParam),HIWORD(lParam)))
				{
					iGameState=GAME_STATE_MAIN;
					gmap.SetPaper();		
				}
				break;
			}					
			InvalidateRect(hWnd,NULL,false);
			break;

		case WM_LBUTTONDOWN:
			switch(iGameState)
			{
			case GAME_STATE_MAIN:
				gmap.LDOWNproc(LOWORD(lParam),HIWORD(lParam));
				InvalidateRect(hWnd,NULL,false);
				break;
			}		
			break;

		case WM_TIMER:
			clock1.Dec();
			InvalidateRect(hWnd,NULL,false);
			break;

		case WM_KEYDOWN:
			switch(iGameState)
			{
			case GAME_STATE_MAIN:
				switch(wParam)
				{
				case VK_SPACE:
					gmap.SetShowBack(1);
					InvalidateRect(hWnd,NULL,false);
					break;
				}
				break;
			}
			break;

		case WM_KEYUP:
			switch(iGameState)
			{
			case GAME_STATE_MAIN:
				switch(wParam)
				{
				case VK_SPACE:
					gmap.SetShowBack(0);
					InvalidateRect(hWnd,NULL,false);
					break;
				}
				break;
			}
			break;

		case WM_DESTROY:
			DeleteDC(hdcmem);
			DeleteDC(hdcscreen);
			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 + -