I'm trying to create a puzzle game where you move tiles to certain positions to make a whole image. I need help with randomizing the tiles start position so that they don't create the whole image at the beginning. There is also something wrong with my offset, that's why it's set to (0,0). I know my code is not good, but I'm just starting to learn.
My start-up tile positioning code
spriteBatch = new SpriteBatch(GraphicsDevice);
PictureTexture = Content.Load<Texture2D>(@"Images/bildgraff");
FrameTexture = Content.Load<Texture2D>(@"Images/framer");
//Laddar in varje liten bild av den stora bilden i en array
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 4; y++)
{
Vector2 position = new Vector2(x * pictureWidth, y * pictureHeight);
position = position + Offset;
Rectangle square = new Rectangle(x * pictureWidth, y * pictureHeight, pictureWidth, pictureHeight);
Square frame = new Square(position, PictureTexture, square, Offset, index);
squareArray[x, y] = frame;
index++;
}
}
For reference, here is the square class:
class Square
{
public Vector2 position;
public Texture2D grafTexture;
public Rectangle square;
public Vector2 offset;
public int index;
public Square(Vector2 position, Texture2D grafTexture, Rectangle square, Vector2 offset, int index)
{
this.position = position;
this.grafTexture = grafTexture;
this.square = square;
this.offset = offset;
this.index = index;
}
public void Draw(SpriteBatch spritebatch)
{
spritebatch.Draw(grafTexture, position, square, Color.White);
}
public void RandomPosition() { }
public void Swap(Vector2 Goal )
{
if (Goal.X > position.X)
{
position.X = position.X + 144;
}
else if (Goal.X < position.X)
{
position.X = position.X - 144;
}
else if (Goal.Y < position.Y)
{
position.Y = position.Y - 95;
}
else if (Goal.Y > position.Y)
{
position.Y = position.Y + 95;
}
}
}
for ( i = 0; i < ROWS; i++ ) for ( j = 0; j < COLS; j++) swapTiles(grid[i][j], grid[Math.random()*ROWS][Math.random()*COLS]);
\$\endgroup\$