当前位置:网站首页>C language course design Tetris (Part 1)
C language course design Tetris (Part 1)
2022-07-26 10:06:00 【SingleDog_ seven】
Tetris is sure to be familiar to everyone , Next we use C Words come and go :
Use the following header function :
#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>First, we define some global variables and structures :
#define ROW 29 // Number of lines in the game area
#define COL 20 // Number of columns in the game area
#define DOWN 80 // Direction key : Next
#define LEFT 75 // Direction key : Left
#define RIGHT 77 // Direction key : Right
#define SPACE 32 // Space bar
#define ESC 27 //Esc key
struct Face
{
int data[ROW][COL + 10]; // Used to mark whether there is a square at the specified position (1 To have ,0 For nothing )
int color[ROW][COL + 10]; // Used to record the square color code of the specified position
}face;
struct Block
{
int space[4][4];
}block[7][4]; // Used to store 7 Each of the basic shape blocks 4 Two forms of information , common 28 Kind of
int max, grade; // Global variables Still use the idea of modules to solve problems :
// hide cursor
void HideCursor();
// The cursor jumps
void CursorJump(int x, int y);
// Initialization interface
void InitInterface();
// Initialization block information
void InitBlockInfo();
// color setting
void color(int num);
// Draw a square
void DrawBlock(int shape, int form, int x, int y);
// Space overlay
void DrawSpace(int shape, int form, int x, int y);
// Legitimacy judgment
int IsLegal(int shape, int form, int x, int y);
// Judge the score and end
int JudeFunc();
// Game body logic function
void StartGame();
// Read the highest score from the file
void ReadGrade();
// Update the highest score to the file
void WriteGrade();Next is the content of each module :
The first is to hide the cursor
// hide cursor
void HideCursor()
{
CONSOLE_CURSOR_INFO curInfo; // Structure variables that define cursor information
curInfo.dwSize = 1; // If there is no assignment , Invalid hidden cursor
curInfo.bVisible = FALSE; // Set the cursor to invisible
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); // Get the console handle
SetConsoleCursorInfo(handle, &curInfo); // Set cursor information
}Next is the operation of cursor jump
// The cursor jumps
void CursorJump(int x, int y)
{
COORD pos; // Structure variables that define the cursor position
pos.X = x; // Abscissa setting
pos.Y = y; // Ordinate setting
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); // Get the console handle
SetConsoleCursorPosition(handle, pos); // Set cursor position
}Then the initialization page :
// Initialization interface
void InitInterface()
{
color(7); // Color set to white
int i,j;
for(i=0;i< ROW;i++)
{
int j;
for ( j = 0; j < COL + 10; j++)
{
if (j == 0 || j == COL - 1 || j == COL + 9)
{
face.data[i][j] = 1; // Mark the position with a square
CursorJump(2 * j, i);
printf("■");
}
else if (i == ROW - 1)
{
face.data[i][j] = 1; // Mark the position with a square
printf("■");
}
else
face.data[i][j] = 0; // Mark that there is no square in this position
}
}
for (i = COL; i < COL + 10; i++)
{
face.data[8][i] = 1; // Mark the position with a square
CursorJump(2 * i, 8);
printf("■");
}
CursorJump(2 * COL, 1);
printf(" Next block :");
CursorJump(2 * COL + 4, ROW - 19);
printf(" Move left :←");
CursorJump(2 * COL + 4, ROW - 17);
printf(" Move right :→");
CursorJump(2 * COL + 4, ROW - 15);
printf(" Speed up :↓");
CursorJump(2 * COL + 4, ROW - 13);
printf(" rotate : Space ");
CursorJump(2 * COL + 4, ROW - 11);
printf(" Pause : S");
CursorJump(2 * COL + 4, ROW - 9);
printf(" sign out : Esc");
CursorJump(2 * COL + 4, ROW - 7);
printf(" restart :R");
CursorJump(2 * COL + 4, ROW - 5);
printf(" The highest record :%d", max);
CursorJump(2 * COL + 4, ROW - 3);
printf(" Current score :%d", grade);
}Then the initialization block information :
// Initialization block information
void InitBlockInfo()
{
int i,j,shape,form;
//“T” shape
for ( i = 0; i <= 2; i++)
block[0][0].space[1][i] = 1;
block[0][0].space[2][1] = 1;
//“L” shape
for ( i = 1; i <= 3; i++)
block[1][0].space[i][1] = 1;
block[1][0].space[3][2] = 1;
//“J” shape
for ( i = 1; i <= 3; i++)
block[2][0].space[i][2] = 1;
block[2][0].space[3][1] = 1;
for ( i = 0; i <= 1; i++)
{
//“Z” shape
block[3][0].space[1][i] = 1;
block[3][0].space[2][i + 1] = 1;
//“S” shape
block[4][0].space[1][i + 1] = 1;
block[4][0].space[2][i] = 1;
//“O” shape
block[5][0].space[1][i + 1] = 1;
block[5][0].space[2][i + 1] = 1;
}
//“I” shape
for ( i = 0; i <= 3; i++)
block[6][0].space[i][1] = 1;
int temp[4][4];
for ( shape = 0; shape < 7; shape++) //7 Species shape
{
for ( form = 0; form < 3; form++) //4 Form ( There is already a kind of , Each one here needs to be added 3 Kind of )
{
// For the first form Form
for ( i = 0; i < 4; i++)
{
for ( j = 0; j < 4; j++)
{
temp[i][j] = block[shape][form].space[i][j];
}
}
// Will be the first form One form rotates clockwise , Get the first form+1 Form
for ( i = 0; i < 4; i++)
{
for ( j = 0; j < 4; j++)
{
block[shape][form + 1].space[i][j] = temp[3 - j][i];
}
}
}
}
}Then there is the color setting of the square :
// color setting
void color(int c)
{
switch (c)
{
case 0:
c = 13; //“T” The square is set to purple
break;
case 1:
case 2:
c = 12; //“L” Xinghe “J” The square is set to red
break;
case 3:
case 4:
c = 10; //“Z” Xinghe “S” The square is set to green
break;
case 5:
c = 14; //“O” The square is set to yellow
break;
case 6:
c = 11; //“I” The square is set to light blue
break;
default:
c = 7; // Other default settings are white
break;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); // color setting
// notes :SetConsoleTextAttribute It's a API( Application programming interface )
}Color can be found on Baidu , I won't do too much narration here .
Draw the contents of the square :
// Draw a square
void DrawBlock(int shape, int form, int x, int y)
{
int i,j;
for ( i = 0; i < 4; i++)
{
for ( j = 0; j < 4; j++)
{
if (block[shape][form].space[i][j] == 1) // If there is a square in this position
{
CursorJump(2 * (x + j), y + i); // The cursor jumps to the specified position
printf("■"); // Output block
}
}
}
}The space covers the content of the part :
// Space overlay
void DrawSpace(int shape, int form, int x, int y)
{
int i,j;
for ( i = 0; i < 4; i++)
{
for ( j = 0; j < 4; j++)
{
if (block[shape][form].space[i][j] == 1) // If there is a square in this position
{
CursorJump(2 * (x + j), y + i); // The cursor jumps to the specified position
printf(" "); // Print space overlay ( Two spaces )
}
}
}
}Part of the legitimacy judgment :
// Legitimacy judgment
int IsLegal(int shape, int form, int x, int y)
{
int i,j;
for ( i = 0; i < 4; i++)
{
for ( j = 0; j < 4; j++)
{
// If the box falls, there will already be a box , It is illegal.
if ((block[shape][form].space[i][j] == 1) && (face.data[y + i][x + j] == 1))
return 0; // illegal
}
}
return 1; // legal This is the last part of today : Judge the score and end :
// Judge the score and end
int JudeFunc()
{
// Judge whether to score
int i,j,m,n;
for ( i = ROW - 2; i > 4; i--)
{
int sum = 0; // Record No. i Number of squares in a row
for ( j = 1; j < COL - 1; j++)
{
sum += face.data[i][j]; // Statistics No i Number of squares in a row
}
if (sum == 0) // There are no squares in this line , There is no need to judge the level above it ( There is no need to continue to judge whether to score )
break; // Out of the loop
if (sum == COL - 2) // The line is full of squares , Can score
{
grade += 10; // Add... To the full line 10 branch
color(7); // Color set to white
CursorJump(2 * COL + 4, ROW - 3); // The cursor jumps to the position where the current score is displayed
printf(" Current score :%d", grade); // Update current score
for ( j = 1; j < COL - 1; j++) // Clear the block information of the obtained Branch
{
face.data[i][j] = 0; // This position is cleared after scoring , Mark as no square
CursorJump(2 * j, i); // The cursor jumps to this position
printf(" "); // Print space overlay ( Two spaces )
}
// Move the entire row above the cleared row down one grid
for (m = i; m >1; m--)
{
sum = 0; // Record the number of squares in the previous line
for ( n = 1; n < COL - 1; n++)
{
sum += face.data[m - 1][n]; // Count the number of squares in the previous line
face.data[m][n] = face.data[m - 1][n]; // Move the identification of the box on the previous line to the next line
face.color[m][n] = face.color[m - 1][n]; // Move the color number of the box on the previous line to the next line
if (face.data[m][n] == 1) // Moving down the previous line is the box , Print box
{
CursorJump(2 * n, m); // The cursor jumps to this position
color(face.color[m][n]); // The color is set to the color of the square
printf("■"); // Print box
}
else // The space moved down from the previous line , Print space
{
CursorJump(2 * n, m); // The cursor jumps to this position
printf(" "); // Print space ( Two spaces )
}
}
if (sum == 0) // All the spaces moved down from the previous line , There is no need to move the upper box down ( End of move )
return 1; // return 1, Indicates that the function needs to be called for judgment ( There may be a full line of moving down )
}
}
}This is the content reproduced at present , I will complete other contents next time , And write all the code .
( Most of the notes also explain in detail )
边栏推荐
- Show default image when wechat applet image cannot be displayed
- Map key not configured and uniapp routing configuration and jump are reported by the uniapp < map >< /map > component
- Applet record
- Uni app learning summary
- regular expression
- Node memory overflow and V8 garbage collection mechanism
- Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
- 新增市场竞争激烈,中国移动被迫推出限制性超低价5G套餐
- JS table auto cycle scrolling, mouse move in pause
- 网易云UI模仿--&gt;侧边栏
猜你喜欢

Like, "new programmer" e-book is free for a limited time!

Keeping alive to realize MySQL automatic failover

Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market

Customize permission validation in blazor

Session based recommendations with recurrent neural networks

Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install

Solve NPM -v sudden failure and no response

Flask框架初学-03-模板

R language ggplot2 visualization: align the legend title to the middle of the legend box in ggplot2 (default left alignment, align legend title to middle of legend)

Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
随机推荐
Leetcode 504. 七进制数
Data communication foundation TCPIP reference model
Customize permission validation in blazor
Interpretation of the standard of software programming level examination for teenagers_ second level
2022 zhongkepan cloud - server internal information acquisition and analysis flag
Principle analysis and source code interpretation of service discovery
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
PHP one-time request lifecycle
Draw arrows with openlayer
分布式网络通信框架:本地服务怎么发布成RPC服务
Jpg to EPS
Tableviewcell highly adaptive
The use of MySQL in nodejs
Distributed network communication framework: how to publish local services into RPC services
Interview shock 68: why does TCP need three handshakes?
Solve NPM -v sudden failure and no response
【有奖提问】向图灵奖得主、贝叶斯网络之父 Judea Pearl 提问啦
Transform between tree and array in JS (hide the children field if the child node of the tree is empty)
Wechat applet learning notes 1
在同一conda环境下先装Pytroch后装TensorFlow