C语言写飞机大战小游戏(有音乐背景和图片)

大家好 , 我是逼哥 , 记得每天好好学习 , 天天向上 , 尤其是大学生 . 不要荒废学业.

首先说明 , 我使用的开发环境是  vs2017  , 有些函数方法可能不通用 ,大家可以百度下其他方法. 向童老师致敬

原归正状 :

第一步 : 放置背景图和战斗机以及敌机和子弹头(静态)

效果图:

C语言写飞机大战小游戏(有音乐背景和图片)_第1张图片

#include 
#include 

// 引用 Windows Multimedia API
#pragma comment(lib,"Winmm.lib")

#define width 591
#define high 864

//全局变量
IMAGE img_bk; // 背景图片
float air_x, air_y; // 飞机位置
float bullet_x, bullet_y; // 子弹位置
float enemy_x, enemy_y; // 敌机位置
IMAGE img_air1, img_air2; // 正常飞机图片
IMAGE img_airExplode1, img_airExplode2; // 爆炸飞机图片
IMAGE img_bullet1, img_bullet2; // 子弹图片
IMAGE img_enemyPlane1, img_enemyPlane2; // 敌机图片
int isExpolde = 0; // 飞机是否爆炸
int score = 0; // 得分


void startup()
{
	initgraph(width, high);
	loadimage(&img_bk, _T("D:\\桌面\\fighter\\background.jpg"));
	// 战斗机
	loadimage(&img_air1, _T("D:\\桌面\\fighter\\planeExplode_1.jpg"));
	loadimage(&img_air2, _T("D:\\桌面\\fighter\\planeExplode_2.jpg"));
	// 子弹
	loadimage(&img_bullet1, _T("D:\\桌面\\fighter\\bullet1.jpg"));
	loadimage(&img_bullet2, _T("D:\\桌面\\fighter\\bullet2.jpg"));
	// 敌机
	loadimage(&img_enemyPlane1, _T("D:\\桌面\\fighter\\enemyPlane1.jpg"));
	loadimage(&img_enemyPlane2, _T("D:\\桌面\\fighter\\enemyPlane2.jpg"));

	air_x = width / 2;
	air_y = high * 2 / 3; 
	bullet_x = air_x + 118 / 2;
	bullet_y = air_y;
	enemy_x = width / 2;
	enemy_y = 0;
	BeginBatchDraw();


	//mciSendString(_T("open D:\\桌面\\super_bird\\background.mp3 alias bkmusic"), NULL, 0, NULL);//打开背景音乐
	//mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL);  // 循环播放
}

void show()
{
	putimage(0, 0, &img_bk);	// 显示背景	
	putimage(air_x, air_y, &img_enemyPlane1, NOTSRCERASE);  // 遮罩透明处理
	putimage(air_x, air_y, &img_enemyPlane2, SRCINVERT);// 显示斗鸡	
	// 子弹
	putimage(bullet_x, bullet_y, &img_bullet1, NOTSRCERASE);  // 遮罩透明处理
	putimage(bullet_x, bullet_y, &img_bullet2, SRCINVERT);// 显示
	// 敌机
	putimage(enemy_x, enemy_y, &img_enemyPlane1, NOTSRCERASE);  // 遮罩透明处理
	putimage(enemy_x, enemy_y, &img_enemyPlane2, SRCINVERT);// 显示

	FlushBatchDraw();
	Sleep(50);
}

void updateWithoutInput()
{
	// 输入空格进行控制小鸟
	char input;
	if (_kbhit())
	{
		input = _getch();
		
	}
}

// 图形更新
void updateWithInput()
{

}

void gameover()
{

	EndBatchDraw();
	_getch();
	closegraph();
}

int main()
{
	startup();  // 数据初始化	
	while (1)  //  游戏循环执行
	{
		show();  // 显示画面
		updateWithoutInput();  // 界面的更新
		updateWithInput();     // 与用户输入有关的更新
	}
	gameover();     // 游戏结束、后续处理
	return 0;
}

 

第二步: 操控

1. 移动战斗机 

2.发射子弹

3. 敌机下落

效果展示:

C语言写飞机大战小游戏(有音乐背景和图片)_第2张图片

#include 
#include 

// 引用 Windows Multimedia API
#pragma comment(lib,"Winmm.lib")

#define width 591
#define high 864

