C language implements Tetris

  • 2020-06-23 01:12:09
  • OfStack

This article shares the specific code of C language Tetris for your reference, the specific content is as follows

This code running environment is VS2013 under Windows
First create tetris.cpp
Then create view.h and view.cpp, model.h, and model.cpp, in turn.

The code is as follows:

view.h


#pragma once


#include <stdio.h>
void ShowBackground();
void ShowBrick();
void ShowGame();
void OnLeft();
void OnRight();
void OnUp();
void OnDown();

view.cpp


#include <stdlib.h>
#include "view.h"
#include "model.h"
void OnLeft()
{// If you can move to the left, move to the left 
 if (IsCanMove(g_nRow, g_nCol - 1))
 {
 g_nCol--;
 ShowGame();
 }
}

void OnRight()
{
 if (IsCanMove(g_nRow, g_nCol + 1))
 {
 g_nCol++;
 ShowGame();
 }
}

void OnUp()
{
 if (IsCanRotate())
 {
 Rotate();
 ShowGame();
 }
}

void OnDown()
{
 if (IsCanMove(g_nRow+1, g_nCol))
 {
 g_nRow++;
 ShowGame();
 }
 else
 {
 // Fix the square to the background and generate a new square 
 CombineBgBrick();
 GetNewBrick();
 // Decide whether the game is over and give corresponding hints 
 }
}

void ShowGame()
{
 system("cls");
 CombineBgBrick();
 ShowBackground();
 DetachBgBrick();
}
void ShowBrick()
{
 for (size_t i = 0; i < 4; i++)
 {
 for (size_t j = 0; j < 4; j++)
 {
 if (g_chBrick[i][j] == 1)
 {
 printf(" s ");
 }
 }
 printf("\r\n");
 }
}

void ShowBackground()
{
 for (size_t nRow = 0; nRow < GAME_ROWS; nRow++)
 {
 for (size_t nCol = 0; nCol < GAME_COLS; nCol++)
 {
 if (g_chBackground[nRow][nCol] == 1)
 {
 printf(" s ");
 }
 else
 {
 printf(" - ");
 }
 }
 printf("\r\n");
 }
}

model.cpp


#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "model.h"


char g_chBackground[GAME_ROWS][GAME_COLS];
char g_chBrick[4][4];
int g_nShape = 0; // Is it a bar or a square? The coefficient is zero 16
int g_nRotate = 0; // The orientation, the coefficient is zero 4
int g_nRow = 0;
int g_nCol = 0;
char g_chBrickPool[][4] = {
//  strip 
1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,

1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,

// T The shape 
1, 1, 1, 0,
0, 1, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

0, 1, 0, 0,
1, 1, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0,

0, 1, 0, 0,
1, 1, 1, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 0, 0, 0,
1, 1, 0, 0,
1, 0, 0, 0,
0, 0, 0, 0,

//L The shape of 
1, 0, 0, 0,
1, 0, 0, 0,
1, 1, 0, 0,
0, 0, 0, 0,

1, 1, 1, 0,
1, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,

1, 1, 0, 0,
0, 1, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0,

0, 0, 1, 0,
1, 1, 1, 0,
0, 0, 0, 0,
0, 0, 0, 0,
};

int IsCanRotate()
{
 char chNextShape[4][4] = { 0 };
 int nNextRotate = (g_nRotate + 1) % 4;
 int nPoolRows = g_nShape * 16 + nNextRotate * 4;
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
 for (size_t nCol = 0; nCol < 4; nCol++)
 {
 chNextShape[nRow][nCol] = g_chBrickPool[nRow + nPoolRows][nCol];
 }
 }
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
 for (size_t nCol = 0; nCol < 4; nCol++)
 {
 if (chNextShape[nRow][nCol] == 1)
 {
 if (g_chBackground[nRow + g_nRow][nCol + g_nCol] == 1)
 {
  return 0; // Can't move 
 }
 }
 }
 }
 return 1;
}

