美文网首页c++/c项目程序员我爱编程
C语言/C++编程基础入门经典小游戏坦克大战

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

作者: 小辰带你看世界 | 来源:发表于2018-02-24 23:12 被阅读92次

    C语言是面向过程的,而C++是面向对象的

    C和C++的区别:

    C是一个结构化语言,它的重点在于算法和数据结构。C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现过程(事务)控制)。

    C++,首要考虑的是如何构造一个对象模型,让这个模型能够契合与之对应的问题域,这样就可以通过获取对象的状态信息得到输出或实现过程(事务)控制。 所以C与C++的最大区别在于它们的用于解决问题的思想方法不一样。之所以说C++比C更先进,是因为“ 设计这个概念已经被融入到C++之中 ”。

    C与C++的最大区别:在于它们的用于解决问题的思想方法不一样。之所以说C++比C更先进,是因为“ 设计这个概念已经被融入到C++之中 ”,而就语言本身而言,在C中更多的是算法的概念。那么是不是C就不重要了,错!算法是程序设计的基础,好的设计如果没有好的算法,一样不行。而且,“C加上好的设计”也能写出非常好的东西。

    小编推荐一个学C语言/C++的学习裙【 二六三,六八八,二七六 】,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!

    game.h下的代码

    #ifndef _GAME_

    #define _GAME_

    #include"tankdef.h"

    void ImportMapGameAreaArry();

    void ImportHomeGameAreaArry();

    void ImportMyTankGameAreaArry();

    void DrawGame_Info();

    int ConsoleSwtichArrX(int x);

    int ConsoleSwtichArrY(int y);

    void TankAdjustUp(Tank *ptank, Dir dir);

    void TankAdjustLeft(Tank *ptank);

    void TankAdjustRight(Tank *ptank);

    void TankAdjustDown(Tank *ptank);

    void TankAdjustUpE(Tank *ptank, Dir dir);

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

    SHELLNODE *shot(Tank *ptank);

    //炮弹向上移动

    int ShellMoveUP(SHELLNODE *psn);

    int ShellMoveDOWN(SHELLNODE *psn);

    int ShellMoveLEFT(SHELLNODE *psn);

    int ShellMoveRIGHT(SHELLNODE *psn);

    //检查链表

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

    //线程处理函数

    DWORD WINAPI ShellDispes(LPVOID lpParam);

    //void WriteFile1();

    void HitTarget(SHELLNODE *psn, int tagert);

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

    void DestroyEnemy(SHELLNODE *psn);

    //清空敌方坦克的数据

    void ClearEnemyTank(Tank *ptank);

    //炮弹节点的比较函数

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

    //敌方坦克移动函数

    DWORD WINAPI EnemyMove(LPVOID 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;

    void 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.wav "), NULL, SND_LOOP);

    PlaySound(TEXT("sound/TankMove.wav "), NULL, SND_LOOP);

    int i;

    for (i = 0; i < ENEMY_Tank; ++i)

    {

    EnemyTank[i] = InitEnemtyTank();

    DrawEnmpty(&EnemyTank[i]);

    ImportEnemyTankGameAreaArry(&EnemyTank[i]);

    }

    while (i--)

    {

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

    CloseHandle(hThread);

    }

    //初始化炮弹链表

    InitLinkList(&g_shell_list);

    //WriteFile1();

    }

    void 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, overlp;

    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)

    {

    overlp = 0;

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

    row = ConsoleSwtichArrY(tank.y);

    col = ConsoleSwtichArrX(tank.x);

    for (i = row; i< row + 3 && !overlp; ++i)

    { //取非为真

    for (j = col; j < col + 3 && !overlp; ++j)

    {

    if (g_area_data[i][j])

    {

    overlp = 1;

    }

    }

    }

    if (!overlp)

    break;

    }

    return tank;

    }

    Tank InitEnemtyTankSpeed()

    {

    int row, col, overlp;

    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)

    {

    overlp = 0;

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

    row = ConsoleSwtichArrY(tank.y);

    col = ConsoleSwtichArrX(tank.x);

    for (i = row; i < row + 3 && !overlp; ++i)

    { //取非为真

    for (j = col; j < col + 3 && !overlp; ++j)

    {

    if (g_area_data[i][j])

    {

    overlp = 1;

    }

    }

    }

    if (!overlp)

    break;

    }

    return tank;

    }

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

    void ImportMapGameAreaArry()

    {

    int i, j;

    for (i = 0; i < 14; ++i)

    {

    for (j = 0; j < Game_Arr_Width; ++j)

    {

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

    }

    }

    }

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

    void ImportHomeGameAreaArry()

    {

    int i, j;

    int row, col;

    row = ConsoleSwtichArrY(Left_Top_Y + 18);

    col = ConsoleSwtichArrX(Left_Top_X + 30);

    for (i = 0; i < 3; ++i)

    {

    for (j = 0; j < 3; ++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;

    }

    }

    }

    void ImportMyTankGameAreaArry()

    {

    int i, j;

    int row, col;

    row = ConsoleSwtichArrY(MyselfTanke.y);

    col = ConsoleSwtichArrX(MyselfTanke.x);

    for (i = 0; i < 3; ++i)

    {

    for (j = 0; j < 3; ++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;

    }

    }

    }

    void ImportEnemyTankGameAreaArry(Tank *ptank)

    {

    int i, j;

    int row, col;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    for (i = 0; i < 3; ++i)

    {

    for (j = 0; j < 3; ++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;

    }

    }

    }

    void ImportMyClearTankGameAreaArry()

    {

    int i, j;

    int row, col;

    row = ConsoleSwtichArrY(MyselfTanke.y);

    col = ConsoleSwtichArrX(MyselfTanke.x);

    for (i = 0; i < 3; ++i)

    {

    for (j = 0; j < 3; ++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);

    }

    void WriteFile1()

    {

    int i,j;

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

    if (fp == NULL)

    return;

    for (i = 0; i < Gmae_Arr_Height; ++i)

    {

    for (j = 0; j < Game_Arr_Width; ++j)

    {

    fprintf_s(fp, "%2d", g_area_data[i][j]);

    }

    fprintf_s(fp," ");

    }

    fclose(fp);

    }

    void TankAdjustUp(Tank *ptank,Dir dir)

    {

    ptank->dir = dir;

    DrawTnakClear(ptank);

    DrawTnak(ptank);

    }

    void TankAdjustLeft(Tank *ptank)

    {

    ptank->dir = LEFT;

    DrawTnakClear(ptank);

    DrawTnak(ptank);

    }

    void TankAdjustRight(Tank *ptank)

    {

    ptank->dir = RIGHT;

    DrawTnakClear(ptank);

    DrawTnak(ptank);

    }

    void TankAdjustDown(Tank *ptank)

    {

    ptank->dir = DOWN;

    DrawTnakClear(ptank);

    DrawTnak(ptank);

    }

    void 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 ShellMoveUP(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 <= 0)

    return OVER_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 ShellMoveDOWN(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 OVER_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 ShellMoveLEFT(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 <= 0)

    return OVER_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 ShellMoveRIGHT(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 OVER_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 < CountOfLinkList(g_shell_list); ++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(LPVOID 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 = ShellMoveUP(psm))

    {

    HitTarget(psm, ret);

    }

    break;

    case DOWN:

    if (ret = ShellMoveDOWN(psm))

    HitTarget(psm, ret);

    break;

    case LEFT:

    if (ret = ShellMoveLEFT(psm))

    HitTarget(psm, ret);

    break;

    case RIGHT:

    if (ret = ShellMoveRIGHT(psm))

    HitTarget(psm, ret);

    break;

    }

    LeaveCriticalSection(&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);

    LeaveCriticalSection(&g_cs);

    return 0;

    }

    int g_die_Enemy = 0;

    void HitTarget(SHELLNODE *psn, int tagert)

    {

    switch (tagert)

    {

    case AREA_SHELL:

    case OVER_LOW:

    psn->shell.left = 0;

    break;

    case AREA_WALL:

    HitWall(&psn->shell);

    psn->shell.left = 0;

    PlaySound(TEXT("sound/击碎.wav "), 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/爆炸.wav "), 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;

    PrintGameOver();

    }

    else

    {

    InitMyTnak(&MyselfTanke);

    DrawTnak(&MyselfTanke);

    ImportMyTankGameAreaArry();

    PlaySound(TEXT("sound/爆炸.wav "), NULL, SND_ASYNC);

    }

    }

    break;

    }

    }

    void DestroyEnemy(SHELLNODE *psn)

    {

    int i;//用于遍历数组

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

    for (i = 0; i < ENEMY_Tank; ++i)

    {

    t = EnemyTank[i];

    if (psn->shell.x >= t.x && psn->shell.x <= t.x + 4 &&

    psn->shell.y >= t.y && psn->shell.y <= t.y + 2)

    {

    //清除坦克

    DrawTnakClear(&t);

    //修改坦克的生命值

    EnemyTank[i].lief = 0;

    //清空敌方坦克的数据

    ClearEnemyTank(&t);

    }

    }

    }

    void ClearEnemyTank(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    for (i = row; i < row + 3; ++i)

    {

    for (j = col; j < col + 3; ++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 EnemyMove(LPVOID 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 (TankMoveUpE(pTank))

    {

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

    }

    break;

    case DOWN:

    if(TankMoveDownE(pTank))

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

    break;

    case LEFT: if(TankMoveLeftE(pTank))

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

    break;

    case RIGHT: if(TankMoveRightE(pTank))

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

    break;

    }

    LeaveCriticalSection(&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 <= ENEMY_Z_ALL_TANK)

    {

    //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, EnemyMove, pTank, 0, NULL);

    CloseHandle(hThread);

    LeaveCriticalSection(&g_cs);

    }

    else if(Die_Enemy_Tank_Count == ENEMY_Z_ALL_TANK)

    {

    EnterCriticalSection(&g_cs);

    PrintVictory();

    LeaveCriticalSection(&g_cs);

    }

    return 0;

    }

    void 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");

    LeaveCriticalSection(&g_cs);

    }

    interface.h

    #include

    #include"tankdef.h"

    #ifndef _INTERFACE_

    #define _INTERFACE_

    //开始游戏

    void Game_Start();

    小编推荐一个学C语言/C++的学习裙【 二六三,六八八,二七六 】,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!

    //画游戏界面

    void DrawGameBord();

    void LoadMap();

    void DrawHome();

    void InitMyTnak(Tank *ptank);

    void DrawTnak(Tank *ptank);

    Tank InitEnemtyTank();

    void DrawEnmpty(Tank *pEtank);

    void DrawTnakClear(Tank *ptank);

    void ImportEnemyTankGameAreaArry();

    void ImportMyClearTankGameAreaArry();

    int TankMoveUp(Tank *ptank);

    int TankMoveLeft(Tank *ptank);

    int TankMoveRight(Tank *ptank);

    int TankMoveDown(Tank *ptank);

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

    void WriteFile1();

    void HitWall(SHELL *pShell);

    void HitHome(SHELL *pShell);

    void PrintGameOver();

    int TankMoveUpE(Tank *ptank);

    int TankMoveDownE(Tank *ptank);

    int TankMoveLeftE(Tank *ptank);

    int TankMoveRightE(Tank *ptank);

    void PrintVictory();

    #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;

    void DrawGameBord()

    {

    g_hout = GetStdHandle(STD_OUTPUT_HANDLE);

    CONSOLE_CURSOR_INFO c;

    GetConsoleCursorInfo(g_hout, &c);

    c.bVisible = 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 < Gmae_Arr_Height + 2; ++row)

    {

    SetConsoleCursorPosition(g_hout, pos);

    for (col = 0; col < Game_Arr_Width + 2; ++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);

    }

    void LoadMap()

    {

    int row, col;

    COORD pos;

    DWORD count;

    pos.X = Left_Top_X + 2;

    pos.Y = Left_Top_Y + 4;

    for (row = 0; row < Gmae_Arr_Height - 6; ++row)

    {

    SetConsoleCursorPosition(g_hout, pos);

    for (col = 0; col < Game_Arr_Width; ++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++;

    }

    }

    void DrawHome()

    {

    int row, col;

    COORD pos;

    DWORD count;

    pos.X = Left_Top_X + 30;

    pos.Y = Left_Top_Y + 18;

    for (row = 0; row < 3; ++row)

    {

    SetConsoleCursorPosition(g_hout, pos);

    for (col = 0; col < 3; ++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++;

    }

    }

    void DrawTnak(Tank *ptank)

    {

    COORD pos;

    DWORD count;

    int row, col;

    pos.X = ptank->x;

    pos.Y = ptank->y;

    for (row = 0; row < 3; ++row)

    {

    SetConsoleCursorPosition(g_hout, pos);

    for (col = 0; col < 3; ++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++;

    }

    }

    void DrawEnmpty(Tank *pEtank)

    {

    COORD pos;

    DWORD count;

    pos.X = pEtank->x;

    pos.Y = pEtank->y;

    int row, col;

    for (row = 0; row < 3; ++row)

    {

    SetConsoleCursorPosition(g_hout, pos);

    for (col = 0; col < 3; ++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++;

    }

    }

    void DrawTnakClear(Tank *ptank)

    {

    COORD pos;

    int row, col;

    pos.X = ptank->x;

    pos.Y = ptank->y;

    for (row = 0; row < 3; ++row)

    {

    SetConsoleCursorPosition(g_hout, pos);

    for (col = 0; col < 3; ++col)

    {

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

    {

    printf(" ");

    }

    else

    printf(" ");

    }

    pos.Y++;

    }

    }

    int TankMoveUp(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (row <= 0)

    return OVER_LOW;

    //判断是否撞墙

    for (i = col; i < col + 3; ++i)

    {

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

    return g_area_data[row - 1][i];

    }

    for (i = row - 1; i < row + 3; ++i)

    {

    for (j = col; j < col + 3; ++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 TankMoveLeft(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (col <= 0)

    return OVER_LOW;

    //判断是否撞墙

    for (i = row; i < row + 3; ++i)

    {

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

    return g_area_data[i][col - 1];

    }

    for (i = row; i < row + 3; ++i)

    {

    for (j = col - 1; j <= col + 2; ++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 TankMoveRight(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (col + 2 >= Game_Arr_Width - 1)

    return OVER_LOW;

    //判断是否撞墙

    for (i = row; i < row + 3; ++i)

    {

    if (g_area_data[i][col + 3])

    return g_area_data[i][col + 3];

    }

    for (i = row; i < row + 3; ++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 TankMoveDown(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (row + 2 >= Gmae_Arr_Height - 1)

    return OVER_LOW;

    //判断是否撞墙

    for (i = col; i < col + 3; ++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 < col + 3; ++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 TankMoveUpE(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (row <= 0)

    return OVER_LOW;

    //判断是否撞墙

    for (i = col; i < col + 3; ++i)

    {

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

    return g_area_data[row - 1][i];

    }

    for (i = row - 1; i < row + 3; ++i)

    {

    for (j = col; j < col + 3; ++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 TankMoveDownE(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (row + 2 >= Gmae_Arr_Height - 1)

    return OVER_LOW;

    //判断是否撞墙

    for (i = col; i < col + 3; ++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 < col + 3; ++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 TankMoveRightE(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (col + 2 >= Game_Arr_Width - 1)

    return OVER_LOW;

    //判断是否撞墙

    for (i = row; i < row + 3; ++i)

    {

    if (g_area_data[i][col + 3])

    return g_area_data[i][col + 3];

    }

    for (i = row; i < row + 3; ++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 TankMoveLeftE(Tank *ptank)

    {

    int row, col;

    int i, j;

    row = ConsoleSwtichArrY(ptank->y);

    col = ConsoleSwtichArrX(ptank->x);

    //判断是否越界

    if (col <= 0)

    return OVER_LOW;

    //判断是否撞墙

    for (i = row; i < row + 3; ++i)

    {

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

    return g_area_data[i][col - 1];

    }

    for (i = row; i < row + 3; ++i)

    {

    for (j = col - 1; j <= col + 2; ++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;

    }

    void 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);

    }

    void 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 < col + 2; ++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 < row + 2; ++i)

    {

    SetConsoleCursorPosition(g_hout, pos);

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

    {

    printf(" ");

    g_area_data[i][col] = AREA_SPACE;

    }

    pos.Y++;

    }

    }

    }

    void HitHome(SHELL *pShell)

    {

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

    SetConsoleCursorPosition(g_hout, pos);

    printf(" ");

    PrintGameOver();

    }

    void PrintGameOver()

    {

    system("cls");

    DWORD count;

    COORD pos = { 32,10};

    SetConsoleCursorPosition(g_hout, pos);

    printf("Game Over!");

    FillConsoleOutputAttribute(g_hout, FOREGROUND_GREEN | FOREGROUND_INTENSITY |

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

    PlaySound(TEXT("sound/Dath.wav "), NULL, SND_LOOP);

    DeleteCriticalSection(&g_cs);

    DestroyList(&g_shell_list);

    exit(0);

    }

    void PrintVictory()

    {

    PlaySound(TEXT("sound/胜利.wav "), NULL, SND_LOOP);

    system("cls");

    DWORD count;

    COORD pos = { 32,10 };

    SetConsoleCursorPosition(g_hout, pos);

    printf(" Voctiory ");

    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;

    //链表操作函数

    //链表初始化

    void InitLinkList(LINKLIST **list);

    //销毁链表

    void DestroyList(LINKLIST **list);

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

    void 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

    //链表操作函数

    //链表初始化

    void InitLinkList(LINKLIST **list)

    {

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

    if (*list == NULL)

    return;

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

    (*list)->size = 0;

    }

    //销毁链表

    void DestroyList(LINKLIST **list)

    {

    if (list && *list)

    {

    free(*list);

    *list = NULL;

    }

    }

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

    void 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 < 0)

    return NULL;

    for (i = 0; i < pos && p; ++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 OVER_LOW -1 //越界

    #define Game_Over 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:\vs2015\坦克大战\坦克大战\sound\Back.wav "), NULL, SND_ASYNC | SND_LOOP | SND_FILENAME);

    /*PlaySound(TEXT("G:\vs2015\坦克大战\坦克大战\hit.wav "), NULL, SND_LOOP | SND_FILENAME);

    PlaySound(TEXT("G:\vs2015\坦克大战\坦克大战\Bang.wav "), NULL, SND_LOOP | SND_FILENAME);

    PlaySound(TEXT("G:\vs2015\坦克大战\坦克大战\Fanfare.wav "), NULL, SND_LOOP | SND_FILENAME);*/

    mciSendString(TEXT("open sound\Back.wav 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

    {

    TankMoveUp(&MyselfTanke);

    }

    LeaveCriticalSection(&g_cs);

    break;

    case 's':

    EnterCriticalSection(&g_cs);

    if (MyselfTanke.dir != DOWN)

    TankAdjustDown(&MyselfTanke);

    else

    {

    TankMoveDown(&MyselfTanke);

    }

    LeaveCriticalSection(&g_cs);

    break;

    case 'a':

    EnterCriticalSection(&g_cs);

    if (MyselfTanke.dir != LEFT)

    TankAdjustLeft(&MyselfTanke);

    else

    {

    TankMoveLeft(&MyselfTanke);

    }

    LeaveCriticalSection(&g_cs);

    break;

    case 'd':

    EnterCriticalSection(&g_cs);

    if (MyselfTanke.dir != RIGHT)

    TankAdjustRight(&MyselfTanke);

    else

    {

    TankMoveRight(&MyselfTanke);

    }

    LeaveCriticalSection(&g_cs);

    break;

    case 'j':

    if (g_self_shell_cout < Myself_Shell_Count)

    {

    PlaySound(TEXT("sound/hit.wav "), 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;

    LeaveCriticalSection(&g_cs);

    }

    return 0;

    }

    小编推荐一个学C语言/C++的学习裙【 二六三,六八八,二七六 】,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!

    这些是C/C++能做的

    服务器开发工程师、人工智能、云计算工程师、信息安全(黑客反黑客)、大数据 、数据平台、嵌入式工程师、流媒体服务器、数据控解、图像处理、音频视频开发工程师、游戏服务器、分布式系统、游戏辅助等

    相关文章

      网友评论

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

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