//全局变量
IMAGE img_bk; // 背景图片
float air_x, air_y; // 飞机位置
float bullet_x, bullet_y; // 子弹位置
float enemy_x, enemy_y; // 敌机位置
IMAGE img_air1, img_air2; // 正常飞机图片
IMAGE img_airExplode1, img_airExplode2; // 爆炸飞机图片
IMAGE img_bullet1, img_bullet2; // 子弹图片
IMAGE img_enemyPlane1, img_enemyPlane2; // 敌机图片
int isExpolde = 0; // 飞机是否爆炸
int score = 0; // 得分


void startup()
{
	initgraph(width, high);
	loadimage(&img_bk, _T("D:\\桌面\\fighter\\background.jpg"));
	// 战斗机
	loadimage(&img_air1, _T("D:\\桌面\\fighter\\planeNormal_1.jpg"));
	loadimage(&img_air2, _T("D:\\桌面\\fighter\\planeNormal_2.jpg"));
	// 子弹
	loadimage(&img_bullet1, _T("D:\\桌面\\fighter\\bullet1.jpg"));
	loadimage(&img_bullet2, _T("D:\\桌面\\fighter\\bullet2.jpg"));
	// 敌机
	loadimage(&img_enemyPlane1, _T("D:\\桌面\\fighter\\enemyPlane1.jpg"));
	loadimage(&img_enemyPlane2, _T("D:\\桌面\\fighter\\enemyPlane2.jpg"));

	air_x = width / 2;
	air_y = high * 2 / 3; 
	bullet_x = air_x + 118 / 2 -15; // 飞机宽度117 , 子弹宽度20
	bullet_y = air_y -24 ;
	enemy_x = width / 2;
	enemy_y = 0;
	BeginBatchDraw();


	//mciSendString(_T("open D:\\桌面\\super_bird\\background.mp3 alias bkmusic"), NULL, 0, NULL);//打开背景音乐
	//mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL);  // 循环播放
}

void show()
{
	putimage(0, 0, &img_bk);	// 显示背景	
	putimage(air_x, air_y, &img_air1, NOTSRCERASE);  // 遮罩透明处理
	putimage(air_x, air_y, &img_air2, SRCINVERT);// 显示斗鸡	
	// 子弹
	putimage(bullet_x, bullet_y, &img_bullet1, NOTSRCERASE);  // 遮罩透明处理
	putimage(bullet_x, bullet_y, &img_bullet2, SRCINVERT);// 显示
	// 敌机
	putimage(enemy_x, enemy_y, &img_enemyPlane1, NOTSRCERASE);  // 遮罩透明处理
	putimage(enemy_x, enemy_y, &img_enemyPlane2, SRCINVERT);// 显示

	FlushBatchDraw();
	Sleep(50);
}

void updateWithoutInput()
{
	// 输入空格进行控制小鸟
	char input;
	if (_kbhit())
	{
		input = _getch();
		if (input == ' ' )
		{
			bullet_x = air_x + 118 / 2 - 15; // 飞机宽度117 , 子弹宽度20
			bullet_y = air_y - 24;
		}
		else if (input == 'a' && air_x > 0)
			air_x -= 20;
		else if (input == 'd' && air_x <> width - 117)
			air_x += 20;
		else if (input == 'w' && air_y > 0)
			air_y -= 20;
		else if (input == 's' && air_y < high - 120)
			air_y += 20;
	}
}

// 图形更新
void updateWithInput()
{
	if( bullet_y > -20)
		bullet_y -= 20;

	if (enemy_y < high)
		enemy_y ++;
	else
	{
		enemy_x = rand() % width;
		enemy_y = -120;
	}
}

void gameover()
{

	EndBatchDraw();
	_getch();
	closegraph();
}

int main()
{
	startup();  // 数据初始化	
	while (1)  //  游戏循环执行
	{
		show();  // 显示画面
		updateWithoutInput();  // 界面的更新
		updateWithInput();     // 与用户输入有关的更新
	}
	gameover();     // 游戏结束、后续处理
	return 0;
}

 

第三步: 子弹射中敌机

1 .  击中敌机

2.  得分

3.撞击爆炸

效果图:

C语言写飞机大战小游戏(有音乐背景和图片)_第3张图片

#include 
#include 
#include 
#include 

// 引用 Windows Multimedia API
#pragma comment(lib,"Winmm.lib")

