2012-11-19 72 views
0

我是完全新的編碼,只是練了幾個星期,我已經分配了一個任務,其中似乎很簡單已經碰到了絆腳石XNA數組和繪圖紋理?

我有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); 
     } 
    } 
} 

任何幫助將是巨大的畫哪一個,因爲我不知道如果IM存儲陣列正確或如果我做隨機的權利

+1

歡迎來到Stack Exchange。通常,你的帖子應該對你的問題有明確的問題。你可以編輯你的文章並添加問題。 (編輯)在代碼的末尾沒有看到它。你應該把它放在源代碼塊之外。 – user1306322

回答

0

我看到你正在存儲你的紋理數組。我沒有在您的示例中看到任何隨機代碼。

話雖這麼說,考慮下面的函數:

Random rand = new Random(); 
private Texture2D GetRandomTexture() 
{ 
    return sprite[rand.Next(0, 2)]; 
} 

調用此函數,將返回包含在你的「精靈」陣列中的隨機紋理。堵在你得出這樣的函數調用可以像這樣的事情:

spriteBatch.Draw(GetRandomTexture(), sprRect1, Color.White); 

既然你宣佈,你是一個初學者,我試着不要去過多贅述。但是,您應該考慮創建一個新的類「Sprite」,該類將包含每個精靈的Texture2D,Position,Rectangle值。然後,而不是存儲紋理數組,您將能夠存儲Sprite對象的數組(或列表)。

0

你要選擇四種精靈,因此,你應該使用數組(或類似結構),它可以包含四個要素:

Texture2D[] sprites = new Texture2D[4]; 

當初始化數組有關於多少每個精靈的三種可能情況會存在:

  • 1-3(一個巨魔,三個侏儒)
  • 2-2(每兩個)
  • 3-1(3食人妖,一個侏儒)

所以,首先,你必須選擇這些發行版之一:

var rnd = new Random(); 
var trolls = rnd.Next(1, 3); 
var gnomes = 4 - trolls; 

然後,您可以填充數組:

for(int i = 0; i < 4; ++i) 
{ 
    if(gnomes == 0) 
    { 
     //choose the troll 
     sprites[i] = troll; 
     --trolls; 
    } 
    else if(trolls == 0) 
    { 
     //choose the gnome 
     sprites[i] = gnome; 
     --gnomes; 
    } 
    else 
    { 
     //choose randomly 
     if(rnd.Next(2) < 1) 
     { 
      sprites[i] = troll; 
      --trolls; 
     } 
     else 
     { 
      sprites[i] = gnome; 
      --gnomes; 
     } 
    } 
} 

你畫他們像

spriteBatch.Draw(sprites[0], sprRect1,Color.White); 
spriteBatch.Draw(sprites[1], , sprRect2,Color.White); 
spriteBatch.Draw(sprites[2], , sprRect3, Color.White); 
spriteBatch.Draw(sprites[3], , sprRect4, Color.White);