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

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

  • 相关阅读:
    bzoj 1257: [CQOI2007]余数之和sum 数论
    codevs 1063 合并果子 STL 优先队列
    HTTP错误code大全
    URL中的特殊字符处理笔记
    单例中懒汉和饿汉的本质区别
    关于静态方法的使用方式
    111
    WebService 简单安全验证
    WebService安全解决方案—简单握手协议
    RESTEasy使用json返回的例子
  • 原文地址:https://www.cnblogs.com/lely/p/12188155.html
Copyright © 2011-2022 走看看