#define width 591
#define high 864

//全局变量
IMAGE img_bk; // 背景图片
int air_x, air_y; // 飞机位置
int bullet_x, bullet_y; // 子弹位置
int enemy_x, enemy_y; // 敌机位置
IMAGE img_air1, img_air2; // 正常飞机图片
IMAGE img_airExplode1, img_airExplode2; // 爆炸飞机图片
IMAGE img_bullet1, img_bullet2; // 子弹图片
IMAGE img_enemyPlane1, img_enemyPlane2; // 敌机图片
int isExpolde = 0; // 飞机是否爆炸
int score = 0; // 得分


void startup()
{
	initgraph(width, high);
	loadimage(&img_bk, _T("D:\\桌面\\fighter\\background.jpg"));
	// 战斗机
	loadimage(&img_air1, _T("D:\\桌面\\fighter\\planeNormal_1.jpg"));
	loadimage(&img_air2, _T("D:\\桌面\\fighter\\planeNormal_2.jpg"));
	//爆炸
	loadimage(&img_airExplode1, _T("D:\\桌面\\fighter\\planeExplode_1.jpg"));
	loadimage(&img_airExplode2, _T("D:\\桌面\\fighter\\planeExplode_2.jpg"));
	// 子弹
	loadimage(&img_bullet1, _T("D:\\桌面\\fighter\\bullet1.jpg"));
	loadimage(&img_bullet2, _T("D:\\桌面\\fighter\\bullet2.jpg"));
	// 敌机
	loadimage(&img_enemyPlane1, _T("D:\\桌面\\fighter\\enemyPlane1.jpg"));
	loadimage(&img_enemyPlane2, _T("D:\\桌面\\fighter\\enemyPlane2.jpg"));

	air_x = width / 2;
	air_y = high * 2 / 3; 
	//bullet_x = air_x + 118 / 2 - 15; // 飞机宽度117 , 子弹宽度20
	//bullet_y = air_y - 24;

	enemy_x = rand() % (width - 104); // 随机生成 104*148
	enemy_y = 50;

	BeginBatchDraw();


	//mciSendString(_T("open D:\\桌面\\super_bird\\background.mp3 alias bkmusic"), NULL, 0, NULL);//打开背景音乐
	//mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL);  // 循环播放
}

void show()
{
	putimage(0, 0, &img_bk);	// 显示背景	
	if (isExpolde == 0)
	{
		putimage(air_x, air_y, &img_air1, NOTSRCERASE);  // 遮罩透明处理
		putimage(air_x, air_y, &img_air2, SRCINVERT);// 显示斗鸡	
													 // 子弹
		putimage(bullet_x, bullet_y, &img_bullet1, NOTSRCERASE);  // 遮罩透明处理
		putimage(bullet_x, bullet_y, &img_bullet2, SRCINVERT);// 显示
															  // 敌机
		putimage(enemy_x, enemy_y, &img_enemyPlane1, NOTSRCERASE);  // 遮罩透明处理
		putimage(enemy_x, enemy_y, &img_enemyPlane2, SRCINVERT);// 显示
	}
	else
	{
		putimage(air_x, air_y, &img_airExplode1, NOTSRCERASE);  // 遮罩透明处理
		putimage(air_x, air_y, &img_airExplode2, SRCINVERT);// 显示斗鸡	
	}
	outtextxy(width *0.5, high *0.9, _T("得分:"));

	TCHAR  s[5];
	swprintf_s(s, _T("%d"), score);		// 高版本 VC 推荐使用 _stprintf_s 函数
	outtextxy(width *0.55, high *0.9, s);

	FlushBatchDraw();
	Sleep(50);
}

void gameover()
{

	EndBatchDraw();
	_getch();
	closegraph();
}

void updateWithoutInput()
{
	// 输入空格进行控制小鸟
	char input;
	if (_kbhit())
	{
		input = _getch();
		if (input == ' ' )
		{
			bullet_x = air_x + 118 / 2 - 15; // 飞机宽度117 , 子弹宽度20
			bullet_y = air_y - 24;
		}
		else if (input == 'a' && air_x > 0)
			air_x -= 20;
		else if (input == 'd' && air_x < width - 117)
			air_x += 20;
		else if (input == 'w' && air_y > 0)
			air_y -= 20;
		else if (input == 's' && air_y < high - 120)
			air_y += 20;
	}
}

