【C++】做一个飞机空战小游戏(十)——子弹击落炮弹、炮弹与飞机相撞

[导读]本系列博文内容链接如下:

【C++】做一个飞机空战小游戏(一)——使用getch()函数获得键盘码值

【C++】做一个飞机空战小游戏(二)——利用getch()函数实现键盘控制单个字符移动
【C++】做一个飞机空战小游戏(三)——getch()函数控制任意造型飞机图标移动

【C++】做一个飞机空战小游戏(四)——给游戏添加背景音乐(多线程技巧应用)

【C++】做一个飞机空战小游戏(五)——getch()控制两个飞机图标移动(控制光标位置)

【C++】做一个飞机空战小游戏(六)——给两架飞机设置不同颜色(cout输出彩色字符、结构体使用技巧)

【C++】做一个飞机空战小游戏(七)——两组按键同时检测平滑移动(GetAsyncKeyState()函数应用)

【C++】做一个飞机空战小游戏(八)——生成敌方炮弹(rand()函数应用)

【C++】做一个飞机空战小游戏(九)——发射子弹的编程技巧

【C++】做一个飞机空战小游戏(十)——子弹击落炮弹、炮弹与飞机相撞

今天这节介绍实现子弹击落炮弹、炮弹与飞机相撞的效果,增加了飞机的血量属性,炮弹撞击一次掉血三分之一,三次撞击则飞机被炮弹击落。还增加了飞机命数属性,飞机设置了3条命,飞机命数大于0的时候可以按复活键进行复活,如果命为0时,飞机不可复活。还增加了游戏暂停功能,按空格键游戏暂停,再按一次即可恢复游戏。

目录

一、炮弹与子弹或飞机相撞

(一)子弹击落炮弹

1、子弹、炮弹初始血量和伤害值

2、子弹与炮弹相撞后血量

3、击落函数

4、击落后血量变化代码

(二)炮弹与飞机相撞

1、飞机结构体

2、飞机初始化函数

(1)单个飞机初始化

(2)所有飞机初始化

3、飞机初始血量

4、飞机与炮弹相撞后血量

(1)炮弹血量

(2)飞机血量

5、飞机命数

6、相撞函数

7、相撞后血量命数变化代码

8、飞机复活

二、游戏暂停功能

(一)game中增加pause属性

(二)游戏暂停按键

(三)游戏暂停代码

三、完整代码

(一)主函数

(二)头文件control_plane.h

(三)库函数control_plane.cpp

四、运行效果

(一)子弹与炮弹相撞

(二)炮弹与飞机相撞、飞机复活


一、炮弹与子弹或飞机相撞

(一)子弹击落炮弹

1、子弹、炮弹初始血量和伤害值

子弹炮弹相撞前:

子弹、炮弹血量hp=1;

子弹、炮弹伤害值dam=1;

2、子弹与炮弹相撞后血量

子弹炮弹相撞后:

相撞后子弹血量=相撞前子弹血量-炮弹伤害值;

相撞后炮弹血量=相撞前炮弹血量-子弹伤害值;

因此,子弹炮弹相撞后,两者血量都为0,alive值都为false,会同归于尽,炮弹被击落。

3、击落函数

 bool shoot(Bullet bullet,Bomb bomb)
{
	bool sbb,sx0,sx1,sy0,sy1;
	sx0=(bomb.location.x>=bullet.location.x-1);
	sx1=(bomb.location.x<=bullet.location.x+1);	
	sy0=(bomb.location.y>=bullet.location.y);
	sy1=(bomb.location.y<=bullet.location.y+1);	
	sbb=sx0 &&sx1&&sy0&&sy1;
	return sbb;
}

4、击落后血量变化代码

这部分代码在子弹位置更新线程中,具体如下:

