用C语言快速实现简单打飞机游戏

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

int main(void)
{
	int i, j;
	int x = 10;
	int y = 10;
	char input;
	int fire = 0;
	int nx = 5; //设置靶子初始的位置
	int kill = 0;
	int height = 25;
	int velocity = 1;
	int Successes = 0;
	int Failures = 0;
	while (Failures != 3)
	{
		nx = nx + velocity;
		system("cls");	//清屏函数
		//显示积分榜
		printf("Successes: %d      Failures: %d\n", Successes, Failures);
		//显示靶子
		if (!kill)
		{
			for (i = 0; i < nx; i++)
				printf(" ");
			printf("+\n");
		}
		if (fire == 0)
		{
			// 显示飞机左边的空白
			for (i = 0; i < x; i++)
				printf(" ");
			// 显示飞机上方的空白
			for (j = 0; j < y; j++)
				printf("\n");
		}
		else
		{   // 显示激光
			for (i = 0; i < y; i++)
			{
				for (j = 0; j < x; j++)
					printf(" ");
				printf("  |\n");
			}
			// +2是因为激光在飞机的正中间,距最左边2个坐标
			if (x + 2 == nx)
			{
				kill = 1;	//击中靶子
				Successes++;	//积分榜+1
			}
			fire = 0; //重置参数 所以激光只显示一次
			Failures++;
		}
		// 显示飞机图案,机头和发射激光要对应好 慢慢调整
		for (i = 0; i < x; i++)
			printf(" ");
		printf("  *\n");
		for (i = 0; i < x; i++)
			printf(" ");
		printf("*****\n");
		for (i = 0; i < x; i++)
			printf(" ");
		printf(" * *\n");
		// 通过键盘输入来控制飞机的移动和射击
		if (_kbhit())
		{
			input = _getch();

			if (input == 'a')
				x--;
			if (input == 'd')
				x++;
			if (input == 'w')
				y--;
			if (input == 's')
				y++;
			if (input == ' ')
				fire = 1;
		}
		if (nx == 0)
			velocity = -velocity;
		if (nx == height)
			velocity = -velocity;
	}
	system("cls");	//清屏函数
	printf("\n\n\n\n\n	hahaha,you are Spicy chicken.\n\n\n\n\n");
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/GameStrategist/article/details/107511077