How to realize Tetris with C language
This article mainly introduces "how to realize Tetris in C language". In daily operation, I believe that many people have doubts about how to realize Tetris in C language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to realize Tetris in C language"! Next, please follow the editor to study!
The specific code of Tetris is implemented in C language, and the operation with VC++6.0 is as follows:
1. File-> New-> File-> Select C _ Header File- on the left > name of the file on the right is "tetris.h"-> the path is assumed to be the desktop folder: tetris- > OK. Then paste the "header file" code marked in red below into it, save and exit (or close the workspace).
2. File-> New-> File-> Select C _ Header File- on the left > name of the file on the right is "tetris.h"-> the path is assumed to be the desktop folder: tetris- > OK. Create a new "tetris.c" source file, paste the following source code into it, save and exit (or close the workspace).
3. File-> New-> Project-> Select Win32 Application on the left > Project name on the right: tetris- > path is assumed to be the desktop folder: tetris- > OK-> an empty project-> complete. Next: project-> add to Project-> File. At this point, add the header file and source code to debug and use.
1. Header file
/ / 1. Custom enumeration types, define 7 forms of game box typedef enum tetris_shape {ZShape=0, SShape, LineShape, TShape, SquareShape, LShape, MirroredLShape} shape;//2. Function declaration / / (1) operate the square function int maxX (); / / get the maximum x coordinate of the current square int minX (); / / get the minimum x coordinate of the current square void turn_left (); / / rotate the current square 90 degrees counterclockwise void turn_right (); int out_of_table (); void transform (); int leftable (); int rightable (); int downable (); void move_left (); void move_right () / / (2) int add_to_table (); void remove_full (); / / (3) control game functions void new_game (); void run_game (); void next_shape (); int random (int seed); / / (4) drawing functions void paint (); void draw_table (); / / (5) other functions void key_down (WPARAM wParam); void resize (); void initialize (); void finalize () / / (6) callback function, which is used to process Windows message LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM)
two。 source code
/ / 1. The file contains # include#include#include#include "tetris.h" / / 2. Constant definition # define APP_NAME "TETRIS" # define APP_TITLE "Tetris Game" # define GAMEOVER "GAMEOVER" # define SHAPE_COUNT 7#define BLOCK_COUNT 4#define MAX_SPEED 5#define COLUMS 10#define ROWS 20#define RED RGB # define YELLOW RGB (255 Letter0) # define GRAY RGB (128128128) # define BLACK RGB (0Med 0) # define WHITE RGB (255255255) # define STONE RGB (192192192) # define CHARS_IN_LINE 14#define SCORE "SCORE% 4d"
3. Global variable definition
/ / (1) char score_ chars [chars _ IN_LINE] = {0}; / / (2) char* press_enter= "Press Enter key..."; / / (3) help message char* help [] = {"press space or up key to transform shape.", "Press left or right key to mover shape.", "Press down key to speed up.", "Press enter key to pause game.", "Enjoy it.:-)", 0} / / (4) enumerate the state of the game enum game_state {game_start, game_run, game_pause, game_over,} state=game_start;// (5) define the color of the square COLORREF shape_color [] = {RGB (255L0), RGB (0LECO), RGB (0Med 0255), RGB (255PM 255L0), RGB (0255255), RGB (255PM 0255), RGB (255255255)} / / (6) Type int shape_ covert [SHAPE _ COUNT] [BLOCK_COUNT] [2] = {{0ret 1}, {0meme0}, {- 1rect 0}, {- 1c 1}, {{0recast 1}, {0pm 0}, {1pm 0}, {1pm 0}, {0pm 0}, {0pm 1}, {0pm 2}}, {{- 1pm 0}, {0pm 0}, {0pm 0}, {0pm 0}, {0pm 1}}, {{0pm 0}, {0pm 1}} {1mine1}}, {{- 1mae1}, {0fujimi 1}, {0meme0}, {0jue 1}}, {{1memere 1}}, {0memmel 1}}, {0jin0}, {0jce1} / / (7) score int score=0;// (8) next square shape next=0;// (9) current square shape current=0;// (10) coordinates of each part of current square int current_coordinate [4] [2] = {0}; / / (11) game desktop int table [ROWS] [COLUMS] = {0}; / / (12) x coordinate of current square int shapex=0;// (13)\ y coordinate int shapey=0 of current square / / (14) Square down speed int speed=0;// (15) start time of each frame clock_t start=0;// (16) end time of each frame clock_t finish=0;// (17) windows drawing variable HWND gameWND;HBITMAP memBM;HBITMAP memBMOld;HDC memDC;RECT clientRC;HBRUSH blackBrush;HBRUSH stoneBrush;HBRUSH shapeBrush [SHAPE _ COUNT]; HPEN grayPen;HFONT bigFont;HFONT smallFont;//4. The main processing function / / (1) takes the maximum coordinate int maxX () {int item0; int xcoordinate current _ coexisting [I] [0]; int massix; for