C# 我现在不能运行它,我不知道我是否应该修改我的代码

C# 我现在不能运行它,我不知道我是否应该修改我的代码,c#,xna,C#,Xna,我最初的想法是在一块平坦的土地上,通过使用数字生成器来识别绘制的区块,从而在草地下方生成矿石。现在我已经创建了循环来创建它,它甚至不会启动。这让我很困惑,为什么我不应该使用do循环,而不是告诉我一种更有效的方法来实现这一点。我已经学习C#大约两个月了,我正在努力掌握它的窍门 using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsof

我最初的想法是在一块平坦的土地上,通过使用数字生成器来识别绘制的区块,从而在草地下方生成矿石。现在我已经创建了循环来创建它,它甚至不会启动。这让我很困惑,为什么我不应该使用do循环,而不是告诉我一种更有效的方法来实现这一点。我已经学习C#大约两个月了,我正在努力掌握它的窍门

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame3
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D EarthGrass;
        Texture2D EarthDirt;
        Texture2D PooperMachoOre;
        Vector2 BlockPos = new Vector2(0, 300);
        System.Random IDB = new System.Random();

        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

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



            // TODO: use this.Content to load your game content here
            EarthGrass = Content.Load<Texture2D>("Sprites/EarthGrass");
            EarthDirt = Content.Load<Texture2D>("Sprites/EarthDirt");
            PooperMachoOre = Content.Load<Texture2D>("Sprites/Ores/PooperMachoOre");
        }

        /// <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)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            int IDBint = IDB.Next(11);
            spriteBatch.Begin();
            do
            {

                if (BlockPos.X == 300)

                    do
                    {
                        spriteBatch.Draw(EarthGrass,BlockPos,Color.White);
                        BlockPos.X +=20;                        
                    } while (BlockPos.X < 800) ;
                if (BlockPos.X <= 800)
                    BlockPos.Y += 20;
                do
                {
                    if (IDBint == 10)
                    {
                        spriteBatch.Draw(PooperMachoOre, BlockPos, Color.White);
                        BlockPos.X += 20;
                    }
                    else
                    {
                        spriteBatch.Draw(EarthDirt, BlockPos, Color.White);
                        BlockPos.X += 20;
                    }
                }while (BlockPos.X < 800);
                BlockPos.Y += 20;
            } while (BlockPos.Y > 800);
            BlockPos.X = 0;
            BlockPos.Y = 300;
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
命名空间WindowsGame3
{
/// 
///这是游戏的主要类型
/// 
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
质地2d土草;
质地2 d泥土;
质感2-D的PooperMachoOre;
向量2 BlockPos=新向量2(0,300);
System.Random IDB=新的System.Random();
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
/// 
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
/// 
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
base.Initialize();
}
/// 
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
/// 
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
//TODO:使用此.Content在此处加载游戏内容
EarthGrass=Content.Load(“精灵/EarthGrass”);
Earth污垢=含量。负载(“精灵/泥土”);
PooperMachoOre=内容物负荷(“雪碧/矿石/PooperMachoOre”);
}
/// 
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
/// 
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
/// 
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
/// 
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
//TODO:在此处添加更新逻辑
更新(游戏时间);
}
/// 
///这就是所谓的比赛应该平局的时候。
/// 
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
//TODO:在此处添加图形代码
int-IDBint=IDB.Next(11);
spriteBatch.Begin();
做
{
如果(块位置X==300)
做
{
spriteBatch.Draw(土草、BlockPos、彩色、白色);
区块位置X+=20;
}而(区块位置X<800);
如果(区块位置X 800);
BlockPos.X=0;
块位置Y=300;
spriteBatch.End();
基础。抽签(游戏时间);
}
}
}

正如我在上一个问题中写的那样,你不应该在
Draw
调用中使用
Random
生成器,因为你的纹理序列每帧都会发生变化(每秒60次),我想你不希望这样


例如,你应该在
初始化
方法中使用数组声明你的随机纹理序列。

好的,你的问题是什么?你知道你的代码是做什么的吗?看起来你不知道,或者你有一些非常奇怪的理由去试着让它工作。现在你只需要在每天确定的随机位置绘制一些精灵抽签决定。这不是你期望从一场真正的比赛中得到的。这个问题是怎么回事?这是你正在谈论的一场比赛吗?给我们少一些似乎无关紧要的个人信息(比如你花了多少时间在这个问题上)还有更多与你想问的问题相关的信息。我试图让它以直线形式生成块,并使用随机生成的数字来决定它将绘制哪个块。当我运行此命令时,它只是说我的游戏停止工作,我必须关闭它。我应该如何修改它?a
do…而
不是“低效的"就其本身而言,但这并不是你正在做的事情的最佳选择。实际上,你想要使用
do…while
循环构造的唯一时间是当你至少需要一次迭代时。在你的代码中,你可能至少有一次发生,但你不需要它发生。最好坚持使用
for
循环,因为你有一个修复程序d具有固定步长的迭代次数(这是使
for
循环更理想的要求)。