飞机小游戏1

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <windows.h>//包含system("cls")函数与_getch()
#include <conio.h>//包含_kbhit()
#include <stdlib.h>//包含随机数函数rand()与获取种子函数srand()
#include <time.h>//包含时间函数
//全局变量
int point_x, point_y, hight, width;//飞机的坐标与画面的长度与宽度
int dan_x, dan_y;//子弹的坐标
int enemy_x, enemy_y;//敌机的坐标
int score, life;//分数以及生命值
int f=0;//循环开启钥匙
void show();//显示函数
void noinput();//无关用户操作的计算
void input();//有关用户操作的计算
void open();//初始化操作
void over();//结束函数
int main()
{
	open();
	while (f==0)
	{
		show();
		noinput();
		input();
		over();
	}
	printf("你失败了!!!!!!");
}
void show()//显示函数
{
	int i, j;
	system("cls");
	for (i = 0; i < hight; i++)
	{
		for (j = 0; j <width; j++)
		{
			if ((i == point_x) && (j == point_y))
				printf("*");//显示飞机
			else if (i == dan_x && j == dan_y)
				printf("|");//显示子弹
			else if (i == enemy_x && j == enemy_y)
				printf("@");
			else
				printf(" ");
		}
		printf("&\n");//制作右边屏障
	}
	for (j = 0; j < width; j++)//制作下边屏障
		printf("&");
	printf("&\n");
	printf("你的分数:%d\n", score);//分数显示
	printf("你的生命:");
	for (i = 0; i < life; i++)
		printf("心");
	printf("\n");
}
void noinput()//无关用户输入处理函数
{
	static int k = 0,s=15;
	if (dan_x == enemy_x && dan_y == enemy_y)//子弹与敌机重合敌机击毁
	{
		dan_y = -1;
		dan_x = -1;
		enemy_y = rand()%(width-1);
		enemy_x = -1;
		score++;
	}
	if (point_x == enemy_x && point_y== enemy_y)//敌机与飞机相遇减生命值
	{
		enemy_y = rand() % (width - 1);
		enemy_x = -1;
		life--;
	}
	if (dan_x > -1)//子弹向前移动
	{
		dan_x--;
	}
	else if (dan_x == -1)//子弹到底自动销毁
	{
		dan_x = -1;
		dan_y = -1;
	}
	if (score == 10)//如果分数高循环次数减少敌机速度加快
	{
		s = 10;
		k = 0;
	}
	if (score == 20)
	{
		s = 5;
		k = 0;
	}
	if (k == s)//减少敌机速度
	{
		if (enemy_x < hight)//敌机移动
		{
			enemy_x++;
		}
		else if (enemy_x == hight)//敌机到底自动销毁并重新设置出现地且扣除生命值
		{
			enemy_x = -1;
			enemy_y = rand() % (width - 1);
			life--;
		}
		k = 0;
	}
	k++;
}
void input()//有关用户输入处理函数
{
	char ch;//获取输入字母
	while (_kbhit())//有按键时返回一个非零值,否则返回0
	{
		ch = _getch();
		if (ch == 'w'&&point_x>0)//加入限制条件限制飞机在边框内移动
		{
			point_x--;//飞机向上移动
		}
		if (ch == 's'&&point_x<hight-1)
		{
			point_x++;//飞机向下移动
		}
		if (ch == 'd'&& point_y <width-1)
		{
			point_y++;//飞机向右移动
		}if (ch == 'a'&&point_y>0)
		{
			point_y--;//飞机向左移动
		}if (ch == ' ')//空格发射子弹
		{
			dan_y = point_y;//子弹位于飞机上方
			dan_x = point_x - 1;
		}
	}
}
void open()//初始化函数
{
	srand(time(NULL));
	hight = 20;
	width = 20;//划定边界

	point_x = hight / 2;
	point_y = width / 2;//飞机坐标初始化

	dan_x = - 1;
	dan_y = -1;//子弹坐标初始化

	enemy_x = -1;
	enemy_y = rand()%(width-1);//敌机坐标初始化

	score = 0;//分数值
	life = 3;//三条生命
}
void over()//结束循环
{
	if (life == 0)
		f = 1;
}

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值