square sprite renders as rectangular!

I had just finished implementing a text file parser so that I could easily design game tiles in a text file, turn them into a Texture2D, then use them as sprites in my game. I don’t know if this is related to my issue. When I went to build the project, the graphics were off. A sprite that’s supposed to be square (8x8 pixels) is squeezed, the pixels are sometimes not square, and there’s this weird margin on the side of the viewport where the sprite doesn’t even get drawn.

Screen Shot 2022-02-01 at 4.44.45 PM Screen Shot 2022-02-01 at 4.45.02 PM
pixel width is not constant–the sprite appears differently depending on where it is

Any idea why this could be happening? I didn’t really mess with the draw functions while I was implementing the aforementioned thing.

I am a beginner with C# and Monogame, so I hope this is something really simple that I’ve just missed.


static int _width = 320;
    static int _height = 160;
    static int ps = 3; // pixel size    


protected override void Draw(GameTime gameTime) {

        _spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp);

        for (int y = 0; y < _height; y++) {
            for (int x = 0; x < _width; x++) {
                if (board[y, x] != 0) {
                    int px = x * ps;
                    int py = y * ps;
                    _spriteBatch.Draw(pixel, new Rectangle(px, py, ps, ps), _colors[board[y, x]]);





public void Draw(SpriteBatch _spriteBatch) {
                new Rectangle(
                    (int)player.Pos.X * ps,
                    (int)player.Pos.Y * ps,
                    ps * player.Sprite.Width,
                    ps * player.Sprite.Height

This is something that happens a lot when there’s some kind of scaling going on. When you’re rendering a position at a float value instead of an integer one, or using a Matrix transform in your SpriteBatch.Begin call.

From the sample code you’ve posted though, I don’t see any scaling going on. You’re adding a full scale factor of 3, but you’re always multiplying this integer by other integers and I don’t see any division happening.

So I’m not sure why this is happening. Based on your code the agent renders in a filled white rectangle, which doesn’t appear on the screen, so unless there’s more code to it, those screens are fully rendered by the “board” code in the first code block you posted?

The only thing I can think of is for you to check how board[y, x] is generated. It looks like you’re storing data to that as a full screen array, then rendering each element of the array as a pixel to your screen. Is the data in that array as you expect it to be?

Past that, I can’t think of anything else… you may have to post a more complete solution via github or something.

Thanks for the reply.

player.Sprite is actually the blue square you see, not a filled white rectangle. I checked board several times, but I couldn’t find any issue with it… Plus, that doesn’t explain why the player sprite gets cut off near the right edge of the screen.

I’ve uploaded my project file to Github… if you’re willing to take a look.

I think I figured it out. In the initialization function for my Agents class, I define a new GraphicsDevice when I could be using _graphics.GraphicsDevice (_graphics being my GraphicsDeviceManager) from the Game1 class. I’m not exactly sure what the difference between the two devices are, but fixing it so that the Agents classe referenced the same variable fixed my issues!

Oh right, you’re not rendering that with a pixel, it’s an actual sprite :slight_smile:

Hmm weird. I know you definitely don’t need to use that, you can just share the one from your main game project over. Having said that, looking at the code you posted on github, you don’t actually reference that device… just create it and hang onto it. The SpriteBatch you render with for Agent is passed in to the Draw method, and that SpriteBatch was created by your main game class with its own GraphicsDevice.

This is definitely a strange one! Perhaps someone more well versed in what GraphicsDevice does might be better able to shed some light as to why just creating another one can skew your screen rendering.

I’m glad you figured it out :slight_smile:

1 Like