zoukankan      html  css  js  c++  java
  • 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;
    }

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

  • 相关阅读:
    地图篇-02.地理编码
    地图篇-01.获取用户位置
    新手教程之使用Xib自定义UITableViewCell
    封装
    NSDate简单介绍
    OC知识点归纳
    Xcode的控制台调试命令
    [开发笔记]UIApplication介绍
    技术分享-开发利器block底层实现
    技术分享-开发利器block
  • 原文地址:https://www.cnblogs.com/lely/p/12188155.html
Copyright © 2011-2022 走看看