C语言小游戏---命令行飞船大战

执行源文件的时候有点问题,建工程然后执行就没问题,不知为啥,难道是编译器问题?

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>

#define High 15        //windows dimention
#define Width 25
#define EnemyNum 5  //enemy flight num

int canves[High][Width]= {0}; //windows canves ,0 white, 1 flight, 2 bullet,3 enemy_flight
int position_x, position_y;  //flight position
int enemy_x[EnemyNum], enemy_y[EnemyNum];         //enemy position

int score;                    //score
int BulletWidth;            //bullet width
int EnemyMoveSpeed;         //enemy move speed

//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
    position_x = High-1;
    position_y = Width/2;
    canves[position_x][position_y] = 1;
    int k;
    for(k=0; k<EnemyNum; k++) {
        enemy_x[k] = rand() % 2;
        enemy_y[k] = rand() % Width;
        canves[enemy_x[k]][enemy_y[k]] = 3;
    }
    BulletWidth = 0;
    score = 0;
    EnemyMoveSpeed = 20;
//    HideCursor();
}

void show() { // show windows
    gotoxy(0,0);  //move mouse to 0,0 origin point, and redraw screen
    int i, j;
    for(i=0; i<High; i++) {
        for(j=0; j<Width; j++) {
            if(canves[i][j] == 0)
                printf(" ");  //show bird *
            else if(canves[i][j] == 1)
                printf("*");
            else if(canves[i][j] == 2)
                printf("|");
            else if(canves[i][j] == 3)
                printf("@");  //show nothing
        }
        printf("\n");
    }
    printf("score:%3d\n",score);
}

void updateWithoutInput() {
    int i, j, k;
    for(i=0; i<High; i++) {
        for(j=0; j<Width; j++) {
            if(canves[i][j] == 2) {
                //if fight enemy flight
                for(k=0; k<EnemyNum; k++) {
                    //bullet hit enemy flight
                    if((i == enemy_x[k]) && (j == enemy_y[k])) {
                        score++;
                        if(score%5==0 && EnemyMoveSpeed>3)//score control enemy flight speed become more quickly
                            EnemyMoveSpeed--;
                        if(score%5==0)//score control bullet become more powerful
                            BulletWidth++;
                        canves[enemy_x[k]][enemy_y[k]] = 0;
                        enemy_x[k] = rand() % 2;
                        enemy_y[k] = rand() % Width;
                        canves[enemy_x[k]][enemy_y[k]] = 3;
//                        canves[i][j] = 0;
                    }
                }
                // bullet move up
                canves[i][j] = 0;
                if(i>0)
                    canves[i-1][j] = 2;
            }
        }
    }
    static int speed = 0;
    if(speed<EnemyMoveSpeed)
        speed++;

    for(k=0; k<EnemyNum; k++) {
        if((position_x==enemy_x[k]) && (position_y==enemy_y[k])) { //died,enemy flight meet user flight
            printf("GAME OVER!");
            Sleep(3000);
            system("pause");
            exit(0);
        }
        if(enemy_x[k] > High) { //enemy over screen high size
            canves[enemy_x[k]][enemy_y[k]] = 0;
            enemy_x[k] = rand() % 2;
            enemy_y[k] = rand() % Width;
            canves[enemy_x[k]][enemy_y[k]] = 3;
            score--;
        }
        if(speed == EnemyMoveSpeed) {
            //enemy flight auto falling down
            for(k=0; k<EnemyNum; k++) {
                canves[enemy_x[k]][enemy_y[k]] = 0;
                enemy_x[k]++;
                speed = 0;
                canves[enemy_x[k]][enemy_y[k]] = 3;
            }
        }
    }
}

//    Sleep(50);


void updateWithInput() {
    char input;
    if(kbhit()) { //runing while user push keyboard
        input = getch();
        if(input == 'a' && position_y>0) { //flight move left
            canves[position_x][position_y]=0;
            position_y--;
            canves[position_x][position_y]=1;
        }
        if(input == 'd' && position_y<Width) { //flight move right
            canves[position_x][position_y++]=0;
            canves[position_x][position_y]=1;
        }
        if(input == 'w') { //flight move up
            canves[position_x--][position_y]=0;
            canves[position_x][position_y]=1;
        }
        if(input == 's') { //flight move down
            canves[position_x++][position_y]=0;
            canves[position_x][position_y]=1;
        }
        if(input == ' ') { //flight move down
            int left, right;
            left = position_y - BulletWidth;
            if(left<0)
                left = 0;
            right = position_y + BulletWidth;
            if(right>Width-1)
                right = Width-1;
            int x;
            for(x=left; x<=right; x++)
                canves[position_x-1][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;
}

 

 

 学习B站  https://www.bilibili.com/video/av34118200?p=18

posted @ 2020-01-13 16:58  yangly  阅读(410)  评论(0编辑  收藏  举报