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

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

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

原归正状 :

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

效果图:

#include <graphics.h>
#include <conio.h>

// 引用 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. 敌机下落

效果展示:

#include <graphics.h>
#include <conio.h>

// 引用 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.撞击爆炸

效果图:

#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>

// 引用 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 <graphics.h>
#include <conio.h>
#include <math.h>
#include <stdio.h>

// 引用 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;
}

 

  • 34
    点赞
  • 208
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
C语言飞机大战小游戏是一个基于控制台的游戏,源代码中引用了plane.h头文件来定义游戏所需的函数和常量。main函数调用了game函数开始游戏,并在游戏结束后使用system("pause")来暂停程序。 在plane.h中,定义了Showmap函数用于打印地图,game函数用于调用开始游戏,Move函数用于控制飞机的移动,Buttle函数用于生成子弹,FadeButtle函数用于消除子弹,Plan函数用于生成敌机,FeoButtle函数用于生成敌机的子弹,Judge函数用于判断游戏的胜负。 在Plan函数中,使用了随机数种子srand((unsigned)time(NULL))来生成随机数,然后根据随机数来确定敌机的坐标。敌机的坐标不能与墙壁重合,也不能与我方飞机碰撞。当找到合适的坐标后,将地方飞机的数据设置为4,并打破循环。 通过调用这些函数和利用随机数生成敌机的坐标,可以实现一个简单的C语言飞机大战小游戏。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C语言飞机大战游戏项目](https://blog.csdn.net/B85951005/article/details/126037896)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逼哥很疯狂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值