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

📄 game.c

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

        /* 玩家2 */
        if(g_pTank2!=NULL)
        {
            if(g_byStopTank!=2)
            {
                if(GetKeyState(g_Config.m_byKeyUp2))
                {
                    if(g_pTank2->m_byDirect==1)
                    {
                        g_pTank2->m_byState=1;
                    }
                    else
                    {
                        ChangeTankDirect(g_pTank2, 1);
                    }
                }
                if(GetKeyState(g_Config.m_byKeyDown2))
                {
                    if(g_pTank2->m_byDirect==2)
                    {
                        g_pTank2->m_byState=1;
                    }
                    else
                    {
                        ChangeTankDirect(g_pTank2, 2);
                    }
                }
                if(GetKeyState(g_Config.m_byKeyLeft2))
                {
                    if(g_pTank2->m_byDirect==3)
                    {
                        g_pTank2->m_byState=1;
                    }
                    else
                    {
                        ChangeTankDirect(g_pTank2, 3);
                    }
                }
                if(GetKeyState(g_Config.m_byKeyRight2))
                {
                    if(g_pTank2->m_byDirect==4)
                    {
                        g_pTank2->m_byState=1;
                    }
                    else
                    {
                        ChangeTankDirect(g_pTank2, 4);
                    }
                }
            }
            if(GetKeyState(g_Config.m_byKeyFire2))
            {
                if(g_pTank2->m_nBullets<g_pTank2->m_byLevel+1) CreateBullet(g_pTank2);
            }
        }

        /* 创建敌方坦克 */
        if(g_PEnemy.m_nRestTanks>0)
        {
            if(
                (g_PEnemy.m_nTanks==g_PEnemy.m_nRestTanks)||
                (g_PEnemy.m_nRestTanks>15&&rand()%5==1)||
                ((g_PEnemy.m_nTanks-g_PEnemy.m_nRestTanks<8)&&rand()%20==1)
              )
            {
                int n=rand()%3*29;

                if(g_pTank[0][n]==NULL&&g_pTank[0][n+1]==NULL&&g_pTank[1][n]==NULL&&g_pTank[1][n+1]==NULL)
                {
                    CreateTank(n, 0, &g_PEnemy);
                }
            }
        }

        /* 敌方坦克动作 */
        pt=g_pTHead;
        while(pt!=NULL)
        {
            if(pt->m_pOwner==&g_PEnemy)
            {
                if(g_byStopTank!=1)
                {
                    switch(pt->m_byState)
                    {
                    case 0:
                        pt->m_byState=1;
                    case 1:
                        if((rand()%30==1))
                        {
                            ChangeTankDirect(pt, rand()%4+1);
                        }
                        if(pt->m_nBullets<pt->m_byLevel&&rand()%5==0) CreateBullet(pt);
                        break;
                    case 2:
                        ClearTank(pt);
                        ChangeTankDirect(pt, rand()%4+1);
                        if(pt->m_nBullets<pt->m_byLevel) CreateBullet(pt);
                        break;
                    }
                }
            }

            if(pt->m_byState==-1)
            {
                CleanTankExplode(pt);
            }

            pt=pt->m_pNext;
        }

        pb=g_pBHead;
        while(pb!=NULL)
        {
            if(pb->m_byState==-1)
            {
                CleanBulletExplode(pb);
            }
            pb=pb->m_pNext;
        }

        /* 搞个小小的彩蛋:同时按 T、A、N、K 四个键,让屏幕上坦克全部爆炸 */
        if(GetKeyState(KEY_T)&&GetKeyState(KEY_A)&&GetKeyState(KEY_N)&&GetKeyState(KEY_K))
        {
            pt=g_pTHead;
            while(pt!=NULL)
            {
                if(pt->m_pOwner->m_bySide==g_PEnemy.m_bySide&&pt->m_byState>=0)
                {
                    KillTank(pt);
                }
                pt=pt->m_pNext;
            }
        }

        UpdateMessage();
    }


    /* 每周期更新画面显示 */
    pt=g_pTHead;
    while(pt!=NULL)
    {
        pt=MoveTank(pt);
    }
    pb=g_pBHead;
    while(pb!=NULL)
    {
        pb=MoveBullet(pb);
    }
    GiftTimer();

    return CheckWin();
}

