c语言控制台小游戏-飞机大战(大概300行代码)

Tips1:运行的时候一定要全屏,不然就显示一部分
Tips2:emm,有点小Bug哈哈,总体运行流畅*
操作说明:键盘左右键控制方向,空格键发射子弹
效果图:*

	                               ****1、开始界面****

在这里插入图片描述

  •                                   *****2、游戏界面******
    

在这里插入图片描述
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-w7OgIfeL-1591540248302)(在这里插入图片描述)]](https://img-blog.csdnimg.cn/2020060722302062.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RPTUpKWQ==,size_16,color_FFFFFF,t_70)
附上代码:

//打飞机游戏
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<Windows.h>
#include<time.h>
#include <conio.h>
#include<time.h>
//定义坐标结构体
struct Pos {
	int x;
	int y;
};

struct Fememy {
	int x;
	int y;
	int life;//敌机血量
	int vy;//敌机移动速度
};


//定义四个边界点坐标
Pos LeftTop = { 10,2 };
Pos LeftDown = { 10,50 };
Pos RightUp = { 110,0 };
Pos RightDown = { 110,100 };
Pos MyAirPlane;
Pos bullet[1000];
int bulletcount = 0;//子弹计数器
Fememy Enemy[5];
int emenycount;//敌机计数器
static int score = 0;//得分

void inint();//初始化
void startmenu();//开始菜单
void start();//开始游戏
void horizontalLine(Pos p, int length);//画横线,p为起始位置,length为线段长度
void Cursor(int x, int y);//定位光标点
void printAirPlane(Pos MyAirPlane);//打印飞机
void showline();//边界绘制
void reprintPlane();//飞机重绘制
void reprintEmeny();//重绘敌机
void cleanEmeny();//清除敌机
void cleanPlane();//清除飞机
void firebullet();//发射子弹
void printBullet(int, int);//绘制子弹
void ReprintBullet();//重绘子弹
void CleanmyBullet(int, int);//清除子弹
void collion();//打中敌机
void tips();//游戏侧边提示 
void endgame();//退出游戏
void  main() {

	//system("mode con cols=100 lines=50 ");
	srand((int)time(0));//随机数种子
	inint();//初始化
	startmenu();//开始菜单
	while (true)
	{
		start();
		Sleep(30);
	}


}

void start() {


	showline();//绘制边界
	reprintPlane();//重绘飞机
	ReprintBullet();//重绘子弹
	reprintEmeny();//重绘敌机
	endgame();//结束游戏
}

void horizontalLine(Pos p, int length)
{
	Cursor(p.x, p.y);               // //光标移动到横线起始位置
	for (int i = 0; i < length; i++)
	{
		printf("*");
	}
}



void Cursor(int x, int y)
{

	COORD cursor;										//实例化结构体
	HANDLE hOutput;										//实例化句柄
	cursor.X = x;										//传入横坐标参数
	cursor.Y = y;										//传入纵坐标参数
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);			//获取输出框界面句柄
	SetConsoleCursorPosition(hOutput, cursor);			//定位坐标位置
}


//画我方飞机
void printAirPlane(Pos MyAirPlane)
{

	Cursor(MyAirPlane.x, MyAirPlane.y);

	printf("_■_");
}

//画敌机
void printEnemy(Fememy myEnemy)
{

	Cursor(myEnemy.x, myEnemy.y);
	printf("====█====");

}

void showline()
{

	//画上边界
	horizontalLine(LeftTop, 100);
	//画下边界
	horizontalLine(LeftDown, 100);
	tips();//游戏提示
}

void inint()
{
	//初始化我方飞机坐标
	MyAirPlane.x = 55;
	MyAirPlane.y = 49;
	emenycount = rand() % 10;//敌机数量
	//初始化敌机坐标
	for (int i = 0; i < emenycount; i++)
	{
		Enemy[i].x = rand() % 100 + 10;
		Enemy[i].y = 2;
		Enemy[i].vy = 1;
	}
}

