C语言编写飞机大战游戏项目

1.效果展示

 

 2.地图构建和飞机的移动实现  

 

构建完地图,构建打印函数和飞机移动函数,效果图和当前代码如下 

 

 

char str[ROW][LINE]={ // 0代表边框(■),1代表游戏区域(空格),2代表飞机(Ж)
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

void Showmap(char str[][LINE])//地图展示
{
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < LINE; j++)
		{
			if (str[i][j] == 0)// 0代表边框(■)
				printf("■");
			else if (str[i][j] == 1)
				printf("  ");//1代表游戏区域(空格)
			else if (str[i][j] == 2)//2代表飞机(Ж)
				printf("Ж");
			else if (str[i][j] == 3)//3表示子弹●
				printf("●");
		}
		printf("\n");
	}
}

void Move(char str[][ROW],int row,int line)//进行移动
{
	int newrow = row;
	int newline = line;
	while (1)
	{
		int quit = 0;
		while (!quit)
		{
			printf("w s a d 控制上下左右\n");
			int ch = 0;
			int move = 0;//接收移动命令
			move = getchar();
			while ((ch = getchar()) != '\n');//吃掉空格等多余的符号
			switch (move)
			{
			case 'w'://向上
				newrow--;
				quit = 1;//成功移动一次就退出,打印地图形成动态效果
				break;
			case 's'://下
				newrow++;
				quit = 1;
				break;
			case 'a'://左
				newline--;
				quit = 1;
				break;
			case 'd'://右
				newline++;
				quit = 1;
				break;
			default:
				printf("输入有误,请从新输入\n");
				break;
			}
		}

		if (str[newrow][newline] != 0)//不为墙壁,更新数据
		{
			str[newrow][newline] = 2;
			str[row][line] = 1;
			row = newrow;
			line = newline;
		}
		else//否则数据交换回来
		{
			newrow = row;
			newline = line;
		}
		system("cls");
		Showmap(str);
		//int size = Buttle(str, row, line);//生成子弹,返回size是给消除子弹函数
		//FadeButtle(str, row, line, size);//消除子弹
	}
}

 

3.子弹函数的构建

每次飞机移动都需要生成子弹,因此我们在移动的同时,调用子弹生成函数打印地图形成动态效果,同时需要构建调用子弹消除函数,将移动后的飞机之前的子弹消除掉,这一步形成的效果和代码如下;

 

int Buttle(char str[][LINE],int row,int line)//子弹
{
	int size =1;//从1开始是要判断子弹前是否是墙壁
	while ((size != 6) && str[row - size][line]!=0)//飞机前面呈现5颗子弹,子弹不能碰墙
	{
		str[row-size][line]=3;//3表示子弹
		size++;
		system("cls");
		Showmap(str);//动态子弹
	}
	return size;
}
void FadeButtle(char str[][LINE], int row, int line,int size)
{
	size--;//生成子弹时,这里多加了一个
	while (size)
	{
		str[row - size][line] = 1;//消除子弹,即将子弹处变成空白
		size--;
	}
}

 

 

4.构建敌机

我们可以统计移动步数,每移动多少步随机出现一辆敌机。需要注意边界判断,敌机不能一出来就在墙上或者与本机相撞,或者撞在子弹上面,当前效果和代码如下:

 

 

void Plan(char str[][LINE])
{
	srand((unsigned)time(NULL));//种下随机数种子
	//敌机坐标
	int foerow = 0;
	int foeline = 0;
	while (1)
	{
		foerow = (rand() %(ROW-2)) + 1;//+1是坐标不能为墙壁
		foeline = (rand() % (LINE-2)) + 1;
		if (str[foerow][foeline] != 2 && str[foerow][foeline] != 3)//随机出现的敌机不能一出来就与我方飞机碰撞
		{
			str[foerow][foeline] = 4;//地方飞机数据用4,图案用Ψ;
			break;
		}
	}
}

 

5.构造敌机子弹和判定结束函数

我们可以在敌机周围构造子弹,子弹数据为5,图案为¤,如果我们的飞机碰到了就代表游戏结束;
这里的敌机子弹函数和判定函数和效果图如下:

 

 

 

void FeoButtle(char str[][LINE],int foerow,int foeline)//敌机子弹
{
	//敌机出生时子弹不能直接撞到本机,子弹不能到墙内部
	if (str[foerow - 1][foeline] != 0 && str[foerow - 1][foeline] != 2)//上方
		str[foerow - 1][foeline] = 5;
	if (str[foerow + 1][foeline] != 0 && str[foerow + 1][foeline] != 2)//下方
		str[foerow + 1][foeline] = 5;
	if (str[foerow][foeline-1] != 0 && str[foerow][foeline-1] != 2)//左方
		str[foerow][foeline-1] = 5;
	if (str[foerow][foeline + 1] != 0 && str[foerow][foeline + 1] != 2)//右方
		str[foerow][foeline + 1] = 5;
}

 

