使用XNA和C#在X轴和Y轴上进行2D平行背景

时间:2013-04-15 02:37:59

标签: c# background xna 2d parallax

我正在使用XNA进行太空射击游戏,并且已经按照多个教程创建了一个视差背景。到目前为止,我可以让它沿着一个轴(X或Y),但不能同时进行。我有一个跟随玩家的相机类,玩家移动(而不是“世界”),因为我认为移动玩家比移动玩家周围的其他所有内容更容易。

到目前为止,背景跟不上播放器,同时也无法理解两个轴。我想过一个瓦片引擎,但这不会让我视差不同的层次吗?

有人可以帮我理解我需要做什么,或者推荐一个可以同时做两个轴的教程吗?这次我似乎无法自己找到答案。

以下是我的后台课程的代码:

class Background
{
    // Textures to hold the two background images
    Texture2D spaceBackground, starsParallax;

    int backgroundWidth = 2048;
    int backgroundHeight = 2048;

    int parallaxWidth = 2048;
    int parallaxHeight = 2048;

    int backgroundWidthOffset;
    int backgroundHeightOffset;

    int parallaxWidthOffset;
    int parallaxHeightOffset;

    public int BackgroundWidthOffset
    {
        get { return backgroundWidthOffset; }
        set
        {
            backgroundWidthOffset = value;
            if (backgroundWidthOffset < 0)
            {
                backgroundWidthOffset += backgroundWidth;
            }
            if (backgroundWidthOffset > backgroundWidth)
            {
                backgroundWidthOffset -= backgroundWidth;
            }
        }
    }

    public int BackgroundHeightOffset
    {
        get { return backgroundHeightOffset; }
        set
        {
            backgroundHeightOffset = value;
            if (backgroundHeightOffset < 0)
            {
                backgroundHeightOffset += backgroundHeight;
            }
            if (backgroundHeightOffset > backgroundHeight)
            {
                backgroundHeightOffset -= backgroundHeight;
            }
        }
    }

    public int ParallaxWidthOffset
    {
        get { return parallaxWidthOffset; }
        set
        {
            parallaxWidthOffset = value;
            if (parallaxWidthOffset < 0)
            {
                parallaxWidthOffset += parallaxWidth;
            }
            if (parallaxWidthOffset > parallaxWidth)
            {
                parallaxWidthOffset -= parallaxWidth;
            }
        }
    }

    public int ParallaxHeightOffset
    {
        get { return parallaxHeightOffset; }
        set
        {
            parallaxHeightOffset = value;
            if (parallaxHeightOffset < 0)
            {
                parallaxHeightOffset += parallaxHeight;
            }
            if (parallaxHeightOffset > parallaxHeight)
            {
                parallaxHeightOffset -= parallaxHeight;
            }
        }
    }

    // Constructor when passed a Content Manager and two strings
    public Background(ContentManager content,
                      string sBackground, string sParallax)
    {
        spaceBackground = content.Load<Texture2D>(sBackground);
        backgroundWidth = spaceBackground.Width;
        backgroundHeight = spaceBackground.Height;

        starsParallax = content.Load<Texture2D>(sParallax);
        parallaxWidth = starsParallax.Width;
        parallaxHeight = starsParallax.Height;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        // Draw the background panel, offset by the player's location
        spriteBatch.Draw(
            spaceBackground,
            new Rectangle(-1 * backgroundWidthOffset,
                          -1 * backgroundHeightOffset, 
                          backgroundWidth,
                          backgroundHeight),
            Color.White);

        // If the right edge of the background panel will end 
        // within the bounds of the display, draw a second copy 
        // of the background at that location.
        if (backgroundWidthOffset > backgroundWidth)
        {
            spriteBatch.Draw(
                spaceBackground,
                new Rectangle(
                  (-1 * backgroundWidthOffset) + backgroundWidth, 0,
                  backgroundWidth, backgroundHeight),
                Color.White);
        }
        else //(backgroundHeightOffset > backgroundHeight - viewportHeight)
        {
            spriteBatch.Draw(
                spaceBackground,
                new Rectangle(
                  0, (-1 * backgroundHeightOffset) + backgroundHeight,
                  backgroundHeight, backgroundHeight),
                Color.White);
        }

        // Draw the parallax star field
        spriteBatch.Draw(
            starsParallax,
            new Rectangle(-1 * parallaxWidthOffset,
                            0, parallaxWidth,
                            parallaxHeight),
            Color.SlateGray);
        // if the player is past the point where the star 
        // field will end on the active screen we need 
        // to draw a second copy of it to cover the 
        // remaining screen area.
        if (parallaxWidthOffset > parallaxWidth)
        {
            spriteBatch.Draw(
                starsParallax,
                new Rectangle(
                    (-1 * parallaxWidthOffset) + parallaxWidth,
                    0,
                    parallaxWidth,
                    parallaxHeight),
                Color.White);
        }
    }
}

然后主游戏与玩家一起移动背景。

background.BackgroundWidthOffset -= 2;
background.ParallaxWidthOffset -= 1;

从视觉上看,背景有点跳跃,似乎随机跳过或重叠背景图块。

2 个答案:

答案 0 :(得分:1)

过去我使用过这种方法效果很好:

http://www.david-gouveia.com/scrolling-textures-with-zoom-and-rotation/

它使用着色器来完成效果,从而实现快速实现。

答案 1 :(得分:0)

有一个完整的例子here

相关问题