// 图形更新
void updateWithInput()
{
	if( bullet_y > -20)
		bullet_y -= 20;

	if (enemy_y < high)
		enemy_y ++;
	else
	{
		enemy_x = rand() % (width - 104) ; // 随机生成 104*148
		enemy_y = 50  ;
	}
	//判断 子弹击中敌机  abs(enemy_y - bullet_y) + abs(enemy_x - bullet_x) < 50
	if ( (bullet_x > enemy_x) && (bullet_x < enemy_x + 104) && (bullet_y  < enemy_y +148))
	{
		score++;
		enemy_x = rand() % (width - 104); // 随机生成 104*148
		enemy_y = 50;
	}

	// 撞机了
	if ((air_x > enemy_x) && (air_x < enemy_x + 104) && (air_y  < enemy_y + 148))
	{
		isExpolde = 1;
		//gameover();     // 游戏结束、后续处理
	}
}


int main()
{
	startup();  // 数据初始化	
	while (1)  //  游戏循环执行
	{
		show();  // 显示画面
		updateWithoutInput();  // 界面的更新
		updateWithInput();     // 与用户输入有关的更新
	}
	gameover();     // 游戏结束、后续处理
	return 0;
}

 

可以添加音乐:

1.添加背景音效.

2.添加打击音效

#include 
#include 
#include 
#include 

// 引用 Windows Multimedia API
#pragma comment(lib,"Winmm.lib")

#define width 591
#define high 864

//全局变量
IMAGE img_bk; // 背景图片
int air_x, air_y; // 飞机位置
int bullet_x, bullet_y; // 子弹位置
int enemy_x, enemy_y; // 敌机位置
IMAGE img_air1, img_air2; // 正常飞机图片
IMAGE img_airExplode1, img_airExplode2; // 爆炸飞机图片
IMAGE img_bullet1, img_bullet2; // 子弹图片
IMAGE img_enemyPlane1, img_enemyPlane2; // 敌机图片
int isExpolde = 0; // 飞机是否爆炸
int score = 0; // 得分


void startup()
{
	initgraph(width, high);
	loadimage(&img_bk, _T("D:\\桌面\\fighter\\background.jpg"));
	// 战斗机
	loadimage(&img_air1, _T("D:\\桌面\\fighter\\planeNormal_1.jpg"));
	loadimage(&img_air2, _T("D:\\桌面\\fighter\\planeNormal_2.jpg"));
	//爆炸
	loadimage(&img_airExplode1, _T("D:\\桌面\\fighter\\planeExplode_1.jpg"));
	loadimage(&img_airExplode2, _T("D:\\桌面\\fighter\\planeExplode_2.jpg"));
	// 子弹
	loadimage(&img_bullet1, _T("D:\\桌面\\fighter\\bullet1.jpg"));
	loadimage(&img_bullet2, _T("D:\\桌面\\fighter\\bullet2.jpg"));
	// 敌机
	loadimage(&img_enemyPlane1, _T("D:\\桌面\\fighter\\enemyPlane1.jpg"));
	loadimage(&img_enemyPlane2, _T("D:\\桌面\\fighter\\enemyPlane2.jpg"));

	air_x = width / 2;
	air_y = high * 2 / 3; 
	//bullet_x = air_x + 118 / 2 - 15; // 飞机宽度117 , 子弹宽度20
	//bullet_y = air_y - 24;

	enemy_x = rand() % (width - 104); // 随机生成 104*148
	enemy_y = 50;

	BeginBatchDraw();


	mciSendString(_T("open D:\\桌面\\fighter\\game_music.mp3 alias bkmusic"), NULL, 0, NULL);//打开背景音乐
	mciSendString(_T("play bkmusic repeat"), NULL, 0, NULL);  // 循环播放
}

void show()
{
	putimage(0, 0, &img_bk);	// 显示背景	
	if (isExpolde == 0)
	{
		putimage(air_x, air_y, &img_air1, NOTSRCERASE);  // 遮罩透明处理
		putimage(air_x, air_y, &img_air2, SRCINVERT);// 显示斗鸡	
													 // 子弹
		putimage(bullet_x, bullet_y, &img_bullet1, NOTSRCERASE);  // 遮罩透明处理
		putimage(bullet_x, bullet_y, &img_bullet2, SRCINVERT);// 显示
															  // 敌机
		putimage(enemy_x, enemy_y, &img_enemyPlane1, NOTSRCERASE);  // 遮罩透明处理
		putimage(enemy_x, enemy_y, &img_enemyPlane2, SRCINVERT);// 显示
	}
	else
	{
		putimage(air_x, air_y, &img_airExplode1, NOTSRCERASE);  // 遮罩透明处理
		putimage(air_x, air_y, &img_airExplode2, SRCINVERT);// 显示斗鸡	
	}
	outtextxy(width *0.5, high *0.9, _T("得分:"));

	TCHAR  s[5];
	swprintf_s(s, _T("%d"), score);		// 高版本 VC 推荐使用 _stprintf_s 函数
	outtextxy(width *0.55, high *0.9, s);

	FlushBatchDraw();
	Sleep(50);
}

void gameover()
{

	EndBatchDraw();
	_getch();
	closegraph();
}

void updateWithoutInput()
{
	// 输入空格进行控制小鸟
	char input;
	if (_kbhit())
	{
		input = _getch();
		if (input == ' ' )
		{
			bullet_x = air_x + 118 / 2 - 15; // 飞机宽度117 , 子弹宽度20
			bullet_y = air_y - 24;
		}
		else if (input == 'a' && air_x > 0)
			air_x -= 20;
		else if (input == 'd' && air_x < width - 117)
			air_x += 20;
		else if (input == 'w' && air_y > 0)
			air_y -= 20;
		else if (input == 's' && air_y < high - 120)
			air_y += 20;
	}
}

// 图形更新
void updateWithInput()
{
	if( bullet_y > -20)
		bullet_y -= 20;

	if (enemy_y < high)
		enemy_y ++;
	else
	{
		enemy_x = rand() % (width - 104) ; // 随机生成 104*148
		enemy_y = 50  ;
	}
	//判断 子弹击中敌机  abs(enemy_y - bullet_y) + abs(enemy_x - bullet_x) < 50
	if ( (bullet_x > enemy_x) && (bullet_x < enemy_x + 104) && (bullet_y  < enemy_y +148))
	{
		score++;
		enemy_x = rand() % (width - 104); // 随机生成 104*148
		enemy_y = 50;

		mciSendString(_T("close gemusic"), NULL, 0, NULL); // 先把前面一次的音乐关闭
		mciSendString(_T("open D:\\桌面\\fighter\\gotEnemy.mp3 alias gemusic"), NULL, 0, NULL); // 打开音乐
		mciSendString(_T("play gemusic"), NULL, 0, NULL); // 仅播放一次
		score++;
		if (score>0 && score % 5 == 0 && score % 2 != 0)
		{
			mciSendString(_T("close 5music"), NULL, 0, NULL); // 先把前面一次的音乐关闭
			mciSendString(_T("open D:\\桌面\\fighter\\5.mp3 alias 5music"), NULL, 0, NULL); // 打开音乐
			mciSendString(_T("play 5music"), NULL, 0, NULL); // 仅播放一次
		}
		if (score % 10 == 0)
		{
			mciSendString(_T("close 10music"), NULL, 0, NULL); // 先把前面一次的音乐关闭
			mciSendString(_T("open D:\\桌面\\fighter\\10.mp3 alias 10music"), NULL, 0, NULL); // 打开音乐
			mciSendString(_T("play 10music"), NULL, 0, NULL); // 仅播放一次
		}
	}

	// 撞机了
	if ((air_x > enemy_x) && (air_x < enemy_x + 104) && (air_y  < enemy_y + 148))
	{
		isExpolde = 1;
		//gameover();     // 游戏结束、后续处理	

		mciSendString(_T("close exmusic"), NULL, 0, NULL); // 先把前面一次的音乐关闭
		mciSendString(_T("open D:\\桌面\\fighter\\explode.mp3 alias 10music"), NULL, 0, NULL); // 打开音乐
		mciSendString(_T("play exmusic"), NULL, 0, NULL); // 仅播放一次
	}
}


int main()
{
	startup();  // 数据初始化	
	while (1)  //  游戏循环执行
	{
		show();  // 显示画面
		updateWithoutInput();  // 界面的更新
		updateWithInput();     // 与用户输入有关的更新
	}
	gameover();     // 游戏结束、后续处理
	return 0;
}

 

你可能感兴趣的:(C语言游戏,C程序,游戏,c++)