void reprintPlane()
{
	if (GetAsyncKeyState(VK_LEFT) && MyAirPlane.x > 10) //当用户左键输入
	{
		cleanPlane();								//清除原有己方飞机
		MyAirPlane.x -= 2;								//向左移动单位网格长度
		printAirPlane(MyAirPlane);								//重新绘制己方飞机
	}
	else if (GetAsyncKeyState(VK_RIGHT) && MyAirPlane.x < 110) //当右键
	{
		cleanPlane();									//清除原有己方飞机
		MyAirPlane.x += 2;								//向左移动单位网格长度
		printAirPlane(MyAirPlane);								//重新绘制己方飞机
	}
	else {
		printAirPlane(MyAirPlane);								//若用户没有键入按键,进行刷新
	}

}

void cleanPlane()
{
	Cursor(MyAirPlane.x, MyAirPlane.y);
	printf("      ");
}


void ReprintBullet()//重绘子弹
{
	firebullet();
	for (int i = 0; i < bulletcount - 1; i++)
	{
		CleanmyBullet(bullet[i].x, bullet[i].y);
	}
	//当子弹到达边界,删除子弹
	for (int i = 0; i < bulletcount; i++)
	{
		if (bullet[i].y > 2)
		{
			bullet[i].y--;

		}
		else
		{
			if (bullet[i].y <= 2)
			{
				for (int i = 0; i < bulletcount - 1; i++)
				{
					bullet[i] = bullet[i + 1];
				}
				bulletcount--;

			}
		}
	}


	for (int i = 0; i < bulletcount - 1; i++)
	{

		printBullet(bullet[i].x, bullet[i].y);

	}
	collion();
}



void CleanmyBullet(int x, int y)
{
	Cursor(x, y);						//定位己方待清除炮弹位置
	printf("  ");						//清除己方炮弹
}


//发射子弹
void firebullet()
{


	if (GetAsyncKeyState(VK_SPACE)) //空格键发射子弹
	{
		bullet[bulletcount].x = MyAirPlane.x + 2;
		bullet[bulletcount].y = MyAirPlane.y - 1;
		bulletcount++;
	}
}

//绘制子弹
void printBullet(int x, int y)
{
	for (int i = 0; i < bulletcount; i++)
	{
		Cursor(x, y);
		printf("||");

	}
}

void reprintEmeny()//重绘敌机
{
	cleanEmeny();
	for (int i = 0; i < emenycount; i++)
	{
		if (Enemy[i].y < 49)
		{
			Enemy[i].vy = rand() % 2 + 1;
			Enemy[i].y += Enemy[i].vy;

		}
		else
		{
			Enemy[i].x = rand() % 100 + 10;
			Enemy[i].y = 2;

		}
	}
	for (int i = 0; i < emenycount; i++)
	{
		printEnemy(Enemy[i]);
	}
}


void cleanEmeny()//清除敌机(绘制清除)
{
	for (int i = 0; i < emenycount; i++)
	{
		Cursor(Enemy[i].x, Enemy[i].y);
		printf("          ");
	}

}

void collion()
{
	for (int i = 0; i < emenycount; i++)
	{
		for (int j = 0; j < bulletcount; j++)
		{
			if (bullet[j].x > Enemy[i].x && bullet[j].x < Enemy[i].x + 9 && bullet[j].y == Enemy[i].y)//当击中时
			{
				Cursor(Enemy[i].x, Enemy[i].y);
				printf("           ");
				Enemy[i].x = rand() % 100 + 10;
				Enemy[i].y = 2;
				score += 100;
			}
		}
	}

}

void tips()
{
	Cursor(120, 20);
	printf("***飞机大作战****");
	Cursor(120, 22);
	printf("游戏得分:%d ", score);
	Cursor(120, 24);
	printf("用左右键分别控制飞机的移动");
	Cursor(120, 26);
	printf("按下空格键即为发射子弹");
	Cursor(120, 28);
	printf("按下ESC退出游戏");


}


void endgame()
{
	if (GetAsyncKeyState(VK_ESCAPE))
	{
		exit(0);
	}
}

void startmenu()//开始菜单
{

	horizontalLine(LeftTop, 100);
	//画下边界
	horizontalLine(LeftDown, 100);
	Cursor(50, 23);
	printf("********************");
	Cursor(50, 25);
	printf("*  AIRPLANE-FIGHT  *");
	Cursor(50, 27);
	printf("********************");
	Cursor(50, 29);
	printf(" 按任意键开始游戏");
	getchar();
	system("cls");
}
  • 10
    点赞
  • 110
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值