Win32 API Implementation 2048 game example _c language

Source: Internet
Author: User
Tags comparison rand win32

Self-Study of the Win32 programming, wrote a Win32 API version 2048, the oneself groped to write, press up and down left arrow to start the game

Copy Code code as follows:

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctime>
Lresult CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
Global Operations Array
int arr[4][4] = {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
Global Window Handle
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 ("Cannot register window class"), TEXT ("Registration error"), Mb_iconerror);
return-1;
}
hwnd = CreateWindow (Szappname, TEXT ("My 2048 Games"), 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 (&AMP;MSG);
DispatchMessage (&AMP;MSG);
}
return 0;
}


void Newnum ()
{
int num = 0;//A book between 0 and 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;
}

To draw an array function
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);
Set the font size for numbers
ZeroMemory (&logfont, sizeof (LOGFONT));
Logfont.lfcharset = Gb2312_charset;
Logfont.lfheight =-50; Set the size of the font
Hfont Hfont = CreateFontIndirect (&logfont);
SetTextColor (hdc, RGB (255, 255, 255));
SetBkColor (HDC, RGB (200, 200, 0));
SetBkMode (hdc, transparent);
SelectObject (hdc, Hfont);
Create a painting brush
Hbrush = CreateSolidBrush (RGB (arr[i][j] *% 256, (arr[i][j]*40)%256, 0));
SetRect (&rt, J * M, I *, J * +, I * 100 + 100);
FillRect (hdc, &AMP;RT, hbrush);
TextOut (HDC, J * M, I * +, szbuffer, wsprintf (Szbuffer, TEXT ("%d"), Arr[i][j]);
ReleaseDC (hwnd, HDC);
DeleteObject (Hbrush);
}
Else
{
SetRect (&rt, J * M, I *, J * +, I * 100 + 100);
HDC = GetDC (hwnd);
Hbrush = CreateSolidBrush (RGB (200, 250, 0));
FillRect (hdc, &AMP;RT, hbrush);
ReleaseDC (hwnd, HDC);
DeleteObject (Hbrush);
}
}
}
}
To determine if the array is full
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;
}

Working with array functions
void Changearr (int direct)
{
BOOL Xiaoyici = false;
BOOL Chendi = false;

Switch (direct)
{
On key done
Case 0:
{

Start array operation
for (int i = 0; i < 4; i++)
{

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

1 first.
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;
}
}
}
}
222 comparison

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

}


}//End Array operation
To judge whether or not to die
if (!xiaoyici && isfull ())
{
MessageBox (G_hwnd, Text ("You hang Off"), Text ("Bad"), MB_OK);
SendMessage (g_hwnd,wm_destroy,0,0);
}
else{
Randomly generate a new 2 or 4 into the array
if (Xiaoyici | | chendi) {newnum ();}
}

}
Break
Next key
Case 1:
{
Start array operation
for (int i = 0; i < 4; i++)
{
for (int a = 0; a < 4; a++)
{

1 first.
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;
}
}
}
}
222 comparison

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

}
}
End Array operation
To judge whether or not to die
if (!xiaoyici && isfull ())
{
MessageBox (G_hwnd, Text ("You hang Off"), Text ("Bad"), MB_OK);
SendMessage (G_hwnd, Wm_destroy, 0, 0);
}
else{
Randomly generate a new 2 or 4 into the array
if (Xiaoyici | | chendi) {newnum ();}
}
}
Break
Left-Done
Case 2:
{

Start array operation
for (int i = 0; i < 4; i++)
{

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

1 first.
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;
}
}
}
}
222 comparison

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

}


    }//End array Operation
    //Determine if Death
     if (! Xiaoyici && isfull ())
     {
      MessageBox (G_hwnd, Text ("You hang it Off"), Text ("Bad"), MB_OK);
      SendMessage (G_hwnd, Wm_destroy, 0, 0);
    }
     else{
     //randomly generate a new 2 or 4 put in an array
       if (Xiaoyici | | chendi) {newnum ();}
    }
 }
  break;
  //Right
 case 3:
 {
    //start array operation
      for (int i = 0; i < 4; i++)
     {
      for (int a = 0; a &l T 4; a++)
      {

      //1 First
       for (int b = 3; b >= 0; b--) br>       {
        for (int m = 3; M >= 0;--m) br>        {
         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;
         }
        }
       }
      }
      //222 comparison

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

}
}
End Array operation
To judge whether or not to die
if (!xiaoyici && isfull ())
{
MessageBox (G_hwnd, Text ("You hang Off"), Text ("Bad"), MB_OK);
SendMessage (G_hwnd, Wm_destroy, 0, 0);
}
else{
Randomly generate a new 2 or 4 into the array
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)
{
If the key is pressed
Case VK_UP:
{

Changearr (0);
Drawcanvas (HWND);

}
Break
If the next key is pressed
Case Vk_down:
{
Changearr (1);
Drawcanvas (HWND);
}
Break
If the left button is pressed
Case Vk_left:
{
Changearr (2);
Drawcanvas (HWND);
}
Break
If the right button is pressed
Case Vk_right:
{
Changearr (3);
Drawcanvas (HWND);
}
Break
Default
Break
}
}
Break
Case Wm_destroy:
PostQuitMessage (0);
}
Return DefWindowProc (hwnd, MSG, WParam, LParam);
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.