您的位置:首页 > 编程语言 > C语言/C++

C语言实现打飞机简易游戏(半完成版)

2018-03-31 12:48 429 查看
打飞机简易游戏(半完成版),如图。
感谢 童晶 老师的教程,链接地址:http://study.163.com/forum/detail/1003961010.htm
未完成内容:①边框绘制 ②多个敌机 ③敌机子弹 ④战机生命 等等。



以下是代码。(在带c99标准的codeblocks下通过,在vc++6.0下未通过!)
*******************************************************************************************
// 打飞机简易游戏(未完成)。
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>

int bullet_x,bullet_y;        
int enemy_x,enemy_y;
int high_max,width_max;
int score;
int x,y;

void gotoxy(int,int);
void picture();
void changed();
void start();
void show();
void other();
void Hide();

int main(){

    SetConsoleTitleA("hit planes");             //设置窗口名称。
    system("mode con cols=30 lines=22");        //设置窗口大小。

    start();

    while(1){                      //循环。
            Hide();
            show();
            other();
            changed();
    }
    return 0;
}

void gotoxy(int x, int y){      //坐标函数。
COORD coord;
coord.X = x;coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void start(){           //参数设置。
    high_max = 20;      //长度。即X轴。
    width_max = 30;     //宽度。即Y轴。
    x = high_max / 2;   //战机坐标。
    y = width_max / 2;
    bullet_x = -2;      //子弹坐标。
    bullet_y = y;
    enemy_x = 0;        //敌机坐标。
    enemy_y = y;
    score = 0;          //得分
}

void show(){            //图形界面绘制。
    gotoxy(0,0);
    int i,j,k;
    for(i = 0;i < high_max;i++){
        for(j = 0;j < width_max;j++){
            if((i == x) && (j == y))
                picture();
            else if((i == enemy_x) && (j == enemy_y))
                printf("O");
            else if((i == bullet_x + 1) && (j == bullet_y))
                printf("|");
            else if((i == 0) || (i == high_max - 1))
                printf("-");
            else if((j == 0) || (j == width_max - 1))
                printf("|");
            else
                printf(" ");
        }
        printf("\n");
    }
    for(k = 0;k < (width_max / 2) - 5;k++)
        printf(" ");
    printf("score:%d\n",score);     //得分。
}

void picture(){         //战机图形。
    printf("*");
}

void changed(){         //操作控制。不区分大小写。WSAD 上下左右,J发射子弹 ,P 暂停,ESC 退出。
    char sizein;
    if(kbhit()){
        sizein = getch();
    if(((sizein == 'a') || (sizein == 'A')) && (y > 1))
        y--;
    if(((sizein == 'd') || (sizein == 'D')) && (y < width_max - 2))
        y++;
    if(((sizein == 's') || (sizein == 'S')) && (x < high_max - 2))
        x++;
    if(((sizein == 'w') || (sizein == 'W')) && (x > 1))
        x--;
    if(sizein == 27)
        exit(0);
    if((sizein == 'p') || (sizein == 'P'))
        system("pause > nul");
    if((sizein == 'j') || (sizein == 'J')){
        bullet_x = x - 1;
        bullet_y = y;
        }
    }
}

void other(){           //敌机循环生成。
    if(bullet_x > -1);
        bullet_x--;
    if((bullet_x == enemy_x) && (bullet_y == enemy_y)){
        score++;
        enemy_x = -1;
        enemy_y = (rand() % (width_max - 1) + 1);
        bullet_x = -2;
    }
    if((enemy_x > high_max) || (enemy_y < 1))
    {
        enemy_x = -1;
        enemy_y = (rand() % (width_max - 1) + 1);
    }
    static int speed = 0;
    if(speed < 10)      //调整敌机移动速度。
        speed++;
    if(speed == 10){
        enemy_x++;
        speed = 0;
    }
}

void Hide(){     //隐藏光标函数调用。
    CONSOLE_CURSOR_INFO CURSORINFO = {1,0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &CURSORINFO);
}

******************************************************************************************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息