void QuitGame()  /* 退出游戏 */
{
    FILE *p;

    p=fopen("tank.cfg", "wb");
    if(p!=NULL)
    {
        fwrite(&g_Config, sizeof(g_Config), 1, p);  /* 保存按键设置 */
        fclose(p);
    }
    if(IsPlaying())
    {
        CloseSound();  /* 关闭喇叭,恢复时钟中断 */
    }

    ShutDownKeyboard();  /* 恢复键盘中断 */
    CloseCharSet();  /* 关闭字库 */
    CloseGraph();  /* 关闭图形模式 */
    exit(0);
}

void Pause()  /* 暂停 */
{
    OutTextXY(491, 365, 626, 380, "按P继续", YELLOW, BLACK);
    while(!(GetKeyState(KEY_P)));
    Box(491, 365, 626, 380, BLACK);
}

void SetKeys()  /* 按键设置 */
{
    Box(523, 193, 626, 272, BLACK);
    OutTextXY(491, 349, 626, 364, "请按提示按键", YELLOW, BLACK);
    OutTextXY(491, 365, 626, 380, "玩家一的上方向键", YELLOW, BLACK);
    g_Config.m_byKeyUp1=BIOSKey();
    OutTextXY(523, 193, 626, 208, GetKeyName(g_Config.m_byKeyUp1), WHITE, 13);
    OutTextXY(491, 365, 626, 380, "玩家一的下方向键", YELLOW, BLACK);
    g_Config.m_byKeyDown1=BIOSKey();
    OutTextXY(523, 209, 626, 224, GetKeyName(g_Config.m_byKeyDown1), WHITE, 13);
    OutTextXY(491, 365, 626, 380, "玩家一的左方向键", YELLOW, BLACK);
    g_Config.m_byKeyLeft1=BIOSKey();
    OutTextXY(523, 225, 626, 240, GetKeyName(g_Config.m_byKeyLeft1), WHITE, 13);
    OutTextXY(491, 365, 626, 380, "玩家一的右方向键", YELLOW, BLACK);
    g_Config.m_byKeyRight1=BIOSKey();
    OutTextXY(523, 241, 626, 256, GetKeyName(g_Config.m_byKeyRight1), WHITE, 13);
    OutTextXY(491, 365, 626, 380, "玩家一的发子弹键", YELLOW, BLACK);
    g_Config.m_byKeyFire1=BIOSKey();
    OutTextXY(523, 257, 626, 272, GetKeyName(g_Config.m_byKeyFire1), WHITE, 13);
    OutTextXY(491, 365, 626, 380, "玩家二的上方向键", YELLOW, BLACK);
    g_Config.m_byKeyUp2=BIOSKey();
    OutTextXY(579, 193, 626, 208, GetKeyName(g_Config.m_byKeyUp2), WHITE, 13);
    OutTextXY(491, 365, 626, 380, "玩家二的下方向键", YELLOW, BLACK);
    g_Config.m_byKeyDown2=BIOSKey();
    OutTextXY(579, 209, 626, 224, GetKeyName(g_Config.m_byKeyDown2), WHITE, 13);
    OutTextXY(491, 365, 626, 380, "玩家二的左方向键", YELLOW, BLACK);
    g_Config.m_byKeyLeft2=BIOSKey();
    OutTextXY(579, 225, 626, 240, GetKeyName(g_Config.m_byKeyLeft2), WHITE, 13);
    OutTextXY(491, 365, 626, 380, "玩家二的右方向键", YELLOW, BLACK);
    g_Config.m_byKeyRight2=BIOSKey();
    OutTextXY(579, 241, 626, 256, GetKeyName(g_Config.m_byKeyRight2), WHITE, 13);
    OutTextXY(491, 365, 626, 380, "玩家二的发子弹键", YELLOW, BLACK);
    g_Config.m_byKeyFire2=BIOSKey();
    OutTextXY(579, 257, 626, 272, GetKeyName(g_Config.m_byKeyFire2), WHITE, 13);
    Box(491, 349, 626, 344, BLACK);
    Box(491, 365, 626, 380, BLACK);
    OutTextXY(491, 349, 626, 364, "设置结束", YELLOW, BLACK);
    OutTextXY(491, 365, 626, 380, "按Enter继续游戏", YELLOW, BLACK);

    while(!(GetKeyState(KEY_ENTER)));
    Box(491, 349, 626, 364, BLACK);
    Box(491, 365, 626, 380, BLACK);
}

