gpt4 book ai didi

c# - 使用 XNA 和 C# 在 X 轴和 Y 轴上创建二维视差背景

转载 作者:太空宇宙 更新时间:2023-11-03 13:43:50 24 4
gpt4 key购买 nike

我正在使用 XNA 开发一款太空射击游戏,并遵循了多个教程来创建视差背景。到目前为止,我可以让它沿一个轴移动,X 轴或 Y 轴,但不能同时沿两个轴移动。我有一个跟随玩家的相机类,玩家移动它(而不是“世界”),因为我认为只移动玩家而不是移动玩家周围的所有其他东西会更容易。

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

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

这是我的背景类的代码:

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;

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

最佳答案

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

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

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

关于c# - 使用 XNA 和 C# 在 X 轴和 Y 轴上创建二维视差背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16006874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com