C# 我需要在XNA中修复这个瓷砖错误

C# 我需要在XNA中修复这个瓷砖错误,c#,math,xna,game-physics,tiles,C#,Math,Xna,Game Physics,Tiles,我目前正在制作一款太空射击游戏。在这个游戏中,我想要一个永远循环的星星背景 为了创建这个,我制作了一个基于瓷砖的系统,它只为每个纹理创建一个矩形。我已经能够使所说的纹理有点循环,但当他们这样做时,我得到了一个非常奇怪的错误 正如你所看到的,我的瓷砖有时会在彼此之间产生一个小的间隙,我不知道为什么 问题可能存在于我的Background.cs文件中,但我不确定在哪里 另外,我想为“Background.cs”取一个更好的名字。你认为像“Tile.cs”这样的东西更好吗 Background.c

我目前正在制作一款太空射击游戏。在这个游戏中,我想要一个永远循环的星星背景

为了创建这个,我制作了一个基于瓷砖的系统,它只为每个纹理创建一个矩形。我已经能够使所说的纹理有点循环,但当他们这样做时,我得到了一个非常奇怪的错误

正如你所看到的,我的瓷砖有时会在彼此之间产生一个小的间隙,我不知道为什么

问题可能存在于我的Background.cs文件中,但我不确定在哪里

另外,我想为“Background.cs”取一个更好的名字。你认为像“Tile.cs”这样的东西更好吗


Background.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SpaceShooter
{
    class Background
{
    public Texture2D texture;
    public List<Rectangle> tiles = new List<Rectangle>();
    public Color color;
    public int tileSize;
    public int speed;
    GraphicsDevice graphicsDevice;

    public Background(Texture2D texture, GraphicsDevice graphicsDevice, int tileSize)
    {
        this.texture = texture;
        this.graphicsDevice = graphicsDevice;
        this.tileSize = tileSize;
        speed = 3;
        this.color = Color.White;
        CalculateTiles();
    }

    public void Update()
    {
        for (int i = 0; i < tiles.Count(); i++)
        {
            tiles[i] = new Rectangle(tiles[i].X, tiles[i].Y + speed, tiles[i].Width, tiles[i].Height);
            if (tiles[i].Y >= graphicsDevice.Viewport.Height)
            {
                tiles[i] = new Rectangle(tiles[i].X, 0 - tiles[i].Height, tiles[i].Width, tiles[i].Height);
            }
        }
    }

    public void CalculateTiles()
    {
        if (tiles.Count() > 0)
        {
            for (int i = 0; i < tiles.Count(); i++)
            {
                tiles.Remove(tiles[i]);
            }
        }
        int XT = (int)(graphicsDevice.Viewport.Width / (tileSize / 1.5f));
        int YT = (int)(graphicsDevice.Viewport.Height / (tileSize / 1.5f));
        for (int i = 0; i < XT; i++)
        {
            tiles.Add(new Rectangle(tileSize * i, 0, tileSize, tileSize));

            for (int j = 0; j < YT; j++)
            {
                tiles.Add(new Rectangle(tileSize * i, tileSize * j, tileSize, tileSize));
            }
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int i = 0; i < tiles.Count(); i++)
        {
            spriteBatch.Draw(texture, tiles[i], color);
        }
    }
}
}
public class Game1 : Microsoft.Xna.Framework.Game
{
    Background background;

    protected override void LoadContent()
    {
        // Assign Background
        background = new Background(Content.Load<Texture2D>("Stars"),GraphicsDevice, 128);
    }  

