0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
/** * 作者:Kola * 链接:http://www.zhihu.com/question/22715390/answer/34294761 * 来源:知乎 * 著作权归作者所有,转载请联系作者获得授权。 */ #include"stdio.h" #include"windows.h" #include"string.h" #include"conio.h" #include"time.h" typedef struct pipe { int x; int y; struct pipe *next; }PIPE; char backGround[14][80]={0}; int Time=0; unsigned int Score=0; void HideCursor() { CONSOLE_CURSOR_INFO cursor_info={1,0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); } void gotoxy(int x, int y) { COORD coord; coord.X=x; coord.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); } void Getready() { int i=0, j=0; for(i=0; i<11; i++) for(j=0;j<80;j++) backGround[i][j]=' '; for(i=0; i<8; i++) strcat(backGround," ┌┐ "); for(i=0; i<8; i++) strcat(backGround,"┌┤│ "); for(i=0; i<8; i++) strcat(backGround,"││├┐ "); puts(backGround); gotoxy(30, 3); printf("G e t R e a d y"); gotoxy(0, 14); for(i=0; i < 40; i++) printf("▁"); for(i=0; i < 11; i++) printf(" ╱╱ "); gotoxy(0, 16); for(i=0; i < 40; i++) printf("▔"); gotoxy(18, 3); printf("*@>"); getch(); gotoxy(30, 3); printf(" "); } 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; else { if(p->x >= 0 && p->x < 80) gotoxy(p->x, j),printf("■"); if(p->x+2>=0 && p->x+2<80) gotoxy(p->x+2, j),printf("■"); 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; } void Bird() { int i=0,bird=3,temp=0; char ch='0'; PIPE *h,*p,*q; p=h=q=(PIPE *)malloc(sizeof(PIPE)); for(i=0; i<5; i++) { p->x=78+16*i; p->y=rand()%9+3; q=(PIPE *)malloc(sizeof(PIPE)); p->next=q; q->next=NULL; p=q; } p->x=78+16*i; p->y=rand()%13+1; q->next=h; 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); }//end of while }//end of Bird void Gameover() { Score=(Time-88)/24<0?0:(Time-88)/24; gotoxy(28, 5); printf("┌──────────┐"); gotoxy(28, 6); printf("│ G A M E O V E R │"); gotoxy(28, 7); printf("│ │"); gotoxy(28, 8); printf("│ %4d │",Score); gotoxy(28, 9); printf("└──────────┘"); }//end of Gameover int main() { HideCursor(); system("title Flappy Bird for c"); system("mode con cols=80 lines=20"); srand((unsigned)time(NULL)); Getready(); Bird(); Gameover(); getch(); } |
0 Comments