C# 无限博弈循环

C# 无限博弈循环,c#,xna,C#,Xna,我正在创建一个21点纸牌游戏,并且在我的LoadContent方法中。我选择了一张随机卡,但问题是我不知道如何正确地结束这个方法,这样它将选择一张随机卡,而不是无限循环。现在,由于一个无限循环,两张随机牌在闪烁 public class Kaart { public Vector2 asukoht = new Vector2(0, 0); public List<Texture2D> tekstuur = new List<Texture2D>();

我正在创建一个21点纸牌游戏,并且在我的
LoadContent
方法中。我选择了一张随机卡,但问题是我不知道如何正确地结束这个方法,这样它将选择一张随机卡,而不是无限循环。现在,由于一个无限循环,两张随机牌在闪烁

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);
    }
}
公共类Kaart
{
公共向量2 asukoht=新向量2(0,0);
公共列表tekstuur=新列表();
Random rand=新的Random();
public void loadContent(ContentManager)//加载第一张卡
{
对于(int j=3;j<7;j++)
{
添加(manager.Load(“Risti”+j.ToString());
}
}
public void loadContent2(ContentManager)//加载第二张卡
{
对于(int j=3;j<7;j++)
{                
添加(manager.Load(“Risti”+j.ToString());
}
}
公共无效抽牌(SpriteBatch sprite)//我在这里随机抽牌
{
sprite.Draw(tekstuur[rand.Next(tekstuur.Count)],Asukhot,Color.White);
sprite.Draw(tekstuur[rand.Next(tekstuur.Count)],Asukhot,Color.White);
}
}
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
向量2 koht=新向量2(0,0);
卡亚特·伊克斯;
卡亚特·卡克斯;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
/// 
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
/// 
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
yks=新Kaart();
kaks=新的Kaart();
base.Initialize();
}
/// 
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
/// 
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
//加载卡
yks.loadContent(本内容);
kaks.loadContent2(this.Content);
//牌位
yks.asukhot.X=100;
yks.asukht.Y=300;
kaks.asukhot.X=200;
kaks.asukhot.Y=400;
//TODO:使用此.Content在此处加载游戏内容
}
/// 
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
/// 
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
/// 
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
/// 
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
//TODO:在此处添加更新逻辑
更新(游戏时间);
}
/// 
///这就是所谓的比赛应该平局的时候。
/// 
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
//约安明
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
yks.Draw(这个spriteBatch);
kaks.Draw(this.spriteBatch);
spriteBatch.End();
//TODO:在此处添加图形代码
基础。抽签(游戏时间);
}
}

}

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


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


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


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



loadContent在我看来,你甚至不知道一个基本的游戏循环是如何工作的。你有一个循环,它会更新你的游戏状态(更新方法)并绘制任何必要的东西(绘制方法)。它一遍又一遍地重复。您需要告诉它自己在代码中停止,否则它将只执行它现在执行的操作。阅读游戏循环和XNA的基本知识。是的,我完全是XNA的noob。但是如果我理解了,那么我必须得到循环的状态,并在更新方法中打破它?不。你需要告诉游戏“现在是抽签的时候了”或“现在不要抽下一张牌,是时候等待用户输入”或类似的东西。游戏状态是您要寻找的-您需要在update()中更新它,然后在draw()中相应地执行(绘制或不绘制)。我相信有很多关于游戏状态的教程,以及如何在互联网上的XNA中实现它们。你需要在更新方法中实现这些逻辑。使用游戏状态来管理何时执行什么,等等。您可以查看coursera初学者XNA课程,它对基础知识有很大帮助(只是我自己做的)。您需要在update方法中执行该逻辑。使用游戏状态来管理何时执行什么,等等。您可以查看coursera初学者XNA课程,它对基础知识有很大帮助(只是我自己做的)。你需要注册,课程是免费的,非常初学者:@user3024888你真的应该从头开始:在我看来,你甚至不知道一个基本的游戏循环是如何工作的。你有一个循环,它会更新你的游戏状态(更新方法)并绘制任何必要的东西(绘制方法)。它一遍又一遍地重复。您需要告诉它自己在代码中停止,否则它将只执行它现在执行的操作。阅读游戏循环和XNA的基本知识。是的,我完全是XNA的noob。但是如果我理解了,那么我必须得到循环的状态,并在更新方法中打破它?不,你需要告诉游戏“现在是关键”