c++实现简易飞机大战游戏

效果展示:

1.导入头文件

#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;

2.设置全局变量

int x, y;
int hight = 10, width = 30;
int bullet_x, bullet_y;
int enemy_x, enemy_y;
int speed = 0, score, maxScore = 0;
bool isGameStart = false; //判断游戏是否运行
bool gameOver = false;

3.主页面

void drewMainMenu() {
	system("cls");
	hide();
	string title = "简易飞机大战";
	int title_len = title.size();
	int title_row = 2;
	int title_start_j = (width - title_len) / 2;
	// 第二行文字:开始游戏提示
	string tip = "按下空格开始游戏"; // 提示文字(去掉多余分号,更简洁)
	int tip_len = tip.size();
	int tip_row = 4; // 提示文字行(标题下方第2行,留1行空隙)
	int tip_start_j = (width - tip_len) / 2; // 提示文字居中起始列
	//第三行文字:最高得分
	string mScore = "最高得分:"; // 提示文字(去掉多余分号,更简洁)
	string mScore_full = mScore + to_string(maxScore);
	int mScore_len = mScore_full.size();
	int mScore_row = 6; // 提示文字行(标题下方第2行,留1行空隙)
	int mScore_start_j = (width - mScore_len) / 2; // 提示文字居中起始列

	for (int i = 0; i <= hight; i++) {
		for (int j = 0; j <= width; j++) {

			// 先绘制居中标题(优先级最高,避免被其他元素覆盖)
			if (i == title_row && j >= title_start_j && j < title_start_j + title_len) {
				cout << title[j - title_start_j];
			} else if (i == tip_row && j >= tip_start_j && j < tip_start_j + tip_len) {
				cout << tip[j - tip_start_j];
			} else if (i == mScore_row && j >= mScore_start_j && j < mScore_start_j + mScore_len) {
				cout << mScore_full[j - mScore_start_j];
			} else if (i == hight) {
				cout << "_";
			} else if (j == width) {
				cout << "|";
			} else {
				cout << " ";
			}
		}
		cout << endl;
	}
	cout << "游戏过程中按下ESC键回到主页面" << endl;
}

4.游戏画面

void show() {
//	system("cls");
	gotoxy(0, 0);
	int i, j;
	for (i = 0; i < hight + 1; i++) {
		for (j = 0; j < width + 1; j++) {
			if ((i == x - 1 && j == y) ||    // 飞机顶部(*)
			    (i == x && j >= y - 1 && j <= y + 1) || // 飞机中间(***)
			    (i == x + 1 && j >= y - 1 && j <= y + 1)) { // 飞机底部(***)
				cout << "*";

			} else if ((i == bullet_x) && (j == bullet_y)) {

				cout << "|";
			} else if ((i == enemy_x) && (j == enemy_y)) {

				cout << "#";
			} else if (i == hight) {
				cout << "_";
			} else if (j == width) {
				cout << "|";
			} else {
				cout << " ";
			}
		}
		cout << endl;
	}
	cout << "得分:" << score << endl;
	cout << "按下空格发射子弹,通过方向键控制移动";
}

5.游戏结束页面

void drawGameOver() {
	system("cls");
	//第一行文字:游戏结束
	string title = "GameOver!!!";
	int title_len = title.size();
	int title_row = 2;
	int title_start_j = (width - title_len) / 2;
	// 第二行文字:回到主页面
	string tip = "按下空格回到主页面"; // 提示文字(去掉多余分号,更简洁)
	int tip_len = tip.size();
	int tip_row = 4; // 提示文字行(标题下方第2行,留1行空隙)
	int tip_start_j = (width - tip_len) / 2; // 提示文字居中起始列

	//第三行文字:得分
	string mScore = "得分:"; // 提示文字(去掉多余分号,更简洁)
	string mScore_full = mScore + to_string(score);
	int mScore_len = mScore_full.size();
	int mScore_row = 6; // 提示文字行(标题下方第2行,留1行空隙)
	int mScore_start_j = (width - mScore_len) / 2; // 提示文字居中起始列

	for (int i = 0; i <= hight; i++) {
		for (int j = 0; j <= width; j++) {

			// 先绘制居中标题(优先级最高,避免被其他元素覆盖)
			if (i == title_row && j >= title_start_j && j < title_start_j + title_len) {
				cout << title[j - title_start_j];
			} else if (i == tip_row && j >= tip_start_j && j < tip_start_j + tip_len) {
				cout << tip[j - tip_start_j];
			} else if (i == mScore_row && j >= mScore_start_j && j < mScore_start_j + mScore_len) {
				cout << mScore_full[j - mScore_start_j];
			}  else if (i == hight) {
				cout << "_";
			} else if (j == width) {
				cout << "|";
			} else {
				cout << " ";
			}
		}
		cout << endl;
	}
}

