C++ [项目] 飞机大战

现在才发现C++游戏的支持率这么高,那就发几篇吧

一、基本介绍

        支持Dev-C++5.11版本(务必调为英文输入法),基本操作看游戏里的介绍,怎么做的……懒得说,能看懂就看注释,没有的自己猜,如果你很固执……私我吧

二、代码部分

/*=============== 2024.8.13=========================*/
#include<iostream>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<string>
#include<cstring>
#define long long long
using namespace std;
bool flag;
int xxx=1,scorecc,score;
string title,titlecc;
/*=============== all the structures ===============*/typedef struct Frame {COORD position[2];int flag;
} Frame;/*=============== all the functions ===============*/void SetPos(COORD a) { // set cursorHANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(out, a);
}void SetPos(int i, int j) { // set cursorCOORD pos= {i, j};SetPos(pos);
}void HideCursor() {CONSOLE_CURSOR_INFO cursor_info = {1, 0};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}//把第y行,[x1, x2) 之间的坐标填充为 ch
void drawRow(int y, int x1, int x2, char ch) {SetPos(x1,y);for(int i = 0; i <= (x2-x1); i++)cout<<ch;
}//在a, b 纵坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
void drawRow(COORD a, COORD b, char ch) {if(a.Y == b.Y)drawRow(a.Y, a.X, b.X, ch);else {SetPos(0, 25);cout<<"error code 01:无法填充行,因为两个坐标的纵坐标(x)不相等";system("pause");}
}//把第x列,[y1, y2] 之间的坐标填充为 ch
void drawCol(int x, int y1, int y2, char ch) {int y=y1;while(y!=y2+1) {SetPos(x, y);cout<<ch;y++;}
}//在a, b 横坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
void drawCol(COORD a, COORD b, char ch) {if(a.X == b.X)drawCol(a.X, a.Y, b.Y, ch);else {SetPos(0, 25);cout<<"error code 02:无法填充列,因为两个坐标的横坐标(y)不相等";system("pause");}
}//左上角坐标、右下角坐标、用row填充行、用col填充列
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 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 drawFrame(Frame frame, char row, char col) {COORD a = frame.position[0];COORD b = frame.position[1];drawFrame(a, b, row, col);
}void drawPlaying() {drawFrame(0, 0, 48, 24, '=', '|');//    draw map frame;drawFrame(49, 0, 79, 4, '-', '|');//        draw output framedrawFrame(49, 4, 79, 9, '-', '|');//        draw score framedrawFrame(49, 9, 79, 20, '-', '|');//    draw operate framedrawFrame(49, 20, 79, 24, '-', '|');//    draw other message frameSetPos(52, 6);cout<<"得分:";SetPos(52, 7);cout<<"称号:";SetPos(52,10);cout<<"操作方式:";SetPos(52,12);cout<<"  a,s,d,w 控制战机移动。";SetPos(52,14);cout<<"  p 暂停游戏。";SetPos(52,16);cout<<"  e 退出游戏。";SetPos(52,18);cout<<"  k 发射子弹。" ;
}//在[a, b)之间产生一个随机整数
int random(int a, int b) {int c=(rand() % (a-b))+ a;return c;
}//在两个坐标包括的矩形框内随机产生一个坐标
COORD random(COORD a, COORD b) {int x=random(a.X, b.X);int y=random(a.Y, b.Y);COORD c= {x, y};return c;
}bool  judgeCoordInFrame(Frame frame, COORD spot) {if(spot.X>=frame.position[0].X)if(spot.X<=frame.position[1].X)if(spot.Y>=frame.position[0].Y)if(spot.Y<=frame.position[0].Y)return true;return false;
}void printCoord(COORD a) {cout    <<"( "<<a.X<<" , "<<a.Y<<" )";
}void printFrameCoord(Frame a) {printCoord(a.position[0]);cout    <<" - ";printCoord(a.position[1]);
}DWORD WINAPI MusicFun(LPVOID lpParamte) {return 0;
}
DWORD WINAPI BulletShot(LPVOID lpParamte) {return 0;
}
DWORD WINAPI StartStage(LPVOID lpParamte) {return 0;
}
int drawMenu() {HANDLE MFUN;MFUN= CreateThread(NULL, 0, StartStage, NULL, 0, NULL);SetPos(30, 1);cout<<"P l a n e  W a r";drawRow(3, 0, 79, '-');drawRow(5, 0, 79, '-');SetPos(28, 4);cout<<"w 和 s 选择, k 确定";SetPos(15, 11);cout<<"1. 简单的敌人";SetPos(15, 13);cout<<"2. 冷酷的敌人";drawRow(20, 0, 79, '-');drawRow(22, 0, 79, '-');SetPos(47, 11);cout<<"简单的敌人:";SetPos(51, 13);cout<<"简单敌人有着较慢的移动速度。";SetPos(28, 21);cout<<"制作:一只水饺吖<*)))><<";int j=11;SetPos(12, j);cout<<">>";drawFrame(46, 9, 89, 17, '=', '|');while(1) {if( _kbhit() ) {char x=_getch();switch (x) {case 'w' : {if( j == 13) {SetPos(12, j);cout<<" ";j = 11;SetPos(12, j);cout<<">>";SetPos(51, 13);cout<<"            ";SetPos(47, 11);cout<<"简单的敌人:";SetPos(51, 13);cout<<"简单敌人有着较慢的移动速度,容易对付。";}break;}case 's' : {if( j == 11 ) {SetPos(12, j);cout<<" ";j = 13;SetPos(12, j);cout<<">>";SetPos(51, 13);cout<<"              ";SetPos(47, 11);cout<<"冷酷的敌人:";SetPos(51, 13);cout<<"冷酷的敌人移动速度较快,难对付哟!!!";}break;}case 'k' : {if (j == 8)    return 1;else return 2;}}}}
}/*================== the Game Class ==================*/class Game {public:COORD position[10];COORD bullet[10];Frame enemy[8];//int score;int rank;int rankf;//string title;int flag_rank;Game ();//初始化所有void initPlane();void initBullet();void initEnemy();//初始化其中一个//void initThisBullet( COORD );//void initThisEnemy( Frame );void planeMove(char);void bulletMove();void enemyMove();//填充所有void drawPlane();void drawPlaneToNull();void drawBullet();void drawBulletToNull();void drawEnemy();void drawEnemyToNull();//填充其中一个void drawThisBulletToNull( COORD );void drawThisEnemyToNull( Frame );void Pause();void Playing();void judgePlane();void judgeEnemy();void Shoot();void GameOver();void printScore();
};Game::Game() {initPlane();initBullet();initEnemy();score = 0;rank = 25;rankf = 0;flag_rank = 0;
}void Game::initPlane() {COORD centren= {39, 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]);if(i!=5)cout<<"O";else if(i==5)cout<<"|";}
}void Game::drawPlaneToNull() {for(int i=0; i<9; i++) {SetPos(position[i]);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]);cout<<"^";}}
}void Game::drawBulletToNull() {for(int i=0; i<10; i++)if( bullet[i].Y != 30 ) {COORD pos= {bullet[i].X, bullet[i].Y+1};SetPos(pos);cout<<" ";}
}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++)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 Game::Pause() {SetPos(61,2);cout<<"               ";SetPos(61,2);cout<<"暂停中...";HANDLE MFUN;MFUN= CreateThread(NULL, 0, BulletShot, NULL, 0, NULL);char c=_getch();while(c!='p')c=_getch();SetPos(61,2);cout<<"         ";MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL);
}void Game::planeMove(char x) {if(x == 'a')if(position[1].X != 1)for(int i=0; i<=9; i++)position[i].X -= 2;if(x == 's')if(position[7].Y != 23)for(int i=0; i<=9; i++)position[i].Y += 1;if(x == 'd')if(position[4].X != 47)for(int i=0; i<=9; i++)position[i].X += 2;if(x == 'w')if(position[5].Y != 3)for(int i=0; i<=9; i++)position[i].Y -= 1;
}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::enemyMove() {for(int i=0; i<8; i++) {for(int j=0; j<2; j++)enemy[i].position[j].Y++;if(24 == enemy[i].position[1].Y) {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::judgePlane() {for(int i = 0; i < 8; i++)for(int j=0; j<9; j++)if(judgeCoordInFrame(enemy[i], position[j])) {SetPos(62, 1);cout<<"坠毁";drawFrame(enemy[i], '+', '+');Sleep(1000);GameOver();break;}}void Game::drawThisBulletToNull( COORD c) {SetPos(c);cout<<" ";
}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::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::printScore() {if(score == 120 && flag_rank == 0) {rank -= 3;flag_rank = 1;}else if( score == 360 && flag_rank == 1) {rank -= 5;flag_rank = 2;} else if( score == 480 && flag_rank == 2) {rank -= 5;flag_rank = 3;}int x=rank/5;SetPos(60, 6);cout<<score;if( rank!=rankf ) {SetPos(60, 7);if( x == 5)title="初级飞行员";else if( x == 4)title="中级飞行员";else if( x == 3)title="高级飞行员";else if( x == 2 )title="王牌飞行员";else title="顶级飞行员";cout<<title;}rankf = rank;
}void Game::Playing() {HANDLE MFUN;MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL);drawEnemy();drawPlane();int flag_bullet = 0;int flag_enemy = 0;while(1) {Sleep(8);if(_kbhit()) {char x = _getch();if ('a' == x || 's' == x || 'd' == x || 'w' == x) {drawPlaneToNull();planeMove(x);drawPlane();judgePlane();} else if ('p' == x)Pause();else if( 'k' == x)Shoot();else if( 'e' == x) {CloseHandle(MFUN);GameOver();break;}}/* 处理子弹 */if( 0 == flag_bullet ) {bulletMove();drawBulletToNull();drawBullet();judgeEnemy();}flag_bullet++;if( 5 == flag_bullet )flag_bullet = 0;/* 处理敌人 */if( 0 == flag_enemy ) {drawEnemyToNull();enemyMove();drawEnemy();judgePlane();}flag_enemy++;if( flag_enemy >= rank )flag_enemy = 0;/* 输出得分 */printScore();}
}void Game::GameOver() {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);cout<<"击落敌机:"<<score/5<<" 架";SetPos(31, 12);cout<<"得  分:"<<score;SetPos(31, 13);cout<<"获得称号:"<<title;SetPos(30, 16);Sleep(1000);
//	cout<<"是否复活 (*≧︶≦))( ̄▽ ̄* )ゞ 是(y)| 否(n)"<<endl;
//	char kkksc03;
//	cin>>kkksc03;
//	if(kkksc03=='y'){
//		xxx--;
//		flag=true;
//		if(xxx==0){
//			cout<<"已使用"<<endl;
//			cout<<"剩余0次机会" <<endl; 
//			score=scorecc;
//			titlecc=title;
//			//Main();
//		}
//		else{
//			cout<<"你没机会了,gun⑧"<<endl;
//		}
//	}cout<<"继续? 是(y)| 否(n)制作:一只水饺吖<*)))><<";HANDLE MFUN;MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL);
as:char x=_getch();if (x == 'n')exit(0);else if (x == 'y') {system("cls");Game game;int a = drawMenu();if(a == 2)game.rank = 20;system("cls");drawPlaying();game.Playing();} else goto as;
}
void Main(){//游戏准备srand((int)time(0));    //随机种子HideCursor();    //隐藏光标Game game;int a = drawMenu();if(a == 2)game.rank = 20;HANDLE MFUN;MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL);system("cls");drawPlaying();if(flag==true){flag=false;score=scorecc;title=titlecc;}game.Playing();
}
/*================== the main function ==================*/
int main() {Main(); return 0; 
}

如果觉得不错就点个赞吧

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/57344.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

学习笔记——交换——STP(生成树)基本概念

三、基本概念 1、桥ID/网桥ID (Bridege ID&#xff0c;BID) 每一台运行STP的交换机都拥有一个唯一的桥ID(BID)&#xff0c;BID(Bridge ID/桥ID)。在STP里我们使用不同的桥ID标识不同的交换机。 (2)BID(桥ID)组成 BID(桥ID)组成(8个字节)&#xff1a;由16位(2字节)的桥优先级…

ObjectMapper简单使用

<!-- 根据自己需要引入相关版本依赖。 --> <dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.9.10</version> </dependency><dependency><groupId…

AD如何制作原理图的模版、原理图模板绘制修改以及如何导入原理图模版

作为硬件工程师&#xff0c;制定原理图模板是一项至关重要的任务&#xff0c;旨在标准化和规范原理图的绘制过程。在AD20中制作、绘制修改以及导入原理图模板的步骤如下&#xff1a; 1制作原理图模板 首先需在AD原理图设计环境下新建一个原理图文件&#xff1b; 在原理图界面…

实用的 Python 小脚本

一、引言 在日常办公和电脑使用中&#xff0c;我们经常会遇到一些重复性的任务或需要快速获取特定信息的情况。Python 作为一种强大而灵活的编程语言&#xff0c;可以用来编写各种小脚本&#xff0c;以自动化这些任务并提高工作效率。本文将介绍一些 Python 常用的小脚本&…

VSCode离线安装插件

下载最新的VSCode&#xff0c;安装。 打开VSCODE&#xff0c;打开左边的EXTENSINS(拓展)&#xff0c;打开 Install from VSIX&#xff0c;找到 .vsix的文件&#xff0c;打开安装。完成。 1&#xff09;去哪找插件&#xff0c;当然是插件官网了&#xff0c;插件官网&#xff0c;…

MySQL 之 存储引擎

存储引擎 MySQL体系结构 连接层&#xff1a;最上层是一些客户端和链接服务&#xff0c;主要完成一些类似于连接处理、授权认证、及相关的安全方案。服务器也会安全接入的每个客户端验证它所具有的操作权限。服务层&#xff1a;第二层完成大多数的核心服务功能&#xff0c;如SQ…

12、论文阅读:SpikeYOLO:高性能低能耗目标检测网络

SpikeYOLO:高性能低能耗目标检测网络 前言解释介绍相关工作论文提出的方法网络输入SpikeYOLO架构概述网络输出宏观设计微观设计I-LIF脉冲神经元LIFI-LIF实验代码前言 脉冲神经网络(Spiking Neural Networks, SNNs)具有生物合理性和低功耗的优势,相较于人工神经网络(Artif…

Python实现股票自动交易:步骤、要点与注意事项有哪些?

炒股自动化&#xff1a;申请官方API接口&#xff0c;散户也可以 python炒股自动化&#xff08;0&#xff09;&#xff0c;申请券商API接口 python炒股自动化&#xff08;1&#xff09;&#xff0c;量化交易接口区别 Python炒股自动化&#xff08;2&#xff09;&#xff1a;获取…

机器学习探索性数据分析 (EDA)

机器学习探索性数据分析 (EDA) 探索性数据分析&#xff08;Exploratory Data Analysis, EDA&#xff09;是机器学习工作流中至关重要的一个步骤&#xff0c;通过深入分析和理解数据的结构、分布和相关性&#xff0c;EDA帮助揭示数据背后的故事&#xff0c;并为后续的建模提供有…

KMP 算法

目录 KMP 算法 算法思路 为什么不需要在主串中进行回退 计算 next 数组 代码实现 next 数组优化 查找所有起始位置 KMP 算法 KMP 算法是一种改进的字符串匹配算法&#xff0c;由 D.E.Knuth&#xff0c;J.H.Morris 和 V.R.Pratt 提出的&#xff0c;因此人们称它为 克努特…

【ODSS】An Open Dataset of Synthetic Speech

文章目录 An Open Dataset of Synthetic Speechkey pointsODSS数据集局限性An Open Dataset of Synthetic Speech 会议/期刊:WIFS 2023 作者: key points 一个由合成语音和自然语音组成的多语言、多说话人数据集ODSS,旨在促进合成语音检测的研究和基准测试。 是由156个声…

Android compose 重建流程1

前言 本文是笔者学习Compose是如何自动触发UI刷新的笔记,可能缺乏一定可读性和教导性.(建议阅读参考文献更具启发性) 使用以下BOM作为研究环境. composeBom "2024.04.01" androidx-compose-bom { group "androidx.compose", name "compose-bom…

HarmonyOS Next应用开发——图像PixelMap压缩保存

【高心星出品】 图片编码保存 图片编码指将PixelMap编码成不同格式的存档图片&#xff0c;当前支持打包为JPEG、WebP、png和 HEIF(不同硬件设备支持情况不同) 格式&#xff0c;用于后续处理&#xff0c;如保存、传输等。图片编码是图片解码-图片处理-图片保存的最后环节&…

C#中的接口的使用

定义接口 public interface IMyInterface {int MyProperty { get; set; }void MyMethod(); } 实现类 internal class MyClass : IMyInterface {public int MyProperty { get; set; }public void MyMethod(){Console.WriteLine("MyMethod is called");} } 目录结构…

logback-spring.xml 配置

<?xml version"1.0" encoding"UTF-8"?> <configuration debug"false"> <!-- 只需配置好 log.dir 和 appName 属性 --> <property name"log.dir" value"/alidata1/admin/prophet-za-metadata"/&g…

什麼是高速HTTP代理?

高速HTTP代理是一種用於加速和優化互聯網連接的技術。它通過在用戶和目標網站之間充當仲介伺服器&#xff0c;幫助用戶快速訪問網路資源。HTTP代理不僅可以提高訪問速度&#xff0c;還能提供一定程度的隱私保護和安全性。 高速HTTP代理的工作原理 HTTP代理伺服器位於用戶設備…

linux使用close函数关闭文件后,变跟内容会立即同步到磁盘吗

Linux使用close函数关闭文件后&#xff0c;变更内容不会立即同步到磁盘‌ ‌close函数的作用‌&#xff1a;Linux系统中的close函数用于关闭一个已经打开的文件描述符。当一个文件描述符被关闭后&#xff0c;它将不再引用任何文件&#xff0c;且这个文件描述符的值可以被后续的…

深度学习 %matplotlib inline

%matplotlib inline 是在 Jupyter Notebook 中使用的一个魔法命令&#xff0c;主要用于配置 Matplotlib 图形的显示方式。具体来说&#xff0c;这个命令的作用是将 Matplotlib 生成的图形直接嵌入到 notebook 中&#xff0c;而不是在弹出的窗口中显示。 使用方法 在 Jupyter …

苏宁商品详情接口技术解析与实战代码

在电商平台的开发中&#xff0c;商品详情接口是至关重要的一环&#xff0c;它提供了商品的详细信息&#xff0c;包括价格、库存、规格、图片等&#xff0c;为用户购物决策提供关键依据。苏宁作为国内知名的电商平台&#xff0c;其提供的商品详情接口为开发者提供了丰富的商品数…

负载箱的作用?

负载箱&#xff0c;顾名思义&#xff0c;就是用来承载电力设备的箱子。在电力系统中&#xff0c;负载箱的作用非常重要&#xff0c;它不仅可以模拟实际的电力负载&#xff0c;还可以对电力设备进行测试和调试&#xff0c;确保其正常运行。下面详细介绍负载箱的作用。 1. 模拟实…