我对编码完全陌生,只练习了几个星期,我被分配了一项看似简单的任务却遇到了绊脚石
我在屏幕上绘制了 4 个精灵,但我必须每次游戏开始时,精灵必须在 1 个精灵或另一个精灵之间随机选择,并且在 2 个精灵中,每个精灵必须至少有一个在屏幕上。
我的导师建议我使用一个数组来存储纹理,然后对其进行编码,以便每次随机选择要绘制的纹理
namespace GamesProgrammingAssement1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
KeyboardState keys;
KeyboardState oldKeys;
GamePadState Pad1;
GamePadState oldPad1;
Texture2D gnome;
Texture2D troll;
Rectangle sprRect1;
Rectangle sprRect2;
Rectangle sprRect3;
Rectangle sprRect4;
SpriteFont Run;
SpriteFont Score;
int scoreNum = 0;
int runNum = 0;
Vector2 scorePos;
Vector2 runPos;
Texture2D[] sprite = new Texture2D[2];
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()
{
sprRect1 = new Rectangle(375, 100, 64, 64);
sprRect2 = new Rectangle(375, 300, 64, 64);
sprRect3 = new Rectangle(225, 200, 64, 64);
sprRect4 = new Rectangle(525, 200, 64, 64);
scorePos = new Vector2(5, 400);
runPos = new Vector2(5, 425);
sprite[0] = gnome;
sprite[1] = troll;
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);
gnome = Content.Load<Texture2D>("Gnome");
troll = Content.Load<Texture2D>("Troll");
Score = Content.Load<SpriteFont>("Score");
Run = Content.Load<SpriteFont>("Run");
}
/// <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)
{
KeyboardState keys = Keyboard.GetState();
KeyboardState oldkeys = keys;
if (keys.IsKeyDown(Keys.Escape)) 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);
spriteBatch.Begin();
spriteBatch.Draw(gnome,sprRect1,Color.White);
spriteBatch.Draw(troll, sprRect2,Color.White);
spriteBatch.Draw(troll, sprRect3, Color.White);
spriteBatch.Draw(troll, sprRect4, Color.White);
spriteBatch.DrawString(Score, "SCORE : "+ scoreNum, scorePos, Color.Black);
spriteBatch.DrawString(Run, "RUN OF TROLL : " + runNum, runPos, Color.Black);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
任何帮助都会很棒,因为我不知道我是否正确存储了数组,或者我是否正确地随机操作