《the Best Flying》2.0

升级版打飞机!

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <conio.h>
#include <cmath>

using namespace std;

char sky[16][11];
bool game;
int NumberOfEnemies;
int NumberOfShowing;
int score;

class player {
    public:
        int x = 13;
        int y = 5;
        char plane = 'A';
        char zd = '|';
        bool attack = false;
        int x_zd;
        int y_zd;
};

class enemy {
    public:
        int x;
        int y;
        char plane = 'V';
};

player player1;
enemy enemies[11];

void color(int a) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}

void init() {
    int i, j;
    for (i=1; i<=15; i++)
        for (j=1; j<=10; j++)
            if (j == 1 || j == 10)
                sky[i][j] = '#';
            else sky[i][j] = ' ';
    game = true;
    NumberOfEnemies = 0;
    NumberOfShowing = 0;
    score = 0;
}

int RandomyOfEnemy() {
    srand(time(0));
    return rand()%8+2;
}

void show() {
    int i, j, k;
    bool ok;
    system("cls");
    for (i=1; i<=15; i++) {
        for (j=1; j<=10; j++) {
            ok = false;
            if (j > 1 && j < 10) {
                if (game == false && ((i == player1.x && (j == player1.y-1 || j == player1.y+1)) || (j == player1.y && (i == player1.x-1 || i == player1.x+1)))) {
                    color(14);
                    cout<<"@";
                    color(15);
                    continue;
                }
                if (i == player1.x && j == player1.y) {
                    if (game == true) color(11);
                    else color(14);
                    cout<<player1.plane;
                    color(15);
                    ok = true;
                }
                else {
                    for (k=1; k<=NumberOfEnemies; k++)
                        if (i == enemies[k].x && j == enemies[k].y) {
                            if (enemies[k].plane == '@') color;
                            else color(12);
                            cout<<enemies[k].plane;
                            color(15);
                            if (enemies[k].plane == '@') {
                                enemies[k].plane = 'V';
                                for (int t=k; t<NumberOfEnemies; t++) {
                                    enemies[t].x = enemies[t+1].x;
                                    enemies[t].y = enemies[t+1].y;
                                }
                                enemies[NumberOfEnemies].x = 1;
                                enemies[NumberOfEnemies].y = RandomyOfEnemy();
                            }
                            ok = true;
                            break;
                        }
                    if (ok == false && player1.attack == true) {
                        if (i == player1.x_zd && j == player1.y_zd) {
                            color(14);
                            cout<<player1.zd;
                            color(15);
                            ok = true;
                        }
                    }
                    if (ok == false) cout<<sky[i][j];
                }
            }
            else {
                color(10);
                cout<<sky[i][j];
                color(15);
                ok = true;
            }
        }
        cout<<endl;
    }
    cout<<"----------"<<endl;
    cout<<"Your score: "<<score;
}

void EnemiesMove() {
    int i;
    for (i=1; i<=NumberOfEnemies; i++)
        enemies[i].x++;
}

void NewEnemy() {
    int i;
    if (enemies[1].x > 15) {
        for (i=1; i<NumberOfEnemies; i++) {
            enemies[i].x = enemies[i+1].x;
            enemies[i].y = enemies[i+1].y;
        }
        enemies[NumberOfEnemies].x = 1;
        enemies[NumberOfEnemies].y = RandomyOfEnemy();
    }
}

void DieEnemy() {
    int i,j;
    bool ok = false;
    for (i=1; i<=NumberOfEnemies; i++) {
        if (abs(enemies[i].x-player1.x_zd) <= 1 && enemies[i].y == player1.y_zd) {
            enemies[i].plane = '@';
            score++;
            ok = true;
        }
    }
    if (ok == true) {
        player1.x_zd = -1;
    }
}

void DiePlayer() {
    int i;
    for (i=1; i<=NumberOfEnemies; i++) {
        if (enemies[i].x == player1.x && enemies[i].y == player1.y) {
            game = false;
            player1.plane = '@';
            break;
        }
    }
}

void ZdFly() {
    if (player1.attack == true )
        player1.x_zd -= 2;
    if (player1.x_zd < 1)
        player1.attack = false;
}

void PlayerAttack() {
    if (player1.attack == false && player1.x > 1) {
        player1.attack = true;
        player1.x_zd = player1.x-1;
        player1.y_zd = player1.y;
    }
}

