当前位置:网站首页>C language course design Tetris (Part 2)
C language course design Tetris (Part 2)
2022-07-26 10:10:00 【SingleDog_ seven】
In this article, we will continue to look at the recurrence process of Tetris :
Catalog
Determine if the game is over
Determine if the game is over , utilize for Cycle to judge the results of each time
// Determine if the game is over
for ( j = 1; j < COL - 1; j++)
{
if (face.data[1][j] == 1) // There are squares on the top ( By the end of 1 Behavior top level , Not the first 0 That's ok )
{
Sleep(1000); // Leave players reaction time
system("cls"); // Clear the screen
color(7); // Color set to white
CursorJump(2 * (COL / 3), ROW / 2 - 3);
if (grade>max)
{
printf(" Congratulations on breaking the record , The highest record was updated to %d", grade);
WriteGrade();
}
else if (grade == max)
{
printf(" In line with the record , Come on, make another success ", grade);
}
else
{
printf(" Please continue refueling , There is a difference between the current record and the highest record %d", max - grade);
}
CursorJump(2 * (COL / 3), ROW / 2);
printf("GAME OVER");
while (1)
{
char ch;
CursorJump(2 * (COL / 3), ROW / 2 + 3);
printf(" Another round ?(y/n):");
scanf("%c", &ch);
if (ch == 'y' || ch == 'Y')
{
system("cls");
main();
}
else if (ch == 'n' || ch == 'N')
{
CursorJump(2 * (COL / 3), ROW / 2 + 5);
exit(0);
}
else
{
CursorJump(2 * (COL / 3), ROW / 2 + 4);
printf(" Wrong choice , Please select... Again ");
}
}
}
}
return 0; // The judgment is over , There is no need to call this function to judge
}The subject of the game
In the main body of the game, it is mainly about the difficulty of the game , Game keys to write , You can also modify it in the code .
/ Game body logic function
void StartGame()
{
int i,j;
int shape = rand() % 7, form = rand() % 4; // Randomly obtain the shape and shape of the box
while (1)
{
int t = 0;
int nextShape = rand() % 7, nextForm = rand() % 4; // Randomly obtain the shape and shape of the next box
int x = COL / 2 - 2, y = 0; // The abscissa and ordinate of the initial falling position of the block
color(nextShape); // The color is set to the color of the next box
DrawBlock(nextShape, nextForm, COL + 3, 3); // Display the next box in the upper right corner
while (1)
{
color(shape); // The color is set to the square currently falling
DrawBlock(shape, form, x, y); // Display the box at the initial drop position
if (t == 0)
{
t = 15000; // here t The smaller it is , The faster the box falls ( You can set the game difficulty according to this )
}
while (--t)
{
if (kbhit() != 0) // If the keyboard is struck , Then exit the loop
break;
}
if (t == 0) // The keyboard is not tapped
{
if (IsLegal(shape, form, x, y + 1) == 0) // It's illegal for the box to fall again ( Has reached the bottom )
{
// Enter the information of the current box face among
//face: Record whether there are squares at each position of the interface , If there is a box, the color of the box at that position shall also be recorded .
for ( i = 0; i < 4; i++)
{
for ( j = 0; j < 4; j++)
{
if (block[shape][form].space[i][j] == 1)
{
face.data[y + i][x + j] = 1; // Mark the position as square
face.color[y + i][x + j] = shape; // Record the color value of the box
}
}
}
while (JudeFunc()); // Judge whether the falling of the box scores and whether the game is over
break; // Jump out of the current loop , Get ready to drop the next box
}
else // Not to the bottom
{
DrawSpace(shape, form, x, y); // Overwrite the position of the current square with a space
y++; // The ordinate increases automatically ( The next time the box is displayed, it's equivalent to falling a grid )
}
}
else // The keyboard is struck
{
char ch = getch(); // Read keycode
switch (ch)
{
case DOWN: // Direction key : Next
if (IsLegal(shape, form, x, y + 1) == 1) // Judge whether the box is legal after moving down one bit
{
// After the box falls, the following operations can be performed legally
DrawSpace(shape, form, x, y); // Overwrite the position of the current square with a space
y++; // The ordinate increases automatically ( The next time the box is displayed, it's equivalent to falling a grid )
}
break;
case LEFT: // Direction key : Left
if (IsLegal(shape, form, x - 1, y) == 1) // Judge whether the box is legal after moving one bit to the left
{
// The following operations can only be performed after the box is moved to the left
DrawSpace(shape, form, x, y); // Overwrite the position of the current square with a space
x--; // Abscissa self subtraction ( The next time the box is displayed, it is equivalent to moving one grid to the left )
}
break;
case RIGHT: // Direction key : Right
if (IsLegal(shape, form, x + 1, y) == 1) // Judge whether the square is legal after moving one bit to the right
{
// The following operations can only be performed after the box is moved to the right
DrawSpace(shape, form, x, y); // Overwrite the position of the current square with a space
x++; // Abscissa self increasing ( The next time the box is displayed, it's equivalent to moving one grid to the right )
}
break;
case SPACE: // Space bar
if (IsLegal(shape, (form + 1) % 4, x, y + 1) == 1) // Judge whether the box is legal after rotation
{
// The following operations can only be performed after the square is rotated
DrawSpace(shape, form, x, y); // Overwrite the position of the current square with a space
y++; // The ordinate increases automatically ( You can't spin in place )
form = (form + 1) % 4; // The shape of the square increases ( The next time the box is displayed, it's equivalent to rotating )
}
break;
case ESC: //Esc key
system("cls"); // Clear the screen
color(7);
CursorJump(COL, ROW / 2);
printf(" Game over ");
CursorJump(COL, ROW / 2 + 2);
exit(0); // End procedure
case 's':
case 'S': // Pause
system("pause>nul"); // Pause ( Press any key to continue )
break;
case 'r':
case 'R': // restart
system("cls"); // Clear the screen
main(); // Re execute the main function
}
}
}
shape = nextShape, form = nextForm; // Get the information of the next box
DrawSpace(nextShape, nextForm, COL + 3, 3); // Overwrite the box information in the upper right corner with a space
}
}Read from file
/ Read the highest score from the file
void ReadGrade()
{
FILE* pf = fopen(" The highest score in Tetris .txt", "r"); // Open the file read-only
if (pf == NULL) // fail to open file
{
pf = fopen(" The highest score in Tetris .txt", "w"); // Open the file in write only mode ( The file does not exist. You can create it automatically )
fwrite(&grade, sizeof(int), 1, pf); // take max write file ( here max by 0), Initialize the highest historical score to 0
}
fseek(pf, 0, SEEK_SET); // Make the file pointer pf Point to the beginning of the file
fread(&max, sizeof(int), 1, pf); // Read the highest historical score in the file to max among
fclose(pf); // Close file
pf = NULL; // The file pointer is set to null in time
}Update the highest score
// Update the highest score to the file
void WriteGrade()
{
FILE* pf = fopen(" The highest score in Tetris .txt", "w"); // Open the file in write only mode
if (pf == NULL) // fail to open file
{
printf(" Failed to save the highest score record \n");
exit(0);
}
fwrite(&grade, sizeof(int), 1, pf); // Write the game score of this game into the file ( Update the highest historical score )
fclose(pf); // Close file
pf = NULL; // The file pointer is set to null in time
}
Reading the highest score from the file and updating the highest score do not affect the integrity of the program , It's mainly about the operation of documents, and I'm not too familiar , It's the partner who completes this part .
Complete code
Next is the complete code , These codes are indeed a little too many with comments , It is presented in the form of links .
If you have any questions, please point them out . The next thing will be right opencv To study .
边栏推荐
- B站这个视频我是跪着看完的
- Logical architecture of MySQL
- Production of a-modal drag function in antui
- AirTest
- PHP executes shell script
- Study notes at the end of summer vacation
- Flask框架初学-04-flask蓝图及代码抽离
- Solve NPM -v sudden failure and no response
- 数通基础-Telnet远程管理设备
- Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
猜你喜欢

2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)

2022 zhongkepan cloud - server internal information acquisition and analysis flag

如何写一篇百万阅读量的文章

Principle analysis and source code interpretation of service discovery

新增市场竞争激烈,中国移动被迫推出限制性超低价5G套餐

Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool

MySQL 5.7.25 source code installation record

Uni app learning summary

挡不住了,纯国产PC已就位,美国的软硬件体系垄断正式被破

Server memory failure prediction can actually do this!
随机推荐
Keeping alive to realize MySQL automatic failover
数通基础-TCPIP参考模型
The diagram of user login verification process is well written!
Flutter Event 派发
Solve NPM -v sudden failure and no response
Phpexcel export Emoji symbol error
[award-winning question] ask Judea pearl, the Turing prize winner and the father of Bayesian networks
Getting started with SQL - combined tables
The fourth week of summer vacation
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
Data communication foundation TCPIP reference model
Node memory overflow and V8 garbage collection mechanism
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
Nodejs service background execution (forever)
Explain automatic packing and unpacking?
spolicy请求案例
Flask框架初学-03-模板
苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收
在同一conda环境下先装Pytroch后装TensorFlow
SSG framework Gatsby accesses the database and displays it on the page