当前位置:网站首页>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 .
边栏推荐
- Error in render: "typeerror: cannot read properties of undefined (reading 'length')" --- error when calling interface
- What is the principle of reflection mechanism?
- 面试突击68:为什么 TCP 需要 3 次握手?
- PMM (percona monitoring and management) installation record
- [MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
- Wechat applet learning notes 1
- Sqoop [put it into practice 02] sqoop latest version full database import + data filtering + field type support description and example code (query parameter and field type forced conversion)
- Phpexcel export Emoji symbol error
- 云原生(三十六) | Kubernetes篇之Harbor入门和安装
- Under win10 64 bit, matlab fails to configure notebook
猜你喜欢

Uni app learning summary

Principle analysis and source code interpretation of service discovery

2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
![[award-winning question] ask Judea pearl, the Turing prize winner and the father of Bayesian networks](/img/0f/01d6e49fff80a325b667784e40bff3.png)
[award-winning question] ask Judea pearl, the Turing prize winner and the father of Bayesian networks

regular expression

论文笔记(SESSION-BASED RECOMMENDATIONS WITHRECURRENT NEURAL NETWORKS)

SQL优化的魅力!从 30248s 到 0.001s

The charm of SQL optimization! From 30248s to 0.001s

Flask框架初学-04-flask蓝图及代码抽离

Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration
随机推荐
Flutter event distribution
Map key not configured and uniapp routing configuration and jump are reported by the uniapp < map >< /map > component
Strange Towers of Hanoi|汉诺塔4柱问题
Explain automatic packing and unpacking?
Yarn 'TSC' is not an internal or external command, nor is it a runnable program or batch file. The problem that the command cannot be found after installing the global package
WARNING: [pool www] server reached pm. max_ children setting (5), consider raising it
SSG框架Gatsby访问数据库,并显示到页面上
Use of selectors
Production of a-modal drag function in antui
Why does new public chain Aptos meet market expectations?
数通基础-STP原理
Applet record
Mysql5.7.25 master-slave replication (one-way)
Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool
网易云UI模仿--&gt;侧边栏
The charm of SQL optimization! From 30248s to 0.001s
PHP one-time request lifecycle
Opencv image processing
Study notes of the second week of sophomore year
Logical architecture of MySQL