How can I draw one sprite in random positions 10 times?

Im brand new to this and have a question. I worked with the exercise at school and at home, but I cannot figure out how to do this.

The fact is that I want to draw one sprite in 10 random positions on the screen, without using a special class of sprites. My problem is that after they are painted, they disappear again.

I decided, thanks for the help!

public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Texture2D turtleTexture; int counter = 0; Random randomera = new Random(); int x; int y; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { base.Initialize(); } /// <summary> /// </summary> protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); turtleTexture = Content.Load<Texture2D>(@"Images/turtle_50x38"); } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); /* if(counter < 10) { x = randomera.Next(600); y = randomera.Next(400); counter++; } */ base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.White); spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend); if (counter < 10) { for (int i = 0; i < 10; i++) { spriteBatch.Draw(turtleTexture, new Vector2(randomera.Next(600), randomera.Next(400)), Color.Black); counter++; } } spriteBatch.End(); base.Draw(gameTime); } } 

}

+6
source share
3 answers

If you want your sprites at the same random positions all the time and still clear your viewport (for example, you can display different content), you can simply reset the random seed of each frame with the same value

 protected override void Draw(GameTime gameTime) { randomera = new Random(seed); GraphicsDevice.Clear(Color.White); spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend); if (counter < 10) { for (int i = 0; i < 10; i++) { spriteBatch.Draw(turtleTexture, new Vector2(randomera.Next(600), randomera.Next(400)), Color.Black); counter++; } } spriteBatch.End(); base.Draw(gameTime); } 

where the seed must be randomly generated in your Initialize() method and will not be changed after it.

You can also simply initialize the list using predefined coordinates:

 List<Vector2> coords = Enumerable.Range(0, 10).Select(i => new Vector2(randomera.Next(600), randomera.Next(400)).ToList(); 

and use this list in your drawing procedure:

 for (int i = 0; i < 10; i++) { spriteBatch.Draw(turtleTexture, coords[i], Color.Black); } 
+7
source
 GraphicsDevice.Clear(Color.White); 

This clears the screen (which means that everything that was painted earlier will be deleted). The Draw function that contains this statement is called about 30-60 times per second, and your randomizer will retrieve new values ​​for each of these iterations.

It is correct to clear the screen at each Draw iteration, so instead of choosing random coordinates for each Draw call, determine the coordinates before starting. The best approach would be to initialize an array of 10 vectors in your constructor, and then use that array in your Draw function (rather than calling randomera.Next() again and again).

EDIT: (see the second half of Bartosch’s answer for an example. I cannot open my XNA IDE at the moment, so I cannot write an example code for you.)

+1
source

You call Clear in the drawing method.

 GraphicsDevice.Clear(Color.White); 

This will clear the screen.

Delete this line and it will save the previous drawings.

+1
source

Source: https://habr.com/ru/post/925243/


All Articles