void PauseGame(void (*pfnToDo)())  /* 暂停程序的框架 */
{
    ClearKeyState();

    OutTextXY(491, 349, 626, 364, "暂停中…", YELLOW, BLACK);
    if(pfnToDo!=NULL) (*pfnToDo)();
    Box(491, 349, 626, 364, BLACK);
    ClearKeyState();
    OutTextXY(491, 349, 626, 364, "即将继续", YELLOW, BLACK);
    while((inportb(0x03da)&0x0008==0));
    while((inportb(0x03da)&0x0008==1));
    Box(491, 349, 626, 364, BLACK);
}

int GameCtrl(int count)  /* 游戏控制 */
{
    /* 程序控制 */
    if(GetKeyState(KEY_ESC))
    {
        OutTextXY(491, 349, 626, 364, "确定要退出吗?", YELLOW, BLACK);
        OutTextXY(491, 365, 626, 380, "按Y退出", YELLOW, BLACK);
        if(BIOSKey()==KEY_Y) QuitGame();
        Box(491, 349, 626, 364, BLACK);
        Box(491, 365, 626, 380, BLACK);
    }
 
    if(GetKeyState(KEY_P)||GetKeyState(KEY_PAUSE))
    {
        PauseGame(Pause);
    }

    if(GetKeyState(KEY_F8))
    {
        PauseGame(SetKeys);
    }

    if(GetKeyState(KEY_F6))
    {
        ClearKey(KEY_F6);
        PausePlay();
    }

    if(!IsPlaying())
    {
        PlayMusic();
    }

    switch(GameAction(count))
    {
    case -1:
        OutTextXY(491, 349, 626, 364, "失败", YELLOW, BLACK);
        break;
    case 1:
        if(g_cntTiming==-1)
        {
            OutTextXY(491, 349, 626, 364, "过关", YELLOW, BLACK);
            g_cntTiming=400;  /* 为了让玩家能够在结束后捡点东西之类的 */
        }
        else
        {
            if((g_cntTiming--)==0)
            {
                Box(491, 349, 626, 364, BLACK);

                if(StartGame(++g_nLv))
                {
                    return -1;  /* 继续游戏 */
                }
                else
                {
                    OutTextXY(491, 349, 626, 364, "胜利", YELLOW, BLACK);
                    break;
                }
            }
        }
    case 0:
        return count;
    }
    
    OutTextXY(491, 365, 626, 380, "按 ESC 退出", YELLOW, BLACK);
    while(1)
    {
        if(GetKeyState(KEY_ESC))
        {
            QuitGame();
        }
    }
}

int main()
{
    int flag=1;
    int count=0;

    Init();

    if(!StartGame(1))
    {
        OutTextXY(491, 349, 626, 364, "找不到 01.map", YELLOW, BLACK);
        OutTextXY(491, 365, 626, 380, "按 ESC 退出", YELLOW, BLACK);
        while(!GetKeyState(KEY_ESC));
        QuitGame();
    }

    while((inportb(0x03da)&0x0008==0));
    while((inportb(0x03da)&0x0008==1));
    while(1)
    {
        if((inportb(0x03da)&0x0008)==0&&flag!=0)  /* 屏幕回扫结束 */
        {
            count=GameCtrl(count);
            count++;
            if(count>=80) count=0;
            flag=0;
        }
        else if((inportb(0x03da)&0x0008)!=0&&flag==0)
        {
            flag=1;
        }
    }
}

⌨️ 快捷键说明

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