int Judge(char str[][LINE],int row,int line)//判断函数
{
	if (str[row][line] == 5)
	{
		printf("你被子弹击中了,游戏结束\n");
		return 1;
	}
	else
		return 0;
}

 

6.完整代码

先初步构建游戏,后续有时间再补充一些功能和修改,有喜欢做游戏的朋友可以看看我做的推箱子和扫雷哟,期待您的指导;

 

#include "plane.h"

char str[ROW][LINE]={ // 0代表边框(■),1代表游戏区域(空格),2代表飞机(Ж)
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

void Showmap(char str[][LINE])//地图展示
{
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < LINE; j++)
		{
			if (str[i][j] == 0)// 0代表边框(■)
				printf("■");
			else if (str[i][j] == 1)
				printf("  ");//1代表游戏区域(空格)
			else if (str[i][j] == 2)//2代表飞机(Ж)
				printf("Ж");
			else if (str[i][j] == 3)//3表示子弹●
				printf("●");
			else if (str[i][j] == 4)//4表示地方飞机Ψ
				printf("Ψ");
			else if (str[i][j] == 5)//5表示敌机子弹
				printf("¤");
		}
		printf("\n");
	}
}

void Move(char str[][ROW],int row,int line)//进行移动
{
	int count = 0;//统计步数生成敌机
	int newrow = row;
	int newline = line;
	while (1)
	{
		int quit = 0;
		while (!quit)
		{
			printf("w s a d 控制上下左右\n");
			int ch = 0;
			int move = 0;//接收移动命令
			move = getchar();
			while ((ch = getchar()) != '\n');//吃掉空格等多余的符号
			switch (move)
			{
			case 'w'://向上
				newrow--;
				quit = 1;//成功移动一次就退出,打印地图形成动态效果
				break;
			case 's'://下
				newrow++;
				quit = 1;
				break;
			case 'a'://左
				newline--;
				quit = 1;
				break;
			case 'd'://右
				newline++;
				quit = 1;
				break;
			default:
				printf("输入有误,请从新输入\n");
				break;
			}
		}

		if (str[newrow][newline] != 0)//不为墙壁,更新数据
		{
			int judge=Judge(str, newrow, newline);//进行判定
			if (judge)//判定被敌机子弹打中
				break;
			str[newrow][newline] = 2;
			str[row][line] = 1;
			row = newrow;
			line = newline;
		}
		else//否则数据交换回来
		{
			newrow = row;
			newline = line;
		}
		int size = Buttle(str, row, line);//生成子弹,返回size是给消除子弹函数
		FadeButtle(str, row, line, size);//消除子弹
		if (count % 2 == 0)//每移动四次生成一辆敌机
		{
			Plan(str);
		}
		count++;
	}
}
int Buttle(char str[][LINE],int row,int line)//子弹
{
	int size =1;//从1开始是要判断子弹前是否是墙壁
	while ((size != 6) && str[row - size][line]!=0)//飞机前面呈现5颗子弹,子弹不能碰墙
	{
		str[row-size][line]=3;//3表示子弹
		size++;
		system("cls");
		Showmap(str);//动态子弹
	}
	return size;
}
void FadeButtle(char str[][LINE], int row, int line,int size)
{
	size--;//生成子弹时,这里多加了一个
	while (size)
	{
		str[row - size][line] = 1;//消除子弹,即将子弹处变成空白
		size--;
	}
}
void Plan(char str[][LINE])//敌机生成
{
	srand((unsigned)time(NULL));//种下随机数种子
	//敌机坐标
	int foerow = 0;
	int foeline = 0;
	while (1)
	{
		foerow = (rand() %(ROW-2)) + 1;//+1是坐标不能为墙壁
		foeline = (rand() % (LINE-2)) + 1;
		if (str[foerow][foeline] != 2 && str[foerow][foeline] != 3)//随机出现的敌机不能一出来就与我方飞机碰撞
		{
			str[foerow][foeline] = 4;//地方飞机数据用4,图案用Ψ;
			FeoButtle(str, foerow, foeline);
			break;
		}
	}
}
void FeoButtle(char str[][LINE],int foerow,int foeline)//敌机子弹
{
	//敌机出生时子弹不能直接撞到本机,子弹不能到墙内部
	if (str[foerow - 1][foeline] != 0 && str[foerow - 1][foeline] != 2)//上方
		str[foerow - 1][foeline] = 5;
	if (str[foerow + 1][foeline] != 0 && str[foerow + 1][foeline] != 2)//下方
		str[foerow + 1][foeline] = 5;
	if (str[foerow][foeline-1] != 0 && str[foerow][foeline-1] != 2)//左方
		str[foerow][foeline-1] = 5;
	if (str[foerow][foeline + 1] != 0 && str[foerow][foeline + 1] != 2)//右方
		str[foerow][foeline + 1] = 5;
}
int Judge(char str[][LINE],int row,int line)//判断函数
{
	if (str[row][line] == 5)
	{
		printf("你被子弹击中了,游戏结束\n");
		return 1;
	}
	else
		return 0;
}
void game()
{
	int row = 18, line = 8;//飞机初始位置
	Plan(str);//先随机生成一架敌机
	Showmap(str);
	Move(str,row,line);
}

 

 

#ifndef _PLANE_H_
#define _PLANE_H_


#include <stdio.h>
#include <windows.h>
#include <time.h>

#define ROW 20
#define LINE 20


void Showmap(char str[][ROW]);//打印地图
void game();//调用开始
void Move(char str[][ROW], int row, int line);//游戏运行
int Buttle(char str[][LINE], int row, int line);//子弹生成
void FadeButtle(char str[][LINE], int row, int line, int size);//子弹消除
void Plan(char str[][LINE]);//敌机
void FeoButtle(char str[][LINE], int foerow, int foeline);//敌机子弹
int Judge(char str[][LINE], int row, int line);//判定函数
#endif

 

 

 

#include "plane.h"


int main()
{
	game();
	system("pause");
	return 0;
}

 

 

 

 

【源码和资料】进群找管理员领取哦!。一起学习交流,群里有一些资料可以帮助大家更好的学习,在学习C语言的过程中遇到任何的问题,都可以发出来一起讨论,大家都是学习C/C++的,或是转行,或是大学生,还有工作中想提升自己能力的前端党,如果你是正在学习C/C++的小伙伴可以加入学习。

点击链接加入群聊【C语言互相学习交流群】:

 

 

 

 

 

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
飞机大战是一款经典的游戏,使用C语言编写可以让你更深入地了解游戏的实现原理。下面是一个简单的飞机大战代码示例: ``` #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #define WIDTH 50 #define HEIGHT 25 int x, y; // 飞机的坐标 int bullet_x, bullet_y; // 子弹的坐标 int enemy_x, enemy_y; // 敌机的坐标 int score; // 得分 void gotoxy(int x, int y) // 光标移动函数 { COORD pos = {x, y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } void draw_plane() // 绘制飞机 { gotoxy(x, y); printf(" /\\"); gotoxy(x, y+1); printf(" | |"); gotoxy(x, y+2); printf("/----\\"); } void draw_bullet() // 绘制子弹 { gotoxy(bullet_x, bullet_y); printf("|"); } void draw_enemy() // 绘制敌机 { gotoxy(enemy_x, enemy_y); printf(" /\\"); gotoxy(enemy_x, enemy_y+1); printf(" | |"); gotoxy(enemy_x, enemy_y+2); printf("/----\\"); } void update_bullet() // 更新子弹位置 { bullet_y--; if (bullet_y <= 0) // 子弹超出屏幕范围,重置位置 { bullet_x = x+2; bullet_y = y-1; } } void update_enemy() // 更新敌机位置 { enemy_y++; if (enemy_y >= HEIGHT) // 敌机超出屏幕范围,重置位置 { enemy_x = rand() % (WIDTH-6) + 3; enemy_y = 0; score++; // 得分加一 } } void check_collision() // 检测碰撞 { if (bullet_x == enemy_x+1 && bullet_y == enemy_y+2) // 子弹击中敌机 { enemy_x = rand() % (WIDTH-6) + 3; enemy_y = 0; bullet_x = x+2; bullet_y = y-1; score += 10; // 得分加十 } if ((x == enemy_x && y == enemy_y) || (x+3 == enemy_x+4 && y == enemy_y)) // 飞机与敌机相撞 { system("cls"); printf("Game Over!\n"); printf("Your Score: %d\n", score); exit(0); } } int main() { x = WIDTH / 2 - 1; y = HEIGHT - 4; bullet_x = x+2; bullet_y = y-1; enemy_x = rand() % (WIDTH-6) + 3; enemy_y = 0; score = 0; while (1) { system("cls"); // 清屏 draw_plane(); // 绘制飞机 draw_bullet(); // 绘制子弹 draw_enemy(); // 绘制敌机 update_bullet(); // 更新子弹位置 update_enemy(); // 更新敌机位置 check_collision(); // 检测碰撞 Sleep(50); // 等待50毫秒 if (_kbhit()) // 检测键盘输入 { char ch = _getch(); if (ch == 'a' && x > 0) x--; // 左移 if (ch == 'd' && x < WIDTH-3) x++; // 右移 if (ch == ' ') // 发射子弹 { bullet_x = x+2; bullet_y = y-1; } } } return 0; } ``` 这个代码示例实现了一个简单的飞机大战游戏,玩家可以使用键盘左右箭头控制飞机移动,空格键发射子弹,得分规则为击落敌机得10分,游戏结束条件为飞机与敌机相撞。你可以根据自己的需求对代码进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值