Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8544017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:27:03+00:00 2026-06-11T12:27:03+00:00

Im totally new at this and have a question. I worked with an exercise

  • 0

Im totally new at this and have a question. I worked with an exercise in school and at home but I can’t figure out how to do it.

The thing is I want to draw a single sprite at 10 random positions on the screen without using a special sprite class. My problem is that after they are drawn they vanish again.

Solved it, thanks for all 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);
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:27:04+00:00Added an answer on June 11, 2026 at 12:27 pm

    If you want to have your sprites on the same random positions all the time and still clearing your viewport (for example, you may want to render other content) you may just reset the random seed every frame to 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 should be randomly generated in your Initialize() method, and not be changed after that.

    You may also just initialize list with predefined coords:

    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 routine:

    for (int i = 0; i < 10; i++)
    {
        spriteBatch.Draw(turtleTexture, coords[i],
            Color.Black);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have worked with NSUserDefault but this keychain concept is totally new for me.
Totally new to programming so kindly excuse the silly question. I have this URL
I know others have asked this question, but I'm totally confused by this: This
Sorry if this is a easy question but I am totally new to WP7.
This may be a silly question but i'm totally new to Apps Engine and
I'm totally new to this WCF Services thing so any guidelines would be greatly
Please bear in mind that I'm totally new to Rails when answering this.My question
This question shows the usage of **var = new Object(); and that's something totally
Totally new to Capistrano. I have a local git repository that I want to
This is a totally new area for me so please be patient. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.