C 语言设计坦克大战(未完成)

//坦克大战
//0、提示界面
//1、边框
//2、指定位置显示自己的坦克
//3、己方坦克随着方向键动起来
//getasynkeustae
//Sleep(毫秒)
//减少闪烁
//不闪烁SetConsoleCursorPosition
//4、指定位置显示敌方坦克
//5、敌方坦克自己动起来
//6、敌方坦克自动发子弹
//7、己方坦克,按空格键发子弹
//8、设置自己的BOSS
//9、设置障碍物
//10、死亡判断
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<string.h>


#define BACK_WIDE 22
#define BACK_HIGHT 22

unsigned char g_arrBackGround[BACK_HIGHT][BACK_WIDE] = {
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
//己方坦克坐标
unsigned char  g_OurPosX = 7;
unsigned char  g_OurPosY = 20;

unsigned char  g_ourPosTempX = 7;
unsigned char  g_ourPosTempY = 20;


//显示提示界面
void StartIU();
//按任意键开始游戏
void StartGame();
//清空显示控制台
void ClearIU();
//显示边框
void ShowBackGround();
//己方坦克动起来
void TanKeRun();
//将新的坦克的位置更新到背景中
void TanKePosUpdateBack();
//记录坦克的坐标
void RemberTanKePos();
//清空坦克旧坐标
void ClearTanKeOldPos();
int main( )
{
    //显示提示界面
    StartIU();
    StartGame();
    ClearIU();
    ShowBackGround();
    while (1) 
    {
        TanKeRun();
        //TanKePosUpdateBack();
        ClearIU();
        ShowBackGround();
    }
    system("pause");
    return 0;
}
void StartIU()
{
    printf("\n\n\n\t\t\t《欢迎来到坦克大战》\n");
    printf("\t\t\t《W S A D 控制坦克行进》\n");    
    printf("\t\t\t《空格发送子弹》\n");
    printf("\t\t\t《按任意键开始游戏》\n");
}
void StartGame()
{
    _getch();//不阻塞读取

}
void ClearIU()
{
    system("cls");
}
//己方坦克按键动起来
void TanKeRun()
{
    char cKetDis = _getch();
    if ('w' == cKetDis || 'W' == cKetDis)//
    {
        g_OurPosY--;
    }
    if ('s' == cKetDis || 'S' == cKetDis)//
    {
        g_OurPosY++;
    }
    if ('A' == cKetDis || 'a' == cKetDis)//
    {
        g_OurPosX--;
    }
    if ('d' == cKetDis || 'D' == cKetDis)//
    {
        g_OurPosX++;
    }
    if (g_ourPosTempX != g_OurPosX || g_ourPosTempY != g_OurPosY)
    {
        TanKePosUpdateBack();
    }

}
//将新的坦克更新到背景中
void TanKePosUpdateBack()
{
    g_arrBackGround[g_OurPosY][g_OurPosX] = 2;
    ClearTanKeOldPos();
    RemberTanKePos();

}
//记录坦克的坐标
void RemberTanKePos()
{
    g_ourPosTempX = g_OurPosX;
    g_ourPosTempY = g_OurPosY;
}
//清空坦克旧坐标
void ClearTanKeOldPos()
{
    g_arrBackGround[g_ourPosTempY][g_ourPosTempX] = 0;
}
void ShowBackGround()
{
    int i, j;
    unsigned char strAllBack[1024] = { 0 };
    for (i = 0; i <BACK_HIGHT; i++)
    {
        for (j = 0; j < BACK_WIDE; j++)
        {
            switch (g_arrBackGround[i][j])
            {
            case 0://空格
                /*printf(" ");*/
                strcat(strAllBack," ");
                break;
            case 1://边框
                /*printf("#");*/
                strcat(strAllBack, "#");
                break;
            case 2://己方坦克
                /*printf("土");*/
                strcat(strAllBack, "F");
                break;
            }
        }
        strcat(strAllBack, "\n");
    }
    printf("%s",strAllBack);
}

 

posted @ 2019-06-24 14:44  Mr_Song_D  阅读(616)  评论(0)    收藏  举报