<>C Language games - Tetris

All codes are as follows :
#include <easyx.h> #include <conio.h> #include <time.h> // Defining constants , Enumerator , structural morphology , global variable #
define WIDTH 10 // Game area width #define HEIGHT 22 // Height of game area #define UNIT 20 //
Actual pixels per game area unit // Define operation type enum CMD { CMD_ROTATE, // Square rotation CMD_LEFT, CMD_RIGHT,
CMD_DOWN, // Square left , right , Move down CMD_SINK, // Square bottom CMD_QUIT // Exit the game }; // Define how to draw a square enum
DRAW { SHOW, // Display square CLEAR, // Erase block FIX // Fixed block }; // Define seven Tetris struct BLOCK {
WORD dir[4]; // Four rotation states of the square COLORREF color; // Color of the square } g_Blocks[7] = { { 0x0F00,
0x4444, 0x0F00, 0x4444, RED }, // I { 0x0660, 0x0660, 0x0660, 0x0660, BLUE },
// mouth { 0x4460, 0x02E0, 0x0622, 0x0740, MAGENTA }, // L { 0x2260, 0x0E20, 0x0644,
0x0470, YELLOW }, // back L { 0x0C60, 0x2640, 0x0C60, 0x2640, CYAN }, // Z { 0x0360,
0x4620, 0x0360, 0x4620, GREEN }, // back Z { 0x4E00, 0x4C40, 0x0E40, 0x4640, BROWN }
}; // T // Define current square , Information for the next block struct BLOCKINFO { byte id; // block ID char x, y; //
Coordinates of the block in the game area byte dir : 2; // direction } g_CurBlock, g_NextBlock; // Define game area BYTE g_World[
WIDTH][HEIGHT] = { 0 }; / // Function declaration / void Init(); // Initialize game void Quit(); // Exit the game
void NewGame(); // Start a new game void GameOver(); // End game CMD GetCmd(); // Get control command void
DispatchCmd(CMD _cmd); // Distribution control command void NewBlock(); // Generate new block bool CheckBlock(
BLOCKINFO _block); // Check whether the specified square can be dropped void DrawUnit(int x, int y, COLORREF c, DRAW
_draw); // Draw cell square void DrawBlock(BLOCKINFO _block, DRAW _draw = SHOW); // Draw a square
void OnRotate(); // Puzzle Guzzle void OnLeft(); // Shift left square void OnRight(); // Shift right square void
OnDown(); // Move box down void OnSink(); // Bottom box void main() { Init(); CMD c; while (
true) { c = GetCmd(); DispatchCmd(c); // When you press exit , Displays a dialog box to ask the user whether to exit if (c == CMD_QUIT) {
HWND wnd= GetHWnd(); if (MessageBox(wnd, _T(" Do you want to exit the game ?"), _T(" remind "), MB_OKCANCEL
| MB_ICONQUESTION) == IDOK) Quit(); } } } // Initialize game void Init() { initgraph(640,
480); srand((unsigned)time(NULL)); setbkmode(TRANSPARENT); // Sets the background color of the hatch to transparent //
Display operating instructions settextstyle(14, 0, _T(" Song typeface ")); outtextxy(20, 330, _T(" Operating instructions ")); outtextxy(
20, 350, _T(" upper : rotate ")); outtextxy(20, 370, _T(" Left : Shift left ")); outtextxy(20, 390, _T(
" right : Shift right ")); outtextxy(20, 410, _T(" lower : Move down ")); outtextxy(20, 430, _T(" Space : sedimentation "));
outtextxy(20, 450, _T("ESC: quit ")); // Set coordinate origin setorigin(220, 20); // Draw game zone boundaries
rectangle(-1, -1, WIDTH * UNIT, HEIGHT * UNIT); rectangle((WIDTH + 1) * UNIT - 1
, -1, (WIDTH + 5) * UNIT, 4 * UNIT); // Start a new game NewGame(); } // Exit the game void Quit() {
closegraph(); exit(0); } // Start a new game void NewGame() { // Clear the game area setfillcolor(BLACK);
solidrectangle(0, 0, WIDTH * UNIT - 1, HEIGHT * UNIT - 1); ZeroMemory(g_World,
WIDTH* HEIGHT); // Generate next block g_NextBlock.id = rand() % 7; g_NextBlock.dir = rand()
% 4; g_NextBlock.x = WIDTH + 1; g_NextBlock.y = HEIGHT - 1; // Get new block NewBlock();
} // End game void GameOver() { HWND wnd = GetHWnd(); if (MessageBox(wnd, _T(
" Game over .\n Would you like to have another round ?"), _T(" Game over "), MB_YESNO | MB_ICONQUESTION) == IDYES) NewGame()
; else Quit(); } // Get control command DWORD m_oldtime; CMD GetCmd() { // Get control value while (true)
{ // If timeout , Auto drop one space DWORD newtime = GetTickCount(); if (newtime - m_oldtime >= 500)
{ m_oldtime = newtime; return CMD_DOWN; } // If there is a key , Return to the function corresponding to the key if (_kbhit()) {
switch (_getch()) { case 'w': case 'W': return CMD_ROTATE; case 'a': case 'A':
return CMD_LEFT; case 'd': case 'D': return CMD_RIGHT; case 's': case 'S':
return CMD_DOWN; case 27: return CMD_QUIT; case ' ': return CMD_SINK; case 0:
case 0xE0: switch (_getch()) { case 72: return CMD_ROTATE; case 75: return
CMD_LEFT; case 77: return CMD_RIGHT; case 80: return CMD_DOWN; } } } // delayed ( reduce
CPU Occupancy ) Sleep(20); } } // Distribution control command void DispatchCmd(CMD _cmd) { switch (_cmd) {
case CMD_ROTATE: OnRotate(); break; case CMD_LEFT: OnLeft(); break; case
CMD_RIGHT: OnRight(); break; case CMD_DOWN: OnDown(); break; case CMD_SINK:
OnSink(); break; case CMD_QUIT: break; } } // Generate new block void NewBlock() {
g_CurBlock.id = g_NextBlock.id, g_NextBlock.id = rand() % 7; g_CurBlock.dir =
g_NextBlock.dir, g_NextBlock.dir = rand() % 4; g_CurBlock.x = (WIDTH - 4) / 2;
g_CurBlock.y = HEIGHT + 2; // Move the new box down until it is partially displayed WORD c = g_Blocks[g_CurBlock.id].dir[
g_CurBlock.dir]; while ((c & 0xF) == 0) { g_CurBlock.y--; c >>= 4; } // Draw a new square
DrawBlock(g_CurBlock); // Draw next box setfillcolor(BLACK); solidrectangle((WIDTH + 1
) * UNIT, 0, (WIDTH + 5) * UNIT - 1, 4 * UNIT - 1); DrawBlock(g_NextBlock); //
Set timer , Used to judge automatic falling m_oldtime = GetTickCount(); } // Draw cell square void DrawUnit(int x, int y
, COLORREF c, DRAW _draw) { // Screen coordinates corresponding to the calculation unit block int left = x * UNIT; int top = (
HEIGHT- y - 1) * UNIT; int right = (x + 1) * UNIT - 1; int bottom = (HEIGHT - y)
* UNIT - 1; // Draw cell square switch (_draw) { case SHOW: // Draw ordinary square setlinecolor(0x006060)
; roundrect(left + 1, top + 1, right - 1, bottom - 1, 5, 5); setlinecolor(
0x003030); roundrect(left, top, right, bottom, 8, 8); setfillcolor(c);
setlinecolor(LIGHTGRAY); fillrectangle(left + 2, top + 2, right - 2, bottom - 2)
; break; case FIX: // Draw a fixed square setfillcolor(RGB(GetRValue(c) * 2 / 3, GetGValue(c)
* 2 / 3, GetBValue(c) * 2 / 3)); setlinecolor(DARKGRAY); fillrectangle(left + 1,
top+ 1, right - 1, bottom - 1); break; case CLEAR: // Erase block setfillcolor(BLACK);
solidrectangle(x * UNIT, (HEIGHT - y - 1) * UNIT, (x + 1) * UNIT - 1, (HEIGHT -
y) * UNIT - 1); break; } } // Draw a square void DrawBlock(BLOCKINFO _block, DRAW _draw) {
WORD b= g_Blocks[_block.id].dir[_block.dir]; int x, y; for (int i = 0; i < 16;
i++, b <<= 1) if (b & 0x8000) { x = _block.x + i % 4; y = _block.y - i / 4; if (
y< HEIGHT) DrawUnit(x, y, g_Blocks[_block.id].color, _draw); } } // Check whether the specified square can be dropped
boolCheckBlock(BLOCKINFO _block) { WORD b = g_Blocks[_block.id].dir[_block.dir]
; int x, y; for (int i = 0; i < 16; i++, b <<= 1) if (b & 0x8000) { x = _block.x
+ i % 4; y = _block.y - i / 4; if ((x < 0) || (x >= WIDTH) || (y < 0)) return
false; if ((y < HEIGHT) && (g_World[x][y])) return false; } return true; } //
Puzzle Guzzle void OnRotate() { // Get rotatable x Offset int dx; BLOCKINFO tmp = g_CurBlock; tmp.
dir++; if (CheckBlock(tmp)) { dx = 0; goto rotate; } tmp.x = g_CurBlock.x - 1;
if (CheckBlock(tmp)) { dx = -1; goto rotate; } tmp.x = g_CurBlock.x + 1; if (
CheckBlock(tmp)) { dx = 1; goto rotate; } tmp.x = g_CurBlock.x - 2; if (
CheckBlock(tmp)) { dx = -2; goto rotate; } tmp.x = g_CurBlock.x + 2; if (
CheckBlock(tmp)) { dx = 2; goto rotate; } return; rotate: // rotate DrawBlock(
g_CurBlock, CLEAR); g_CurBlock.dir++; g_CurBlock.x += dx; DrawBlock(g_CurBlock);
} // Shift left square void OnLeft() { BLOCKINFO tmp = g_CurBlock; tmp.x--; if (CheckBlock(
tmp)) { DrawBlock(g_CurBlock, CLEAR); g_CurBlock.x--; DrawBlock(g_CurBlock); } }
// Shift right square void OnRight() { BLOCKINFO tmp = g_CurBlock; tmp.x++; if (CheckBlock(tmp
)) { DrawBlock(g_CurBlock, CLEAR); g_CurBlock.x++; DrawBlock(g_CurBlock); } }
// Move box down void OnDown() { BLOCKINFO tmp = g_CurBlock; tmp.y--; if (CheckBlock(tmp)
) { DrawBlock(g_CurBlock, CLEAR); g_CurBlock.y--; DrawBlock(g_CurBlock); } else
OnSink(); // When cannot move down , implement “ Bottom box ” operation } // Bottom box void OnSink() { int i, x, y; // Move box down consecutively
DrawBlock(g_CurBlock, CLEAR); BLOCKINFO tmp = g_CurBlock; tmp.y--; while (
CheckBlock(tmp)) { g_CurBlock.y--; tmp.y--; } DrawBlock(g_CurBlock, FIX); //
Fixed block in the game area WORD b = g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir]; for (i = 0; i <
16; i++, b <<= 1) if (b & 0x8000) { if (g_CurBlock.y - i / 4 >= HEIGHT) { //
If the fixed position of the block exceeds the height , End game GameOver(); return; } else g_World[g_CurBlock.x + i % 4][
g_CurBlock.y - i / 4] = 1; } // Check if rows need to be eliminated , And mark BYTE remove = 0; // low 4
Bits are used to mark the 4 Whether the line has elimination behavior for (y = g_CurBlock.y; y >= max(g_CurBlock.y - 3, 0); y--)
{ i = 0; for (x = 0; x < WIDTH; x++) if (g_World[x][y] == 1) i++; if (i == WIDTH
) { remove |= (1 << (g_CurBlock.y - y)); setfillcolor(LIGHTGREEN); setlinecolor(
LIGHTGREEN); setfillstyle(BS_HATCHED, HS_DIAGCROSS); fillrectangle(0, (HEIGHT -
y- 1) * UNIT + UNIT / 2 - 5, WIDTH * UNIT - 1, (HEIGHT - y - 1) * UNIT + UNIT /
2 + 5); setfillstyle(BS_SOLID); } } if (remove) // If whole line elimination occurs { // delayed 300 millisecond Sleep
(300); // Erase the line just marked IMAGE img; for (i = 0; i < 4; i++, remove >>= 1) { if (remove
& 1) { for (y = g_CurBlock.y - i + 1; y < HEIGHT; y++) for (x = 0; x < WIDTH; x
++) { g_World[x][y - 1] = g_World[x][y]; g_World[x][y] = 0; } getimage(&img, 0,
0, WIDTH * UNIT, (HEIGHT - (g_CurBlock.y - i + 1)) * UNIT); putimage(0, UNIT, &
img); } } } // Generate new block NewBlock(); }
Like it !

Technology
©2019-2020 Toolsou All rights reserved,
【C++ Must see for entry 】C++ from 0 reach 1 Introductory programming axios Interceptor packaging and use Spring Boot Interview must ask : Automatic configuration principle VMware 16 install centos 7 Detailed tutorial C Language data structure - Sequence table delete duplicates V2.0.0 The 12th Blue Bridge Cup c++b Group personal problem solving On sending data from serial port single chip microcomputer to upper computer centos7 install RabbitMqjava Polymorphic array of opencv-python Fourier transform and inverse transform