6.游戏控制代码

void userInput() { //与用户有关的输入
	int input;
	if (kbhit()) {
		input = _getch();
//		cout<<input<<endl;
		if (input == 75)y--;
		if ((input == 77 ) && (y <= 30))y++;
		if (input == 72)x--;
		if ((input == 80) && (x <= 20))x++;
		if (input == ' ') {
			bullet_x = x - 1;
			bullet_y = y;
		}
		if (input == 27) {
			isGameStart = false;
			maxScore = max(maxScore, score);
		}

	}
}
void update() { //游戏画面更新逻辑
	if (bullet_x > -1) {
		bullet_x--;
	}
	if ((bullet_x == enemy_x) && (bullet_y == enemy_y)) {
		score++;
		enemy_x = -1;
		bullet_x = -2;
		enemy_y = rand() % width;
	}
	if (enemy_x > hight) {
//		enemy_x = -1;
//		enemy_y = rand() % width;
		maxScore = max(maxScore, score);
		gameOver = true; //漏掉敌机,游戏结束。
	}
	if (speed < 5) {
		speed++;
	} else {
		enemy_x++;
		speed = 0;
	}
}

7. 关闭画面光标与屏幕闪烁

void gotoxy(short a, short b) { //将光标移到0,0 坐标重画游戏页面降低闪烁
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos = {a, b};
	SetConsoleCursorPosition(handle, pos);
}
void hide() {
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

8. 主函数

int main() {
	hide();
	//循环:主页面->游戏页面->主页面
	while (1) {
		// 1. 绘制主页面
		drewMainMenu();

		// 2. 等待用户按下空格开始游戏
		while (1) {
			if (kbhit() && _getch() == ' ') {
				system("cls"); // 清空主页面,准备进入游戏
				break;
			}
		}
		startup();
		isGameStart = true;
		gameOver = false;
		while (isGameStart) {
			//TODO
			show();
			update();
			userInput();
			if (gameOver) {
				drawGameOver();
				while (1) {
					if (kbhit() && _getch() == ' ') {
						isGameStart = false;
						system("cls"); // 清空主页面,准备进入游戏
						break;
					}
				}
			}
			Sleep(50);
		}
		system("cls");

	}
	return 0;
}

9.完整项目代码

#include<bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
int x, y;
int hight = 10, width = 30;
int bullet_x, bullet_y;
int enemy_x, enemy_y;
int speed = 0, score, maxScore = 0;
bool isGameStart = false; //判断游戏是否运行
bool gameOver = false;
void hide() {
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup() {
	hight = 20;
	width = 30;
	x = 18;
	y = width / 2;
	bullet_x = -1;
	bullet_y = y;
	enemy_x = 0;
	enemy_y = y;
	score = 0;
}
void gotoxy(short a, short b) {
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos = {a, b};
	SetConsoleCursorPosition(handle, pos);
}
void show() {
//	system("cls");
	gotoxy(0, 0);
	int i, j;
	for (i = 0; i < hight + 1; i++) {
		for (j = 0; j < width + 1; j++) {
			if ((i == x - 1 && j == y) ||    // 飞机顶部(*)
			    (i == x && j >= y - 1 && j <= y + 1) || // 飞机中间(***)
			    (i == x + 1 && j >= y - 1 && j <= y + 1)) { // 飞机底部(***)
				cout << "*";

			} else if ((i == bullet_x) && (j == bullet_y)) {

				cout << "|";
			} else if ((i == enemy_x) && (j == enemy_y)) {

				cout << "#";
			} else if (i == hight) {
				cout << "_";
			} else if (j == width) {
				cout << "|";
			} else {
				cout << " ";
			}
		}
		cout << endl;
	}
	cout << "得分:" << score << endl;
	cout << "按下空格发射子弹,通过方向键控制移动";
}
void userInput() {
	int input;
	if (kbhit()) {
		input = _getch();
//		cout<<input<<endl;
		if (input == 75)y--;
		if ((input == 77 ) && (y <= 30))y++;
		if (input == 72)x--;
		if ((input == 80) && (x <= 20))x++;
		if (input == ' ') {
			bullet_x = x - 1;
			bullet_y = y;
		}
		if (input == 27) {
			isGameStart = false;
			maxScore = max(maxScore, score);
		}

	}
}
void update() {
	if (bullet_x > -1) {
		bullet_x--;
	}
	if ((bullet_x == enemy_x) && (bullet_y == enemy_y)) {
		score++;
		enemy_x = -1;
		bullet_x = -2;
		enemy_y = rand() % width;
	}
	if (enemy_x > hight) {
//		enemy_x = -1;
//		enemy_y = rand() % width;
		maxScore = max(maxScore, score);
		gameOver = true; //漏掉敌机,游戏结束。
	}
	if (speed < 5) {
		speed++;
	} else {
		enemy_x++;
		speed = 0;
	}
}
void drewMainMenu() {
	system("cls");
	hide();
	string title = "简易飞机大战";
	int title_len = title.size();
	int title_row = 2;
	int title_start_j = (width - title_len) / 2;
	// 第二行文字:开始游戏提示
	string tip = "按下空格开始游戏"; // 提示文字(去掉多余分号,更简洁)
	int tip_len = tip.size();
	int tip_row = 4; // 提示文字行(标题下方第2行,留1行空隙)
	int tip_start_j = (width - tip_len) / 2; // 提示文字居中起始列
	//第三行文字:最高得分
	string mScore = "最高得分:"; // 提示文字(去掉多余分号,更简洁)
	string mScore_full = mScore + to_string(maxScore);
	int mScore_len = mScore_full.size();
	int mScore_row = 6; // 提示文字行(标题下方第2行,留1行空隙)
	int mScore_start_j = (width - mScore_len) / 2; // 提示文字居中起始列

	for (int i = 0; i <= hight; i++) {
		for (int j = 0; j <= width; j++) {

			// 先绘制居中标题(优先级最高,避免被其他元素覆盖)
			if (i == title_row && j >= title_start_j && j < title_start_j + title_len) {
				cout << title[j - title_start_j];
			} else if (i == tip_row && j >= tip_start_j && j < tip_start_j + tip_len) {
				cout << tip[j - tip_start_j];
			} else if (i == mScore_row && j >= mScore_start_j && j < mScore_start_j + mScore_len) {
				cout << mScore_full[j - mScore_start_j];
			} else if (i == hight) {
				cout << "_";
			} else if (j == width) {
				cout << "|";
			} else {
				cout << " ";
			}
		}
		cout << endl;
	}
	cout << "游戏过程中按下ESC键回到主页面" << endl;
}
void drawGameOver() {
	system("cls");
	//第一行文字:游戏结束
	string title = "GameOver!!!";
	int title_len = title.size();
	int title_row = 2;
	int title_start_j = (width - title_len) / 2;
	// 第二行文字:回到主页面
	string tip = "按下空格回到主页面"; // 提示文字(去掉多余分号,更简洁)
	int tip_len = tip.size();
	int tip_row = 4; // 提示文字行(标题下方第2行,留1行空隙)
	int tip_start_j = (width - tip_len) / 2; // 提示文字居中起始列

	//第三行文字:得分
	string mScore = "得分:"; // 提示文字(去掉多余分号,更简洁)
	string mScore_full = mScore + to_string(score);
	int mScore_len = mScore_full.size();
	int mScore_row = 6; // 提示文字行(标题下方第2行,留1行空隙)
	int mScore_start_j = (width - mScore_len) / 2; // 提示文字居中起始列

	for (int i = 0; i <= hight; i++) {
		for (int j = 0; j <= width; j++) {

			// 先绘制居中标题(优先级最高,避免被其他元素覆盖)
			if (i == title_row && j >= title_start_j && j < title_start_j + title_len) {
				cout << title[j - title_start_j];
			} else if (i == tip_row && j >= tip_start_j && j < tip_start_j + tip_len) {
				cout << tip[j - tip_start_j];
			} else if (i == mScore_row && j >= mScore_start_j && j < mScore_start_j + mScore_len) {
				cout << mScore_full[j - mScore_start_j];
			}  else if (i == hight) {
				cout << "_";
			} else if (j == width) {
				cout << "|";
			} else {
				cout << " ";
			}
		}
		cout << endl;
	}
}
int main() {
	hide();
	//循环:主页面->游戏页面->主页面
	while (1) {
		// 1. 绘制主页面
		drewMainMenu();

		// 2. 等待用户按下空格开始游戏
		while (1) {
			if (kbhit() && _getch() == ' ') {
				system("cls"); // 清空主页面,准备进入游戏
				break;
			}
		}
		startup();
		isGameStart = true;
		gameOver = false;
		while (isGameStart) {
			//TODO
			show();
			update();
			userInput();
			if (gameOver) {
				drawGameOver();
				while (1) {
					if (kbhit() && _getch() == ' ') {
						isGameStart = false;
						system("cls"); // 清空主页面,准备进入游戏
						break;
					}
				}
			}
			Sleep(50);
		}
		system("cls");

	}
	return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值