|
分享源码
界面截图: |
|
是否带模块: |
纯源码 |
备注说明: |
- |
断断续续学了好几月win32C++了,琢磨着写个小游戏,没写完(已经能玩)。
卡壳了,想着根据窗口大小调节字体的大小,可是对createfont的参数,都没弄明白,仍需继续努力!决定继续学习一段时间再回头写这些。
然后,学win32感觉收获还是蛮大的,大家如果易语言玩的差不多了,可以考虑转C++来学win32,能知道好多易语言可能从来不需要关心的问题,比如,有可能很多朋友还不知道消息机制是什么
如果就在论坛做擦边的东西,易语言再合适不过了。
如果是出于爱好啊什么的,完全可以学下C++ win32。
C++和win32是两个不同的东西,不要弄混了。
C++上手的话真的不难,无非就是那几个关键字,不需要精通,这个过程可以在开发中慢慢深入。
需要记的也就是win32那些api,机制,原理了(这些都可以在过程中慢慢中深入,首先了解消息机制,了解一下createwindow函数的参数,就可以弄出个界面来了)
代码贴上来,大家如果知道int,return,for,while,就差不多能看懂,就这么简单……(粘贴到vs就可以跑)
- #include <windows.h>
- #include <vector>
- #include <time.h>
- #include <math.h>
- #include <CommCtrl.h>
- #if defined _M_IX86
- #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144c*Ff' language='*'"")
- #elif defined _M_X64
- #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144c*Ff' language='*'"")
- #else
- #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144c*Ff' language='*'"")
- #endif
- #define ID_MI_NEW 1
- #define ID_MI_L1 2
- #define ID_MI_L2 3
- #define ID_MI_L3 4
- #define ID_MI_LX 5
- #define ID_MI_OPEN 6
- #define ID_MI_SAVE 7
- #define ID_MI_INFO 8
- #define ID_MI_EXIT 9
- #define ID_MI_MODE 10
- #define ID_MI_UNDO 11
- #define ID_MI_ABOUTE 12
- #define MARGIN 10//可显示区域和边缘的距离
- #define MARGINCELL 16//格子之间的距离
- #define ROUND 10//可显示区域圆角
- #define ROUNDCELL 5//格子圆角
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- class My2048 {//未做方法调用容错处理
- public:
- int *mdata;
- int mx, my;//mx表示横向格子数量,my表示纵向格子数量
- bool mmode;//true宽限模式,false严格模式
- int mstate;//0未开始游戏,1游戏中,2游戏结束
- int mscore;//分数
- int mstep;//步数
- HBRUSH hBrush[15];
- void newGame(int x,int y,bool mode) {
- clear();
- mx = x, my = y, mmode = mode, mstate = 1, mscore = 0, mstep = 0;
- mdata = new int[mx*my];
- memset(mdata, 0, sizeof(int)*mx*my);
- makeOne(),makeOne();
- }
- void setValue(int x,int y,int value) {
- mdata[mx*y + x] = value;
- }
- int getValue(int x,int y) {
- return mdata[mx*y + x];
- }
- bool change(bool direction,bool position) {//合并和移动,direction 横向 纵向,position 正向 反向
- int offSet = position ? 1 : -1;
- int start = position ? 0 : (direction ? mx : my) - 1;
- int end = position ? (direction ? mx : my) - 1 : 0;
- bool worked{false};
- if (direction) {//横向
- for (int y = 0; y < my; ++y) {//遍历每一行
- int block = position ? mx : -1;//定义一个阻止位置,防止多次合并
- for (int x = end; x!=start-offSet; x -= offSet) {//按方向(相反)遍历每个列格子
- if (mdata[mx*y + x]) {
- for (int i = x + offSet; i != block; i += offSet) {//如果格子不是0,尝试按方向移动或合并
- if (mdata[mx*y + i]) {//如果检查目标格子不是0
- if (mdata[mx*y + i - offSet] == mdata[mx*y + i]) {//如果检查目标格子和准备移动/合并格子值相等
- mdata[mx*y + i] *= 2;
- mdata[mx*y + i - offSet] = 0;//成功合并
- mscore += mdata[mx*y + i];//累加分数
- block = i;//当前块是合并出来的,不再允许检查
- worked = true;
- }
- break;//如果不是0,不管是否合并,都没有继续检查的必要了,break
- } else {//往空格子移动
- mdata[mx*y + i] = mdata[mx*y + i - offSet];
- mdata[mx*y + i - offSet] = 0;//成功移动
- worked = true;
- }
- }
- }
- }
- }
- } else {
- for (int x = 0; x < mx; ++x) {//遍历每一列
- int block = position ? my : -1;//定义一个阻止位置,防止多次合并
- for (int y = end; y!=start-offSet; y -= offSet) {//按方向(相反)遍历每个行格子
- if(mdata[mx*y + x])
- for (int i = y + offSet; i!=block; i += offSet) {//如果格子不是0,尝试按方向移动或合并
- if (mdata[mx*i + x]) {//如果检查目标格子不是0
- if (mdata[mx*(i - offSet) + x] == mdata[mx*i + x]) {//如果检查目标格子和准备移动/合并格子值相等
- mdata[mx*i + x] *= 2;
- mdata[mx*(i - offSet) + x] = 0;//成功合并
- mscore += mdata[mx*i + x];//累加分数
- block = i;//当前块是合并出来的,不再允许检查
- worked = true;
- }
- break;//如果不是0,不管是否合并,都没有继续检查的必要了,break
- } else {
- mdata[mx*i + x] = mdata[mx*(i - offSet) + x];
- mdata[mx*(i - offSet) + x] = 0;//成功移动
- worked = true;
- }
- }
- }
- }
- }
- if (mmode || worked) {
- ++mstep;
- makeOne();
- return true;
- } else return false;
- }
- bool isNotOver() {//判断游戏是否未结束
- for (int y = 0; y < my; ++y) {
- for (int x = 0; x < mx; ++x) {
- if (mdata[mx*y + x] == 0) {//存在空白格子
- return true;
- } else {
- if (x < mx - 1 && mdata[mx*y + x + 1] == mdata[mx*y + x]) {//存在横向相同相邻格子
- return true;
- }
- if (y < my - 1 && mdata[mx*(y + 1) + x] == mdata[mx*y + x]) {//存在纵向相同相邻格子
- return true;
- }
- }
- }
- }
- return false;
- }
- HBRUSH getBrush(int value) {
- //int value2 = (log(value)) / log(2);
- //return hBrush[value2];
- ///*
- switch (value) {
- case 0:return hBrush[0];
- case 2:return hBrush[1];
- case 4:return hBrush[2];
- case 8:return hBrush[3];
- case 16:return hBrush[4];
- case 32:return hBrush[5];
- case 64:return hBrush[6];
- case 128:return hBrush[7];
- case 256:return hBrush[8];
- case 512:return hBrush[9];
- case 1024:return hBrush[10];
- case 2048:return hBrush[11];
- case 4096:return hBrush[12];
- case 8192:return hBrush[13];
- default:return hBrush[14];
- }
- //*/
- }
- My2048() {
- int color[]{0xB4C1CD,0xDAE4EE ,0xC8E0ED ,0x79B1F2,
- 0x6395F5,0x5F7*F0x3B5EF6,0x72*F,
- 0x61CCED,0x50C8ED,0x3FC5ED,0x2EC2ED,0xFF0000,0xFF0000,0xA0ADBB
- };
- for (int i = 0; i < sizeof(hBrush) / sizeof(HBRUSH); ++i) {
- hBrush[i] = CreateSolidBrush(color[i]);
- }
- mx = 4, my = 4, mstate = 0;//用于打印未开始游戏界面
- mdata = new int[mx*my];
- memset(mdata, 0, sizeof(int)*mx*my);
- mdata[mx*1 + 1] = 2;
- mdata[mx*2 + 1] = 4;
- mdata[mx*2 + 2] = 8;
- }
- ~My2048() {
- clear();
- for (int i = 0; i < sizeof(hBrush) / sizeof(HBRUSH); ++i) {
- DeleteObject(hBrush[i]);
- }
- }
- private:
- bool makeOne() {
- std::vector<int> cells;
- for (int i = 0; i < mx*my; ++i) {
- if (!mdata[i]) cells.push_back(i);
- }
- if (cells.size()) {
- srand((unsigned int)time(NULL));
- int &cell = mdata[cells[rand() % (cells.size())]];
- srand((unsigned int)time(NULL));
- cell = rand() % (3 + 1) == 0 ? 4 : 2;
- return true;
- } else return false;
- }
- void clear() {
- delete[] mdata;
- }
- };
- My2048 my2048;
- int lastLevel;//0常规 1扩展 2高级 3自定义
- bool mode;//true宽限模式,false严格模式
- int rwocolumn;
- int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) {
- WNDCLASS wndClass;
- wndClass.cbClsExtra = 0;
- wndClass.cbWndExtra = 0;
- wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
- wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wndClass.hInstance = hInstance;
- wndClass.lpfnWndProc = &WndProc;
- wndClass.lpszClassName = TEXT("sfwindow");
- wndClass.lpszMenuName = NULL;
- wndClass.style = CS_VREDRAW | CS_HREDRAW;
- if (RegisterClass(&wndClass)) {
- HMENU hMenu = CreateMenu();
- HMENU hMenuPopup;
- hMenuPopup = CreatePopupMenu();
- AppendMenu(hMenuPopup, MF_STRING, ID_MI_NEW, TEXT("新游戏(&N)\tF2"));
- HMENU hMenuChild = CreatePopupMenu();
- AppendMenu(hMenuChild, MF_STRING, ID_MI_L1, TEXT("常规(4*4)"));
- AppendMenu(hMenuChild, MF_STRING, ID_MI_L2, TEXT("扩展(5*5)"));
- AppendMenu(hMenuChild, MF_STRING | MF_MENUBREAK, ID_MI_L3, TEXT("高级(6*6)"));
- AppendMenu(hMenuChild, MF_STRING, ID_MI_LX, TEXT("自定义(?*?)"));
- AppendMenu(hMenuPopup, MF_POPUP, (UINT_PTR)hMenuChild, TEXT("级别(&L)"));
- AppendMenu(hMenuPopup, MF_SEPARATOR, NULL, NULL);
- AppendMenu(hMenuPopup, MF_STRING, ID_MI_OPEN, TEXT("打开(&O)\tCtrl+O"));
- AppendMenu(hMenuPopup, MF_STRING, ID_MI_SAVE, TEXT("保存(&S)\tCtrl+S"));
- AppendMenu(hMenuPopup, MF_STRING, ID_MI_INFO, TEXT("统计信息(&I)\tF4"));
- AppendMenu(hMenuPopup, MF_SEPARATOR, NULL, NULL);
- AppendMenu(hMenuPopup, MF_STRING, ID_MI_EXIT, TEXT("退出(&X)"));
- AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hMenuPopup, TEXT("游戏(&G)"));
- hMenuPopup = CreatePopupMenu();
- AppendMenu(hMenuPopup, MF_STRING, ID_MI_MODE, TEXT("模式选择(&O)"));
- AppendMenu(hMenuPopup, MF_STRING, ID_MI_UNDO, TEXT("撤销(&U)\tCtrl+Z"));
- AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hMenuPopup, TEXT("选项(&O)"));
- hMenuPopup = CreatePopupMenu();
- AppendMenu(hMenuPopup, MF_STRING, ID_MI_ABOUTE, TEXT("关于(&A)"));
- AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hMenuPopup, TEXT("帮助(&H)"));
- HWND hwndMain = CreateWindow(TEXT("sfwindow"), TEXT("2048++"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 600, 400, NULL, hMenu, hInstance, NULL);
- if (hwndMain) {
- ShowWindow(hwndMain, nCmdShow);
- UpdateWindow(hwndMain);
- MSG msg;
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return msg.wParam;
- }
- }
- return -1;
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
- switch (message) {
- case WM_CREATE: {
- //CreateWindow(STATUSCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0, hWnd, NULL, ((LPCREATESTRUCT) lParam)->hInstance, NULL);
- TCHAR szPath[MAX_PATH];
- GetModuleFileName(NULL, szPath, sizeof(szPath));
- wsprintf(szPath, TEXT("%s.ini"), szPath);
- TCHAR szValue[16];
- GetPrivateProfileString(TEXT("常规"), TEXT("级别"), TEXT("0"), szValue, sizeof(szValue), szPath);
- lastLevel = _wtoi(szValue);
- GetPrivateProfileString(TEXT("常规"), TEXT("宽限模式"), TEXT("false"), szValue, sizeof(szValue), szPath);
- mode = szValue == TEXT("true");
- GetPrivateProfileString(TEXT("常规"), TEXT("矩阵大小"), TEXT("458759"), szValue, sizeof(szValue), szPath);
- rwocolumn = _wtoi(szValue);
- return 0;
- break;
- }
- case WM_PAINT: {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hWnd, &ps);
- RECT rc;
- GetClientRect(hWnd, &rc);
- int width = rc.right - MARGIN * 2;//可显示区域宽度
- int height = rc.bottom - MARGIN * 2;//可显示区域高度
- if (width >= 0 && height >= 0) {
- SelectObject(hdc, GetStockObject(NULL_PEN));
- SelectObject(hdc, my2048.getBrush(-1));
- RoundRect(hdc, MARGIN, MARGIN, rc.right - MARGIN, rc.bottom - MARGIN, ROUND, ROUND);
- int widthCell = (width - MARGINCELL * (my2048.mx + 1)) / my2048.mx;
- int heightCell = (height - MARGINCELL * (my2048.my + 1)) / my2048.my;
- if (widthCell >= 0 && heightCell >= 0) {
- SetBkMode(hdc, TRANSPARENT);
- HFONT hFont=CreateFont(-75, -50, 0, 0, FW_BOLD, false, false, false, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, TEXT("黑体"));
- SelectObject(hdc, hFont);
- for (int y = 0; y < my2048.my; ++y) {
- for (int x = 0; x < my2048.mx; ++x) {
- RECT rcCell{ MARGIN + widthCell*x + MARGINCELL * (x + 1) ,MARGIN + heightCell*y + MARGINCELL * (y + 1),MARGIN + widthCell*(x + 1) + MARGINCELL * (x + 1),MARGIN + heightCell*(y + 1) + MARGINCELL * (y + 1) };
- SelectObject(hdc, my2048.getBrush(my2048.getValue(x, y)));
- RoundRect(hdc, rcCell.left, rcCell.top, rcCell.right, rcCell.bottom, ROUNDCELL, ROUNDCELL);
- if (my2048.getValue(x, y)||(my2048.mstate==0&&y==1&&x==2)) {
- TCHAR szBuff[5];
- wsprintf(szBuff, TEXT("%d"), my2048.getValue(x, y));
- DrawText(hdc, szBuff, -1, &rcCell, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- }
- }
- }
- DeleteObject(SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT)));
- }
-
- }
- EndPaint(hWnd, &ps);
- break;
- }
- case WM_COMMAND:
- switch (LOWORD(wParam)) {
- case ID_MI_NEW:
- if (my2048.mstate != 1 || MessageBox(hWnd, TEXT("正在游戏中,需要重新开始吗?"), TEXT("提示"), MB_OKCANCEL | MB_ICONQUESTION) == IDOK) {
- my2048.newGame(4, 4, mode);
- //code
- }
- break;
- case ID_MI_L1:
- if (my2048.mstate != 1 || MessageBox(hWnd, TEXT("正在游戏中,需要重新开始吗?"), TEXT("提示"), MB_OKCANCEL | MB_ICONQUESTION) == IDOK) {
- lastLevel = 0;
- my2048.newGame(4, 4, mode);
- }
- break;
- case ID_MI_L2:
- if (my2048.mstate != 1 || MessageBox(hWnd, TEXT("正在游戏中,需要重新开始吗?"), TEXT("提示"), MB_OKCANCEL | MB_ICONQUESTION) == IDOK) {
- lastLevel = 1;
- my2048.newGame(5, 5, mode);
- }
- break;
- case ID_MI_L3:
- if (my2048.mstate != 1 || MessageBox(hWnd, TEXT("正在游戏中,需要重新开始吗?"), TEXT("提示"), MB_OKCANCEL | MB_ICONQUESTION) == IDOK) {
- lastLevel = 2;
- my2048.newGame(6, 6, mode);
- }
- break;
- case ID_MI_LX:
- //lastLevel = 3;
- break;
- }
- case WM_KEYDOWN:
- switch (wParam) {
- case VK_RIGHT:case 'D':
- if (my2048.mstate == 1) {
- my2048.change(true, true);
- }
- break;
- case VK_LEFT:case 'A':
- if (my2048.mstate == 1) {
- my2048.change(true, false);
- }
- break;
- case VK_DOWN:case 'S':
- if (my2048.mstate == 1) {
- my2048.change(false, true);
- }
- break;
- case VK_UP:case 'W':
- if (my2048.mstate == 1) {
- my2048.change(false, false);
- }
- break;
- }
- InvalidateRect(hWnd, NULL, true);
- return 0;
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- break;
- }
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
复制代码 然后发下编译好的exe:
2048 .rar
(17.96 KB, 下载次数: 18)
|
|