当前位置:网站首页>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 .
边栏推荐
- Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
- Solve NPM -v sudden failure and no response
- SQL Server 2008 R2 installation problems
- Uni app learning summary
- Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
- Error in render: "typeerror: cannot read properties of undefined (reading 'length')" --- error when calling interface
- I finished watching this video on my knees at station B
- [datawhale] [machine learning] Diabetes genetic risk detection challenge
- Why does new public chain Aptos meet market expectations?
- Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
猜你喜欢

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

PMM (percona monitoring and management) installation record

Introduction to latex, EPS picture bounding box

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

SSG框架Gatsby访问数据库,并显示到页面上

Beginner of flask framework-04-flask blueprint and code separation

Transform between tree and array in JS (hide the children field if the child node of the tree is empty)

Flask框架初学-03-模板

点赞,《新程序员》电子书限时免费领啦!

Mysql5.7.25 master-slave replication (one-way)
随机推荐
JS judge the data types object.prototype.tostring.call and typeof
PMM (percona monitoring and management) installation record
数通基础-网络基础知识
[datawhale] [machine learning] Diabetes genetic risk detection challenge
Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
Rocky basic exercise -shell script 2
Data communication foundation TCPIP reference model
解释一下自动装箱和自动拆箱?
数通基础-TCPIP参考模型
The charm of SQL optimization! From 30248s to 0.001s
Azkaban【基础知识 01】核心概念+特点+Web界面+架构+Job类型(一篇即可入门Azkaban工作流调度系统)
Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication
Study notes of the third week of sophomore year
Spolicy request case
Solution of inputting whole line string after inputting integer
Tower of Hanoi II | tower of Hanoi 4 columns
Interpretation of the standard of software programming level examination for teenagers_ second level
Nodejs service background execution (forever)
30 minutes to thoroughly understand the synchronized lock upgrade process