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