⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tank.c

📁 坦克大战,实现基本的游戏功能
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
    tank.c

    坦克相关的函数实现
    最后修改: 2007-06-19

*/

#include "graph.h"
#include "map.h"
#include "gift.h"
#include "tank.h"
#include "player.h"

#ifndef NULL
#define NULL 0
#endif

extern CPlayer g_PPlayer1, g_PPlayer2, g_PEnemy;
extern char g_byStopTank, g_byProtectBase;
extern int g_cntStopTank, g_cntProtectBase;
extern CGift *g_pTheGift, *g_pGift[60][60];;

char g_byTank[3][16][16] =
{
    {
        /* 玩家 1 坦克 */
        {13, 13, 13, 13,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2},
        {13, 13, 13, 13,  2, 13,  2,  2,  2, 13,  2,  2,  2, 13, 13,  2},
        {13, 13, 13, 13,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2},
        {13, 13, 13, 13, 13,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2, 13},
        {13, 13, 13, 13, 13,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2, 13},
        {13, 13, 13, 13, 13,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2, 13},
        { 2, 13, 13, 13, 13,  2,  2, 13, 13, 13, 13, 13,  3,  2,  2, 13},
        { 2,  2,  2,  2,  2,  2,  2, 13,  2,  2,  2,  2,  3,  2,  2, 13},
        { 2,  2,  2,  2,  2,  2,  2, 13,  2,  2,  2,  2,  3,  2,  2, 13},
        { 2, 13, 13, 13, 13,  2,  2, 13,  3,  3,  3,  3,  3,  2,  2, 13},
        {13, 13, 13, 13, 13,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2, 13},
        {13, 13, 13, 13, 13,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2, 13},
        {13, 13, 13, 13, 13,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2, 13},
        {13, 13, 13, 13,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2},
        {13, 13, 13, 13,  2, 13,  2,  2,  2, 13,  2,  2,  2, 13, 13,  2},
        {13, 13, 13, 13,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2}
    },
    {
        /* 玩家 2 坦克 */
        {13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14},
        {13, 13, 13, 13, 14, 13, 14, 14, 14, 13, 14, 14, 14, 13, 13, 14},
        {13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14},
        {13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 13},
        {13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 13},
        {13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 13},
        {14, 13, 13, 13, 13, 14, 14,  7,  7,  7,  7,  7,  5, 14, 14, 13},
        {14, 14, 14, 14, 14, 14, 14,  7, 14, 14, 14, 14,  5, 14, 14, 13},
        {14, 14, 14, 14, 14, 14, 14,  7, 14, 14, 14, 14,  5, 14, 14, 13},
        {14, 13, 13, 13, 13, 14, 14,  7,  5,  5,  5,  5,  5, 14, 14, 13},
        {13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 13},
        {13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 13},
        {13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 13},
        {13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14},
        {13, 13, 13, 13, 14, 13, 14, 14, 14, 13, 14, 14, 14, 13, 13, 14},
        {13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14}
    },
    {
        /* 敌方坦克 */
        {13, 13, 13, 13,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7},
        {13, 13, 13, 13,  7, 13,  7,  7,  7, 13,  7,  7,  7, 13, 13,  7},
        {13, 13, 13, 13,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7},
        {13, 13, 13, 13, 13,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7, 13},
        {13, 13, 13, 13, 13,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7, 13},
        {13, 13, 13, 13, 13,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7, 13},
        { 7, 13, 13, 13, 13,  7,  7,  8,  8,  8,  8,  8,  7,  7,  7, 13},
        { 7,  7,  7,  7,  7,  7,  7,  8,  7,  7,  7,  7,  7,  7,  7, 13},
        { 7,  7,  7,  7,  7,  7,  7,  8,  7,  7,  7,  7,  7,  7,  7, 13},
        { 7, 13, 13, 13, 13,  7,  7,  8,  7,  7,  7,  7,  7,  7,  7, 13},
        {13, 13, 13, 13, 13,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7, 13},
        {13, 13, 13, 13, 13,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7, 13},
        {13, 13, 13, 13, 13,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7, 13},
        {13, 13, 13, 13,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7},
        {13, 13, 13, 13,  7, 13,  7,  7,  7, 13,  7,  7,  7, 13, 13,  7},
        {13, 13, 13, 13,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7}
    }
};

