程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> win32 api完成2048游戲示例

win32 api完成2048游戲示例

編輯:關於C++

win32 api完成2048游戲示例。本站提示廣大學習愛好者:(win32 api完成2048游戲示例)文章只能為提供參考,不一定能成為您想要的結果。以下是win32 api完成2048游戲示例正文


自學的win32編程,寫了一個win32 API版2048,本身探索著寫的,按高低閣下箭頭開端游戲


#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctime>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//全局操作數組
int arr[4][4] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
//全局窗口句柄
HWND g_hwnd;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
 TCHAR *szAppName = TEXT("2048");
 HWND hwnd;
 MSG msg;
 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 = szAppName;
 wndclass.lpszMenuName = NULL;
 wndclass.style = CS_HREDRAW | CS_VREDRAW;
 if (!RegisterClass(&wndclass))
 {
  MessageBox(NULL, TEXT("沒法注冊窗口類"), TEXT("注冊毛病"), MB_ICONERROR);
  return -1;
 }
 hwnd = CreateWindow(szAppName, TEXT("我的2048游戲"), WS_OVERLAPPEDWINDOW^WS_THICKFRAME,
  CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,
  NULL, NULL, hInstance, NULL);
 g_hwnd = hwnd;
 ShowWindow(hwnd, iCmdShow);
 UpdateWindow(hwnd);
 while (GetMessage(&msg, NULL, 0, 0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
 return 0;
}


void newNum()
{
 int num = 0;//一個在0至9之間的書
 int xPos = 0, yPos = 0;
 srand(time(0));
 do{
  xPos = rand() % 4;
  yPos = rand() % 4;
 } while (arr[xPos][yPos] != 0);
 num = rand() % 10;
 if (num < 7)
  arr[xPos][yPos] = 2;
 else
  arr[xPos][yPos] = 4;
}

//畫數組函數
void DrawCanvas(HWND hwnd)
{
 HBRUSH hBrush;
 HDC hdc;
 RECT rt;
 TCHAR szBuffer[10];
 for (int i = 0; i < 4; i++)
 {
  for (int j = 0; j < 4; j++)
  {
   if (arr[i][j] != 0)
   {
    LOGFONT logfont;
    hdc = GetDC(hwnd);
    //設置數字的字體年夜小
    ZeroMemory(&logfont, sizeof(LOGFONT));
    logfont.lfCharSet = GB2312_CHARSET;
    logfont.lfHeight = -50; //設置字體的年夜小
    HFONT hFont = CreateFontIndirect(&logfont);
    SetTextColor(hdc, RGB(255, 255, 255));
    SetBkColor(hdc, RGB(200, 200, 0));
    SetBkMode(hdc, TRANSPARENT);
    SelectObject(hdc, hFont);
    //創立畫刷
    hBrush = CreateSolidBrush(RGB((arr[i][j] * 10) % 256, (arr[i][j]*40)%256, 0));
    SetRect(&rt, j * 100, i * 100, j * 100 + 100, i * 100 + 100);
    FillRect(hdc, &rt, hBrush);
    TextOut(hdc, j * 100 , i * 100 + 25, szBuffer, wsprintf(szBuffer, TEXT("%d"), arr[i][j]));
    ReleaseDC(hwnd, hdc);
    DeleteObject(hBrush);
   }
   else
   {
    SetRect(&rt, j * 100, i * 100, j * 100 + 100, i * 100 + 100);
    hdc = GetDC(hwnd);
    hBrush = CreateSolidBrush(RGB(200, 250, 0));
    FillRect(hdc, &rt, hBrush);
    ReleaseDC(hwnd, hdc);
    DeleteObject(hBrush);
   }
  }
 }
}
//斷定數組能否滿了
bool isFull()
{
 bool full = true;
 for (int i = 0; i < 4; i++)
 {
  for (int j = 0; j < 4; j++)
  {
   if (arr[i][j] == 0)
   {
    full = false;
   }
  }
 }
 return full;
}

//處置數組函數
void changeArr(int direct)
{
 bool xiaoyici = false;
 bool chendi = false;

 switch (direct)
 {
  //上鍵 done
 case 0:
 {

     //開端數組操作
     for (int i = 0; i < 4; i++)
     {

      for (int a = 0; a < 4; a++){

       //1先沉底
       for (int b = 0; b < 4; b++)
       {
        for (int m = 0; m < 4; ++m)
        {
         for (int k = m; k < 3; ++k)
         {

          if (arr[k][i] == 0 && arr[k+1][i]!=0)
          {
           chendi = true;
           arr[k][i] = arr[k + 1][i];
           arr[k + 1][i] = 0;
          }
         }
        }
       }
       //2兩兩比擬

        for (int j = a; j < 4; ++j)
        {

        
         if (arr[j][i] != 0 && arr[j][i] == arr[j + 1][i])
         {
          xiaoyici = true;
          arr[j][i] <<= 1;
          arr[j + 1][i] = 0;
          j++;
          a++;

         }
         else
         {
          continue;
         }
        }

      }

     
     }//停止數組操作
     //斷定能否逝世亡
     if (!xiaoyici && isFull())
     {
      MessageBox(g_hwnd, TEXT("你掛失落了"), TEXT("蹩腳"), MB_OK);
      SendMessage(g_hwnd,WM_DESTROY,0,0);
     }
     else{
      //隨機生成一個新的2或4放入數組
      if (xiaoyici || chendi){ newNum(); }
     }

 }
  break;
  //下鍵
 case 1:
 {
     //開端數組操作
     for (int i = 0; i < 4; i++)
     {
      for (int a = 0; a < 4; a++)
      {

       //1先沉底
       for (int b = 3; b >= 0; b--)
       {
        for (int m = 3; m >= 0; --m)
        {
         for (int k = m; k > 0; --k)
         {

          if (arr[k][i] == 0&&arr[k-1][i]!=0)
          {
           chendi = true;
           arr[k][i] = arr[k - 1][i];
           arr[k - 1][i] = 0;
          }
         }
        }
       }
       //2兩兩比擬

     for (int j = 3 - a; j >= 0; --j)
     {
      if (arr[j][i] != 0 && arr[j][i] == arr[j - 1][i])
      {
       xiaoyici = true;
       arr[j][i] += arr[j][i];
       arr[j - 1][i] = 0;
       a++;
       --j;
      }
      else
      {
       continue;
      }
     }

      }
     }
     //停止數組操作
     //斷定能否逝世亡
     if (!xiaoyici && isFull())
     {
      MessageBox(g_hwnd, TEXT("你掛失落了"), TEXT("蹩腳"), MB_OK);
      SendMessage(g_hwnd, WM_DESTROY, 0, 0);
     }
     else{
      //隨機生成一個新的2或4放入數組
      if (xiaoyici || chendi){ newNum(); }
     }
 }
  break;
  //左鍵done
 case 2:
 {

     //開端數組操作
     for (int i = 0; i < 4; i++)
     {

      for (int a = 0; a < 4; a++){

       //1先沉底
       for (int b = 0; b < 4; b++)
       {
        for (int m = 0; m < 4; ++m)
        {
         for (int k = m; k < 3; ++k)
         {

          if (arr[i][k] == 0&&arr[i][k+1]!=0)
          {
           chendi = true;
           arr[i][k] = arr[i][k+1];
           arr[i][k+1] = 0;
          }
         }
        }
       }
       //2兩兩比擬

       for (int j = a; j < 4; ++j)
       {


        if (arr[i][j] != 0 && arr[i][j] == arr[i][j+1])
        {
         xiaoyici = true;
         arr[i][j] <<= 1;
         arr[i][j+1] = 0;
         j++;
         a++;

        }
        else
        {
         continue;
        }
       }

      }


     }//停止數組操作
     //斷定能否逝世亡
     if (!xiaoyici && isFull())
     {
      MessageBox(g_hwnd, TEXT("你掛失落了"), TEXT("蹩腳"), MB_OK);
      SendMessage(g_hwnd, WM_DESTROY, 0, 0);
     }
     else{
      //隨機生成一個新的2或4放入數組
      if (xiaoyici || chendi){ newNum(); }
     }
 }
  break;
  //右鍵
 case 3:
 {
     //開端數組操作
     for (int i = 0; i < 4; i++)
     {
      for (int a = 0; a < 4; a++)
      {

       //1先沉底
       for (int b = 3; b >= 0; b--)
       {
        for (int m = 3; m >= 0; --m)
        {
         for (int k = m; k > 0; --k)
         {

          if (arr[i][k] == 0&&arr[i][k-1]!=0)
          {
           chendi = true;
           arr[i][k] = arr[i][k-1];
           arr[i][k-1] = 0;
          }
         }
        }
       }
       //2兩兩比擬

       for (int j = 3 - a; j >= 0; --j)
       {
        if (arr[i][j] != 0 && arr[i][j] == arr[i][j-1])
        {
         xiaoyici = true;
         arr[i][j] += arr[i][j];
         arr[i][j-1] = 0;
         a++;
         --j;
        }
        else
        {
         continue;
        }
       }

      }
     }
     //停止數組操作
     //斷定能否逝世亡
     if (!xiaoyici && isFull())
     {
      MessageBox(g_hwnd, TEXT("你掛失落了"), TEXT("蹩腳"), MB_OK);
      SendMessage(g_hwnd, WM_DESTROY, 0, 0);
     }
     else{
      //隨機生成一個新的2或4放入數組
      if (xiaoyici || chendi){ newNum(); }
     }
 }
  break;
 default:
  return;
 }
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
 switch (msg)
 {
 case WM_CREATE:
 {
       newNum();
       //newNum();
 }
  break;
 case WM_KEYDOWN:
 {
        switch (wParam)
        {
         //假如上鍵被按下
        case VK_UP:
        {

          changeArr(0);
          DrawCanvas(hwnd);
         

        }
         break;
         //假如下鍵被按下
        case VK_DOWN:
        {
            changeArr(1);
            DrawCanvas(hwnd);
        }
         break;
         //假如左鍵被按下
        case VK_LEFT:
        {
            changeArr(2);
            DrawCanvas(hwnd);
        }
         break;
         //假如右鍵被按下
        case VK_RIGHT:
        {
          changeArr(3);
          DrawCanvas(hwnd);
        }
         break;
        default:
         break;
        }
 }
  break;
 case WM_DESTROY:
  PostQuitMessage(0);
 }
 return DefWindowProc(hwnd, msg, wParam, lParam);
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved