c语言简单的飞机计分小游戏

比较简单的飞机小游戏,直接用C语言即可实现功能,我已经做了较为完整的注释,希望大家可以看得懂,对学C的有所帮助。

 

#include <stdio.h>
#include <stdlib.h>
#include <conio.h> //用于抓去键盘输入
#include <Windows.h>

//函数外得定义一些全局变量,
int position_x,position_y;//飞机的定位
int high,width;//游戏画面的尺寸
int bullet_x,bullet_y; //子弹的定位

int enem_x,enem_y; //敌机

int score ; //得分


//必须写足够的注释

// 光标闪烁清除
void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1 , 0};  //第二个值为0表示隐藏光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}

void init()
{
	high = 20;
	width = 30;

	position_x = high/2;
	position_y = width/2;

	bullet_x = 0;
	bullet_y = position_y;

	enem_x = 0;
	enem_y = 2;

	score =0;

	HideCursor();
}

// 清屏程序
void gotoxy(int x,int y)  //光标移动到xy的位置
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(handle,pos);
}




void show()
{
	//  "cls"清屏速度太快,以至于会闪烁。
	//system("cls");  //清屏

	//调用 函数gotoxy。不会刷屏太快。
	gotoxy(0,0);

	int i,j; //空格位置
	for(i=0;i<high;i++)
	{
		for(j=0;j<width;j++)
		{
			if((i == position_x) && (j == position_y))
				printf("*"); //飞机位置
			else if ((i == bullet_x) && (j == bullet_y))
				printf("|"); //子弹的飞出
			else if((i == enem_x) && (j == enem_y))
				printf("@");  //敌机
			else
				printf(" "); //其余位置为空格
		}
		printf("\n");
	}
		printf("得分:%d",score);
}

void updateWithInput() //用户输入
{
	char input;
	if(kbhit()) //检测按键是否有效
	{
		input = getch();

		if(input == 'a')
			position_y--;
		if(input == 'w')
			position_x--;
		if(input == 'd')
			position_y++;
		if(input == 's')
			position_x++;
		if(input ==' ')
		{
			bullet_x = position_x -1;
			bullet_y = position_y;
		}
	}
}

void updateWithoutInput()  //用户输入无关
{
	if((bullet_x == enem_x) && (bullet_y == enem_y))
	{
		score ++;  //击中得分
		bullet_x = -1;  // 子弹消失
		enem_x =0;  // 敌机重新出现
		enem_y = rand() % width;  // 敌机位置随机生成,对宽度取余,会在宽度内。
	}
	if(position_x > high)
	{
		position_x = high-1;
	}
	if(position_x < 0)
	{
		position_x = 0;
	}
	if(position_y > width)
	{
		position_y = width-1;
	}
	if(position_y < 0)
	{
		position_y = 0;
	}

	if( bullet_x > -1) //子弹发射,到顶消失
		bullet_x--;

	static int speed = 0; //控制飞机的速度 ,变量
	if(speed < 20)  //循环20次,飞机下落一格
		speed ++ ;

	if(speed ==20)
	{
		if(enem_x > high)
		{
			enem_x = 0;  //敌机消失后,重新生成
			enem_y = rand() % width;  // 敌机位置随机生成,对宽度取余,会在宽度内。
		}
		else
		{
			enem_x++;  //敌机向下落
			speed = 0;
		}
	}
	
}

int main()
{
	init(); //数据初始化

	while(1)
	{
		show();  //显示画面
		updateWithoutInput();  //以用户输入无关的更新
		updateWithInput();  //与用户输入有关的

	}
	return 0;
}

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言飞机大战小游戏是一个基于控制台的游戏,源代码中引用了plane.h头文件来定义游戏所需的函数和常量。main函数调用了game函数开始游戏,并在游戏结束后使用system("pause")来暂停程序。 在plane.h中,定义了Showmap函数用于打印地图,game函数用于调用开始游戏,Move函数用于控制飞机的移动,Buttle函数用于生成子弹,FadeButtle函数用于消除子弹,Plan函数用于生成敌机,FeoButtle函数用于生成敌机的子弹,Judge函数用于判断游戏的胜负。 在Plan函数中,使用了随机数种子srand((unsigned)time(NULL))来生成随机数,然后根据随机数来确定敌机的坐标。敌机的坐标不能与墙壁重合,也不能与我方飞机碰撞。当找到合适的坐标后,将地方飞机的数据设置为4,并打破循环。 通过调用这些函数和利用随机数生成敌机的坐标,可以实现一个简单C语言飞机大战小游戏。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C语言飞机大战游戏项目](https://blog.csdn.net/B85951005/article/details/126037896)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值