user3024888
user3024888

Reputation: 49

Infinite Game Loop

I'm creating a blackjack card game, and in my LoadContent method. I'm picking a random card, but the problem is that I don't know how to properly end this method so that it will pick one random card and not loop infinitely. Right now 2 random cards are blinking because of an infinite loop.

public class Kaart
{

    public Vector2 asukoht = new Vector2(0, 0);

    public List<Texture2D> tekstuur = new List<Texture2D>();


    Random  rand = new Random();


    public void loadContent(ContentManager manager) // loading first card
    {

        for (int j = 3; j < 7; j++)
        {


                tekstuur.Add(manager.Load<Texture2D>("Risti" + j.ToString()));


        }
    }
    public void loadContent2(ContentManager manager) // loading second card
    {

        for (int j = 3; j < 7; j++)
        {                

                tekstuur.Add(manager.Load<Texture2D>("Risti" + j.ToString()));


               }

    }

    public void Draw(SpriteBatch sprite) // here ill draw the random card
    {

        sprite.Draw(tekstuur[rand.Next(tekstuur.Count)], asukoht, Color.White);
        sprite.Draw(tekstuur[rand.Next(tekstuur.Count)], asukoht, Color.White);

    }

}


 public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Vector2 koht = new Vector2(0,0);

    Kaart yks;
    Kaart kaks;


    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here
        yks = new Kaart();
        kaks = new Kaart();

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        //loading cards 
        yks.loadContent(this.Content);

        kaks.loadContent2(this.Content);


        // card positions
        yks.asukoht.X = 100;
        yks.asukoht.Y = 300;

        kaks.asukoht.X = 200;
        kaks.asukoht.Y = 400;



        // TODO: use this.Content to load your game content here
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();


        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        // kaartide joonistamine 
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        yks.Draw(this.spriteBatch);
        kaks.Draw(this.spriteBatch);

        spriteBatch.End();

        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }
}

}

Upvotes: 0

Views: 283

Answers (1)

Bryan Arbelo
Bryan Arbelo

Reputation: 39

In xna you have 4 main methods :

loadContent <- should be executed only once every time you run the game, here you load the game content, textures and such.

initialize <- this method will be executed only once as well, here you can set up game resolution, antialiasing, and extra configuration of audio options and so on.

update <- this will be executed 60 times every second, here you will have to calculate whether a character died or you ran out of hp

draw <- after every update method, draw will be called, here you will draw your current game objects, like move a character up or down, make something float ,etc

Having this into account please move your code around (refactorize it) and it should work fine, let me know if you have any further questions.

Also, when you do rand.Next(tekstuur.Count) you should do +1 since random will give you a number that is between 0 and the upper limit that you specified, like if you give it 5 there, it will return you numbers up to 4 and never five..so you are missing one "tekstuur"

Upvotes: 3

Related Questions