飞机打靶子小游戏

1.飞机可以通过键盘移动,发射子弹.

2.显示得分.

3.靶子随机

4.打到靶子加分并换位

#define _CRT_SECURE_NO_WARNINGS 1

#include
#include
#include //_getch()函数
#include

#define HIGH 20
#define WIDE 50

int main()
{
	srand(time(NULL));//种子
	int x = 10,y = 20;//飞机的坐标
	int target;//靶子的横坐标
	char input;//键盘的输入
	target = 2 + rand() % 50;//随机值
	int score=0;//得分
	while(1)//死循环
	{
		input = _getch();
		//控制边框
		while (x > HIGH)
			x--;
		while (x < 0)
			x++;
		while (y > WIDE)
			y--;
		while (y < 2)
			y++;
		while (target > WIDE)
			target--;
		system("cls");//清屏
		for (int j = 0; j < target; j++)
			printf(" ");
		printf("---");//靶子
		if(input==' ')//射击
		{
			printf("\n");
			for (int i = 1; i < x; i++)
			{
				for (int j = 0; j < y; j++)
					printf(" ");
				printf("|\n");
			}
			for (int j = 0; j < y; j++)
				printf(" ");
			if (y == target||y==target+2||y==target+1)
			{
				target = 2 + rand() % 50;
				score++;
			}
		}
		else//移动
		{
			for (int i = 0; i < x; i++)
				printf("\n");
			for (int j = 0; j < y; j++)
				printf(" ");
		}
		//打印飞机
		printf("*\n");
		for (int j = 0; j < y-2; j++)
			printf(" ");
		printf("*****\n");
		for (int j = 0; j < y - 1; j++)
			printf(" ");
		printf("* *\n");
		//键盘控制飞机
		if (input == 'w')
			x--;
		if (input == 's')
			x++;
		if (input == 'a')
			y--;
		if (input == 'd')
			y++;
		printf("\n");
		printf("score=%d", score);//显示分数
	}
	return 0;
}

你可能感兴趣的:(算法,c++,开发语言,c语言,数据结构)