A complete Win32 program implemented with VC in the first real sense. //Block.h//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- ---- / * Define the structure of each block * /
#if! defined _block_h_ # define _block_h_ # define block_version & h01000000 // Main version number, secondary version, attached version, additional 2 # define block_size 6 // Take a number of bytes occupied when files #define block_header_size 8 // Storage file header Information size struct _block {// Note: These fields have a prior part of the storage file. Unsigned int name: 8; int Width: 4; int tent: 4; int offsety: 4; int offsetx: 4; unsigned int elements; // is entity};
Typedef _Block Block; #ENDIF / / -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- ----- // Customgdi.h // --------------------------------------- -------------------------------------------------- --------- # prgma overce # include "windows.h" // # include "d3d9.h"
#define border_style_none 0 # define border_style_flat 1 / / For simplicity, only FLAT / / # define border_style_fixed3d 2 // # Define border_style_inner3d 4 # define border_style_default border_style_none
#define inner_style_empty 0 # define inner_style_solid 1 # define Inner_Style_Default Inner_Style_empty
#define transparent_color RGB (0,0,0) #define background_default_color RGB (0,0,0)
TYPEDEF INT INNER_STYLE; TYPEDEF INT BORDER_STYLE; TYPEDEF DWORD GDI_COLOR;
class CCustomGDI {public: // CCustomGDI (void); CCustomGDI (HWND hWnd); ~ CCustomGDI (void); void TextOut (int x, int y, const char * lpString, int cbString); void Drawbox (PRECT prect, BORDER_STYLE bstyle = BORDER_STYLE_DEFAULT, INNER_STYLE istyle = INNER_STYLE_DEFAULT); void Drawbox (int x, int y, int height, int width, BORDER_STYLE bstyle = BORDER_STYLE_DEFAULT, INNER_STYLE istyle = INNER_STYLE_DEFAULT); void DrawPic (int x, int y, int height, int width, const char * file); Void DrawPic (prect prect, const char * file); void clear (prect prect); void clear (gdi_color color); void clear (prect prect, gdi_color color); Void Clear (int x, int y, int height, int width, GDI_COLOR color); void SetBkColor (GDI_COLOR color); // set the background color void SetColor (GDI_COLOR color); // Sets the foreground color void Set3DColor (GDI_COLOR ul_color, GDI_COLOR rb_color) ; // set the color when rendering the 3D left corner and lower right corner of void SetBorderColor (GDI_COLOR color); void SetBorderSize (int size); protected: // LPDIRECT3D9 m_pD3D; // LPDIRECT3DDEVICE9 m_pd3dDevice; // HDC m_hdc; HWND m_ HWnd; // Hbrush M_HBRBKGND; GDI_COLOR M_BACKGROUND; int m_bordersize; gdi_color m_bordercolor; gdi_color m_color;}; // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- -------------------- // Customgdi.cpp // ----------------------- -------------------------------------------------- ------------------------ # include "./customgdi.h"// //ccustomgdi ::ccustomgdi(Void) / (//} // CCustomGDI :: CCustomGDI (HWND hWnd) {// initialize the graphics device m_hwnd = hWnd; // m_hdc = GetDC (hWnd); m_background = BACKGROUND_DEFAULT_COLOR; m_bordersize = 1; m_bordercolor = RGB (0x80,0x80,0x80); // Gray M_Color = RGB (0xFF, 0xFF, 0xFF); // White}
CCUSTOMGDI :: ~ ccustomgdi (void) {// releasedc (m_hwnd, m_hdc);}
Void ccustomgdi :: TextOut (int X, int CBString) {HDC HDC = getdc (m_hwnd); :: TextOut (HDC, X, Y, LPSTRING, CBSTRING); ReleaseDC (M_HWND, HDC) } void ccustomgdi :: DRAWBOX (prect prect, border_style bstyle, inner_style istyle) {Hbrush Hbr; HDC HDC;
HDC = GETDC (M_HWND);
Rect Rect; Rect.top = prect-> top; Rect.Bottom = prect-> bottom; rest = prect-> left; rest; rest; rest; rest; rest; rest;
IF (bstyle == border_style_flat) {// If border_style_flat hbr = createsolidbrush; frameERECT (HDC, & Rect, HBR); // I don't know how to draw lines, huh, huh.
DeleteObject (HBR);
Rect.Left = m_bordersize; Rect.right- = m_bordersize; Rect.top = m_bordersize; Rect.Bottom- = m_bordersize;
}
IF (iStyle == Inner_Style_Solid) {Hbr = Createsolidbrush (m_color); FillRect (HDC, & Rect, Hbr); deleteObject (HBR);
ReleaseDC (M_HWND, HDC);
Void CCUSTOMGDI :: DrawBox (int X, int y, int tent, int width, border_style bstyle, inner_style istyle) {RECT Rect; Rect.top = Y; Rect.Left = x; Rect.Bottom = Y Height-1; Rect.right = x width-1; DrawBox (& Rect, bstyle, iStyle);
Void ccustomgdi :: DrawPic (int X, int y, int hotht, int width, const char * file) {}
Void ccustomgdi :: drawpic (prect prect, const char * file) {}
Void ccustomgdi :: clear (void) {// Clear screen Rect Rect; getClientRect (M_HWND, & Rect); Clear (& Rect, M_Background);}
Void ccustomgdi :: CLEAR (prect prect) {clear (prect, m_background);}
Void ccustomgdi :: CLEAR (GDI_COLOR color) {Rect Rect; getClientRect (M_HWND, & Rect); Clear (& Rect, Color);}
Void ccustomgdi :: CLEAR (prect prect, gdi_color color) {HDC HDC; HDC = getDC (m_hwnd);
Hbrush Hbr = Createsolidbrush (Color); FillRect (HDC, prect, HBR); deleteObject (HBR); ReleaseDC (M_HWND, HDC);}
Void ccustomgdi :: CLEAR (int X, int y, int hotht, int width, gdi_color color) {Rect Rect; Rect.Left = x; Rect.top = Y; Rect.Right = x height-1; Rect.Bottom = Y width-1; Clear (& Rect, Color);}
Void ccustomgdi :: setbkcolor (gdi_color color) {m_background = color;}
Void ccustomgdi :: setColor (gdi_color color) {m_color = color;}
Void CCUSTOMGDI :: Set3DColor (GDI_COLOR UL_COLOR, GDI_COLOR RB_COLOR) {}
Void ccustomgdi :: setBordersize (int size) {}
Void CCustomGDI :: SetBorderColor (GDI_COLOR color) {} // ------------------------------------- -------------------------------------------------- ----------- // Els.h // -------------------------------- -------------------------------------------------- --------------- # include
#define playarea_height 24 # define playarea_width 12 # define playarea_empty_color rgb (0,0,0)
#define IDT_Play 2005
#define game_status_stopped 0 # define game_status_running 1 # define game_status_paused -1
#define PLAYER_DEFAULT_NAME "
Struct_lssys {int level; // Current game level int clientHeight; // Current customer area high int clientWidth; // Current customer area width int playareax; // Game area X coordinate (Note: Do not include borders outside the game area, etc.) Int Playareay; // Game area Y coordinate (Note: Do not include borders outside the game area, etc.) int singleheight; // single grid INT SINGLEWIDTH; // single grid width int bordersize; // Border size INT spacing; / The interval between the microphone int nextx; / / The next block display area X coordinate int nexty; / / The next block display area Y coordinate BOOL ISNEXTSHOWN; / / The next block is displayed}; typedef _player player; type Typedef _Lssys evironment; // -------------------------------------------------------------------------------------------------------- -------------------------------------------------- ----- // Els.cpp // -------------------------------------- -------------------------------------------------- ----------- # include "els.h" // Direct3D Object // LPDIRECT3D9 g_pd3d = NULL;
// Direct3D device // lpdirect3ddevice9 g_pd3ddevice = null;
// HWNDHWND G_HWND;
INT G_STATUS = GAME_STATUS_STOPPED; // Store status // current time interval // int g_interval = 1000;
// The game area, stores the color value of each grid, if 0, indicating that the current grid is not occupied, otherwise it is occupied. GDI_COLOR G_PLAYAREA [Playarea_Height] [Playarea_Width];
// Current line, column INT g_currow = 0; int g_curcol = 0;
// The index of the current square int g_curblock;
// Current square color gdi_color g_curblockcolor;
/ / The index of the next block INT g_nextblock;
GDI_COLOR G_NEXTBLOCKCOLOR;
/ / Store all blocks block * block = null;
// The number of square INT g_blockcount = 0;
// Current level INT g_level = 0; int g_nextlevelpower = 0; int g_nextlevelscore = 1000;
// Environmental Information Evironment G_ENV;
// User's information Player g_player = {"player", 0,0};
// Declare your GDI object CCUSTOMGDI * G_GDI;
// All function definition extern void caculateenv (void); // calculate interface environment information Extern void render (void); // Draw Interface
Extern HRESULT LoadBlocks (Void); / / Loading Square Extern Void ClearBlocks (Void); // Clear all squares of memory
Extern void _bottom (void); / / Square After the processing process Extern Void Bottom (Void); / / Square handover process extern void down (void); // Square drop extern void mapcurblocktoplayarea (void); / / Push the current square to the game area /// void showdebugmsg (const char * smsg, int x = 100, int y = 100) // {// HDC HDC; // HDC = getDC (g_hwnd); // Textout (HDC, X, Y, SMSG, (INT) Strlen (SMSG)); //} //// Clear Release All Squares The memory void Clearblocks () {if (blocks! = null) {free (blocks); }
// Turn all blocks from the file to all block hResult loadingblocks () {INT i; file * fp; char spath [_max_path]; char header [block_header_size]; // char smsg [255];
IF (blocks! = null) {cleanblocks ();
_Getcwd (spath, _max_path); // This is not judged
STRCAT (Spath, "//turn.right.blk"); fp = fopen (spath, "rb");
IF (fp == null) {// There is an error. Return E_FAIL;
Fread (Header, 1, Block_header_size, fp); // This file is no longer determined whether the file is correct, and it is not judged. // 0-2 bytes are: BLK // 3-6 bytes are: Version information // 7 bytes are squares. G_blockcount = (unsigned int) header [block_header_size-1];
/ / Then apply for a block array of g_blockcount sizes blocks = (block *) malloc (sizeof (block) * g_blockcount;
For (i = 0; i Fclose (fp); Return S_OK; } // _ Drawbox: // Draws the cell line of the first row and the column C column in the Russian square area. Void _DrawBox (int R, int C, gdi_color color) {int x, y; x = g_env.playareax g_env.bordersize (c 1) * g_env.spacing c * g_env.singleWidth; y = g_env.playareay G_env.bordersize (R 1) * g_env.spacing r * g_env.singleheight; g_GDI-> SetColor (color); // g_GDI-> Set3DColor (WHITE, BLACK); if (color == PLAYAREA_EMPTY_COLOR) {g_GDI-> Drawbox (x, y, g_Env.SingleHeight, g_Env.SingleWidth, BORDER_STYLE_NONE, INNER_STYLE_SOLID); } else {g_GDI-> Drawbox (x, y, g_Env.SingleHeight, g_Env.SingleWidth, BORDER_STYLE_FLAT, INNER_STYLE_SOLID);}} void HideCurBlock () {// clear the on-screen display of the current block int row, col; for (row = 0; ROW <= blocks [g_curblock] .height; row ) {if (g_currow row <0) Continue; for (col = 0; col <= blocks [g_curblock] .width; col ) {IF (((blocks [ g_curblock] .Elements & (1 << (row * (blocks [g_curblock] .width 1) col)))))))))! = 0) {_DRAWBOX (G_Currow Row, G_Curcol Col, PlayaRe_empty_Color);}}}} Void showcurblock () {// Display current block int Row, col; for (row = 0; ROW <= blocks [g_curblock] .height; row ) {if (g_currow row <0) Continue; For (col = 0; col <= blocks [g_curblock] .width; col ) {IF ((BLOCKS [g_curblock] .elements) & (1 << (row * (blocks [g_curblock] .width 1) coll ))))))! = 0) {_DRAWBOX (g_currow row, g_curcol col, g_curblockcolor); // Note whether the g_curblockcolor value set}}} // char s [200]; //sprintf (S, "block:%D ,%2X;size :%D.", g_curblock, blocks [g_curblock] .Elements, sizeof (block)); // g_gdi-> DrawBox (0, g_env.clientHeight-35, 30, g_env.clientwidth * 3 / 10-10, border_style_flat, inner_style_solid); //g_gdi ityTextOut (10, G_ENV.ClientHeight-30, S, (int )Strlen) )); Void showNextBlock () {// Display the next block int Row, Col; int BLKROW, BLKCOL; in the display box of the next block. BLKROW = (3-blocks [g_nextblock] .height) / 2; BLKCOL = (3-blocks [g_nextblock] .width) / 2; g_gdi-> setColor; g_gdi-> drawbox (g_env.nextx, g_env.nexty, ((g_env.singleHeight g_env.spacing << 2) g_env.spacing (g_env.bordersize << 1), ((((( G_env.singleWidth g_env.spacing) << 2) g_env.spacing (g_env.bordersize << 1), border_style_flat, inner_style_solid); for (row = 0; ROW <4; ROW ) {for (col (col = 0; <4; col ) {if (row> = blkRow && row <= blkRow Blocks [g_NextBlock] .Height && col> = blkCol && col <= blkCol Blocks [g_NextBlock] .Width) {if ((Blocks [g_NextBlock] .Elements & ((Row-BLKROW) * (Blocks [g_nextblock] .width 1) (col-blkcol)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))! = 0) {g_gdi-> setColor (g_nextblockcolor); g_gdi-> drawbox (g_Env.NextX g_Env.BorderSize g_Env.Spacing (g_Env.SingleWidth g_Env.Spacing) * col, g_Env.NextY g_Env.BorderSize g_Env.Spacing (g_Env.SingleHeight g_Env.Spacing) * row, g_Env.SingleHeight , g_Env.SingleWidth, BORDER_STYLE_FLAT, INNER_STYLE_SOLID); continue;}} g_GDI-> SetColor (PLAYAREA_EMPTY_COLOR); g_GDI-> Drawbox (g_Env.NextX g_Env.BorderSize g_Env.Spacing (g_Env.SingleWidth g_Env.Spaci ng) * col, g_Env.NextY g_Env.BorderSize g_Env.Spacing (g_Env.SingleHeight g_Env.Spacing) * row, g_Env.SingleHeight, g_Env.SingleWidth, BORDER_STYLE_FLAT, INNER_STYLE_SOLID);}}} Void showgameInfo () {char s [200]; // Display game data information if (g_status == game_status_stopped {structure (s, "game status: stopped.");} IF (g_status == game_status_running) {structure (s, "game status: running.");} IF (g_status == game_status_paused) {structure (s, "gamestatus: paused.");} g_gdi-> textout (g_env.clientwidth * 3/4, g_env.clientheight-30, "", 30); g_gdi-> textout (g_env.clientwidth * 3/4, g_env.clientHeight-30, S, (int) Strlen (s)); // User score, current level sprintf (s, "score:% d", g_player.score); g_gdi-> textout (g_env.clientwidth * 3/4, g_env.clientheight / 2-50, "" ", 30); g_gdi-> textout (g_env.clientwidth * 3/4, g_env.clientheight / 2-50, s, (int) Strintf (s, "limited:% d", g_level); g_gdi-> textout (g_env.clientwidth * 3/4, g_env.clientheight / 2-30, ", 30); g_gdi-> textout (g_env.clientwidth * 3/4, g_env.clientheight / 2-30, s, (int) Strlen (s)); } Bool canmovedown () {int Row, col; IF (blocks == null) Return False; if (g_blockcount <= 0) Return False; if (g_curblock <0 | G_CurBlock> = g_blockcount) return false; // If you have reached the bottom, you can't move down. . IF ((g_currow blocks [g_curblock] .height 1)> = playarea_height) Return False; For (COL = 0; col <= blocks [g_curblock] .width; col ) {for (row = blocks [g_curblock] .height; row> = 0; row-- {// Find the underlying non-empty IF ( ((Blocks [g_curblock] .Elements) & (1 << (block * (block 1) col)))! = 0) {// If the next line of the game area is not available, It is no longer moving down. IF (g_playarea [g_currow row 1] [g_curcol col]! = Playarea_empty_color) {Return False;}}}} Return True;} Bool canmoveleft () {int Row, col; IF (blocks == null) Return False; if (g_blockcount <= 0) Return False; if (g_curblock <0 || g_curblock> = g_blockcount) Return False; // If you have reached the leftmost column, you cannot move left. IF (g_curcol <= 0) Return False; For (row = 0; row <= blocks [g_curblock] .height; row ) {for (col = 0; col <= blocks [g_curblock] .width; col ) {// Find the left non-empty IF ((((( Blocks [g_curblock] .Elements & (1 << (rotks [g_curblock] .width 1) co))))))))))! = 0) {// If the left position of the game area is not empty, you can no longer Left moved left. IF (g_playarea [g_currow row] [g_curcol col-1]! = Playarea_empty_color) {Return False;}}}}}}}}}}}}}}}}} Bool canmoveright () {if (blocks == null) Return false; if (g_blockcount <= 0) Return False; if (g_curblock <0 || g_curblock> = g_blockcount) return false; // If you have reached the rightmost column, you can't Right moved right. if (g_CurCol Blocks [g_CurBlock] .Width 1> = PLAYAREA_WIDTH) return FALSE; int row, col; for (row = 0; row <= Blocks [g_CurBlock] .Height; row ) {for (col = Blocks [g_CurBlock ] .Width; col> = 0; col -) {// Find the rightmost non-empty IF ((blocks [g_curblock] .Elements) & (1 << (row * (blocks [g_curblock] .width 1 ) co))))! = 0) {// If the right position of the game area is not empty, it cannot be moved again. IF (g_playarea [g_currow row] [g_curcol col 1]! = Playarea_empty_color) {Return False;}}}} Return True;} Bool Canturn () {Int Row, Col; Int, Acol; Block * Next if (Blocks == NULL) return FALSE; if (g_BlockCount <= 0) return FALSE; if (g_CurBlock <0 || g_CurBlock> = g_BlockCount) return FALSE; if (Blocks [g_CurBlock] .NextID <0 || Blocks [g_CurBlock ] .Nextid> = (unsigned int) g_blockcount) Return False; / / Indicates that the original data file has an error and needs to be corrected Next = & blocks [blocks [g_curblock] .nextid]; // The offsety, offsetx here should be provided by the current block. Arow = g_currow blocks [g_curblock] .offsety; acol = g_curcol blocks [g_curblock] .offsetx; // If it exceeds the height, it should not be allowed to change, or always press the transform key, may always fall uneven. . IF (Arow Next-> Height 1> Playara_Height) Return False; // If the column exceeds the game area, you can handle it. IF (acol <0) acol = 0; if (acol next-> width 1> = playarea_width) {acol = playarea_width-next-> width-1;} // When implementing Canturn, suddenly, the current square Empty, don't record for (row = 0; row <= next-> height; row ) {for (COL = 0; col <= next-> width; col ) {IF (((NEXT-> Elements) ) & (1 << (Row * Next-> Width COL 1))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))! = 0) {// If the next transform is not empty, if (g_playarea [arow row] [ACOL COL] ! = Playarea_empty_color) {// If the game area is also non-empty returnaf false;}}}} Return True;} Void _turn () {g_currow = g_currow blocks [g_curblock] .offsety; g_curcol = g_curcol blocks [g_curblock] .offsetx; g_curblock = blocks [g_curblock] .nextid; / / The transformation should be the same as the algorithm in Canturn. IF (g_curcol <0) g_curcol = 0; if (g_curcol blocks [g_curblock] .width 1> = playarea_width) {g_curcol = playarea_width-blocks [g_curblock] .width-1;} // transformation} Void Turn () {if (g_status! = game_status_running) Return; if (canturn () == true) {hidecurblock (); _turn (); showcurblock ();}} Void _Moveleft () {g_curcol-; Void moveleft () {if (g_status! = game_status_running) Return; if (canmoveleft () == True) {hidecurblock (); _Moveleft (); showcurblock ();}} Void _Moveright () {g_curcol ;} void MoveRight () {if (g_Status = GAME_STATUS_RUNNING!) return; if (CanMoveRight () == TRUE) {HideCurBlock (); _MoveRight (); ShowCurBlock ();}} void _Down () {g_CurRow ;} Void mapcurblocktoplayarea () {int Row, col; row = 0; col = 0; for (row = 0; row <= blocks [g_curblock] .height; row ) {for (col = 0; col <= blocks [g_curblock] .Width; col ) {IF ((Blocks [g_curblock] .elements & (1 << (row * (block 1) col)))! = 0) {g_playarea [g_currow row] [ g_curcol col] = g_curblockcolor;}}}} Void Gain () {int Row, Col; Int Gain = 0; BOOL FULL; // Whether the current line fills FOR (ROW = 0; ROW <= blocks [g_curblock] .height; row ) {full = true; (COL = 0; col For (tmpcol = 0; tmpcol Gain ;}}} / / Change the user's related case if (GAIN> 0) {g_player.score = (100 * (GAIN * GAIN 1)); // 1: 100; 2: 300; 3: 700; 4: 1300; IF (g_player.score> player_max_score) {g_player.power ; g_player.score% = (Player_Max_score 1);} ShowgameInfo ();}} void UpdateSys () {// change RELATED case if (g_Player.Power == g_NextLevelPower) {if (g_Player.Score> = g_NextLevelScore) {// change level g_Level ; g_Level% = PLAYER_MAX_LEVEL; g_NextLevelScore = (100 * (g_Level * g_Level) 1000); if (g_NextLevelScore> PLAYER_MAX_SCORE) {g_NextLevelPower ; g_NextLevelScore% = (PLAYER_MAX_SCORE 1);} // changed system speed KillTimer (g_hwnd, IDT_PLAY); SetTimer (g_hwnd, IDT_PLAY, 1000 / (g_Level 1) NULL); }}} BOOL NextBlock () {g_CurBlock = g_NextBlock; g_CurBlockColor = g_NextBlockColor; g_CurRow = 0; g_CurCol = (PLAYAREA_WIDTH-Blocks [g_CurBlock] .Width) / 2; srand ((unsigned) time (NULL)); g_NextBlock = rand ()% g_BlockCount G_nextblockcolor = RGB ((RAND ()% 4) * 64 63, (RAND ()% 4) * 64 63, (Rand ()% 4) * 64 63); showNextBlock (); showcurblock (); Int row, col; for (row <= block [g_curblock] .height; row ) {for (col = 0; col <= blocks [g_curblock] .width; col ) {ix ((blocks [g_curblock] .Elements & (1 << (row * (Blocks [g_CurBlock] .Width 1) col)))! = 0) {if (g_PlayArea [g_CurRow row] [g_CurCol col]! = PLAYAREA_EMPTY_COLOR) // if The location is no longer empty Game over. {Return False;}}}} Return True;} Void Stop () {if (g_status! = game_status_running) return; killtimer (g_hwnd, idt_play); g_status = game_status_stopped; showgameinfo (); Void Down () {if (g_status! = game_status_running) Return; //g_gdi, butterheight, 60, 20, border_style_flat, inner_style_solid); // g_gdi-> textout (10, g_env.clientHeight-30, "Downning!", 9); if (canmovedown () == True) {// beginpaint (g_hwnd, null); hidecurblock (); _down (); showcurblock (); // validateect (g_hwnd, null); // updateWindow (g_hwnd);} else {_bottom ();} else {_bottom (); } void _bottom () { // Map the current square to MapCurBlockToplayarea (); // check if there is a possible break, change the user score GAIN (); // Check if the game level needs to improve UpdateSys (); // Set the current square to the next Square, and re-generating a next block if (NextBlock () == false {// If the next block is dead, you should be Game over. STOP (); // Stop the game first. / / Then check the highest score and record the leaderboard. } Showgameinfo (); } Void bottom () {if (g_status! = game_status_running) return; while (can) {hidecurblock (); _down (); showcurblock ();} _bottom (); Void Pause () {if (g_status! = game_status_running) Return; // Clear Timer. KillTimer (g_hwnd, idt_play); g_status = game_status_paused; showgameInfo (); // Write to RESUME, I feel that a variable should be introduced to identify if the game has started, and whether START () can only be counted as a re-head. Void Resume () {if (g_status! = game_status_paused) return; settimer (g_hwnd, idt_play, 1000 / (g_level 1), null); g_status = game_status_running; showgameInfo (); } Void start () {if (g_status! = game_status_stopped) return; Int row, col; For (row = 0; row ) {for (col = 0; col // Generate the current square and the next block SRAND (NULL)); g_curblock = rand ()% g_blockcount; g_curblockcolor = RGB ((RAND ()% 4) * 64 63, (Rand ()% 4 ) * 64 63, (RAND ()% 4) * 64 63); g_nextblock = rand ()% g_blockcount; g_nextblockcolor = RGB ((RAND ()% 4) * 64 63, (RAND ()% 4) * 64 63, (Rand ()% 4) * 64 63); / / Decide the location g_currow = 0 of the current square; g_curcol = (playarea_width-blocks [g_curblock] .width / 2; // Initialization system information g_level = 0; // can customize the current level G_NextLevelPower = 0 in the future, g_nextlevelscore = 1000; // Initialize user information strcpy (g_player.name, player_default_name); g_player.power = 0; g_player.score = 0; // Then start the game render (); // Re-draw the interface g_status = game_status_running; settimer (g_hwnd, idt_play, (10-g_level) * 100, null); showcurblock (); showcurblock (); showgameInfo ();} Void Caculateenv () {RECT; GetClientRect (g_hwnd, & review); // Get the size of the current window // Changing environment variable g_env.clientHeight = Rect.bottom-rect.top 1; g_env.clientWidth = Rect.right-Rect.Left 1; g_env.bordersize = 3; g_env.spacing = 1; g_env.isnextshown = true; // Get all region coordinate // game area g_env.playareax = g_env.clientwidth * 3/10 g_env.bordersize; g_env.playareay = g_env.bordersize; g_Env.SingleHeight = (g_Env.ClientHeight-g_Env.PlayAreaY * 2-g_Env.BorderSize * 2-g_Env.Spacing) /PLAYAREA_HEIGHT-g_Env.Spacing; g_Env.SingleWidth = (g_Env.ClientWidth - g_Env.PlayAreaX * 2-g_Env.BorderSize * 2-g_env.spacing /playarea_width-g_env.spacing; // Next zone g_env.nextx = g_env.clientwidth * 17/20-g_env.Bordersize-g_env.spacing * 5/2-g_env.singlewidth * 2; g_env.nexty = g_env.bordersize * 2; // UpdateWindow (g_hwnd); } Void render () {// Draw the entire interface Char S [200]; / / Clear all G_GDi-> Clear (RGB (0x80, 0x80, 0x80)); // Draw picture //g_gdi- butadrawpic (G_env.Bordersize*2, g_env.bordersize*2, g_env.clientwidth / 5-g_env.bordersize * 5, g_env.clientwidth / 5-g_env.bordersize * 5, "MyDaughter. BMP "); // Draw logo //g_gdi, butt, g _v.bordersize*2 ,ract.bottom-g_env.bordersize*2, 100, 100," logo.bmp "); // Draw the game area g_gdi-> setColor (0); g_gdi-> DRAWBOX (g_env.playareax, g_env.playareay, (g_env.singleheight g_env.spacing) * playarea_height g_env.spacing g_env.bordersize * 2, (g_env .SingleWidth g_Env.Spacing) * PLAYAREA_WIDTH g_Env.Spacing g_Env.BorderSize * 2, BORDER_STYLE_FLAT, INNER_STYLE_SOLID); int row, col; for (row = 0; row / / Draw each button STRCPY (S, "[Enter]: start / resume"); g_gdi-> textout (g_env.clientwidth * 3/4, g_env.clientHeight / 2, S, (int) strlen (s)); STRCPY (S, "[PAUSE]: PAUSE"); g_gdi-> textout (g_env.clientwidth * 3/4, g_env.clientheight / 2 20, s, (int) strlen (s)); struffpy (s, " ↑: Turning block "); g_gdi-> textout (g_env.clientwidth * 3/4, g_env.clientHeight / 2 40, s, (int) strlen (s)); struct (s," ↓: Dropping "); G_gdi-> textout (g_env.clientwidth * 3/4, g_env.clientHeight / 2 60, s, (int) strlen (s)); strcpy (s," ←: left shift "); g_gdi-> Textout (g_env.clientwidth * 3/4, g_env.clientheight / 2 80, s, (int) strlen (s)); strcpy (s, "→: right shift"); g_gdi-> textout (g_env.clientwidth * 3/4, g_env.clientHeight / 2 100, S, (int) Strlen (s)); strcpy (s, "[space]: set"); g_gdi-> textout (g_env.clientwidth * 3/4, g_env.clientheight / 2 120, s, (int) Strlen (s)); strcpy (s, "[esc]: stop game"); g_gdi-> textout (g_env.clientwidth * 3/4, g_env.clientHeight / 2 140, S, (int) Strlen (s)); // Draw the current state showgameInfo (); / / The following is the debugging information //sprintf (S, "ClientWidth:%d" ,g_env.clientwidth); // g_gdi-> textout (0, 0, s, (int) strlen (s)); // Sprintf s, "ClientHeight:% D", g_env.clientheight); // g_gdi-> textout (0, 20, s, (int) strlen (s)); // Sprintf (s, "playareax:% d", g_env .Playareax); // g_gdi-> textout (0, 40, s, (int) strlen (s)); //sprintf (2, "playareay :%D" ,g_env.playareay); // g_gdi-> textout (0, 60, s, (int) strlen (s)); //sprintf (2, "singleheight:%d" ,g_env.singleHeight); // g_gdi-> textout (0, 80, s, (int) Strlen (s)); //sprintf (S, "SingleWidth:%d" ,g_env.singlewidth); // g_gdi-> textout (0,100, s, (int) strlen (s));} LRESULT WINAPI MSGPROC (hwnd hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {switch (msg) {case WM_TIMER: Down (); return 0; case WM_DESTROY: PostQuitMessage (0); return 0; case WM_ACTIVATE: if (wParam == WA_INACTIVE) {Pause ();} Break; Case WM_SIZE: IF (WPARAM! = SIZE_MINIMIMIMIZED) {CaculateEnv (); if (wparam == size_restore) {render ();} // updateWindow (hwnd);} return 0; Case WM_Paint: Render ( ValidateRec T (hwnd, null); // huh, it is still not able to remove the RETURN 0; // Case WM_SYSKEYDOWN: CASE WM_KEYDOWN: SWITCH (WPARAM) {Case VK_UP: Turn (); Break; Case vk_down: Down (); Break Case vk_left: moveleft (); Break; Case vk_right: Moveright (); Break; Case vk_space: bottom (); Break; Case vk_return: if (g_status == Game_status_paused) {resume ();} else {start (); } Break; Case VK_PAUSE: PAUSE (); Break; // Case VK_END: CASE VK_ESCAPE: Stop (); Break;}} Return DEFWINDOWPROC (HWND, MSG, WPARAM, LPARAM); // System Initialization HRESULT INITIALIZE () {// Binds the current window to its own GDI g_gdi = new ccustomGDI (g_hwnd); if (successdeded)) {Return S_OK;} Return E_FAIL; // Release system resource void cleanup () {// Release square resource clearblocks (); // Release GDI DELETE G_GDI;} int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {// create a form WNDCLASS wndclass = {0, MsgProc, 0L, 0L, hInstance, NULL, NULL, NULL, NULL, "Els"}; RegisterClass (WndClass); G_hWnd = CreateWindow ("ELS", "ELS V1.0.0.0", WS_OVERLAPPEDWINDOW, 100, 100, 640, 480, GetDesktopWindow (), NULL, WNDCLASS.HINSTANCE, NULL // Initialize IF (SUCCEEDED ())) {ShowWindow (g_hwnd, sw_showdefault); UpdateWindow (g_hwnd); // Enter Window Message Cyclic MSG Msg; While (GetMessage (& MSG, NULL, 0)) {TranslateMessage (& MSG); DispatchMessage (& MSG);}} Cleanup (); UnregisterClass ("ELS", WNDCLASS.HINSTANCE); RETURN 0;} // --------------------------------- -------------------------------------------------- ---------------------