void Rotate()
{
 g_nRotate = (g_nRotate + 1) % 4;
 int nPoolRows = g_nShape * 16 + g_nRotate*4;
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
 for (size_t nCol = 0; nCol < 4; nCol++)
 {
 g_chBrick[nRow][nCol] = g_chBrickPool[nRow + nPoolRows][nCol];
 }
 }
}

int IsCanMove(int nToRow, int nToCol)
{
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
 for (size_t nCol = 0; nCol < 4; nCol++)
 {
 if (g_chBrick[nRow][nCol] == 1)
 {
 if (g_chBackground[nRow + nToRow][nCol + nToCol] == 1)
 {
  return 0; // Can't move 
 }
 }
 }
 }
 return 1;
}

void GetNewBrick()
{
 srand((unsigned)time(NULL));
 g_nRow = 0;
 g_nCol = GAME_COLS / 2 - 1;
 int nShapeCount = sizeof(g_chBrickPool) / sizeof(g_chBrickPool[0]) /16;
 g_nShape = rand() % nShapeCount;
 g_nRotate = rand() % 4;
 int nPoolRows = g_nShape * 16 + g_nRotate * 4;
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
 for (size_t nCol = 0; nCol < 4; nCol++)
 {
 g_chBrick[nRow][nCol] = g_chBrickPool[nRow+nPoolRows][nCol];
 }
 }
}

void DetachBgBrick()
{
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
 for (size_t nCol = 0; nCol < 4; nCol++)
 {
 if (g_chBrick[nRow][nCol] == 1)
 {
 g_chBackground[nRow + g_nRow][nCol + g_nCol] = 0;
 }
 }
 }
}

void CombineBgBrick()
{// Combination of block 
 for (size_t nRow = 0; nRow < 4; nRow++)
 {
 for (size_t nCol = 0; nCol < 4; nCol++)
 {
 if (g_chBrick[nRow][nCol] == 1)
 {
 g_chBackground[nRow+g_nRow][nCol+g_nCol] = 1;
 }
 }
 }
}

void InitBackground()
{// Initialization background 
 for (size_t nRow = 0; nRow < GAME_ROWS; nRow++)
 {
 for (size_t nCol = 0; nCol < GAME_COLS; nCol++)
 {
 if (nRow == GAME_ROWS - 1
 || nCol == 0
 || nCol == GAME_COLS - 1)
 {
 g_chBackground[nRow][nCol] = 1;
 }
 else
 {
 g_chBackground[nRow][nCol] = 0;
 }
 }
 }
}

model.h


#pragma once

#define GAME_ROWS 20
#define GAME_COLS 12

extern char g_chBackground[GAME_ROWS][GAME_COLS];
extern char g_chBrick[4][4];
extern int g_nRow;
extern int g_nCol;

void InitBackground();
void GetNewBrick();
void CombineBgBrick();
void DetachBgBrick();
int IsCanMove(int nToRow, int nToCol);
void Rotate();
int IsCanRotate();

tetris.cpp


#include "stdafx.h"
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include "model.h"
#include "view.h"


int main(int argc, char* argv[])
{
 InitBackground();
 GetNewBrick();
 CombineBgBrick();
 ShowBackground();
 DetachBgBrick();
 char chInput = 0;
 clock_t clkStart = clock();
 clock_t clkEnd = clock();
 while (1)
 {
 clkEnd = clock();
 if (clkEnd - clkStart > 1000)
 {
 clkStart = clkEnd;
 OnDown();
 }
 if (_kbhit() != 0)
 {
 chInput = _getch();
 }
 switch (chInput)
 {
 case 'a':
 OnLeft();
 break;
 case 'w':
 OnUp();
 break;
 case 's':
 OnDown();
 break;
 case 'd':
 OnRight();
 break;
 default:
 break;
 }
 chInput = 0;
 }
 return 0;
}

For more articles on Tetris, please click here to view the special topic: Tetris.


Related articles: