ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

飞机大战——简单易懂

2020-12-20 22:02:00  阅读:241  来源: 互联网

标签:飞机 int 大战 foeline str foerow 易懂 line row


文章目录

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

在这里插入图片描述
构建完地图,构建打印函数和飞机移动函数,效果图和当前代码如下
在这里插入图片描述

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);//消除子弹
	}
}

2.子弹函数的构建

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

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

3.构建敌机

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

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

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

我们可以在敌机周围构造子弹,子弹数据为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;
}

5.完整代码

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

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

标签:飞机,int,大战,foeline,str,foerow,易懂,line,row
来源: https://blog.csdn.net/ych9527/article/details/111461452

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有