简单的飞机打靶游戏

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


int main()
{
    int i,j;
    //定义飞机的头部
    int x = 10;
    int y = 5;
    //获取键盘的输入
    char input;
    //定义靶子的位置
    int ny = 5;

    //判断是否开火
    int isFired = 0;
    //判断是否被击中
    int isKilled = 0;

    while(1)
    {
        system("cls");// 清屏函数


        // 输出靶子
        if(isKilled==0)
        {
            for(j =0; j<ny; j++)
                printf(" ");
            printf("+\n");
        }
        //输出子弹
        if(isFired == 0)
        {
            for(i= 0; i<x; i++)
                printf("\n");
        }
        else
        {
            for(i= 0; i<x; i++)
            {
                for(j =0; j<y; j++)
                    printf(" ");
                printf("!\n");
            }
            if(y==ny)
                isKilled = 1;
            isFired = 0;
        }
        for(j =0; j<y; j++)
            printf(" ");

        // 下面输出一个复杂的飞机图案
        printf("*\n");
        for(j =0; j<y-2; j++)
            printf(" ");
        printf("*****\n");
        for(j =0; j<y-1; j++)
            printf(" ");
        printf("* *\n");



       if(kbhit())  // 判断是否有输入
		{
			input = getch();  // 根据用户的不同输入来移动,不必输入回车
			if (input == 'a')
				y--;  // 位置左移
			if (input == 'd')
				y++;  // 位置右移
			if (input == 'w')
				x--;  // 位置上移
			if (input == 's')
				x++;  // 位置下移
			if (input == ' ')//发射子弹
				isFired = 1;
		}
        //当靶子被击中后随机生成一个靶子
        if(isKilled == 1)
        {
            ny = rand() % 10 +1;
            isKilled = 0;
        }

    }



    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39591612/article/details/88908827
今日推荐