for(int k=0;k

(二)炮弹与飞机相撞

1、飞机结构体

//定义飞机结构体 
typedef struct{
	Location location;		//飞机坐标 
	int color;				//飞机颜色 
	int icon;				//飞机图标编号 
	direction_cmd keycmd;	//飞机移动方向命令 
	bool fire;				//飞机是否开火 
	int cnt_bullets; 		//按一次开火键加1,然后初始化bullet[cnt_bullets],子弹死亡一个减1
	int hp;					//飞机血量 
	bool alive;				//飞机存活标志 
	int No;					//飞机序号 
	int life;				//飞机命数
}Plane;

2、飞机初始化函数

(1)单个飞机初始化

//单个飞机初始化函数
Plane init_plane(Plane plane)
{
	plane.alive=true;	
	plane.hp=3;	
	plane.keycmd=none_cmd;
	plane.location=plocation[plane.No];
	plane.color=plane.No+1;
	plane.icon=plane.No+1;		
	return plane;
}

初始化函数用在游戏开始和飞机复活时,函数中没有设置No和life两项,因为No值不同,而life值每次死亡时减1,复活后不能再恢复为原值。

(2)所有飞机初始化

//所有飞机初始化函数 
void init_planes(void)
{
	for(int i=0;i

3、飞机初始血量

飞机初始血量hp=3,飞机没有设置伤害值,默认其伤害值为无穷大。

4、飞机与炮弹相撞后血量

(1)炮弹血量

炮弹与飞机相撞后,炮弹血量直接降为0,alive值为false。

(2)飞机血量

飞机与炮弹相撞后,相撞后飞机血量值=相撞前血量值-炮弹伤害值。飞机血量值小于等于0时,飞机死亡,alive值为false。

5、飞机命数

飞机命数life初始值为3。如果飞机死亡,其命数life值减1。

6、相撞函数

 bool collide(Plane plane,Bomb bomb)
{
	bool cpb,cx0,cx1,cy0,cy1;
	cx0=bomb.location.x>=plane.location.x-1;
	cx1=bomb.location.x<=plane.location.x+plane_width;
	cy0=bomb.location.y>=plane.location.y;
	cy1=bomb.location.y<=plane.location.y+plane_height; 
	
	cpb=cx0 && cx1 && cy0 && cy1;
	return cpb;
}

7、相撞后血量命数变化代码

飞机血量、命数的变化代码放置在炮弹位置更新线程中,具体内容如下:

for(int j=0;j

8、飞机复活

按复活键可复活,当命数小于1时,不能再复活,永久死亡。plane[0]的复活代码如下:

if(plane[0].life>0)
{
	if (GetAsyncKeyState('O') & 0x8000)							//飞机复活指令
	{
		plane[0]=init_plane(plane[0]);
		show_plane(plane[0]);
	}
}

二、游戏暂停功能

(一)game中增加pause属性

//定义游戏结构体 
typedef struct{
	int stage;					//游戏当前关 	
	int bombs_round;			//敌方每轮发射炮弹数量 
	int bombs_stage;			//每关总计出现炮弹数量 
	bool clear;					//游戏过关 
	bool complete;				//游戏通关 
	bool gameover;				//游戏结束
	int num_plane;				//飞机数量 
	int cur_num_bomb;			//当前已发射炮弹数量 
	int bomb_interval; 			//位置更新间隔 
	bool bomb_move;				//炮弹是否移动
	bool bullet_move;			//子弹是否移动
	bool pause;					//游戏是否暂停 
}Game;

(二)游戏暂停按键

游戏暂停键为空格键,游戏进行时,按下空格键game.pause=true,游戏暂停,再按一次game.pause=false,游戏恢复,代码在key()函数中,内容如下:

if (GetAsyncKeyState(VK_SPACE) & 0x8000)
{
	Sleep(50);
	game.pause=!game.pause;					
}

代码中Sleep(50)为按键去抖动作用,防止按了一次被识别为多次。

(三)游戏暂停代码

游戏暂停时,主函数中除背景音乐之外所有的线程都暂停。在需要暂停的线程中加入如下代码:

while(game.pause)
{
	;
}

三、完整代码

(一)主函数

#include "control_plane.h"
#include "quenue.h"
using namespace std; 
 
Plane plane[eq_plane];
Game game;
Bomb bomb[eq_bombs_round];
Bullet bullet[eq_plane][eq_bullets_round];
Location plocation[]={{2*r_b/3,b_b},{r_b/3,b_b}};
 
int main(int argc, char** argv) {	
	
	
	init();	//初始化					 
 
	bgmusic();//播放背景音乐
	getkey();
	bomb_location_update();	
	bullet_location_update();	
	
	while(1)					//循环等待键盘指令 
	{
		while(game.pause)
		{
			;
		}
		
		if(plane[0].keycmd!=none_cmd ||plane[1].keycmd!=none_cmd ||game.bomb_move ||game.bullet_move)
		{
			game.bomb_move=false;
			game.bullet_move=false;
			system("cls");
			for(int i=0;i

(二)头文件control_plane.h

#ifndef CONTROL_PLANE_H
#define CONTROL_PLANE
#include 
#include 
#include 
#include
#include
#include //导入线程头文件库
#include  //导入声音头文件库
#pragma comment(lib,"winmm.lib")//导入声音的链接库
#define _CRT_SECURE_NO_WARNINGS 
using namespace std;
 
#define t_b 0  	//图形显示区域上侧边界 
#define l_b 0	//图形显示区域左侧边界
#define r_b 100	//图形显示区域右侧边界
#define b_b 20	//图形显示区域下侧边界
#define plane_width 9
#define plane_height 6

#define eq_plane 2	//飞机架数
#define eq_bombs_round 23	//eq=end quantity最终炮弹数量 
#define eq_rt 10	//复活最大时间
#define eq_bullets_round 20	//eq=end quantity飞机一次发射最多子弹数量 
 
//定义飞机造型 
const string icon_plane1[]={"    ■","■  ■  ■","■■■■■","■  ■  ■","    ■","  ■■■"};
const string icon_plane2[]={"    ■","■  ■  ■","■■■■■","    ■","  ■■■","■■■■■"};

//定义炮弹造型
const string icon_bomb="■";

//定义子弹造型
const string icon_bullet="■";
 
//定义坐标结构体 
typedef struct{
	int x;
	int y;
} Location;
 
 
//定义移动方向命令枚举类型 
typedef  enum {none_cmd,up_cmd,down_cmd,left_cmd,right_cmd} direction_cmd;

//定义游戏结构体 
typedef struct{
	int stage;					//游戏当前关 	
	int bombs_round;			//敌方每轮发射炮弹数量 
	int bombs_stage;			//每关总计出现炮弹数量 
	bool clear;					//游戏过关 
	bool complete;				//游戏通关 
	bool gameover;				//游戏结束
	int num_plane;				//飞机数量 
	int cur_num_bomb;			//当前已发射炮弹数量 
	int bomb_interval; 			//位置更新间隔 
	bool bomb_move;				//炮弹是否移动
	bool bullet_move;			//子弹是否移动
	bool pause;					//游戏是否暂停 
}Game;
 
//定义飞机结构体 
typedef struct{
	Location location;		//飞机坐标 
	int color;				//飞机颜色 
	int icon;				//飞机图标编号 
	direction_cmd keycmd;	//飞机移动方向命令 
	bool fire;				//飞机是否开火 
	int cnt_bullets; 		//按一次开火键加1,然后初始化bullet[cnt_bullets],子弹死亡一个减1
	int hp;					//飞机血量 
	bool alive;				//飞机存活标志 
	int No;					//飞机序号 
	int life;				//飞机命数
}Plane;

//定义敌方炮弹结构体 
typedef struct{
	Location location;	//炮弹位置 
	bool alive;			//炮弹是否存活 
	int color;			//炮弹颜色
	string icon;		//炮弹图标
	int rt;				//rt=respawn time复活时间 
	int hp;				//hp=hit point 生命值,此值<=0时,敌方炮弹死亡,敌方炮弹被飞机子弹击中hp会减少,坠地或与飞机相撞hp直接降为0 
	int dam;			//dam=damage 伤害值
	int type;			//炮弹类型 
}Bomb;

//定义子弹结构体 
typedef struct{
	Location location;	//子弹位置 
	bool alive;			//子弹是否存活 
	int color;			//子弹颜色
	string icon;		//子弹图标	
	int hp;				//hp=hit point 生命值,子弹击中炮弹或冲出屏幕上方hp直接降为0,子弹死亡 
	int dam;			//dam=damage 伤害值
	int type;			//子弹类型 
}Bullet;

 
extern Plane plane[eq_plane];
extern Game game;
extern Bomb bomb[eq_bombs_round];
extern Bullet bullet[eq_plane][eq_bullets_round];

extern Location plocation[];
//extern Location plocation1={r_b/3,b_b};
 
//声明刷新飞机位置函数
void show_plane(Plane plane);
 
//获取键盘指令 
void key(void);
 
//更新所有飞机坐标
void plane_location_update(void);
 
//初始化函数 
void init(void);
 
//播放背景音乐线程 
void* thread_bgmusic(void* arg);
void play_bgmusic();
void bgmusic();
 
//获取按键指令线程
void* thread_key(void* arg);
void getkey();
 
//输出彩色字符函数
template	//T表示任何可以被cout输出的类型 
void ColorCout(T t, const int ForeColor = 7, const int BackColor = 0);

void init_bombs(void);
Bomb init_(Bomb bomb);
void* thread_bomb(void* arg);
void bomb_location_update();
void show_bomb(Bomb bomb);


void bullet_location_update();			//子弹位置更新 
void* thread_bullet(void* arg);			//子弹线程函数 
Bullet init_bullet(Bullet bullet);		//单个子弹初始化
void init_bullets(void);				//所有子弹初始化
Bullet fire(Plane plane,Bullet bullet);	//飞机开火函数,确定子弹出现的起始位置 
void show_bullet(Bullet bullet);		//显示子弹图标 


bool collide(Plane plane,Bomb bomb);
bool shoot(Bullet bullet,Bomb bomb);

Plane init_plane(Plane plane);
void init_planes(void);

#endif

(三)库函数control_plane.cpp

#include 
#include "conio.h"
#include 
#include "control_plane.h"
#include
using namespace std;
 
 
//彩色输出函数
template	//T表示任何可以被cout输出的类型
void ColorCout(T t, const int ForeColor = 7, const int BackColor = 0)
{
	//	0 = 黑色	1 = 蓝色	 2 = 绿色	 3 = 浅绿色		 4 = 红色	 5 = 紫色	 6 = 黄色	 7 = 白色
	//	8 = 灰色	9 = 淡蓝色	10 = 淡绿色	11 = 淡浅绿色	12 = 淡红色	13 = 淡紫色	14 = 淡黄色	15 = 亮白色
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ForeColor + BackColor * 0x10);
	cout << t;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
 
 
//隐藏光标函数
HANDLE han = GetStdHandle(-11);
void hide(){
	CONSOLE_CURSOR_INFO cursor;
	cursor.bVisible = 0;
	cursor.dwSize = 1;
	SetConsoleCursorInfo(han,&cursor);
}
 
//初始化函数 
void init(void)
{
	plane[0].No=0;
	plane[1].No=1;
	
	plane[0].life=3;		
	plane[1].life=3;
	
	srand(time(NULL));
	
	game.num_plane=2;
	game.bombs_round=3;
	game.bomb_move=false;
	game.bullet_move=false;
	game.bomb_interval=1000;
	game.stage=1;
	game.bombs_stage=100;
	game.pause=false;	
	
	init_bombs();
	init_bullets();
	init_planes();
	
	system("cls");
	for(int i=0;i0)
			{
				if (GetAsyncKeyState('O') & 0x8000)							//飞机复活指令
				{
					plane[0]=init_plane(plane[0]);
					show_plane(plane[0]);
				}
			}
			
		}
		
		if(plane[1].alive)
		{
			direction_cmd d=none_cmd;
			if (GetAsyncKeyState('W') & 0x8000)	d = up_cmd;
			if (GetAsyncKeyState('S') & 0x8000)	d = down_cmd;
			if (GetAsyncKeyState('A') & 0x8000)	d = left_cmd;
			if (GetAsyncKeyState('D') & 0x8000)	d = right_cmd;
			plane[1].keycmd=d;//刷新飞机图标
			if (GetAsyncKeyState('F') & 0x8000)			plane[1].fire = true;
		}
		else
		{
			if(plane[1].life>0)
			{
				if (GetAsyncKeyState('Q') & 0x8000)
				{
					plane[1]=init_plane(plane[1]);
					show_plane(plane[1]);
				}
			}					
		}	
	}	
}
 
void gotoxy(int x, int y) {
	COORD pos = { x,y };
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出设备句柄
	SetConsoleCursorPosition(hOut, pos);//两个参数分别指定哪个窗口,具体位置
}
 
 
//飞机图标刷新函数 
void show_plane(Plane plane)		//预先定义字符定位显示函数,x是列坐标,y是行坐标,原点(x=0,y=0)位于屏幕左上角 
{
	int x,y;
	int i,j;
	int rows;
	x=plane.location.x;
	y=plane.location.y;
	
	switch(plane.icon)
	{
		case 1://第一种造型 
			rows=sizeof(icon_plane1)/sizeof(icon_plane1[0]);
			for(i=0;ib_b)			//限定y高度 
					{
						y=b_b;
					}
					break;
				case left_cmd:
					x--;				//字符左移一列,列值x减1
					if(xr_b)
					{
						x=r_b;			//限定x宽度
					}
					break;
				
			}
			plane[i].location.x=x;
 			plane[i].location.y=y;
 			plane[i].keycmd=none_cmd;	
		}
					
	} 		
}

//单个炮弹初始化函数 
Bomb init_bomb(Bomb bomb)
{
	bomb.location.x=rand()%r_b;	
	bomb.location.y=b_b+6;
	bomb.icon=icon_bomb;
	bomb.color=6;
	bomb.dam=1;
	bomb.hp=1;
	bomb.alive=false;
	bomb.rt=rand()%(eq_rt+1)+1;	
	return bomb;			
}

//所有炮弹初始化函数 
void init_bombs(void)
{
	game.bomb_move=false;
	
	for(int i=0;ib_b+5)
				{
					bomb[i].hp=0;									
				}
			
				for(int j=0;j=plane.location.x-1;
	cx1=bomb.location.x<=plane.location.x+plane_width;
	cy0=bomb.location.y>=plane.location.y;
	cy1=bomb.location.y<=plane.location.y+plane_height; 
	
	cpb=cx0 && cx1 && cy0 && cy1;
	return cpb;
}

 bool shoot(Bullet bullet,Bomb bomb)
{
	bool sbb,sx0,sx1,sy0,sy1;
	sx0=(bomb.location.x>=bullet.location.x-1);
	sx1=(bomb.location.x<=bullet.location.x+1);	
	sy0=(bomb.location.y>=bullet.location.y);
	sy1=(bomb.location.y<=bullet.location.y+1);	
	sbb=sx0 &&sx1&&sy0&&sy1;
	return sbb;
}

//********************************************************************************
 //以下三个函数为播放背景音乐功能 
//********************************************************************************
 
 //播放一遍背景音乐 
 void play_bgmusic() {  
 
	mciSendString(TEXT("open hero.mp3 alias s1"),NULL,0,NULL);
	mciSendString(TEXT("play s1"),NULL,0,NULL);
 
	Sleep(153*1000);//153*1000意思是153秒,是整首音乐的时长 
	mciSendString(TEXT("close S1"),NULL,0,NULL); 
  
}
 
//循环播放音乐线程函数 
void* thread_bgmusic(void* arg) //
{ 
  while(1)
  {  	
  	play_bgmusic();
  }
} 
 
//创建音乐播放线程,开始循环播放音乐 
void bgmusic()
{
	pthread_t tid; 
  	pthread_create(&tid, NULL, thread_bgmusic, NULL);
}

四、运行效果

(一)子弹与炮弹相撞

【C++】做一个飞机空战小游戏(十)——子弹击落炮弹、炮弹与飞机相撞_第1张图片

(二)炮弹与飞机相撞、飞机复活

【C++】做一个飞机空战小游戏(十)——子弹击落炮弹、炮弹与飞机相撞_第2张图片

 (未完待续)

你可能感兴趣的:(程序与算法,C与C++,c++,开发语言,游戏,键盘)