    protected override void Update(GameTime gameTime)
    {
        if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
            this.Exit();

        background.Update();
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(bgc);
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
        // Call all drawing functions here 
        background.Draw(spriteBatch);
        player1.Draw(spriteBatch);
        player2.Draw(spriteBatch);
        // 
        spriteBatch.End();
        base.Draw(gameTime);
    }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
太空射手
{
阶级背景
{
公共纹理2D纹理;
公共列表分幅=新列表();
公共色彩;
公共int tileSize;
公共交通速度;
图形设备图形设备;
公共背景(Texture2D纹理、GraphicsDevice GraphicsDevice、int tileSize)
{
这个纹理=纹理;
this.graphicsDevice=graphicsDevice;
this.tileSize=tileSize;
速度=3;
this.color=color.White;
计算器();
}
公共无效更新()
{
对于(int i=0;i=graphicsDevice.Viewport.Height)
{
tiles[i]=新矩形(tiles[i].X,0-tiles[i]。高度,tiles[i]。宽度,tiles[i]。高度);
}
}
}
公共void计算器()
{
如果(tiles.Count()>0)
{
对于(int i=0;i
Game.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SpaceShooter
{
    class Background
{
    public Texture2D texture;
    public List<Rectangle> tiles = new List<Rectangle>();
    public Color color;
    public int tileSize;
    public int speed;
    GraphicsDevice graphicsDevice;

    public Background(Texture2D texture, GraphicsDevice graphicsDevice, int tileSize)
    {
        this.texture = texture;
        this.graphicsDevice = graphicsDevice;
        this.tileSize = tileSize;
        speed = 3;
        this.color = Color.White;
        CalculateTiles();
    }

    public void Update()
    {
        for (int i = 0; i < tiles.Count(); i++)
        {
            tiles[i] = new Rectangle(tiles[i].X, tiles[i].Y + speed, tiles[i].Width, tiles[i].Height);
            if (tiles[i].Y >= graphicsDevice.Viewport.Height)
            {
                tiles[i] = new Rectangle(tiles[i].X, 0 - tiles[i].Height, tiles[i].Width, tiles[i].Height);
            }
        }
    }

    public void CalculateTiles()
    {
        if (tiles.Count() > 0)
        {
            for (int i = 0; i < tiles.Count(); i++)
            {
                tiles.Remove(tiles[i]);
            }
        }
        int XT = (int)(graphicsDevice.Viewport.Width / (tileSize / 1.5f));
        int YT = (int)(graphicsDevice.Viewport.Height / (tileSize / 1.5f));
        for (int i = 0; i < XT; i++)
        {
            tiles.Add(new Rectangle(tileSize * i, 0, tileSize, tileSize));

            for (int j = 0; j < YT; j++)
            {
                tiles.Add(new Rectangle(tileSize * i, tileSize * j, tileSize, tileSize));
            }
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int i = 0; i < tiles.Count(); i++)
        {
            spriteBatch.Draw(texture, tiles[i], color);
        }
    }
}
}
public class Game1 : Microsoft.Xna.Framework.Game
{
    Background background;

    protected override void LoadContent()
    {
        // Assign Background
        background = new Background(Content.Load<Texture2D>("Stars"),GraphicsDevice, 128);
    }  

    protected override void Update(GameTime gameTime)
    {
        if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
            this.Exit();

        background.Update();
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(bgc);
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
        // Call all drawing functions here 
        background.Draw(spriteBatch);
        player1.Draw(spriteBatch);
        player2.Draw(spriteBatch);
        // 
        spriteBatch.End();
        base.Draw(gameTime);
    }
公共类游戏1:Microsoft.Xna.Framework.Game
{
背景;
受保护的覆盖void LoadContent()
{
//分配背景
背景=新背景(Content.Load(“Stars”),GraphicsDevice,128);
}  
受保护覆盖无效更新(游戏时间游戏时间)
{
if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
这是Exit();
background.Update();
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备清除(bgc);
spriteBatch.Begin(SpriteSortMode.Deferred,BlendState.AlphaBlend);
//在此处调用所有绘图函数
背景。绘制(spriteBatch);
玩家1.抽签(spriteBatch);
玩家2.抽签(spriteBatch);
// 
spriteBatch.End();
基础。抽签(游戏时间);
}

我认为您的问题可以通过线性RAP轻松解决

如果您在游戏中执行以下操作1.绘制

spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearWrap);
background.Draw(spriteBatch);
spriteBatch.End();
要获得比例,只需执行以下计算:

float scale = 128 / 512; // Where 128 is your desired size. and 512 your actual texture size.
在你的Background.cs绘图方法中

spriteBatch.Draw(texture, new Vector2(0,0), new Rectangle(pos.X, pos.Y, graphicsDevice.Viewport.Width / scale, graphicsDevice.Viewport.Height / scale), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
这将在整个屏幕上重复您的纹理

编辑:

要使图像动态,您可以使用相机。但使用您现有的系统,您只需在更新时执行以下操作:

pos += new Vector2(movementX, movementY);

请注意,spriteBatch.Draw有一个参数“Source Rectangle”。如果更改此矩形的位置,纹理将发生移动。

我认为使用LinearWrap可以轻松解决您的问题

如果您在游戏中执行以下操作1.绘制

spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearWrap);
background.Draw(spriteBatch);
spriteBatch.End();
要获得比例,只需执行以下计算:

float scale = 128 / 512; // Where 128 is your desired size. and 512 your actual texture size.
在你的Background.cs绘图方法中

spriteBatch.Draw(texture, new Vector2(0,0), new Rectangle(pos.X, pos.Y, graphicsDevice.Viewport.Width / scale, graphicsDevice.Viewport.Height / scale), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
这将在整个屏幕上重复您的纹理

编辑:

要使图像动态,您可以使用相机。但使用您现有的系统,您只需在更新时执行以下操作:

pos += new Vector2(movementX, movementY);

请注意,spriteBatch.Draw有一个参数“Source Rectangle”。如果更改此矩形的位置,纹理将发生移动。

问题已解决。
我现在把瓷砖放在相对于屏幕高度的位置,而不是将其编程为0

            if (tiles[i].Y >= graphicsDevice.Viewport.Height)
            {
                tiles[i] = new Rectangle(tiles[i].X, tiles[i].Y - graphicsDevice.Viewport.Height - tiles[i].Height, tiles[i].Width, tiles[i].Height);
            }

问题已经解决。
我现在把瓷砖放在相对于屏幕高度的位置,而不是将其编程为0

            if (tiles[i].Y >= graphicsDevice.Viewport.Height)
            {
                tiles[i] = new Rectangle(tiles[i].X, tiles[i].Y - graphicsDevice.Viewport.Height - tiles[i].Height, tiles[i].Width, tiles[i].Height);
            }

确实如此。但是,像这样的静止图像并不是我想要的背景。我需要将背景平铺,这样我就可以将其安装到任何屏幕上,并使其向下平移(产生向前移动的效果)@riddarn97只需更改源矩形的X和Y位置,如我的编辑中所示。这应该会给你你想要的效果。事实上并非如此。你看,即使这会使我的游戏平移如我所愿;矩形在屏幕上被拉伸,因此,使我的128x128纹理看起来非常非常糟糕。我的意思是,物理效果非常糟糕e、 我只是在做一些数学上的错误(我想)。你认为有没有一种方法可以使用我目前的瓷砖系统来制作