char g_byTankBomb[16][16] =
{
    /* 坦克爆炸图案 */
    {13, 13, 13, 13, 13, 13,  4, 13, 13, 13, 13, 13, 13, 13, 13, 13},
    {13,  4, 13, 13, 13, 13,  4, 13, 13, 13, 13, 13, 13, 13,  4, 13},
    {13, 13,  4, 13, 13, 13,  4,  4, 13, 13, 13, 13,  4,  4, 13, 13},
    {13, 13,  4,  4, 13, 13,  4,  4, 13, 13, 13,  4,  4, 13, 13, 13},
    {13, 13, 13,  4,  4, 13,  4,  4, 13,  4,  4,  4, 13, 13, 13, 13},
    {13, 13, 13, 13,  4,  4,  4,  4,  4,  4,  4, 13, 13, 13, 13, 13},
    {13, 13, 13, 13,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4},
    {13, 13, 13, 13, 13,  4,  4,  4,  4,  4,  4,  4,  4,  4, 13, 13},
    {13, 13,  4,  4,  4,  4,  4,  4,  4,  4,  4, 13, 13, 13, 13, 13},
    { 4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4, 13, 13, 13, 13},
    {13, 13, 13, 13, 13,  4,  4,  4,  4,  4,  4,  4, 13, 13, 13, 13},
    {13, 13, 13, 13,  4,  4,  4, 13,  4,  4, 13,  4,  4, 13, 13, 13},
    {13, 13, 13,  4,  4, 13, 13, 13,  4,  4, 13, 13,  4,  4, 13, 13},
    {13, 13,  4,  4, 13, 13, 13, 13,  4,  4, 13, 13, 13,  4, 13, 13},
    {13,  4, 13, 13, 13, 13, 13, 13, 13,  4, 13, 13, 13, 13,  4, 13},
    {13, 13, 13, 13, 13, 13, 13, 13, 13,  4, 13, 13, 13, 13, 13, 13}
};


CTank *g_pTank[60][60];
CTank *g_pTHead=NULL, *g_pTTail=NULL, *g_pTank1=NULL, *g_pTank2=NULL;

void DrawTank(CTank *pThis)  /* 画坦克 */
{
    int i, j, index;

    if(pThis==NULL) return;

    if(pThis->m_pOwner==&g_PPlayer1) index=0;
    else if(pThis->m_pOwner==&g_PPlayer2) index=1;
    else index=2;

    switch(pThis->m_byDirect)
    {
    case 1:
        for(i=0;i<16;i++)
        {
            for(j=0;j<16;j++)
            {
                PutPix(pThis->m_nX+i, pThis->m_nY+j, g_byTank[index][i][j]);
            }
        }
        break;
    case 2:
        for(i=0;i<16;i++)
        {
            for(j=0;j<16;j++)
            {
                PutPix(pThis->m_nX+15-i, pThis->m_nY+15-j, g_byTank[index][i][j]);
            }
        }
        break;
    case 3:
        for(i=0;i<16;i++)
        {
            for(j=0;j<16;j++)
            {
                PutPix(pThis->m_nX+j, pThis->m_nY+i, g_byTank[index][i][j]);
            }
        }
        break;
    case 4:
        for(i=0;i<16;i++)
        {
            for(j=0;j<16;j++)
            {
                PutPix(pThis->m_nX+15-j, pThis->m_nY+15-i, g_byTank[index][i][j]);
            }
        }
        break;
    }

    if(pThis->m_byProtected==1&&pThis->m_byHasBoat==0)
    {
        Rect(pThis->m_nX, pThis->m_nY, pThis->m_nX+15, pThis->m_nY+15, 2);
    }
    else if(pThis->m_byProtected==0&&pThis->m_byHasBoat==1)
    {
        Rect(pThis->m_nX, pThis->m_nY, pThis->m_nX+15, pThis->m_nY+15, 1);
    }
    else if(pThis->m_byProtected==1&&pThis->m_byHasBoat==1)
    {
        Rect(pThis->m_nX, pThis->m_nY, pThis->m_nX+15, pThis->m_nY+15, 3);
    }
    
    g_pTank[pThis->m_nX/8  ][pThis->m_nY/8  ]=pThis;
    g_pTank[pThis->m_nX/8+1][pThis->m_nY/8  ]=pThis;
    g_pTank[pThis->m_nX/8  ][pThis->m_nY/8+1]=pThis;
    g_pTank[pThis->m_nX/8+1][pThis->m_nY/8+1]=pThis;
    
    if(pThis->m_nX%8!=0)
    {
        g_pTank[pThis->m_nX/8+2][pThis->m_nY/8  ]=pThis;
        g_pTank[pThis->m_nX/8+2][pThis->m_nY/8+1]=pThis;
    }

    if(pThis->m_nY%8!=0)
    {
        g_pTank[pThis->m_nX/8  ][pThis->m_nY/8+2]=pThis;
        g_pTank[pThis->m_nX/8+1][pThis->m_nY/8+2]=pThis;
    }
    
}

