工具
VS13
项目描述:
本项目是一个2048游戏,基于Win32开发,以C++语言为编程基础。在游戏中,方块的出现完全随机,仅出现在空方格中。当方块出现时,方块上的数字有 90% 的几率为2, 10% 的几率为4。合并规则主要是上下左右移动后,相同的数合并,不相同的数原样输出,要判断每一行或每一列有没有相同的,还有一点就是,数字有移动,就产生新的数,没有,什么都不用做。
项目框架:
效果图:
紫粉色区为预留区,添加其他功能使用
主程序
// _2048.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "_2048.h"
#define MAX_LOADSTRING 100
MCI_OPEN_PARMS m_mciOpen;
MCI_PLAY_PARMS m_mciPlay;
bool PlayMp3(TCHAR* szFileNmae);
HMENU hmenu1 = CreateMenu();
HMENU hmenu2 = CreateMenu();
HMENU hmenu3 = CreateMenu();
HMENU g_hmenu = CreateMenu();
TCHAR cBuffer[] = _T("src/ Continued Story.mp3");
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
HWND g_hwnd;
CManage *mGame = NULL;
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC__2048, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC__2048));
HDC hdc = GetDC(g_hwnd);
mGame = new CManage(hdc);
mGame->InitGame();
// 主消息循环:
ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
hdc = GetDC(g_hwnd);
mGame->UpdataGame();
mGame->RenderGame(hdc);
if (mGame->Gamejudge())
{
TCHAR* str = L"你输了!";
WORD Val = MessageBox(g_hwnd, str, L"2048", 0);
if (Val == IDYES)
SendMessage(g_hwnd, WM_DESTROY, 0, 0);
}
if (mGame->isWin())
{
TCHAR* str = L"恭喜你,组成2048赢得胜利!";
WORD Val=MessageBox(g_hwnd, str, L"2048", 0);
if (Val == IDYES)
SendMessage(g_hwnd, WM_DESTROY, 0, 0);
}
ReleaseDC(g_hwnd, hdc);
}
DEL_OBJ(mGame);
/*while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}*/
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = h