C Language course set to a class, the teacher provides a C language Tetris game let us feel, this semester C language course, mainly a variety of sentence practice, this time is to use the knowledge to feel a real system.
First install the C-free, then open the teacher sent us the small program.
Interface is very simple, did not expect C language can also do so interesting things, really did not think.
Analysis of the program, feeling more complex, but this semester learning the combination of simple statements, the use of such a magic.
1. First is the statement that appears on the screen printf
2. Use the For statement to create a window
for (i=2;i<2*frame_width-2;i+=2)
{
Gotoxy (Hout,framex+i,framey);
printf ("═"); Print up a horizontal frame
}
for (i=2;i<2*frame_width-2;i+=2)
{
Gotoxy (Hout,framex+i,framey+frame_height);
printf ("═"); Print the bottom horizontal frame
a[framex+i][framey+frame_height]=2; Remember, the bottom frame has a pattern.
}
for (i=1;i<frame_height;i++)
{
Gotoxy (Hout,framex,framey+i);
printf ("║"); Print left vertical frame
a[framex][framey+i]=2; Remember the left vertical frame has a pattern
}
for (i=1;i<frame_height;i++)
{
Gotoxy (Hout,framex+2*frame_width-2,framey+i);
printf ("║"); Print Right Vertical frame
a[framex+2*frame_width-2][framey+i]=2; Remember the right vertical frame has a pattern
}
3. Make Tetris and rotate it
void Make_tetris (struct Tetris *tetris)
{
a[tetris->x][tetris->y]=b[0]; Graph State of Center block position: 1-there, 0-None
Switch (Tetris->flag)//A total of 6 categories, 19 types
{
Case 1://tin Word Box
{
a[tetris->x][tetris->y-1]=b[1];
a[tetris->x+2][tetris->y-1]=b[2];
a[tetris->x+2][tetris->y]=b[3];
Break
}
Case 2://Straight BLOCK:----
{
a[tetris->x-2][tetris->y]=b[1];
a[tetris->x+2][tetris->y]=b[2];
a[tetris->x+4][tetris->y]=b[3];
Break
}
Case 3://Straight BLOCK: |
{
a[tetris->x][tetris->y-1]=b[1];
a[tetris->x][tetris->y-2]=b[2];
a[tetris->x][tetris->y+1]=b[3];
Break
}
Case 4://t Word box
{
a[tetris->x-2][tetris->y]=b[1];
a[tetris->x+2][tetris->y]=b[2];
a[tetris->x][tetris->y+1]=b[3];
Break
}
Case 5://t word clockwise 90-degree block
{
a[tetris->x][tetris->y-1]=b[1];
a[tetris->x][tetris->y+1]=b[2];
a[tetris->x-2][tetris->y]=b[3];
Break
}
Case 6://t word clockwise 180-degree block
{
a[tetris->x][tetris->y-1]=b[1];
a[tetris->x-2][tetris->y]=b[2];
a[tetris->x+2][tetris->y]=b[3];
Break
}
Case 7://t word clockwise 270-degree block
{
a[tetris->x][tetris->y-1]=b[1];
a[tetris->x][tetris->y+1]=b[2];
a[tetris->x+2][tetris->y]=b[3];
Break
}
Case 8://z Word Box
{
a[tetris->x][tetris->y+1]=b[1];
a[tetris->x-2][tetris->y]=b[2];
a[tetris->x+2][tetris->y+1]=b[3];
Break
}
Case 9://z word clockwise 90-degree block
{
a[tetris->x][tetris->y-1]=b[1];
a[tetris->x-2][tetris->y]=b[2];
a[tetris->x-2][tetris->y+1]=b[3];
Break
}
Case://z word clockwise turn 180 degree block
{
a[tetris->x][tetris->y-1]=b[1];
a[tetris->x-2][tetris->y-1]=b[2];
a[tetris->x+2][tetris->y]=b[3];
Break
}
Case one://z word clockwise turn 270 degree block
{
a[tetris->x][tetris->y+1]=b[1];
a[tetris->x+2][tetris->y-1]=b[2];
a[tetris->x+2][tetris->y]=b[3];
Break
}
Case 12://7 Word Box
{
a[tetris->x][tetris->y-1]=b[1];
a[tetris->x][tetris->y+1]=b[2];
a[tetris->x-2][tetris->y-1]=b[3];
Break
}
Case 13://7 word clockwise 90-degree block
{
a[tetris->x-2][tetris->y]=b[1];
a[tetris->x-2][tetris->y+1]=b[2];
a[tetris->x+2][tetris->y]=b[3];
Break
}
Case 14://7 word clockwise 180-degree block
{
a[tetris->x][tetris->y-1]=b[1];
a[tetris->x][tetris->y+1]=b[2];
a[tetris->x+2][tetris->y+1]=b[3];
Break
}
Case 15://7 word clockwise 270-degree block
{
a[tetris->x-2][tetris->y]=b[1];
a[tetris->x+2][tetris->y-1]=b[2];
a[tetris->x+2][tetris->y]=b[3];
Break
}
Case 16://pour 7 word blocks
{
a[tetris->x][tetris->y+1]=b[1];
a[tetris->x][tetris->y-1]=b[2];
a[tetris->x+2][tetris->y-1]=b[3];
Break
}
Case 17://Inverted 7-character clockwise to 90-degree block
{
a[tetris->x-2][tetris->y]=b[1];
a[tetris->x-2][tetris->y-1]=b[2];
a[tetris->x+2][tetris->y]=b[3];
Break
}
Case 18://pour 7 words clockwise to 180 degree block
{
a[tetris->x][tetris->y-1]=b[1];
a[tetris->x][tetris->y+1]=b[2];
a[tetris->x-2][tetris->y+1]=b[3];
Break
}
Case 19://pour 7 words clockwise to 270 degree block
{
a[tetris->x-2][tetris->y]=b[1];
a[tetris->x+2][tetris->y+1]=b[2];
a[tetris->x+2][tetris->y]=b[3];
Break
}
}
4, determine whether full line and delete the full line of Tetris
void Del_full (HANDLE hout,struct Tetris *tetris)
{//When a row has a frame_width-2 block, the line is full
int k,del_count=0; Variables to record the number of blocks in a row and the number of rows to delete a block
for (j=framey+frame_height-1;j>=framey+1;j--)
{
I=r;
for (i=framex+2;i<framex+2*frame_width-2;i+=2)
{
if (a[i][j]==1)//vertical coordinates from bottom to top, horizontal axis in turn from left to right to determine whether full line
{
k++; Record the number of this row of squares
if (k==frame_width-2)
{
for (k=framex+2;k<framex+2*frame_width-2;k+=2)
{//delete full rows of squares
a[k][j]=0;
Gotoxy (HOUT,K,J);
printf ("");
Sleep (1);
}
for (k=j-1;k>framey;k--)
{//If there is a block above the deleted row, clear it first and then move the block down one position
for (i=framex+2;i<framex+2*frame_width-2;i+=2)
{
if (a[i][k]==1)
{
a[i][k]=0;
Gotoxy (HOUT,I,K);
printf ("");
A[i][k+1]=1;
Gotoxy (hout,i,k+1);
printf ("-");
}
}
}
j + +; After the block moves down, you can re-determine if the deleted row is full.
del_count++; Record the number of rows to delete a block
}
}
}
}
tetris->score+=100*del_count; 100 points for each deleted row
if (del_count>0 && (tetris->score%1000==0 | | tetris->score/1000>tetris->level-1))
{//If you have 1000 points to accumulate 10 lines, speed up 20ms and raise one level
tetris->speed-=20;
tetris->level++;
}
}
5 Experience
1, C language Game program can see, C language is our first programming language, from the computer does not have any programming ability rookie, to now can read C language program, feel oneself in slowly progress.
2, the computer application system, is continuously receives the user input, then according to the corresponding logic, displays the corresponding content the process.
3, according to this idea, I can change the program into a game to fly, control aircraft around certain, that is, my plane can not only run below, but also run around, this as long as the limit of my aircraft in 21 lines of the restrictions to cancel on it.
This article from "11629612" blog, declined reprint!
Tetris Game: The initial experience of C programming