美文网首页
C语言/C艹编程入门经典小游戏坦克大战

C语言/C艹编程入门经典小游戏坦克大战

作者: 美丽的宝宝 | 来源:发表于2018-12-19 21:16 被阅读0次

    (小编推荐一个学C语言/C++的学习群:948954484,入群即送C/C++全套学习资料,满满的干货!)

    game.h下的代码

    #ifndef GAME

    #define GAME

    #include’‘tankdef.h’’

    oid ImportMapGameAreaArry();

    oid ImportHomeGameAreaArry();

    oid ImportMyTankGameAreaArry();

    oid DrawGame_Info();

    int ConsoleSwtichArrX(int x);

    int ConsoleSwtichArrY(int y);

    oid TankAdjustUp(Tank *ptank, Dir dir);

    oid TankAdjustLeft(Tank *ptank);

    oid TankAdjustRight(Tank *ptank);

    oid TankAdjustDown(Tank *ptank);

    oid TankAdjustUpE(Tank *ptank, Dir dir);

    //发射炮弹,参数为发射改炮弹的坦克

    SHELLNODE *shot(Tank *ptank);

    //炮弹向上移动

    int ShellMoeUP(SHELLNODE *psn);

    int ShellMoeDOWN(SHELLNODE *psn);

    int ShellMoeLEFT(SHELLNODE *psn);

    int ShellMoeRIGHT(SHELLNODE *psn);

    //检查链表

    SHELLNODE *CheckLinkList(int owner,int x,int y);

    //线程处理函数

    DWORD WINAPI ShellDispes(LPO lpParam);

    //oid WriteFile1();

    oid HitTarget(SHELLNODE *psn, int tagert);

    //根据炮弹的坐标销毁相应的坦克

    oid DestroyEnemy(SHELLNODE *psn);

    //清空敌方坦克的数据

    oid ClearEnemyTank(Tank *ptank);

    //炮弹节点的比较函数

    int ShellCompareByNum(NODE *pNode1, NODE *pNode2);

    //敌方坦克移动函数

    DWORD WINAPI EnemyMoe(LPO lpParam);

    #endif

    game.c下的代码

    #define _CRT_SECURE_NO_WARNINGS

    #include’‘tankdef.h’’

    #include’‘interface.h’’

    #include

    #include

    #include

    #include

    #include ‘‘game.h’’

    int g_start;

    oid Game_Start()

    {

    HANDLE hThread;

    //初始化临界区资源

    system(’‘mode con cols=95 lines=25’’);

    SetConsoleTitle(TEXT(’‘坦克大战’’));

    InitializeCriticalSection(&g_cs);

    srand((unsigned int)time(NULL));

    DrawGameBord();

    DrawGame_Info();

    LoadMap();

    DrawHome();

    InitMyTnak(&MyselfTanke);

    DrawTnak(&MyselfTanke);

    ImportMapGameAreaArry();

    ImportHomeGameAreaArry();

    ImportMyTankGameAreaArry();

    PlaySound(TEXT(’'sound/Tnak.wa ‘’), NULL, SND_LOOP);

    PlaySound(TEXT(’'sound/TankMoe.wa ‘’), NULL, SND_LOOP);

    int i;

    for (i = 0; i

    {

    EnemyTank[i] = InitEnemtyTank();

    DrawEnmpty(&EnemyTank[i]);

    ImportEnemyTankGameAreaArry(&EnemyTank[i]);

    }

    while (i–)

    {

    hThread = CreateThread(NULL, 0, EnemyMoe, &EnemyTank[i], 0, NULL);

    CloseHandle(hThread);

    }

    //初始化炮弹链表

    InitLinkList(&g_shell_list);

    //WriteFile1();

    }

    oid InitMyTnak(Tank *ptank)

    {

    ptank->belong = MYSELF;

    ptank->dir = UP;

    ptank->lief = 1;

    ptank->x = Left_Top_X 24;

    ptank->y = Left_Top_Y 18;

    }

    Tank InitEnemtyTank()

    {

    int row, col, oerlp;

    int i, j;

    Tank tank;

    tank.belong = ENEMY;

    tank.dir = rand() % 4;

    tank.lief = 1;

    tank.speed = 400;

    tank.y = Left_Top_Y 1;

    while (1)

    {

    oerlp = 0;

    tank.x = rand() % 29 * 2 Left_Top_X 2;

    row = ConsoleSwtichArrY(tank.y);

    col = ConsoleSwtichArrX(tank.x);

    for (i = row; i

    { //取非为真

    for (j = col; j

    {

    if (g_area_data[i][j])

    {

    oerlp = 1;

    }

    }

    }

    if (!oerlp)

    break;

    }

    return tank;

    }

    Tank InitEnemtyTankSpeed()

    {

    int row, col, oerlp;

    int i, j;

    Tank tank;

    tank.belong = ENEMY;

    tank.dir = rand() % 4;

    tank.lief = 1;

    tank.speed = 100;

    tank.y = Left_Top_Y 1;

    while (1)

    {

    oerlp = 0;

    tank.x = rand() % 29 * 2 Left_Top_X 2;

    row = ConsoleSwtichArrY(tank.y);

    col = ConsoleSwtichArrX(tank.x);

    for (i = row; i

    { //取非为真

    for (j = col; j

    {

    if (g_area_data[i][j])

    {

    oerlp = 1;

    }

    }

    }

    if (!oerlp)

    break;

    }

    return tank;

    }

    //将地图导入到二维数组

    oid ImportMapGameAreaArry()

    {

    int i, j;

    for (i = 0; i

    {

    for (j = 0; j

    {

    g_area_data[i 3][j] = map[i][j];

    }

    }

    }

    //将老家导入到二维数组

    oid ImportHomeGameAreaArry()

    {

    int i, j;

    int row, col;

    row = ConsoleSwtichArrY(Left_Top_Y 18);

    col = ConsoleSwtichArrX(Left_Top_X 30);

    for (i = 0; i

    {

    for (j = 0; j

    {

    if (i == 1 && j == 1)

    g_area_data[row i][col j] = AREA_HOME;

    else

    g_area_data[row i][col j] = AREA_WALL;

    }

    }

    }

    oid ImportMyTankGameAreaArry()

    {

    int i, j;

    int row, col;

    row = ConsoleSwtichArrY(MyselfTanke.y);

    col = ConsoleSwtichArrX(MyselfTanke.x);

    for (i = 0; i

    {

    for (j = 0; j

    {

    if (tank[MyselfTanke.dir][i][j])

    {

    g_area_data[i row][j col] = AREA_SELF;

    }

    else

    g_area_data[i row][j col] = AREA_SPACE;

    }

    }

    }

    oid ImportEnemyTankGameAreaArry(Tank *ptank)

    {

    int i, j;

    int row, col;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    for (i = 0; i

    {

    for (j = 0; j

    {

    if (tank[ptank->dir][i][j])

    {

    g_area_data[i row][j col] = AREA_ENEMY;

    }

    else

    g_area_data[i row][j col] = AREA_SPACE;

    }

    }

    }

    oid ImportMyClearTankGameAreaArry()

    {

    int i, j;

    int row, col;

    row = ConsoleSwtichArrY(MyselfTanke.y);

    col = ConsoleSwtichArrX(MyselfTanke.x);

    for (i = 0; i

    {

    for (j = 0; j

    {

    if (tank[MyselfTanke.dir][i][j])

    {

    g_area_data[i row][j col] = AREA_SPACE;

    }

    }

    }

    }

    int ConsoleSwtichArrX(int x)

    {

    return (x - (Left_Top_X 2)) / 2;

    }

    int ConsoleSwtichArrY(int y)

    {

    return y - (Left_Top_Y 1);

    }

    oid WriteFile1()

    {

    int i,j;

    FILE *fp = fopen(’‘1.txt’’, ‘‘w’’);

    if (fp == NULL)

    return;

    for (i = 0; i

    {

    for (j = 0; j

    {

    fprintf_s(fp, ‘’-’’, g_area_data[i][j]);

    }

    fprintf_s(fp,’’ ‘’);

    }

    fclose(fp);

    }

    oid TankAdjustUp(Tank *ptank,Dir dir)

    {

    ptank->dir = dir;

    DrawTnakClear(ptank);

    DrawTnak(ptank);

    }

    oid TankAdjustLeft(Tank *ptank)

    {

    ptank->dir = LEFT;

    DrawTnakClear(ptank);

    DrawTnak(ptank);

    }

    oid TankAdjustRight(Tank *ptank)

    {

    ptank->dir = RIGHT;

    DrawTnakClear(ptank);

    DrawTnak(ptank);

    }

    oid TankAdjustDown(Tank *ptank)

    {

    ptank->dir = DOWN;

    DrawTnakClear(ptank);

    DrawTnak(ptank);

    }

    oid TankAdjustUpE(Tank *ptank, Dir dir)

    {

    ptank->dir = dir;

    DrawTnakClear(ptank);

    DrawEnmpty(ptank);

    }

    SHELLNODE *shot(Tank *ptank)

    {

    static unsigned int num = 0;

    SHELLNODE psn = (SHELLNODE)malloc(sizeof(SHELLNODE));

    if (psn == NULL)

    {

    return NULL;

    }

    psn->node.next = NULL;

    psn->shell.belong = ptank->belong;

    psn->shell.dir = ptank->dir;

    psn->shell.isshow = 0;

    psn->shell.speed = 70;

    psn->shell.left = 1;

    psn->shell.num = num ;

    switch (ptank->dir)

    {

    case UP:

    psn->shell.x = ptank->x 2;

    psn->shell.y = ptank->y;

    break;

    case DOWN:

    psn->shell.x = ptank->x 2;

    psn->shell.y = ptank->y 2;

    break;

    case LEFT:

    psn->shell.x = ptank->x;

    psn->shell.y = ptank->y 1;

    break;

    case RIGHT:

    psn->shell.x = ptank->x 4;

    psn->shell.y = ptank->y 1;

    break;

    }

    //放入链表中

    AddNode(g_shell_list, (NODE*)psn);

    return psn;

    }

    int ShellMoeUP(SHELLNODE *psn)

    {

    int x, y;

    SHELLNODE *ps;

    x = ConsoleSwtichArrX(psn->shell.x);

    y = ConsoleSwtichArrY(psn->shell.y);

    if (psn->shell.isshow)//如果炮弹之前已经运行

    DrawShell(&psn->shell, ‘’ ‘’);//擦除炮弹

    if (y

    return OER_LOW;//越界

    //如果上方有墙壁或坦克,就返回

    if (g_area_data[y - 1][x] != AREA_SPACE)//撞到其它物体

    {

    psn->shell.isshow = 0;//停止运行

    psn->shell.y -= 1;//调整炮弹坐标

    return g_area_data[y - 1][x];

    }

    //如果上方有对方炮弹,就返回

    //遍历链表,查找有无对方炮弹存在

    if (ps = CheckLinkList(!psn->shell.belong, psn->shell.x, psn->shell.y - 1))

    {

    ps->shell.left = 0;//让对方的炮弹的生命结束

    /*sprintf_s(buf, 100, ‘‘Owner %d Shell Num %d died of x:%d,y:%d’’, ps->shell.owner, ps->shell.num, ps->shell.x, ps->shell.y);

    WriteToFile(buf);*/

    return AREA_SHELL;

    }

    //其它情况,则显示该炮弹

    psn->shell.y -= 1;//调整炮弹坐标

    if (psn->shell.left)

    DrawShell(&psn->shell, SHELL_LETTER);

    psn->shell.isshow = 1;

    return 0;

    }

    int ShellMoeDOWN(SHELLNODE *pShell)

    {

    int x, y;

    SHELLNODE *ps;

    x = ConsoleSwtichArrX(pShell->shell.x);

    y = ConsoleSwtichArrY(pShell->shell.y);

    if (pShell->shell.isshow)//如果炮弹之前已经运行

    DrawShell(&pShell->shell, ‘’ ‘’);//擦除炮弹

    if (y >= Gmae_Arr_Height - 1)

    return OER_LOW;//越界

    //如果上方有墙壁或坦克,就返回

    if (g_area_data[y 1][x] != AREA_SPACE)//撞到其它物体

    {

    pShell->shell.isshow = 0;//停止运行

    pShell->shell.y = 1;//调整炮弹坐标

    return g_area_data[y 1][x];

    }//如果上方有对方炮弹,就返回

    //遍历链表,查找有无对方炮弹存在

    if (ps = CheckLinkList(!pShell->shell.belong, pShell->shell.x, pShell->shell.y 1))

    {

    ps->shell.left = 0;//让对方的炮弹的生命结束

    /*sprintf_s(buf, 100, ‘‘Owner %d Shell Num %d died of x:%d,y:%d’’, ps->shell.owner, ps->shell.num, ps->shell.x, ps->shell.y);

    WriteToFile(buf);*/

    return AREA_SHELL;

    }

    //其它情况,则显示该炮弹

    pShell->shell.y = 1;//调整炮弹坐标

    if (pShell->shell.left)

    DrawShell(&pShell->shell, SHELL_LETTER);

    pShell->shell.isshow = 1;

    return 0;

    }

    int ShellMoeLEFT(SHELLNODE *pShell)

    {

    int x, y;

    SHELLNODE *ps;

    x = ConsoleSwtichArrX(pShell->shell.x);

    y = ConsoleSwtichArrY(pShell->shell.y);

    if (pShell->shell.isshow)//如果炮弹之前已经运行

    DrawShell(&pShell->shell, ‘’ ‘’);//擦除炮弹

    if (x

    return OER_LOW;//越界

    //如果上方有墙壁或坦克,就返回

    if (g_area_data[y][x - 1] != AREA_SPACE)//撞到其它物体

    {

    pShell->shell.isshow = 0;//停止运行

    pShell->shell.x -= 2;//调整炮弹坐标

    return g_area_data[y][x - 1];

    }//如果上方有对方炮弹,就返回

    //遍历链表,查找有无对方炮弹存在

    if (ps = CheckLinkList(!pShell->shell.belong, pShell->shell.x - 2, pShell->shell.y))

    {

    ps->shell.left = 0;//让对方的炮弹的生命结束

    /*sprintf_s(buf, 100, ‘‘Owner %d Shell Num %d died of x:%d,y:%d’’, ps->shell.owner, ps->shell.num, ps->shell.x, ps->shell.y);

    WriteToFile(buf);*/

    return AREA_SHELL;

    }

    //其它情况,则显示该炮弹

    pShell->shell.x -= 2;//调整炮弹坐标

    if (pShell->shell.left)

    DrawShell(&pShell->shell, SHELL_LETTER);

    pShell->shell.isshow = 1;

    return 0;

    }

    int ShellMoeRIGHT(SHELLNODE *pShell)

    {

    int x, y;

    SHELLNODE *ps;

    x = ConsoleSwtichArrX(pShell->shell.x);

    y = ConsoleSwtichArrY(pShell->shell.y);

    if (pShell->shell.isshow)//如果炮弹之前已经运行

    DrawShell(&pShell->shell, ‘’ ‘’);//擦除炮弹

    if (x >= Game_Arr_Width - 1)

    return OER_LOW;//越界

    //如果右方有墙壁或坦克,就返回

    if (g_area_data[y][x 1] != AREA_SPACE)//撞到其它物体

    {

    pShell->shell.isshow = 0;//停止显示

    pShell->shell.x = 2;//调整炮弹坐标

    return g_area_data[y][x 1];

    }//如果上方有对方炮弹,就返回

    //遍历链表,查找有无对方炮弹存在

    if (ps = CheckLinkList(!pShell->shell.belong, pShell->shell.x 2, pShell->shell.y))

    {

    ps->shell.left = 0;//让对方的炮弹的生命结束

    /*sprintf_s(buf, 100, ‘‘Owner %d Shell Num %d died of x:%d,y:%d’’, ps->shell.owner, ps->shell.num, ps->shell.x, ps->shell.y);

    WriteToFile(buf);*/

    return AREA_SHELL;

    }

    //其它情况,则显示该炮弹

    pShell->shell.x = 2;//调整炮弹坐标

    if (pShell->shell.left)

    DrawShell(&pShell->shell, SHELL_LETTER);

    pShell->shell.isshow = 1;

    return 0;

    }

    //查找链表中有没有敌方的炮弹

    SHELLNODE *CheckLinkList(int owner, int x, int y)

    {

    SHELLNODE *psn = NULL;

    int i;

    for (i = 0; i

    {

    psn = (SHELLNODE *)GetNode(g_shell_list, i);

    if (psn->shell.x == x && psn->shell.y == y

    && psn->shell.belong == owner)

    {

    return psn;

    }

    }

    return NULL;

    }

    DWORD WINAPI ShellDispes(LPO lpParam)

    {

    SHELLNODE *psm = (SHELLNODE *)lpParam;

    int ret; //返回值

    while (psm->shell.left == 1)

    {

    if (g_start == 1)

    continue;

    EnterCriticalSection(&g_cs);

    switch (psm->shell.dir)

    {

    case UP:

    if (ret = ShellMoeUP(psm))

    {

    HitTarget(psm, ret);

    }

    break;

    case DOWN:

    if (ret = ShellMoeDOWN(psm))

    HitTarget(psm, ret);

    break;

    case LEFT:

    if (ret = ShellMoeLEFT(psm))

    HitTarget(psm, ret);

    break;

    case RIGHT:

    if (ret = ShellMoeRIGHT(psm))

    HitTarget(psm, ret);

    break;

    }

    LeaeCriticalSection(&g_cs);

    Sleep(psm->shell.speed);

    }

    //为了保护链表删除时要同步

    EnterCriticalSection(&g_cs);

    if (psm->shell.isshow)

    DrawShell(&psm->shell, ‘’ ‘’);

    if (psm->shell.belong == MYSELF)

    –g_self_shell_cout;

    if (DeleteNode(g_shell_list, (NODE*)psm, ShellCompareByNum))

    free(psm);

    LeaeCriticalSection(&g_cs);

    return 0;

    }

    int g_die_Enemy = 0;

    oid HitTarget(SHELLNODE *psn, int tagert)

    {

    switch (tagert)

    {

    case AREA_SHELL:

    case OER_LOW:

    psn->shell.left = 0;

    break;

    case AREA_WALL:

    HitWall(&psn->shell);

    psn->shell.left = 0;

    PlaySound(TEXT(’'sound/击碎.wa ‘’), NULL,SND_ASYNC );

    break;

    case AREA_HOME:

    HitHome(&psn->shell);

    psn->shell.left = 0;

    break;

    case AREA_ENEMY:

    if (psn->shell.belong == MYSELF)

    {

    g_die_Enemy;

    psn->shell.left = 0;

    DestroyEnemy(psn);

    PlaySound(TEXT(’'sound/爆炸.wa ‘’), NULL, SND_ASYNC );

    DrawGame_Info();

    }

    break;

    case AREA_SELF:

    if (psn->shell.belong == ENEMY)

    {

    psn->shell.left = 0;

    ClearEnemyTank(&MyselfTanke);

    DrawTnakClear(&MyselfTanke);

    g_die_shellf;

    DrawGame_Info();

    if (g_die_shellf == 3)

    {

    music = 0;

    PrintGameOer();

    }

    else

    {

    InitMyTnak(&MyselfTanke);

    DrawTnak(&MyselfTanke);

    ImportMyTankGameAreaArry();

    PlaySound(TEXT(’'sound/爆炸.wa ‘’), NULL, SND_ASYNC);

    }

    }

    break;

    }

    }

    oid DestroyEnemy(SHELLNODE *psn)

    {

    int i;//用于遍历数组

    Tank t; //一个临时的坦克

    for (i = 0; i

    {

    t = EnemyTank[i];

    if (psn->shell.x >= t.x && psn->shell.x

    psn->shell.y >= t.y && psn->shell.y

    {

    //清除坦克

    DrawTnakClear(&t);

    //修改坦克的生命值

    EnemyTank[i].lief = 0;

    //清空敌方坦克的数据

    ClearEnemyTank(&t);

    }

    }

    }

    oid ClearEnemyTank(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    for (i = row; i

    {

    for (j = col; j

    {

    g_area_data[i][j] = AREA_SPACE;

    }

    }

    }

    //炮弹节点比较函数

    int ShellCompareByNum(NODE *pNode1, NODE *pNode2)

    {

    SHELLNODE p1 = (SHELLNODE)pNode1;

    SHELLNODE P2 = (SHELLNODE)pNode2;

    return p1->shell.num == P2->shell.num ? 0 : 1;

    }

    int x = 0;

    DWORD WINAPI EnemyMoe(LPO lpParam)

    {

    Tank * pTank = (Tank*)lpParam;

    SHELLNODE *psn = NULL;

    HANDLE hThread;

    unsigned int nStep = 0;

    unsigned int nShot = 0;

    nShot = 2;

    int nDir = rand() % 5 5;

    while (pTank->lief)

    {

    if(g_start == 1)

    continue;

    EnterCriticalSection(&g_cs);

    nStep;

    if(nStep % nDir == 0)

    TankAdjustUpE(pTank, rand()%4);

    if (nStep % nShot == 0) //改发射炮弹了

    {

    if (psn = shot(pTank))

    {

    hThread = CreateThread(NULL, 0, ShellDispes, psn, 0, NULL);

    CloseHandle(hThread);

    }

    }

    switch (pTank->dir)

    {

    case UP:

    if (TankMoeUpE(pTank))

    {

    TankAdjustUpE(pTank, (pTank->dir 1) % 4);

    }

    break;

    case DOWN:

    if(TankMoeDownE(pTank))

    TankAdjustUpE(pTank, (pTank->dir 1) % 4);

    break;

    case LEFT: if(TankMoeLeftE(pTank))

    TankAdjustUpE(pTank, (pTank->dir 1) % 4);

    break;

    case RIGHT: if(TankMoeRightE(pTank))

    TankAdjustUpE(pTank, (pTank->dir 1) % 4);

    break;

    }

    LeaeCriticalSection(&g_cs);

    Sleep(pTank->speed);

    }

    Die_Enemy_Tank_Count;

    // The number of tanks on the map Number of dead tanks

    if (ENEMY_Tank Die_Enemy_Tank_Count

    {

    //Create a new enemy tank

    HANDLE hThread;

    EnterCriticalSection(&g_cs);

    if (Die_Enemy_Tank_Count % 1 == 0)

    {

    *pTank = InitEnemtyTankSpeed();

    ImportEnemyTankGameAreaArry(pTank);

    }

    else

    {

    *pTank = InitEnemtyTank();

    ImportEnemyTankGameAreaArry(pTank);

    }

    hThread = CreateThread(NULL, 0, EnemyMoe, pTank, 0, NULL);

    CloseHandle(hThread);

    LeaeCriticalSection(&g_cs);

    }

    else if(Die_Enemy_Tank_Count == ENEMY_Z_ALL_TANK)

    {

    EnterCriticalSection(&g_cs);

    Printictory();

    LeaeCriticalSection(&g_cs);

    }

    return 0;

    }

    oid DrawGame_Info()

    {

    EnterCriticalSection(&g_cs);

    SetConsoleTextAttribute(g_hout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);

    COORD pos;

    pos.X = Left_Top_X Game_Arr_Width * 2 8;

    pos.Y = Left_Top_Y 1;

    SetConsoleCursorPosition(g_hout, pos);

    printf(’‘C语言坦克大战项目’’);

    pos.Y = 2;

    SetConsoleCursorPosition(g_hout, pos);

    printf(’‘信息窗口:’’);

    pos.Y;

    if (g_start == 1)

    {

    SetConsoleCursorPosition(g_hout, pos);

    printf(’‘游戏运行状态: Pause’’);

    }

    if(g_start == 0)

    {

    SetConsoleCursorPosition(g_hout, pos);

    printf(’'游戏运行状态: Run ‘’);

    }

    pos.Y = 2;

    SetConsoleCursorPosition(g_hout, pos);

    printf(’‘敌方坦克总数:%d’’, ENEMY_Z_ALL_TANK);

    pos.Y ;

    SetConsoleCursorPosition(g_hout, pos);

    printf(’‘敌方坦克阵亡数量:%d’’, g_die_Enemy);

    pos.Y ;

    SetConsoleCursorPosition(g_hout, pos);

    printf(’'敌方坦克剩余数量:%d ‘’,ENEMY_Z_ALL_TANK - g_die_Enemy);

    pos.Y = 2;

    SetConsoleCursorPosition(g_hout, pos);

    printf(’‘我方坦克总数:%d’’, AREA_SELF);

    pos.Y ;

    SetConsoleCursorPosition(g_hout, pos);

    printf(’‘我方坦克阵亡数量:%d’’, g_die_shellf);

    pos.Y ;

    SetConsoleCursorPosition(g_hout, pos);

    printf(’‘我方坦克剩余数量:%d’’, AREA_SELF - g_die_shellf);

    pos.Y = 2;

    SetConsoleCursorPosition(g_hout, pos);

    printf(’‘游戏暂停或开始:P’’);

    pos.Y ;

    SetConsoleCursorPosition(g_hout, pos);

    printf(’‘坦克向上移动:W’’);

    pos.Y ;

    SetConsoleCursorPosition(g_hout, pos);

    printf(’‘坦克向左移动:A’’);

    pos.Y ;

    SetConsoleCursorPosition(g_hout, pos);

    printf(’‘坦克向右移动:D’’);

    pos.Y ;

    SetConsoleCursorPosition(g_hout, pos);

    printf(’‘坦克向下移动:S’’);

    pos.Y ;

    SetConsoleCursorPosition(g_hout, pos);

    printf(’‘坦克发射炮弹:J’’);

    LeaeCriticalSection(&g_cs);

    }

    interface.h

    #include

    #include’‘tankdef.h’’

    #ifndef INTERFACE

    #define INTERFACE

    //开始游戏

    oid Game_Start();

    C语言/C艹编程入门经典小游戏坦克大战!

    //画游戏界面

    oid DrawGameBord();

    oid LoadMap();

    oid DrawHome();

    oid InitMyTnak(Tank *ptank);

    oid DrawTnak(Tank *ptank);

    Tank InitEnemtyTank();

    oid DrawEnmpty(Tank *pEtank);

    oid DrawTnakClear(Tank *ptank);

    oid ImportEnemyTankGameAreaArry();

    oid ImportMyClearTankGameAreaArry();

    int TankMoeUp(Tank *ptank);

    int TankMoeLeft(Tank *ptank);

    int TankMoeRight(Tank *ptank);

    int TankMoeDown(Tank *ptank);

    oid DrawShell(SHELL *Pshell,const char *liter);

    oid WriteFile1();

    oid HitWall(SHELL *pShell);

    oid HitHome(SHELL *pShell);

    oid PrintGameOer();

    int TankMoeUpE(Tank *ptank);

    int TankMoeDownE(Tank *ptank);

    int TankMoeLeftE(Tank *ptank);

    int TankMoeRightE(Tank *ptank);

    oid Printictory();

    #endif

    interface.c

    #include’‘tankdef.h’’

    #include’‘interface.h’’

    #include

    #include’‘game.h’’

    HANDLE g_hout;

    HWND hwnd = NULL;

    CONSOLE_SCREEN_BUFFER_INFO csbi;

    int music = 1;

    oid DrawGameBord()

    {

    g_hout = GetStdHandle(STD_OUTPUT_HANDLE);

    CONSOLE_CURSOR_INFO c;

    GetConsoleCursorInfo(g_hout, &c);

    c.bisible = 0;

    SetConsoleCursorInfo(g_hout, &c);

    int row, col;

    DWORD count;

    COORD pos;

    pos.X = Left_Top_X;

    pos.Y = Left_Top_Y;

    for (row = 0; row

    {

    SetConsoleCursorPosition(g_hout, pos);

    for (col = 0; col

    {

    if (row == 0 || row == Gmae_Arr_Height 1 ||

    col == 0 || col == Game_Arr_Width 1)

    {

    GetConsoleScreenBufferInfo(g_hout, &csbi);

    printf(’’’’);

    FillConsoleOutputAttribute(g_hout, BORDER_Color,2,csbi.dwCursorPosition, &count);

    }

    else

    printf(’’ ‘’);

    }

    pos.Y ;

    }

    pos.X = 34;

    SetConsoleCursorPosition(g_hout, pos);

    GetConsoleScreenBufferInfo(g_hout, &csbi);

    printf(’‘坦克大战’’);

    FillConsoleOutputAttribute(g_hout, TANKTEXT, 8, csbi.dwCursorPosition, &count);

    }

    oid LoadMap()

    {

    int row, col;

    COORD pos;

    DWORD count;

    pos.X = Left_Top_X 2;

    pos.Y = Left_Top_Y 4;

    for (row = 0; row

    {

    SetConsoleCursorPosition(g_hout, pos);

    for (col = 0; col

    {

    if (map[row][col])

    {

    GetConsoleScreenBufferInfo(g_hout, &csbi);

    printf(’’%s’’, TANKLE_LETTER);

    FillConsoleOutputAttribute(g_hout, BORDER_Color1, 2, csbi.dwCursorPosition, &count);

    }

    else

    printf(’’ ‘’);

    }

    pos.Y ;

    }

    }

    oid DrawHome()

    {

    int row, col;

    COORD pos;

    DWORD count;

    pos.X = Left_Top_X 30;

    pos.Y = Left_Top_Y 18;

    for (row = 0; row

    {

    SetConsoleCursorPosition(g_hout, pos);

    for (col = 0; col

    {

    if (row == 1 && col == 1)

    {

    GetConsoleScreenBufferInfo(g_hout, &csbi);

    printf(’’%s’’, HOME_LETTER);

    FillConsoleOutputAttribute(g_hout,HOME_Color, 2, csbi.dwCursorPosition, &count);

    }

    else

    printf(’’%s’’, BORDER_LEFTER);

    }

    pos.Y ;

    }

    }

    oid DrawTnak(Tank *ptank)

    {

    COORD pos;

    DWORD count;

    int row, col;

    pos.X = ptank->x;

    pos.Y = ptank->y;

    for (row = 0; row

    {

    SetConsoleCursorPosition(g_hout, pos);

    for (col = 0; col

    {

    if (tank[ptank->dir][row][col] == 1)

    {

    GetConsoleScreenBufferInfo(g_hout, &csbi);

    printf(’’’’);

    FillConsoleOutputAttribute(g_hout, Tank_Color, 2, csbi.dwCursorPosition, &count);

    }

    else

    {

    if (tank[0][row][col] == 0 || tank[1][row][col] == 0)

    printf(’‘│’’);

    if (tank[ptank->dir][row][col] == 2)

    {

    printf(’’’’);

    }

    if (tank[3][row][col] == 0 || tank[2][row][col] == 0)

    printf(’‘─’’);

    }

    }

    pos.Y ;

    }

    }

    oid DrawEnmpty(Tank *pEtank)

    {

    COORD pos;

    DWORD count;

    pos.X = pEtank->x;

    pos.Y = pEtank->y;

    int row, col;

    for (row = 0; row

    {

    SetConsoleCursorPosition(g_hout, pos);

    for (col = 0; col

    {

    if (tankE[pEtank->dir][row][col])

    {

    GetConsoleScreenBufferInfo(g_hout, &csbi);

    printf(’’’’);

    FillConsoleOutputAttribute(g_hout, FOREGROUND_GREEN | FOREGROUND_INTENSITY, 2, csbi.dwCursorPosition, &count);

    }

    else

    printf(’’ ‘’);

    }

    pos.Y ;

    }

    }

    oid DrawTnakClear(Tank *ptank)

    {

    COORD pos;

    int row, col;

    pos.X = ptank->x;

    pos.Y = ptank->y;

    for (row = 0; row

    {

    SetConsoleCursorPosition(g_hout, pos);

    for (col = 0; col

    {

    if (tank[ptank->dir][row][col])

    {

    printf(’’ ‘’);

    }

    else

    printf(’’ ‘’);

    }

    pos.Y ;

    }

    }

    int TankMoeUp(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (row

    return OER_LOW;

    //判断是否撞墙

    for (i = col; i

    {

    if (g_area_data[row - 1][i])

    return g_area_data[row - 1][i];

    }

    for (i = row - 1; i

    {

    for (j = col; j

    {

    if (i == row 2)

    g_area_data[i][j] = 0;

    else

    g_area_data[i][j] = g_area_data[i 1][j];

    }

    }

    DrawTnakClear(ptank);

    –ptank->y;

    DrawTnak(ptank);

    return 0;

    }

    int TankMoeLeft(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (col

    return OER_LOW;

    //判断是否撞墙

    for (i = row; i

    {

    if (g_area_data[i][col - 1])

    return g_area_data[i][col - 1];

    }

    for (i = row; i

    {

    for (j = col - 1; j

    {

    if (j == col 2)

    g_area_data[i][j] = 0;

    else

    g_area_data[i][j] = g_area_data[i][j 1];

    }

    }

    DrawTnakClear(ptank);

    ptank->x -= 2;

    DrawTnak(ptank);

    return 0;

    }

    int TankMoeRight(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (col 2 >= Game_Arr_Width - 1)

    return OER_LOW;

    //判断是否撞墙

    for (i = row; i

    {

    if (g_area_data[i][col 3])

    return g_area_data[i][col 3];

    }

    for (i = row; i

    {

    for (j = col 3; j >= col; --j)

    {

    if (j == col)

    g_area_data[i][j] = 0;

    else

    g_area_data[i][j] = g_area_data[i][j - 1];

    }

    }

    DrawTnakClear(ptank);

    ptank->x = 2;

    DrawTnak(ptank);

    return 0;

    }

    int TankMoeDown(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (row 2 >= Gmae_Arr_Height - 1)

    return OER_LOW;

    //判断是否撞墙

    for (i = col; i

    {

    if (g_area_data[row 3][i])

    return g_area_data[row 3][i];

    }

    for (i = row 3; i >= row; --i)

    {

    for (j = col; j

    {

    if (i == row)

    g_area_data[i][j] = 0;

    else

    g_area_data[i][j] = g_area_data[i - 1][j];

    }

    }

    DrawTnakClear(ptank);

    ptank->y;

    DrawTnak(ptank);

    return 0;

    }

    int TankMoeUpE(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (row

    return OER_LOW;

    //判断是否撞墙

    for (i = col; i

    {

    if (g_area_data[row - 1][i])

    return g_area_data[row - 1][i];

    }

    for (i = row - 1; i

    {

    for (j = col; j

    {

    if (i == row 2)

    g_area_data[i][j] = 0;

    else

    g_area_data[i][j] = g_area_data[i 1][j];

    }

    }

    DrawTnakClear(ptank);

    –ptank->y;

    if (x == 0)

    DrawEnmpty(ptank);

    /*if (x == 1)

    DrawEnmptySpeed(ptank);*/

    return 0;

    }

    int TankMoeDownE(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (row 2 >= Gmae_Arr_Height - 1)

    return OER_LOW;

    //判断是否撞墙

    for (i = col; i

    {

    if (g_area_data[row 3][i])

    return g_area_data[row 3][i];

    }

    for (i = row 3; i >= row; --i)

    {

    for (j = col; j

    {

    if (i == row)

    g_area_data[i][j] = 0;

    else

    g_area_data[i][j] = g_area_data[i - 1][j];

    }

    }

    DrawTnakClear(ptank);

    ptank->y;

    if(x == 0)

    DrawEnmpty(ptank);

    //if (x == 1)

    //DrawEnmptySpeed(ptank);

    return 0;

    }

    int TankMoeRightE(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (col 2 >= Game_Arr_Width - 1)

    return OER_LOW;

    //判断是否撞墙

    for (i = row; i

    {

    if (g_area_data[i][col 3])

    return g_area_data[i][col 3];

    }

    for (i = row; i

    {

    for (j = col 3; j >= col; --j)

    {

    if (j == col)

    g_area_data[i][j] = 0;

    else

    g_area_data[i][j] = g_area_data[i][j - 1];

    }

    }

    DrawTnakClear(ptank);

    ptank->x = 2;

    if (x == 0)

    DrawEnmpty(ptank);

    /* if (x == 1)

    DrawEnmptySpeed(ptank);*/

    return 0;

    }

    int TankMoeLeftE(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (col

    return OER_LOW;

    //判断是否撞墙

    for (i = row; i

    {

    if (g_area_data[i][col - 1])

    return g_area_data[i][col - 1];

    }

    for (i = row; i

    {

    for (j = col - 1; j

    {

    if (j == col 2)

    g_area_data[i][j] = 0;

    else

    g_area_data[i][j] = g_area_data[i][j 1];

    }

    }

    DrawTnakClear(ptank);

    ptank->x -= 2;

    if (x == 0)

    DrawEnmpty(ptank);

    /* if (x == 1)

    DrawEnmptySpeed(ptank);*/

    return 0;

    }

    oid DrawShell(SHELL *Pshell,const char *litter)

    {

    DWORD cout;

    COORD pos = { Pshell->x,Pshell->y };

    SetConsoleCursorPosition(g_hout, pos);

    printf(’’%s’’, litter);

    FillConsoleOutputAttribute(g_hout, FOREGROUND_RED | FOREGROUND_INTENSITY, 2, pos, &cout);

    }

    oid HitWall(SHELL *pShell)

    {

    int i;

    int row, col;

    row = ConsoleSwtichArrY(pShell->y);

    col = ConsoleSwtichArrX(pShell->x);

    if (pShell->dir == UP || pShell->dir == DOWN)

    {

    COORD pos = { pShell->x - 2,pShell->y };

    for (i = col - 1; i

    {

    SetConsoleCursorPosition(g_hout, pos);

    if(g_area_data[row][i] == AREA_WALL)

    {

    printf(’’ ‘’);

    g_area_data[row][i] = AREA_SPACE;

    }

    pos.X = 2;

    }

    }

    else

    {

    COORD pos = { pShell->x,pShell->y - 1 };

    for (i = row - 1; i

    {

    SetConsoleCursorPosition(g_hout, pos);

    if(g_area_data[i][col] == AREA_WALL)

    {

    printf(’’ ‘’);

    g_area_data[i][col] = AREA_SPACE;

    }

    pos.Y ;

    }

    }

    }

    oid HitHome(SHELL *pShell)

    {

    COORD pos = { pShell->x,pShell->y };

    SetConsoleCursorPosition(g_hout, pos);

    printf(’’ ‘’);

    PrintGameOer();

    }

    oid PrintGameOer()

    {

    system(’‘cls’’);

    DWORD count;

    COORD pos = { 32,10};

    SetConsoleCursorPosition(g_hout, pos);

    printf(’‘Game Oer!’’);

    FillConsoleOutputAttribute(g_hout, FOREGROUND_GREEN | FOREGROUND_INTENSITY |

    BACKGROUND_RED | BACKGROUND_INTENSITY, 10, pos, &count);

    PlaySound(TEXT(’'sound/Dath.wa ‘’), NULL, SND_LOOP);

    DeleteCriticalSection(&g_cs);

    DestroyList(&g_shell_list);

    exit(0);

    }

    oid Printictory()

    {

    PlaySound(TEXT(’'sound/胜利.wa ‘’), NULL, SND_LOOP);

    system(’‘cls’’);

    DWORD count;

    COORD pos = { 32,10 };

    SetConsoleCursorPosition(g_hout, pos);

    printf(’’ octiory ‘’);

    FillConsoleOutputAttribute(g_hout, FOREGROUND_GREEN | FOREGROUND_INTENSITY |

    BACKGROUND_RED | BACKGROUND_INTENSITY, 10, pos, &count);

    DeleteCriticalSection(&g_cs);

    DestroyList(&g_shell_list);

    exit(0);

    }

    #ifndef LIKLIST

    #define LIKLIST

    //定义节点结构

    typedef struct node

    {

    struct node *next; //指向下一个节点

    }NODE;

    typedef struct linklist

    {

    NODE head; //头节点

    int size; //大小

    }LINKLIST;

    //链表操作函数

    //链表初始化

    oid InitLinkList(LINKLIST **list);

    //销毁链表

    oid DestroyList(LINKLIST **list);

    //添加一个节点的链表到尾部

    oid AddNode(LINKLIST *list, NODE *pNode);

    //删除一个指定的节点,删除成功返回成功节点的指针,失败返回NULL

    NODE *DeleteNode(LINKLIST *list, NODE *pNode, int(*compare)(NODE *, NODE *));

    //返回链表中节点的个数

    int CountOfLinkList(LINKLIST *list);

    //返回指定位置的节点

    NODE *GetNode(LINKLIST *list, int pos);

    #endif

    linklist.c 下的代码

    #include’‘linklist.h’’

    #include

    #include

    //链表操作函数

    //链表初始化

    oid InitLinkList(LINKLIST **list)

    {

    list = (LINKLIST)malloc(sizeof(LINKLIST));

    if (*list == NULL)

    return;

    (*list)->head.next = NULL;

    (*list)->size = 0;

    }

    //销毁链表

    oid DestroyList(LINKLIST **list)

    {

    if (list && *list)

    {

    free(*list);

    *list = NULL;

    }

    }

    //添加一个节点的链表到尾部

    oid AddNode(LINKLIST *list, NODE *pNode)

    {

    NODE *p = &list->head;

    while (p->next)

    p = p->next;

    p->next = pNode;

    list->size ; //节点的大小 1

    }

    //删除一个指定的节点,删除成功返回成功节点的指针,失败返回NULL

    NODE *DeleteNode(LINKLIST *list, NODE *pNode, int(*compare)(NODE *, NODE *))

    {

    NODE *p = &list->head;

    NODE *pfree = NULL;

    while (p->next)

    {

    if (compare(p->next, pNode) == 0)

    {

    pfree = p->next;

    p->next = p->next->next;

    list->size–;

    break;

    }

    p = p->next;

    }

    return pfree;

    }

    //返回链表中节点的个数

    int CountOfLinkList(LINKLIST *list)

    {

    return list->size;

    }

    NODE *GetNode(LINKLIST *list, int pos)

    {

    int i;

    NODE *p = list->head.next;

    if (pos

    return NULL;

    for (i = 0; i

    {

    p = p->next;

    }

    return p;

    }

    tankdef.h

    #include

    #include ‘‘linklist.h’’

    #include’‘game.h’’

    #ifndef TANKDEFH

    #define TANKDEFH

    #define Game_Arr_Width 31 //游戏区域的宽度

    #define Gmae_Arr_Height 20 //游戏区域的高度

    #define Left_Top_X 4 //初始x坐标

    #define Left_Top_Y 1 //初始y坐标

    #define BORDER_LEFTER ‘’’’ //游戏边框字符

    #define TANKLE_LETTER ‘’’’ //坦克字符

    #define HOME_LETTER ‘‘★’’ //老家字符

    #define SHELL_LETTER ‘’’’ //炮弹字符

    #define MYSELF 1 //我方坦克

    #define ENEMY 0 //敌方坦克

    #define BORDER_Color FOREGROUND_BLUE | FOREGROUND_INTENSITY //窗口的颜色

    #define BORDER_Color1 FOREGROUND_GREEN | FOREGROUND_INTENSITY | FOREGROUND_BLUE//关卡颜色

    #define TANKTEXT FOREGROUND_RED | FOREGROUND_INTENSITY //文字

    #define HOME_Color FOREGROUND_RED | FOREGROUND_INTENSITY //老家颜色

    #define Tank_Color FOREGROUND_RED | FOREGROUND_INTENSITY | FOREGROUND_BLUE | FOREGROUND_GREEN //坦克颜色

    #define ENEMY_Tank 4

    #define ENEMY_Z_ALL_TANK 10

    #define AREA_SPACE 0 //空

    #define AREA_WALL 1 //墙

    #define AREA_HOME 2 //老家

    #define AREA_SELF 3 //自己

    #define AREA_ENEMY 4 //敌人

    #define AREA_SHELL 5 //炮弹

    #define OER_LOW -1 //越界

    #define Game_Oer 0 //游戏结束

    #define Myself_Shell_Count 3 //我方炮弹的数量

    typedef enum { UP, DOWN, LEFT, RIGHT }Dir;

    typedef struct

    {

    int lief; //声明

    int x; //x坐标

    int y; //y坐标

    Dir dir; //方向

    int belong; //属于

    int speed; //速度

    }Tank;

    typedef struct

    {

    int x;

    int y;

    Dir dir;

    int left;

    int speed;

    int belong;

    int isshow; //是否显示

    int num;

    }SHELL;

    //炮弹节点

    typedef struct

    {

    NODE node;

    SHELL shell;

    }SHELLNODE;

    //***********声明全局变量

    extern HANDLE g_hout;

    extern char map[14][Game_Arr_Width];

    extern char tank[4][3][3];

    extern Tank MyselfTanke; //我方坦克

    extern Tank EnemyTank[ENEMY_Tank]; //敌方坦克

    extern char g_area_data[Gmae_Arr_Height][Game_Arr_Width];

    extern LINKLIST * g_shell_list;

    extern char tankE[4][3][3];

    extern char tankSpeed[4][3][3];

    extern int g_self_shell_cout;

    extern CRITICAL_SECTION g_cs;

    extern int Die_Enemy_Tank_Count;

    extern int x;

    extern int g_die_shellf;

    extern int g_start;

    extern int g_die_Enemy;

    extern int music;

    #endif

    gamedata.c 下的文件

    #include’‘tankdef.h’’

    #include’‘interface.h’’

    char map[14][Game_Arr_Width] = {

    { 0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0 },

    { 0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0 },

    { 0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,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,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,0,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,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0 },

    { 1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1 },

    { 1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1 },

    { 0,0,0,0,0,0,0,0,0,0,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,0,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,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0 },

    { 0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0 },

    { 0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0 },

    { 0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0 }

    };

    char tank[4][3][3] = {

    {

    1,0,1,

    1,2,1,

    1,1,1

    },

    {

    1,1,1,

    1,2,1,

    1,0,1

    },

    {

    1,1,1,

    0,2,1,

    1,1,1

    },

    {

    1,1,1,

    1,2,0,

    1,1,1

    }

    };

    char tankE[4][3][3] = {

    {

    0,1,0,

    1,1,1,

    1,0,1

    },

    {

    1,0,1,

    1,1,1,

    0,1,0

    },

    {

    0,1,1,

    1,1,0,

    0,1,1

    },

    {

    1,1,0,

    0,1,1,

    1,1,0

    }

    };

    char tankSpeed[4][3][3] = {

    {

    0,3,0,

    1,2,1,

    1,0,1

    },

    {

    1,0,1,

    1,2,1,

    0,3,0

    },

    {

    0,1,1,

    3,2,0,

    0,1,1

    },

    {

    1,1,0,

    0,2,3,

    1,1,0

    }

    };

    char g_area_data[Gmae_Arr_Height][Game_Arr_Width];

    Tank MyselfTanke; //我方坦克

    Tank EnemyTank[ENEMY_Tank]; //敌方坦克

    //炮弹链表

    LINKLIST * g_shell_list;

    // 我放炮弹的数量

    int g_self_shell_cout = 0;

    int Die_Enemy_Tank_Count = 0; //The number of enemy tank deaths

    int g_die_shellf = 0; // 敌方阵亡的坦克数量

    CRITICAL_SECTION g_cs;

    main.c 下的文件

    #include ‘‘tankdef.h’’

    #include ‘‘interface.h’’

    #include

    #include

    #include ‘‘game.h’’

    #include

    #pragma comment(lib,’‘winmm.lib’’)

    int main()

    {

    char ch;

    HANDLE hThread;

    SHELLNODE *psn = NULL;

    Game_Start();

    //PlaySound(TEXT(’'G:\s2015\坦克大战\坦克大战\sound\Back.wa ‘’), NULL, SND_ASYNC | SND_LOOP | SND_FILENAME);

    /*PlaySound(TEXT(’'G:\s2015\坦克大战\坦克大战\hit.wa ‘’), NULL, SND_LOOP | SND_FILENAME);

    PlaySound(TEXT(’'G:\s2015\坦克大战\坦克大战\Bang.wa ‘’), NULL, SND_LOOP | SND_FILENAME);

    PlaySound(TEXT(’'G:\s2015\坦克大战\坦克大战\Fanfare.wa ‘’), NULL, SND_LOOP | SND_FILENAME);*/

    mciSendString(TEXT(’‘open sound\Back.wa alias music’’), NULL, 0, NULL);

    mciSendString(TEXT(’‘play music’’), NULL, 0, NULL);

    while (1)

    {

    if (_kbhit())

    {

    ch = _getch();

    if(ch == ‘p’)

    while (1)

    {

    g_start = 1;

    DrawGame_Info();

    ch = _getch();

    if (ch == ‘p’)

    {

    g_start = 0;

    DrawGame_Info();

    break;

    }

    }

    switch (ch)

    {

    case ‘w’:

    EnterCriticalSection(&g_cs);

    if (MyselfTanke.dir != UP)

    TankAdjustUp(&MyselfTanke,UP);

    else

    {

    TankMoeUp(&MyselfTanke);

    }

    LeaeCriticalSection(&g_cs);

    break;

    case ‘s’:

    EnterCriticalSection(&g_cs);

    if (MyselfTanke.dir != DOWN)

    TankAdjustDown(&MyselfTanke);

    else

    {

    TankMoeDown(&MyselfTanke);

    }

    LeaeCriticalSection(&g_cs);

    break;

    case ‘a’:

    EnterCriticalSection(&g_cs);

    if (MyselfTanke.dir != LEFT)

    TankAdjustLeft(&MyselfTanke);

    else

    {

    TankMoeLeft(&MyselfTanke);

    }

    LeaeCriticalSection(&g_cs);

    break;

    case ‘d’:

    EnterCriticalSection(&g_cs);

    if (MyselfTanke.dir != RIGHT)

    TankAdjustRight(&MyselfTanke);

    else

    {

    TankMoeRight(&MyselfTanke);

    }

    LeaeCriticalSection(&g_cs);

    break;

    case ‘j’:

    if (g_self_shell_cout

    {

    PlaySound(TEXT(’'sound/hit.wa ‘’), NULL, SND_ASYNC | SND_NOWAIT);

    psn = shot(&MyselfTanke);

    g_self_shell_cout;

    hThread = CreateThread(NULL, 0, ShellDispes, psn, 0, NULL);

    CloseHandle(hThread);

    }

    break;

    }

    }

    }

    mciSendString(TEXT(’‘close music’’), NULL, 0, NULL);

    while(CountOfLinkList(g_shell_list))

    {

    EnterCriticalSection(&g_cs);

    psn = (SHELLNODE *)GetNode(g_shell_list, 0);

    if (psn)

    psn->shell.left = 0;

    LeaeCriticalSection(&g_cs);

    }

    return 0;

    }

    ---------------------

    作者:IT互联网C++程序员小文

    来源:CSDN

    原文:https://blog.csdn.net/weixin_43770609/article/details/85106055

    版权声明:本文为博主原创文章,转载请附上博文链接!

    相关文章

      网友评论

          本文标题:C语言/C艹编程入门经典小游戏坦克大战

          本文链接:https://www.haomeiwen.com/subject/enhocqtx.html