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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章