File one
///[stdafx.cpp]/
// stdafx.cpp: Source File That Includes Just The Standard Includes // Toybricks.pch Will Be The pre-compiled header // stdafx.obj Will Contain The Pre-Compiled Type Information
#include "stdafx.h"
// Todo: Reference Any Additional Headers you need in stdafx.h // and not in this fil
File 2
//[stdafx.h]//
// stdafx.h: include File for Standard System Include Files, // OR Project Specific Include Files That Are Used Frequently, But // Are Changed Infrequently //
#if! defined (AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__included _) # define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__included_
#if _MSC_VER> 1000 # prgma overce # endif //_MSC_VER> 1000
#define Win32_Lean_and_mean // Exclude Rarely-useed Stuff from Windows Headers
#include
// Todo: Reference Additional Headers your Program Requires Here
// {{AFX_INSERT_LOCATION}} // Microsoft Visual C Will Insert Additional Declarations Immediately Before The Previous Line.
#ENDIF /! Defined (AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__included_)
File three
/[game.cpp]
// Toybricks.cpp: Defines the entry point for the application.//
#include "stdafx.h"
/ ************************************************** ****** /
#include
#define cell 15 // [PIX) #define W 20 // Game area width (12 [square] long, 8 lattice used to draw "Next" square) #define h 26 // Game area high (26 [square] edge length) #define ms_newblock WM_USER 1 // message ID, generate new [square] #define ms_draw wm_user 2 // message ID, used to paint [square] #define ms_nextblock WM_USER 3 // message ID, used to display the next [Russian square] shape // ---------------------- window functions Description ------------------------ Lresult Callback WndProc (HWND, UINT, WPARAM, LPARAM);
/ / -------------------------------------------------------------------------------------------- --------------- Int WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance, Pstr Szcmdline, ICMDSHOW)
{Static Char AppName [] = "Toybrick"; // Window class name hWnd hwnd; msg msg; // message structure WNDCLASSEX WNDCLASS; // window type ISCREENWIDE; / / Define a integer variable to obtain the width of the window
wndclass.cbSize = sizeof (wndclass); wndclass.style = CS_HREDRAW | CS_VREDRAW; // Window Type // CS_HREDRAW: Redraws the entire window if a movement or size // adjustment changes the width of the client area // CS_VREDRAW: Redraws. . the entire window if a movement or size // adjustment changes the height of the client area wndclass.lpfnWndProc = WndProc; // window procedure is WndProc wndclass.cbClsExtra = 0; // window class without extension wndclass.cbWndExtra = 0; // window instance no extension wndclass.hInstance = hInstance; // this instance handle wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION); // default icon wndclass.hCursor = LoadCursor (NULL, IDC_ARROW); // arrow cursor wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); // black background wndclass.lpszMenuName = NULL; // no menu window wndclass.lpszClassName = AppName; // class called "ToyBrick" wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION) ; // - ------------------------------------------------ ---------------------------
If (! registerclassex (& wndclass)) // If the registration fails, alert sound, return false {messagebeep (0); return false;}
/ / Get the X-Value IScreenWide of the display resolution, place the program window in the center iscreenwide = getSystemMetrics (SM_CXFULLSCREEN);
HWND = CREATEWINDOW (AppName, // Window Class Name "Russian Square Simulation Project (Software College. Qian Yanjiang)", // Window Instance Title WS_MINIMIXBOX | WS_SYSMENU, // Window Style IscreenWide / 2-w * Cell / 2 , // Window Level Coordinate (X) Cell, // Window Left upper corner longitudinal width (Y) W * Cell, // Window high null, // Fired window NULL , // Window No Menu Hinstance, // Currently Create this window's current handle NULL // does not use this value); if (! HWnd) return false; // Display window showWindow (hwnd, icmdshow); // Draw UpdateWindow (hwnd); MessageBox (HWND, "Start Game / N / N_ Software Instimation Project", "Start", MB_OK; SendMessage (HWND, MS_NEWBLOCK, 0, 0); SetTimer (hwnd, 1, 500, NULL); // Message Cycle While (GetMessage (& MSG, NULL, 0, 0)) {TranslateMessage (& MSG); DISPATCHMESSAGE (& MSG);} // When the message loop returns the message to the system RETURN MSG. WPARAM;} // Function Drawract: Draw [Square] ----- (□) // Parameters: Equipment Environment Handle and [Square] Four Corner Coordinate Void DrawRect (HDC HDC, INT L, INT T, INT R, INT) b) {MoveToex (HDC, L, T, NULL); // Move the cursor To (L, T) LINETO (HDC, R, T); LINETO (HDC, R, B); LINETO (HDC, L, B); LINETO (HDC, L, T);
// Function Drawcell: Draw [Checkered] ----- (Red ■) // Parameter: Equipment Environment Handle and [Checkered] Four Corner Coordinate Void Drawcell (HDC HDC, Int L, Int T, Int R, int R, int L, INT b) {hbrush hbrush;
Hbrush = Createsolidbrush (RGB (255, 0)); // Red painting SelectObject (HDC, Hbrush); Rectangle (HDC, L, T, R, B); deleteObject (Hbrush);
}
// Pain the square of the game area, including "Game Space" and "Display Next [Square] Space" // This function is included in the Cover function. Parameters: Equipment Environment Handle VOID DrawGamePlace (HDC HDC) {INT I, J; HPEN HPEN1, HPEN2; HPEN1 = Createpen (PS_SOLID, 1, RGB (0,255,0)); HPEN2 = Createpen (ps_dashdotdot, 3, RGB (0, 0,255));
// Draw split line SelectObject (HDC, HPEN2); MoveToex (HDC, (W-8) * Cell, 0, NULL); LINETO (HDC, (W-8) * Cell, H * Cell); // Draw game Regional square line (green) SelectObject (HDC, HPEN1); for (i = 1; i // Function DrawBlock: Drawing [Russian Square] // Parameter: Equipment Environment Handle and [Russian Square] 4 [Checkered] in the game area // Each [Russian square] is from four [Checkered] Composition 7 different shapes VOID DrawBlock (HDC HDC, INT Block [4] [2]) {INT I; for (i = 0; i <4; i ) Drawcell (HDC, (Block [i] [0] -1) * Cell, (Block [I] [1] -1) * cell, // .... block [i] [0] * Cell, Block [i] [1] * Cell); } // Function Cover: Clear the original position [Russian square] // parameter: equipment environment handle and "Russian square] // effect (1) Clear [Russian square], every [Russian square] Checkered] Picture of a square white / / / (2) resend the square of the game area Void Cover (HDC HDC, int ORG [4]) {INT i; hbrush hbrush; // Re-draw the game area DrawGamePlace (HDC); Hbrush = (Hbrush) GetStockObject (White_brush); SelectObject (HDC, Hbrush); for (i = 0; i <4; i ) Rectangle (HDC, (ORG [I] [0] -1) * Cell, (ORG [ I] [1] -1) * Cell, // ..... Org [i] [0] * Cell, org [i] [1] * Cell); deleteObject (Hbrush); } // ------------------- Window process function WNDPROC ------------------------- ---- Lresult Callback WndProc (Hwnd Hwnd, Uint IMSG, WPARAM WPARAM, LPARAM LPARAM) {INT I, J, K, LINES, R; Static Int Top, Sel, Flag; Static Int Cells [W-6] [H]; // Control game area [square matrix] static int org [4] [2], block [4] [2], org2 [4] [2]; // [square] HDC HDC; HPEN HPEN; PAINTSTRUCT PS; Switch (IMSG) {copy wm_create: // When an application uses a function CREATEWINDOW or CREATEWINDOWEX to create a window, / / / / The system will send this message to this new window process. This message will send the message before the window // will be sent, which will be sent before the CREATEWINDOW or CREATEWEX function returns. TOP = H-1; // Put the first column and the last column [square] 1, control [square] does not exceed the game area for (i = 0; i / / Other [square] 0, the game block can only move for for (i = 1; i <= W-8; i ) for (j = 0; j Case ms_newblock: flag = 0; // flag Represents [square] rotated several times For (i = top; i // Circular statement checks if there is a certain row all is filled with FOR (J = 1; J <= W-7; J ) IF (! Cells [J] [i]) {lines = 1; Break } // If the row is filled, the fill state of the previous line is copied to the line, and the row is pushed //, and all [square] is moved down, and all [! Lines) {for (FOR) J = 1; J // This function adds the rectangle in the specified window user area to the update area of the window / /. This rectangle is invalid. This invalid rectangle, along with other areas in the update area, will be redrawn when receiving the next // one WM_PAINT message. The invalid zone has been accumulated in the update area until the next WM_PAINT message occurs when the next WM_PAINT message occurs. INVALIDATERECT (HWND, NULL, TRUE);}} // Generate random number 0 to 7, representing 7 shape SRAND ((unsigned) Time (NULL) of [Square]; SEL = RAND ()% 7; // [square] shape initialization // [square] shape is determined by each [square] position // game area width w = 20, block [?] [0] = 4/5/6/7, Block [?] [1] = 0/1/2 // This [square] Initial location in the top of the game area {cas 0: // ▓▓ // ▓▓ ▓ ▓ ▓ ▓ ▓ ▓ ▓ [▓ [[[[0] [ 0] = block [0] [0] = 5; org [0] [1] = block [0] [1] = 0; org [1] [0] = block [1] [0] = 6; Org [1] [1] = Block [1] [1] = 0; org [2] [0] = block [2] [0] = 5; org [2] [1] = block [2] [1] = 1; ORG [3] [0] = block [3] [0] = 6; org [3] [1] = block [3] [1] = 1; for (i = 0; i <4; i ) {ORG2 [I] [0] = ORG [I] [0] 11; org2 [i] [1] = org [i] [1] 5;} Break; Case 1: // ▓▓▓ or [0] [0] = Block [0] [0] = 4; org [0] [1] = block [0] [1] = 0; org [1] [ 0] = Block [1] [0] = 5; ORG [1] [1] = block [1] [1] = 0; org [2] [0] = block [2] [0] = 6; Org [2] [1] = Block [2] [1] = 0; org [3] [0] = block [3] [0] = 7; org [3] [1] = block [3] [1] = 0; for (i = 0; i <4; i ) {ORG2 [i] [0] = org [i] [0] 11; org2 [i] [1] = org [i] [1] 5; Break; Case 2: // ▓ // ▓▓ // ▓ ▓ [[0] [0] = block [0] [0] = 5; org [0] [1] = block [0] [1] = 0; Org [1] [0] = Block [1] [0] = 5; org [1] [1] = block [1] [1] = 1; ORG [2] [0] = block [2] [0] = 6; org [2] [1] = block [2] [1] = 1; ORG [3] [0] = block [3] [0] = 6; org [3] [1] = block [3 ] [1] = 2; for (i = 0; i <4; i ) {ORG2 [i] [0] = org [i] [0] 11; org2 [i] [1] = org [i] [1] 5; Break; Case 3: // ▓ // ▓▓ // ▓ ▓ [[0] [0] = block [0] [0] = 6; ORG [0] [1] = block [0] [1] = 0; Org [1] [0] = Block [1] [0] = 6; org [1] [1] = block [1] [1] = 1; org [2] [0] = block [2] [0] = 5; Org [2] [1] = block [2] [1] = 1; org [3] [0] = block [3] [0] = 5; org [3] [1] = block [3 ] [1] = 2; for (i = 0; i <4; i ) {ORG2 [i] [0] = org [i] [0] 11; org2 [i] [1] = org [i] [1] 5;} Break; case 4: // ▓ // ▓ // ▓ ▓ or or or [[[[[[[[= ▓ = ▓ ▓ ▓ ▓ ▓ ▓ ▓ ▓ ▓ = ▓ ▓ ▓ ▓ [0] = 5; Org [0] [1] = block [ 0] [1] = 0; ORG [1] [0] = Block [1] [0] = 5; ORG [1] [1] = block [1] [1] = 1; ORG [2] [0 ] = Block [2] [0] = 5; org [2] [1] = block [2] [1] = 2; org [3] [0] = block [3] [0] = 6; Org [ 3] [1] = Block [3] [1] = 2; for (i = 0; i <4; i ) {ORG2 [i] [0] = org [i] [0] 11; org2 [i ] [1] = ORG [I] [1] 5;} Break; Case 5: // ▓ // ▓ // ▓ ▓ ▓ ▓ [[0] [0] = block [0] [0] = 5; ORG [0] [1] = block [0] [1] = 0; ORG [1] [0] = Block [1] [0] = 5; org [1] [1] = block [1] [1] = 1; ORG [2] [0] = block [2] [0] = 5; org [2] [1] = block [2] [1] = 2; org [3] [0] = block [3] [0] = 4; org [3] [1] = block [3 ] [1] = 2; for (i = 0; i <4; i ) {ORG2 [i] [0] = org [i] [0] 11; org2 [i] [1] = org [i] [1] 5; Break; Case 6: // ▓ // ▓▓ ▓ ▓ or [[0] [0] = block [0] [0] = 5; ORG [0] [1] = block [0] [1] = 0; Org [1] [0] = Block [1] [0] = 4; org [1] [1] = block [1] [1] = 1; org [2] [0] = block [2] [0] = 5; org [2] [1] = block [2] [1] = 1; ORG [3] [0] = block [3] [0] = 6; org [3] [1] = block [3 ] [1] = 1; for (i = 0; i <4; i ) {ORG2 [i] [0] = org [i] [0] 11; org2 [i] [1] = org [i] [1] 5; SendMessage (hwnd, ms_nextblock, 0, 0); Break; default: SendMessage (hwnd, ms_newblock, 0, 0); sendMessage (hwnd, ms_nextblock, 0, 0); Break;} Return 0; Case WM_TIMER: // Each time the shoot [square] automatically moves down for FOR (i = 0; i <4; i ) block [i] [1] ; // Check [Square] Down to be filed, that is, if the new location is judged after the new position is [square] for (i = 0; i <4; i ) IF (Cells [block [i] [0]]]]] [Block [I]]]) {SendMessage (HWND, MS_NEXTBLOCK, 0, 0); For (i = 0; i <4; i ) cells [org [i] [0]] [org [i] [1]] = 1; IF (TOP> org [0] [1] -2) TOP = ORG [0] [1] -2; IF (TOP <1) {KillTimer (HWND, 1); MessageBox (hwnd, "game is over! / N Sichuan University Software College," Exit ", MB_ok); PostquitMessage (0);} SendMessage (hwnd, MS_NEWBLOCK, 0, 0); RETURN 0;} SendMessage (HWND, MS_DRAW, 0, 0); Return 0; // Response Keyboard Control Case WM_Keydown: R = 0; Switch (INT) WPARAM) {CASE VK_LEFT: FOR (i = 0; I <4; i ) Block [i] [0] -; Break Case vk_right: for (i = 0; i <4; i ) block [i] [0] ; break; case vk_down: for (i = 0; i <4; i ) block [i] [1] BREAK; / / Press [Up Key], [Square] Clockwise // [Square] rotation is not true rotation, but according to different [square] shape and the [square] rotation // number of times to move it One or a few [square] to achieve the effect of rotation. This is very complicated, algorithm // is not ideal, but it is possible to maintain [square] The center of gravity is relatively stable. Case vk_up: r = 1; Flag ; // [square] rotation plus 1 Switch (sel) // SEL represents the shape of the current [square] {casse 0: Break; Case 1: flag = flag% 2; for (i = 0; i <4; i ) {Block [i] [(flag 1)% 2] = org [2] [(Flag 1)% 2]; Block [i] [flag] = org [2] [FLAG] -2 i;} Break; Case 2: Flag = flag% 2; if (flag) {Block [0] [1] = 2; Block [3] [0] - = 2;} else {block [0] [1] - = 2; Block [3] [0] = 2;} Break; Case 3: Flag = flag% 2; if (flag) {Block [0] [1] = 2; Block [3] [0] = 2; } Else {block [0] [1] - = 2; block [3] [0] - = 2; Case 4: flag = flag% 4; switch (flag) {case 0: block [2] [0] = 2; Block [3] [0] = 2; Block [2] [1] = 1; Block [3] [1] = 1; Break; Case 1: Block [2] [0] = 1; Block [3] [0] = 1; Block [2] [1] - = 2; Block [3] [1] - = 2; Break; Case 2: Block [2] [0] - = 2; Block [3] [0] - = 2; Block [2] [1] - = 1; block [ 3] [1] - = 1; Break; Case 3: Block [2] [0] - = 1; Block [3] [0] - = 1; Block [2] [1] = 2; block [3 ] [1] = 2; Break;} Break; Case 5: flag = flag% 4; switch (flag) {case 0: block [2] [0] = 1; block [3] [0] = 1; Block [2] [1] = 2; Block [3] [1] = 2; Break; Case 1: Block [2] [0] = 2; Block [3] [0] = 2; Block [2] [1] - = 1; Block [3] [1] - = 1; Break; Case 2: Block [2] [0] - = 1; Block [3] [0] - = 1; Block [2] [1] - = 2; block [ 3] [1] - = 2; Break; Case 3: Block [2] [0] - = 2; Block [3] [0] - = 2; Block [2] [1] = 1; Block [3 ] [1] = 1; BREAK;} Break; Case 6: flag = flag% 4; switch [0] [0] ; block [0] [1] -; block [1] [0] -; block [1 ] [1] -; Block [3] [0] ; Block [3] [1] ; Break; Case 1: Block [1] [0] ; Block [1] [1] ; Break; Case 2: Block [0] [0] -; Block [0] [1] ; Break; Case 3: Block [3] [0] -; Block [3] [1] - -; Break;} //} Break;} // Judgment [square] After the new position has [square], if there is, rotate cancels for (i = 0; i <4; i ) IF (Cells [ Block [i] [0]] [block [i] [1]]) {if (r) flag = 3; for (i = 0; i <4; i ) for (j = 0; J <2; J ) block [i] [j] = org [i] [j]; return 0;} sendMessage (hwnd, ms_draw, 0, 0); Return 0; // Clear the current [square], and draw the next [square] case ms_nextblock: next block " HDC = Getdc (HWND); Cover (HDC, ORG2); // DrawBlock (HDC, ORG2); Return 0; / / Clear the current [square] and re-draw in the new location [square] Case MS_DRAW: HDC = GetDC (HWND); Cover (HDC, ORG); DrawBlock (HDC, ORG2); For (i = 0; i <4; i ) for (j = 0; j <2; j ) org [i] [j] = block [i] [j]; DrawBlock (HDC, Block); ReleaseDC (HWND, HDC); Return 0; // Re-draw the [square] Case WM_Paint: HDC = BeginPaint (HWND, & PS); DrawGamePlace (HDC); Textout (HDC, 15 * Cell, 12 * Cell, "Score", Lstrlen ("score"); Textout (HDC, 15 * Cell, 13 * Cell, "I", LSTRLEN ("I")); Textout (HDC, 15 * Cell, 15 * Cell, "Level", LSTRLEN "Level"); Textout (HDC, 15 * Cell-5, 19 * Cell, "Qian Yanjiang", LSTRLEN ("Qian Yanjiang"); HPEN = Createpen (PS_SOLID, 1, RGB (0, 255, 0)); SelectObject (HDC, HPEN); for (i = top; i Return DefWindowProc (HWND, IMSG, WPARAM, LPARAM); /*************************-----end------***** ********************************** /