做游戏,学编程(C语言) 飞机游戏 2 (思考改进)

思路来自;https://zhuanlan.zhihu.com/p/24652156


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

int main()
{
	int a,b;
	int c=5; //定义敌机初始位置
	int d=0; //定义敌机初始位置
	int x=17; //控制飞机横向位置
	int y=17; //控制飞机纵向位置
	int score=0;

	char input; //符号变量
	int isfire = 0; //通过变量检测是否发射子弹
	int isKilled = 0; //记录是否击中
	
	int velocity_x = 1;
	int velocity_y = 1;
	int left = 0;
	int right = 15;
	int top = 0;
	int bottom = 10;
	//战机移动相关
	
	while(1)
	{
		system("cls"); //清屏
		Sleep(10);
		//输出敌机
		c=c+velocity_x;
	    d=d+velocity_y;
		if ((c==left)||(c==right))
			velocity_x = -velocity_x;
		if ((d==top)||(d==bottom))
			velocity_y = -velocity_y;
	    if(score<=100)
		{
			for(b=0;b<d;b++)
				printf("\n");
			for(a=0;a<c;a++)
				printf(" ");
			printf("o\n");
		}
		system("cls");

		//输出飞机上面的空行
		if(isfire==0)
		{
			for(b=0;b<y;b++)
				printf("\n");
		}
		
		//输出激光束线
		else 
		{
			for(b=0;b<y;b++)
			{
				for (a=0;a<x;a++)
					printf(" ");
				printf("  |\n");
			}
			if (x+2==c)
				isKilled=1;// 击中靶子
			if (x+2==c)
			    score++;
			isfire = 0;
		}
		//输出飞机
		for(a=0;a<x;a++)
			printf(" ");
		printf("  *\n");
		for(a=0;a<x;a++)
			printf(" ");
		printf("*****\n");
		for(a=0;a<x;a++)
			printf(" ");
		printf(" * *\n");  
		printf("\n");

		if(kbhit()) // 判断是否有输入
		{
			input=getch();
			if(input=='a')
				x--; //左移
			if(input=='d')
				x++; //右移
			if(input=='w')
				y--; //上移
			if(input=='s')
				y++; //下移
			if(input==' ')
				isfire=1;
		}
		system("cls");
		for(a=0;a<20;a++)
			printf("\n");
		printf("score=%d ",score);
		/**if(score>=100)
			break;**/
	}
	return 0;
}

	


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值