我现在无法运行它,我不知道我是否应该修改我的代码
本文关键字:是否 修改 代码 我的 我不知道 运行 | 更新日期: 2023-09-27 18:31:48
我最初的是有一块平坦的土地,通过使用数字生成器来识别绘制的块,从而在草地下生成矿石。现在,由于我已经制作了循环来创建它,它甚至不会启动。这让我感到困惑,为什么以及如果我不应该使用 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);
}
}
}
正如我在你之前的问题中所写的,你不应该在调用中使用Random
生成器Draw
因为你的纹理序列会改变每一帧(每秒 60 次),我认为你不希望这样。
例如,您应该使用数组在 Initialize
方法中声明纹理的随机序列。