#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<windows.h> int high, width;//windows int bird_x, bird_y; // bird position int bar_y, bar_minx, bar_maxx; // bar position int score; void HideCursor() { // if cursor CONSOLE_CURSOR_INFO cursor_info= {1,0}; //second value is 0 mean hide cursor SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); } void gotoxy(int x, int y) { // move mouse to x,y position, similer clear screen HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle, pos); } void startup() { // init data high = 18; width = 25; bird_x = high / 2; bird_y = width / 3; bar_y = width; bar_minx = high / 4; bar_maxx = high / 2; score = 0; HideCursor(); } void show() { // show windows gotoxy(0,0); //move mouse to 0,0 origin point, and redraw screen int i, j; // system("cls"); //clear screen for(i=0; i<=high; i++) { for(j=0; j<=width; j++) { if(i==bird_x && j==bird_y) printf("@"); //show bird * else if(j==bar_y &&(i<bar_minx || i>bar_maxx)) printf("|"); else printf(" "); //show nothing } printf(" "); } printf("score:%d ",score); } void updateWithoutInput() { if(bird_y==bar_y) { if(bird_x>bar_minx && bird_x<bar_maxx) score++; else exit(0); } bird_x++; if(bar_y>0) // move bar bar_y--; else { bar_y = width; // new bar int randPosition = rand() % (high-5); bar_minx = randPosition; bar_maxx = randPosition + high/4 ; } Sleep(150); } void updateWithInput() { char input; if(kbhit()) { //runing while user push keyboard input = getch(); if(input == ' ') { bird_x = bird_x - 2; } } } int main() { startup(); // init data while(1) { // game loop run show(); // show windows updateWithoutInput(); //update don't need user updateWithInput(); //update need user } return 0; }
从下面的截图也能看出来数组中内容是依次变化