c语言飞机大战

制作飞机大战,需要几个主要步骤

1.显示菜单

2.打印飞机

3.玩家控制飞机移动,射击

4.敌机的随机产生,移动等

5.记录玩家的得分情况

我们制作过程中可以分为以下几个部分

显示菜单

void menu()
{
	int b;
	printf("**************************\n");
	printf("********1.开始游戏********\n");
	printf("********2.结束游戏********\n");
	printf("**************************\n");
	while (1)
	{
		scanf("%d", &b);
		if (b == 2)
			exit(0);//结束进程
		if (b == 1)
			break;
		if(b!=1)
			printf("输入错误,请重新输入\n");
	}
}

初始化数据

void data()
{
	position_x = 5,position_y = 10;//战机初始位置
	top = 0, bottom = 20, left = 0, right = 30;//移动边界
	enemy_x = 0, enemy_y = rand() % right;//敌机产生位置
	score = 0;//玩家得分
}

打印的部分

void display()
{
	gotoxy(0, 0);
	for (j = 0;j < bottom;j++)
	{
		for (k = 0;k < right;k++)
		{
			if (j == position_x && k == position_y)
				printf("*");
			if (j == position_x + 1 && k == position_y - 2)
				printf("*****");
			if (j == position_x + 2 && k == position_y - 1)
				printf("* *");//打印飞机
			if (j == buttle_x && k == position_y)
				printf("|");//打印子弹
			if (j == enemy_x && k == enemy_y)
				printf("+");//“+”表示敌机
			else
				printf(" ");
		}
		printf("\n");
	}
	printf("得分:%d", score);
	printf("结束游戏请按“q”");
}

玩家输入部分

void print()
{
		char input;
		if (kbhit())
		{
			input = getch();
			if (input == 'w')
				position_x--;
			if (input == 's')
				position_x++;
			if (input == 'a')
				position_y-- ;
			if (input == 'd')
				position_y++ ;
			if (input == 'j')
				buttle_x = position_x - 1, buttle_y = position_y;
            if (input == 'q')
				exit(0);
		 }
}

实现子弹移动和敌机随机产生

void object()
{
	static int speed = 0;
	int a;
	if (buttle_x >= 0)
		buttle_x--;
	if (buttle_x == enemy_x && buttle_y == enemy_y)
	{
		score++;
		enemy_y = rand() % right;
	}
	else
	{
		if (speed < 10)
			speed++;
		if (speed == 10)
		{
			int a = rand() % 12 + 1;
			if (a < 4)
				enemy_x--;
			if (a > 3 && a <= 6)
				enemy_x++;
			if (a > 6 && a <= 9)
				enemy_y++;
			if (a > 9 && a <= 12)
				enemy_y--;//实现敌机随机移动
			speed = 0;//控制敌机移动速度
		}
		if (enemy_x > right || enemy_x<left || enemy_y>bottom || enemy_y < top)
			enemy_x = rand() % 5 + 1, enemy_y = rand() % right;//超出边界后重新生成
	}
}

最后就是完成主函数了

int main()
{
	data();
	menu();
	while (1)
	{
		system("cls");
		HideCursor();
		display();
		print();
		object();
	}
	return 0;
}

当我们做完这些后会发现游戏频闪非常严重

这时就需要这两个函数(目前我只知道用途)

void gotoxy(int x, int y)
{
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(hOut, pos);
}
void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

这样分步写以后做修改时会比较方便

还需要包含以下几个头文件

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>

游戏还有许多要改进的地方,以及一些bug,但是现在能力不足,等以后再完善

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值