-4

我最初的想法是通过使用数字生成器来确定绘制了哪个块,从而在草丛下方生成矿石的平坦土地。现在,当我创建循环来创建它时,它甚至不会启动。它让我感到困惑,为什么以及是否不应该使用 do 循环而不是告诉我一个更有效的方法来做到这一点。我已经学习 C# 大约 2 个月了,我正在努力掌握它。

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

1 回答 1

1

正如我在上一个问题中写给你的那样,你不应该Random在你Draw调用的时候使用生成器,因为你的纹理序列会改变每一帧(每秒 60 次),我认为你不想要这个。

例如,您应该Initialize使用数组在方法中声明纹理的随机序列。

于 2013-09-18T23:20:43.953 回答