• C语言小游戏---命令行愤怒的小鸟


    #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;
    }

    从下面的截图也能看出来数组中内容是依次变化

  • 相关阅读:
    一道面试题引发的对JavaScript类型转换的思考
    微信后台开发第一步:nodeJS+express接入微信后台详细教程
    class命名
    了解真实的『REM』手机屏幕适配
    js刷新框架子页面的七种方法代码
    移动前端开发之viewport的深入理解
    移动web点5像素的秘密
    refactor window_x64微信小程序环境搭建
    JSON API免费接口
    webpack
  • 原文地址:https://www.cnblogs.com/lely/p/12188155.html
Copyright © 2020-2023  润新知