无限游戏循环

时间:2013-11-26 11:04:56

标签: c# xna

我正在使用LoadContent方法创建一个二十一点纸牌游戏。我正在挑选一张随机卡,但问题是我不知道如何正确地结束这种方法,以便它将选择一张随机卡而不是无限循环。现在有2张随机卡因无限循环而闪烁。

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);
    }
}

}

1 个答案:

答案 0 :(得分:3)

在xna中,您有4种主要方法:

loadContent&lt; - 每次运行游戏时都应执行一次,在这里加载游戏内容,纹理等。

初始化&lt; - 此方法也只会执行一次,在这里您可以设置游戏分辨率,抗锯齿和音频选项的额外配置等。

更新&lt; - 这将每秒执行60次,在这里你必须计算一个角色是否死亡或你是否用完了hp

draw&lt; - 在每次更新方法之后,将调用draw,在这里你将绘制当前的游戏对象,比如向上或向下移动一个角色,使某些东西浮动等等。

考虑到这一点,请移动您的代码(重构它),它应该可以正常工作,如果您有任何其他问题,请告诉我。

此外,当你执行rand.Next(tekstuur.Count)时,你应该做+1,因为随机会给你一个介于0和你指定的上限之间的数字,比如你在那里给它5,它会返回你的数字最多4个而且从不5个......所以你错过了一个“tekstuur”