Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Today with the simplest C language to make a black and white interface pixel bird. Pixel bird game is a bird in the sky non, avoiding the wall. You lose when you hit the wall. Running past a wall adds one point.

A good idea is shown below

for(i = 0; i < 11; i++) { for(j = 0; j < 80; j++) { backGround[i][j] = ' '; } } for(i = 0; i < 8; I ++) {strcat(backGround[I], "viewpoint "); } for(i = 0; i < 8; I ++) {strcat(backGround[I], "┤│ "); } for(i = 0; i < 8; I ++) {strcat(backGround[I], "├ ─ "); } puts(backGround[1]);Copy the code

The first thing you need to do is draw the background floors, which are basically the columns behind them, without which the game would be very monotonous.

	printf("*@>");
Copy the code

Then the bird replaces it with the string above.

PIPE *Pipefun(PIPE *h) { int i = 0, j = 0; PIPE *p; for(p = h, i = 0; i<6; i++) { for(j = 0; j<14; j++) { if( j == p->y-1 || j == p->y || j == p->y+1 ) continue; The else {the if (p - > > x = 0 & p - > x < 80) gotoxy (p - > x, j), printf (" s "); If (p - > x + 2 > = 0 && - > x + 2 p < 80) gotoxy (p - > x + 2, j), printf (" s "); if(p->x+4 >= 0 && p->x+4 < 80) gotoxy(p->x+4, j), printf("%c%c",backGround[j][p->x+4],backGround[j][p->x+5]); } } p->x = p->x-2; p = p->next ; } if(h->x < -4) { h->x = 78+12; h->y = rand()%9+3; h = h->next; } return h; }Copy the code

The above code is to draw the pipe wall with ■ characters, and randomly out the position.

while(1) { Time++; temp = bird; if(kbhit()) ch=getch(); ch == ' ' ? bird-- : bird++; ch = '0'; if(bird < 0 || bird >= 14) break; gotoxy(18, temp); printf(" %c%c", backGround[temp][20], backGround[temp][21]); gotoxy(18, bird); printf("*@>"); if(Time%3 == 1) h = Pipefun(h); for(p = h, i = 0; i<6; i++, p = p->next) if(p->x == 18 && p->y-1 ! = bird && p->y ! = bird && p->y+1 ! = bird) break; if(p->x == 18 && p->y-1 ! = bird && p->y ! = bird && p->y+1 ! = bird) break; Sleep(300); }Copy the code

Finally, there is the operation logic, which is the bird position operation. This code is inside the while(1) loop, when the space bar is pressed migratory birds will go up, otherwise they will all fall down. There’s also a Sleep(300) that controls the response time of the game.

You are welcome to discuss procedural questions with me and answer any questions you may have. Follow the public number: poetic code, make a friend.