void PlayerDo() {
    while (kbhit()) {
        switch (getch()) {
            case 'w':
                if (player1.x > 1)
                    player1.x--;
            break;
            case 's':
                if (player1.x < 15)
                    player1.x++;
            break;
            case 'a':
                if (player1.y > 2)
                    player1.y--;
            break;
            case 'd':
                if (player1.y < 9)
                    player1.y++;
            break;
            case ' ':
                PlayerAttack();
            break;
        }
    }
}

void ProcedureOfGame() {
    init();
    cout<<"╔═══════════════╗"<<endl;
    cout<<"║       the Best Flying        ║"<<endl;
    cout<<"╠═══════════════╣"<<endl;
    cout<<"║  Made by Peter Winchester    ║"<<endl;
    cout<<"╠═══════════════╣"<<endl;
    cout<<"║         How to play          ║"<<endl;
    cout<<"║ Press w, s, a, d to move. The║"<<endl;
    cout<<"║A is your plane, and the V is ║"<<endl;
    cout<<"║enemies' plane. Press space to║"<<endl;
    cout<<"║attack enemies.               ║"<<endl;
    cout<<"║ How much score can you get?  ║"<<endl;
    cout<<"║ Press any key to start.      ║"<<endl;
    cout<<"╚═══════════════╝"<<endl;
    getch();
    while (game == true) {
        NumberOfShowing++;
        if (NumberOfShowing <= 10 && NumberOfEnemies < 10 && NumberOfShowing%3 == 0) {
            NumberOfEnemies++;
            enemies[NumberOfEnemies].x = 1;
            enemies[NumberOfEnemies].y = RandomyOfEnemy();
        }
        show();
        Sleep(100);
        DieEnemy();
        EnemiesMove();
        DieEnemy();
        ZdFly();
        NewEnemy();
        PlayerDo();
        DiePlayer();
    }
    show();
    cout<<endl;
    cout<<"Game over!\n>>>Press m to quit.\n>>>";
}

void pause(char *a) {
    do {
        *a = getch();
    }while (*a != 'm');
    cout<<*a;
}

int main() {
    char a;
    ProcedureOfGame();
    pause(&a);
    return 0;
}
全部评论

相关推荐

M_bao:换个排版吧哥们,看着费劲
点赞 评论 收藏
分享
hso_:哈哈哈哈哈哈我没offer一样在同一道题开喷了
投递深圳同为数码等公司10个岗位
点赞 评论 收藏
分享
评论
1
收藏
分享
正在热议
# 25届秋招总结 #
440928次浏览 4493人参与
# 春招别灰心,我们一人来一句鼓励 #
41537次浏览 524人参与
# 阿里云管培生offer #
119928次浏览 2219人参与
# 地方国企笔面经互助 #
7933次浏览 18人参与
# 同bg的你秋招战况如何? #
75751次浏览 552人参与
# 虾皮求职进展汇总 #
114497次浏览 885人参与
# 北方华创开奖 #
107331次浏览 599人参与
# 实习,投递多份简历没人回复怎么办 #
2454159次浏览 34849人参与
# 实习必须要去大厂吗? #
55696次浏览 960人参与
# 提前批简历挂麻了怎么办 #
149839次浏览 1977人参与
# 投递实习岗位前的准备 #
1195754次浏览 18547人参与
# 你投递的公司有几家约面了? #
33181次浏览 188人参与
# 双非本科求职如何逆袭 #
661963次浏览 7394人参与
# 如果公司给你放一天假,你会怎么度过? #
4734次浏览 55人参与
# 机械人春招想让哪家公司来捞你? #
157606次浏览 2267人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
11402次浏览 275人参与
# 发工资后,你做的第一件事是什么 #
12447次浏览 61人参与
# 工作中,努力重要还是选择重要? #
35638次浏览 384人参与
# 参加完秋招的机械人,还参加春招吗? #
20093次浏览 240人参与
# 我的上岸简历长这样 #
451937次浏览 8088人参与
# 实习想申请秋招offer,能不能argue薪资 #
39248次浏览 314人参与
# 非技术岗是怎么找实习的 #
155855次浏览 2120人参与
牛客网
牛客企业服务