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

📄 game.c

📁 最新版IAR FOR ARM(EWARM)5.11中的代码例子
💻 C
📖 第 1 页 / 共 5 页
字号:
                }

                //
                // This bullet is no longer active.
                //
                g_pusBulletX[ulLoop] = 0;
                g_pusBulletY[ulLoop] = 0;
            }
        }
    }
}

//*****************************************************************************
//
// Draws the player in the middle of the display, handling the animation of the
// player based on its motion and direction.
//
//*****************************************************************************
static tBoolean
DrawPlayer(unsigned char ucDirection)
{
    unsigned long ulLoop, ulIdx, ulNum;
    const unsigned char *pucAnimation;
    unsigned char ucData1, ucData2;
    tBoolean bBoom;

    //
    // Assume no collisions until one is found.
    //
    bBoom = false;

    //
    // See if the player is moving in the same direction as the previous time.
    //
    if(ucDirection != (g_ucDirection & 0x0f))
    {
        //
        // The player is not moving the same as before, so see if the player is
        // moving or stopped.
        //
        if(ucDirection)
        {
            //
            // The player is moving, so save the direction of motion.
            //
            g_ucDirection = ucDirection;
        }
        else
        {
            //
            // The player is stopped, so save the previous direction of motion
            // (which determines the direction that the player faces when
            // stopped).
            //
            g_ucDirection <<= 4;
        }

        //
        // Reset the animation count to zero.
        //
        g_ucCount = 0;
    }
    else
    {
        //
        // The player is moving in the same direction as before, so increment
        // the animation count.
        //
        g_ucCount++;
    }

    //
    // Determine the animation to use based on the player's direction of
    // motion.
    //
    switch(g_ucDirection)
    {
        //
        // The player is moving up.
        //
        case 0x01:
        {
            //
            // Use the animation for the player walking up.
            //
            pucAnimation = g_pucPlayerWalkingUp;
            ulNum = sizeof(g_pucPlayerWalkingUp);

            //
            // Done handling this animation.
            //
            break;
        }

        //
        // The player is moving down.
        //
        case 0x02:
        {
            //
            // Use the animation for the player walking down.
            //
            pucAnimation = g_pucPlayerWalkingDown;
            ulNum = sizeof(g_pucPlayerWalkingDown);

            //
            // Done handling this animation.
            //
            break;
        }

        //
        // The player is moving left.
        //
        case 0x04:
        {
            //
            // Use the animation for the player walking left.
            //
            pucAnimation = g_pucPlayerWalkingLeft;
            ulNum = sizeof(g_pucPlayerWalkingLeft);

            //
            // Done handling this animation.
            //
            break;
        }

        //
        // The player is moving right.
        //
        case 0x08:
        {
            //
            // Use the animation for the player walking right.
            //
            pucAnimation = g_pucPlayerWalkingRight;
            ulNum = sizeof(g_pucPlayerWalkingRight);

            //
            // Done handling this animation.
            //
            break;
        }

        //
        // The player is dying.
        //
        case 0x0f:
        {
            //
            // Use the animation sequence for the player dying.
            //
            pucAnimation = g_pucPlayerDying;
            ulNum = sizeof(g_pucPlayerDying);

            //
            // Done handling this animation.
            //
            break;
        }

        //
        // The player is standing still but previously was moving up.
        //
        case 0x10:
        {
            //
            // Use the animation for the player standing while facing up.
            //
            pucAnimation = g_pucPlayerStandingUp;
            ulNum = sizeof(g_pucPlayerStandingUp);

            //
            // Done handling this animation.
            //
            break;
        }

        //
        // The player is standing still but previously was moving down.
        //
        default:
        case 0x20:
        {
            //
            // Use the animation for the player standing while facing down.
            //
            pucAnimation = g_pucPlayerStandingDown;
            ulNum = sizeof(g_pucPlayerStandingDown);

            //
            // Done handling this animation.
            //
            break;
        }

        //
        // The player is standing still but previously was moving left.
        //
        case 0x40:
        {
            //
            // Use the animation for the player standing while facing left.
            //
            pucAnimation = g_pucPlayerStandingLeft;
            ulNum = sizeof(g_pucPlayerStandingLeft);

            //
            // Done handling this animation.
            //
            break;
        }

        //
        // The player is standing still but previously was moving right.
        //
        case 0x80:
        {
            //
            // Use the animation for the player standing while facing right.
            //
            pucAnimation = g_pucPlayerStandingRight;
            ulNum = sizeof(g_pucPlayerStandingRight);

            //
            // Done handling this animation.
            //
            break;
        }
    }

    //
    // Find the index into the animation sequence based on the current
    // animation count.
    //
    for(ulLoop = 0; ulLoop < ulNum; ulLoop += 2)
    {
        if(pucAnimation[ulLoop] > g_ucCount)
        {
            break;
        }
    }

    //
    // See if the animation sequence has been completed.
    //
    if(ulLoop == ulNum)
    {
        //
        // Reset the animation count.
        //
        g_ucCount = 0;

        //
        // Use the first image in the animation sequence.
        //
        ulIdx = pucAnimation[1];
    }
    else
    {
        //
        // Otherwise, use the most recent image in the animation sequence.
        //
        ulIdx = pucAnimation[ulLoop - 1];
    }

    //
    // Loop over the bytes of the sprite for this sequence in the animation.
    //
    for(ulLoop = 0; ulLoop < 72; ulLoop++)
    {
        //
        // Copy the sprite data into the local frame buffer and check for a
        // collision.
        //
        ucData1 = g_pucFrame[(((ulLoop / 6) + 41) * 64) + (ulLoop % 6) + 29];
        ucData2 = g_ppucSprites[ulIdx][ulLoop];
        if((ucData2 & 0xf0) != 0)
        {
            if((ucData1 & 0xf0) != 0)
            {
                bBoom = true;
            }
            ucData1 &= 0x0f;
        }
        if((ucData2 & 0x0f) != 0)
        {
            if((ucData1 & 0x0f) != 0)
            {
                bBoom = true;
            }
            ucData1 &= 0xf0;
        }
        g_pucFrame[(((ulLoop / 6) + 41) * 64) + (ulLoop % 6) + 29] =
            ucData1 | ucData2;
    }

    //
    // Return an indication of the collision detection.
    //
    return(bBoom);
}

//*****************************************************************************
//
// Draws the active explosions onto the display.
//
//*****************************************************************************
static void
DrawExplosions(void)
{
    long lX, lY, lExplosionX, lExplosionY;
    unsigned char ucData1, ucData2;
    unsigned long ulLoop, ulIdx;

    //
    // Loop through all the explosions.
    //
    for(ulLoop = 0; ulLoop < 4; ulLoop++)
    {
        //
        // Skip this explosion if it is not active.
        //
        if((g_pusExplosionX[ulLoop] == 0) && (g_pusExplosionY[ulLoop] == 0))
        {
            continue;
        }

        //
        // Increment the explosion animation count.
        //
        g_pucExplosionCount[ulLoop]++;

        //
        // Find the index into the animation sequence based on the current
        // animation count.
        //
        for(ulIdx = 0; ulIdx < sizeof(g_pucExplosion); ulIdx += 2)
        {
            if(g_pucExplosion[ulIdx] > g_pucExplosionCount[ulLoop])
            {
                break;
            }
        }

        //
        // See if the animation sequence has been completed.
        //
        if(ulIdx == sizeof(g_pucExplosion))
        {
            //
            // This explosion is done, so disable it.
            //
            g_pusExplosionX[ulLoop] = 0;
            g_pusExplosionY[ulLoop] = 0;

            //
            // Go to the next explosion.
            //
            continue;
        }
        else
        {
            //
            // Otherwise, use the most recent image in the animation sequence.
            //
            ulIdx = g_pucExplosion[ulIdx - 1];
        }

        //
        // Find the position of this explosion on the display.
        //
        lExplosionX = g_pusExplosionX[ulLoop] - (g_usPlayerX - (64 - 6));
        lExplosionY = g_pusExplosionY[ulLoop] - (g_usPlayerY - (47 - 6));

        //
        // Loop through the scan lines of this explosion.
        //
        for(lY = (lExplosionY < 0) ? -lExplosionY : 0;
            (lY < 12) && ((lExplosionY + lY) < 94); lY++)
        {
            //
            // Loop through the columns of this explosion.
            //
            for(lX = (lExplosionX < 0) ? -lExplosionX : 0;
                (lX < 12) && ((lExplosionX + lX) < 128); lX++)
            {
                //
                // Copy the sprite data into the local frame buffer.
                //
                ucData1 = g_pucFrame[((lExplosionY + lY) * 64) +
                                     ((lExplosionX + lX) / 2)];
                ucData2 = g_ppucSprites[ulIdx][(lY * 6) + (lX / 2)];
                if((ucData2 & 0xf0) != 0x00)
                {
                    ucData1 &= 0x0f;
                }
                if((ucData2 & 0x0f) != 0x00)
                {
                    ucData1 &= 0xf0;
                }
                g_pucFrame[((lExplosionY + lY) * 64) +
                           ((lExplosionX + lX) / 2)] = ucData1 | ucData2;
            }
        }
    }
}

//*****************************************************************************
//
// Draws a single number to the local frame buffer, with an optional trailing
// dot.  The mask specifies the minimum number of digits to draw (i.e. a mask
// of 100 will always draw digits for the hundreds, tens, and ones digit even
// if not required).
//
//*****************************************************************************
static unsigned long
DrawNumb

⌨️ 快捷键说明

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