当前位置:网站首页>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 .
边栏推荐
猜你喜欢
El table implements adding / deleting rows, and a parameter changes accordingly
数通基础-二层交换原理
Installation and use of cocoapods
Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration
Applet record
Basic usage of protobuf
30 minutes to thoroughly understand the synchronized lock upgrade process
Server memory failure prediction can actually do this!
AirTest
Wechat applet learning notes 1
随机推荐
Li Kou - binary tree pruning
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
分布式网络通信框架:本地服务怎么发布成RPC服务
Uniapp error 7 < Map >: marker ID should be a number
2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
PHP one-time request lifecycle
如何写一篇百万阅读量的文章
数通基础-STP原理
Beginner of flask framework-04-flask blueprint and code separation
Encapsulation of tabbarcontroller
SQL Server 2008 server engine failed to start?
JS judge the data types object.prototype.tostring.call and typeof
数通基础-网络基础知识
Set view dynamic picture
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
Like, "new programmer" e-book is free for a limited time!
编写一个在bash / shell 和 PowerShell中均可运行的脚本
汉诺塔II|汉诺塔4柱
What is the principle of reflection mechanism?
时间序列异常检测