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

📄 game.c

📁 最新版IAR FOR ARM(EWARM)5.11中的代码例子
💻 C
📖 第 1 页 / 共 5 页
字号:
                    // Set the position of the bullet at the bottom of the
                    // player.
                    //
                    g_pusBulletX[ulLoop] = g_usPlayerX + 4;
                    g_pusBulletY[ulLoop] = g_usPlayerY + 8;

                    //
                    // This bullet has been positioned.
                    //
                    break;
                }

                //
                // The bullet is being fired left.
                //
                case 4:
                {
                    //
                    // Set the position of the bullet at the left of the
                    // player.
                    //
                    g_pusBulletX[ulLoop] = g_usPlayerX;
                    g_pusBulletY[ulLoop] = g_usPlayerY + 6;

                    //
                    // This bullet has been positioned.
                    //
                    break;
                }

                //
                // The bullet is being fired right.
                //
                case 8:
                {
                    //
                    // Set the position of the bullet at the right of the
                    // player.
                    //
                    g_pusBulletX[ulLoop] = g_usPlayerX + 8;
                    g_pusBulletY[ulLoop] = g_usPlayerY + 6;

                    //
                    // This bullet has been positioned.
                    //
                    break;
                }
            }
        }
    }

    //
    // Loop through the four bullets.
    //
    for(ulLoop = 0; ulLoop < 4; ulLoop++)
    {
        //
        // Skip this bullet if it has not been fired.
        //
        if((g_pusBulletX[ulLoop] == 0) && (g_pusBulletY[ulLoop] == 0))
        {
            continue;
        }

        //
        // Determine the direction this bullet was fired.
        //
        switch(g_pucBulletDir[ulLoop])
        {
            //
            // This bullet was fired up.
            //
            case 1:
            {
                //
                // Move the bullet up.
                //
                g_pusBulletY[ulLoop] -= 4;

                //
                // See if the bullet has struck a wall.
                //
                if((g_pusBulletY[ulLoop] < 1128) &&
                   (g_ppcMaze[g_pusBulletY[ulLoop] / 12]
                             [g_pusBulletX[ulLoop] / 12] != 0))
                {
                    //
                    // Add an explosion at the point the bullet struck the
                    // wall.
                    //
                    AddExplosion(g_pusBulletX[ulLoop] - 6,
                                 ((g_pusBulletY[ulLoop] / 12) * 12) + 6);

                    //
                    // Play the sound of the wall being struck.
                    //
                    AudioPlaySound(g_pusWallEffect,
                                   sizeof(g_pusWallEffect) / 2);

                    //
                    // The bullet is now unfired.
                    //
                    g_pusBulletX[ulLoop] = 0;
                    g_pusBulletY[ulLoop] = 0;
                }

                //
                // Done moving this bullet.
                //
                break;
            }

            //
            // This bullet was fired down.
            //
            case 2:
            {
                //
                // Move the bullet down.
                //
                g_pusBulletY[ulLoop] += 4;

                //
                // See if the bullet has struck a wall.
                //
                if((g_pusBulletY[ulLoop] < 1125) &&
                   (g_ppcMaze[(g_pusBulletY[ulLoop] + 3) / 12]
                             [g_pusBulletX[ulLoop] / 12] != 0))
                {
                    //
                    // Add an explosion at the point the bullet struck the
                    // wall.
                    //
                    AddExplosion(g_pusBulletX[ulLoop] - 6,
                                 (((g_pusBulletY[ulLoop] - 4) / 12) * 12) + 6);

                    //
                    // Play the sound of the wall being struck.
                    //
                    AudioPlaySound(g_pusWallEffect,
                                   sizeof(g_pusWallEffect) / 2);

                    //
                    // The bullet is now unfired.
                    //
                    g_pusBulletX[ulLoop] = 0;
                    g_pusBulletY[ulLoop] = 0;
                }

                //
                // Done moving this bullet.
                //
                break;
            }

            //
            // This bullet was fired left.
            //
            case 4:
            {
                //
                // Move the bullet left.
                //
                g_pusBulletX[ulLoop] -= 4;

                //
                // See if the bullet has struck a wall.
                //
                if((g_pusBulletX[ulLoop] < 1524) &&
                   (g_ppcMaze[g_pusBulletY[ulLoop] / 12]
                             [g_pusBulletX[ulLoop] / 12] != 0))
                {
                    //
                    // Add an explosion at the point the bullet struck the
                    // wall.
                    //
                    AddExplosion(((g_pusBulletX[ulLoop] / 12) * 12) + 6,
                                 g_pusBulletY[ulLoop] - 6);

                    //
                    // Play the sound of the wall being struck.
                    //
                    AudioPlaySound(g_pusWallEffect,
                                   sizeof(g_pusWallEffect) / 2);

                    //
                    // The bullet is now unfired.
                    //
                    g_pusBulletX[ulLoop] = 0;
                    g_pusBulletY[ulLoop] = 0;
                }

                //
                // Done moving this bullet.
                //
                break;
            }

            //
            // This bullet was fired right.
            //
            case 8:
            {
                //
                // Move the bullet right.
                //
                g_pusBulletX[ulLoop] += 4;

                //
                // See if the bullet has struck a wall.
                //
                if((g_pusBulletX[ulLoop] < 1521) &&
                   (g_ppcMaze[g_pusBulletY[ulLoop] / 12]
                             [(g_pusBulletX[ulLoop] + 3) / 12] != 0))
                {
                    //
                    // Add an explosion at the point the bullet struck the
                    // wall.
                    //
                    AddExplosion((((g_pusBulletX[ulLoop] - 4) / 12) * 12) + 6,
                                 g_pusBulletY[ulLoop] - 6);

                    //
                    // Play the sound of the wall being struck.
                    //
                    AudioPlaySound(g_pusWallEffect,
                                   sizeof(g_pusWallEffect) / 2);

                    //
                    // The bullet is now unfired.
                    //
                    g_pusBulletX[ulLoop] = 0;
                    g_pusBulletY[ulLoop] = 0;
                }

                //
                // Done moving this bullet.
                //
                break;
            }
        }

        //
        // If the bullet is no longer within the maze (i.e. it was fired
        // through the maze exit) then remove it from the display and make it
        // be unfired.
        //
        if((g_pusBulletX[ulLoop] > 1524) || (g_pusBulletY[ulLoop] > 1128))
        {
            g_pusBulletX[ulLoop] = 0;
            g_pusBulletY[ulLoop] = 0;
        }

        //
        // See if this bullet needs to be drawn.
        //
        if((g_pusBulletX[ulLoop] != 0) || (g_pusBulletY[ulLoop] != 0))
        {
            //
            // Until a collision is found, assume there is no collision.
            //
            bHit = false;

            //
            // Get the position on the display where the bullet should be
            // drawn.
            //
            lX = (long)g_pusBulletX[ulLoop] - (long)g_usPlayerX + (64 - 6);
            lY = (long)g_pusBulletY[ulLoop] - (long)g_usPlayerY + (47 - 6);

            //
            // See if the bullet is completely off the display.
            //
            if((lX < -3) || (lX > 127) || (lY < -3) || (lY > 93))
            {
                //
                // Make this bullet be unfired since it is no longer on the
                // display.
                //
                g_pusBulletX[ulLoop] = 0;
                g_pusBulletY[ulLoop] = 0;
            }

            //
            // Otherwise, see if the bullet is fired vertically.
            //
            else if((g_pucBulletDir[ulLoop] & 0x03) != 0)
            {
                //
                // Loop through the four rows of the bullet.
                //
                for(ulIdx = (lY < 0) ? -lY : 0, lY = (lY < 0) ? 0 : lY;
                    (ulIdx < 4) && (lY < 94); ulIdx++, lY++)
                {
                    //
                    // See if this pixel is already set.
                    //
                    if((g_pucFrame[(lY * 64) + (lX / 2)] & 0x0f) != 0)
                    {
                        //
                        // Indicate that a collision has occurred.
                        //
                        bHit = true;
                    }

                    //
                    // Draw this pixel on the display.
                    //
                    g_pucFrame[(lY * 64) + (lX / 2)] |= 0x0f;
                }
            }

            //
            // Otherwise, the bullet is fired horizontally.
            //
            else
            {
                //
                // Loop through the four columns of the bullet.
                //
                for(ulIdx = (lX < 0) ? -lX : 0, lX = (lX < 0) ? 0 : lX;
                    (ulIdx < 2) && (lX < 128); ulIdx++, lX += 2)
                {
                    //
                    // See if either of these pixels is already set.
                    //
                    if(g_pucFrame[(lY * 64) + (lX / 2)] != 0)
                    {
                        //
                        // Indicate that a collision has occurred.
                        //
                        bHit = true;
                    }

                    //
                    // Draw these pixel on the display.
                    //
                    g_pucFrame[(lY * 64) + (lX / 2)] |= 0xff;
                }
            }

            //
            // See if a collision has occurred.
            //
            if(bHit)
            {
                //
                // Play the sound effect for a monster dying.
                //
                AudioPlaySound(g_pusMonsterEffect,
                               sizeof(g_pusMonsterEffect) / 2);

                //
                // Add one hundred points to the score.
                //
                g_ulScore += 100;

                //
                // See if this bullet was travelling vertically or horizontally
                // and determine the adjustment made to the top and left edge
                // of the monster position to account for the fact that the
                // bullet is not a single point.
                //
                if((g_pucBulletDir[ulLoop] & 0x03) != 0)
                {
                    //
                    // The bullet was travelling vertically.  Set the monster
                    // position adjust accordingly.
                    //
                    lX = 0;
                    lY = -3;
                }
                else
                {
                    //
                    // The bullet was travelling horizontally.  Set the monster
                    // position adjust accordingly.
                    //
                    lX = -3;
                    lY = 0;
                }

                //
                // Loop over all the monsters.
                //
                for(ulIdx = 0; ulIdx < 100; ulIdx++)
                {
                    //
                    // Skip this monster if it is already dead.
                    //
                    if((g_pusMonsterX[ulIdx] == 0) &&
                       (g_pusMonsterY[ulIdx] == 0))
                    {
                        continue;
                    }

                    //
                    // See if the bullet hit this monster.
                    //
                    if(((g_pusMonsterX[ulIdx] + lX) < g_pusBulletX[ulLoop]) &&
                       ((g_pusMonsterX[ulIdx] + 12) > g_pusBulletX[ulLoop]) &&
                       ((g_pusMonsterY[ulIdx] + lY) < g_pusBulletY[ulLoop]) &&
                       ((g_pusMonsterY[ulIdx] + 12) > g_pusBulletY[ulLoop]))
                    {
                        //
                        // Add an explosion at the position of the monster.
                        //
                        AddExplosion(g_pusMonsterX[ulIdx],
                                     g_pusMonsterY[ulIdx]);

                        //
                        // Indicate that this monster is now dead.
                        //
                        g_pusMonsterX[ulIdx] = 0;
                        g_pusMonsterY[ulIdx] = 0;

                        //
                        // Stop looking for monsters.
                        //
                        break;
                    }

⌨️ 快捷键说明

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