void ExplodeTank(CTank *pThis)  /* 爆炸坦克 */
{

    int i, j;

    if(pThis==NULL) return;

    for(i=0;i<16;i++)
    {
        for(j=0;j<16;j++)
        {
            pThis->m_byPix[i][j]=GetPix(pThis->m_nX+i, pThis->m_nY+j);
            if(g_byTankBomb[i][j]!=0) PutPix(pThis->m_nX+i, pThis->m_nY+j, g_byTankBomb[i][j]);
        }
    }

}

void CleanTankExplode(CTank *pThis)  /* 清理爆炸图案 */
{

    int i, j;

    if(pThis==NULL) return;

    for(i=0;i<16;i++)
    {
        for(j=0;j<16;j++)
        {
            if(GetPix(pThis->m_nX+i, pThis->m_nY+j)==g_byTankBomb[i][j])
                PutPix(pThis->m_nX+i, pThis->m_nY+j, pThis->m_byPix[i][j]);
        }
    }

    pThis->m_byState=-2;
}

void ClearTank(CTank *pThis)  /* 清除坦克 */
{
    int i, j;

    if(pThis==NULL) return;

    RestoreMapUnit(pThis->m_nX/8,   pThis->m_nY/8  );
    RestoreMapUnit(pThis->m_nX/8+1, pThis->m_nY/8  );
    RestoreMapUnit(pThis->m_nX/8,   pThis->m_nY/8+1);
    RestoreMapUnit(pThis->m_nX/8+1, pThis->m_nY/8+1);

    g_pTank[pThis->m_nX/8  ][pThis->m_nY/8  ]=NULL;
    g_pTank[pThis->m_nX/8+1][pThis->m_nY/8  ]=NULL;
    g_pTank[pThis->m_nX/8  ][pThis->m_nY/8+1]=NULL;
    g_pTank[pThis->m_nX/8+1][pThis->m_nY/8+1]=NULL;

    if(pThis->m_nX%8!=0)
    {
        RestoreMapUnit(pThis->m_nX/8+2, pThis->m_nY/8  );
        RestoreMapUnit(pThis->m_nX/8+2, pThis->m_nY/8+1);
        g_pTank[pThis->m_nX/8+2][pThis->m_nY/8  ]=NULL;
        g_pTank[pThis->m_nX/8+2][pThis->m_nY/8+1]=NULL;
    }

    if(pThis->m_nY%8!=0)
    {
        RestoreMapUnit(pThis->m_nX/8,   pThis->m_nY/8+2);
        RestoreMapUnit(pThis->m_nX/8+1, pThis->m_nY/8+2);
        g_pTank[pThis->m_nX/8  ][pThis->m_nY/8+2]=NULL;
        g_pTank[pThis->m_nX/8+1][pThis->m_nY/8+2]=NULL;
    }
    
    if(g_pTheGift!=NULL) DrawGift(g_pTheGift);

}

void CreateTank(int nLX, int nLY, CPlayer *pThis)  /* 创建坦克 */
{
    CTank *pTank=(CTank *)malloc(sizeof(CTank));

    if(pThis==NULL) return;

    pTank->m_pOwner=pThis;
    pTank->m_nX=nLX*8;
    pTank->m_nY=nLY*8;
    pTank->m_nBullets=0;
    pTank->m_byHasBoat=pThis->m_byHasBoat;
    pTank->m_byLevel=pThis->m_byLevel;
    pTank->m_byState=0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -