当前位置:网站首页>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 )
边栏推荐
- Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
- 服务发现原理分析与源码解读
- The charm of SQL optimization! From 30248s to 0.001s
- Common errors when starting projects in uniapp ---appid
- 【有奖提问】向图灵奖得主、贝叶斯网络之父 Judea Pearl 提问啦
- Wechat applet learning notes 1
- Time series anomaly detection
- R language ggpubr package ggsummarystats function visualizes the grouping box diagram (custom grouping color) and adds the statistical values corresponding to the grouping under the x-axis label (samp
- 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)
挡不住了,纯国产PC已就位,美国的软硬件体系垄断正式被破
Production of a-modal drag function in antui
Customize permission validation in blazor
Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
MySQL 5.7.25 source code installation record
Flask framework beginner-03-template
Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
Development to testing: a six-year road to automation starting from 0
Meeting OA project (III) -- my meeting (meeting seating and submission for approval)
随机推荐
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
Development to testing: a six-year road to automation starting from 0
Use of selectors
Encapsulation of tabbarcontroller
R language ggpubr package ggsummarystats function visualizes the grouping box diagram (custom grouping color) and adds the statistical values corresponding to the grouping under the x-axis label (samp
Formwork (III)
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
Customize permission validation in blazor
Tableviewcell highly adaptive
Opencv image processing
数通基础-二层交换原理
Study notes at the end of summer vacation
spolicy请求案例
Learning notes: what are the common array APIs that change the original array or do not change the original array?
Fuzzy PID control of motor speed
Solution of inputting whole line string after inputting integer
Explain automatic packing and unpacking?
Study notes of the second week of sophomore year
Wechat H5 payment on WAP, for non wechat browsers
新公链Aptos何以拉满市场期待值?