2013-04-10 54 views
0

我不敢相信我被困在這個上面,目前我的玩家代碼允許玩家四處走動,甚至允許玩家在按下Ctrl和K按鈕時自殺。現在我的問題是,當玩家自殺時,它會在角落畫一串文字,並帶有一個有趣的陳述。但我不希望這是唯一的一個,我希望它根據你自殺,我不知道究竟如何把這個變成運動,我的繼承人播放器代碼3個不同的語句之間交替:如何在XNA中調用一個隨機字符串?

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; 
    using Microsoft.Xna.Framework.Net; 
    using Microsoft.Xna.Framework.Storage; 

    namespace Teir_Tactical_2A 
    { 
    public class Player 
    { 
    public Random random; 
    public Texture2D playerunarmed; 
    public Texture2D playerM1911; 
    public Texture2D playerM4; 
    public KeyboardState keyState; 
    public SpriteFont Font1; 
    public KeyboardState oldKeyboardState; 
    public Vector2 playerPosition; 
    public SpriteBatch spriteBatch; 
    public float Angle { get; set; } 
    public float AngularVelocity { get; set; } 
    public Vector2 playerVelocity = Vector2.One; 
    public bool M1911; 
    public bool M4; 
    public bool player; 
    public bool LMBpressed; 
    public bool RMBpressed; 
    public bool isplayeralive; 
    public bool respawnscreen; 
    float angle; 





    public Player(ContentManager content, Vector2 location) 
    { 
     this.playerunarmed = content.Load<Texture2D>("PLAYER"); 
     this.playerM1911 = content.Load<Texture2D>("PLAYERM1911"); 
     this.playerM4 = content.Load<Texture2D>("PLAYERM4"); 
     playerPosition = location; 
     M1911 = true; 
     M4 = true; 
     player = true; 
     LMBpressed = false; 
     random = new Random(3); 
     RMBpressed = false; 
     isplayeralive = true; 
     respawnscreen = false; 
     Font1 = content.Load<SpriteFont>("Font1"); 
    } 


    public void Update() 
    { 
     MouseState curMouse = Mouse.GetState(); 

     if (isplayeralive == false) 
     { 
      alive(); 
     } 

     if (respawnscreen == true) 
     { 
      respawn(); 
     } 

     if (M1911 == true) 
     { 
      armedM1911(); 
     } 

     if (M4 == true) 
     { 
      armedM4(); 
     } 
     if (player == true) 
     { 
      unarmed(); 
     } 
     if (LMBpressed == true) 
     { 
      LMBpressedA(); 
     } 
     if (RMBpressed == true) 
     { 
      RMBpressedA(); 
     } 
     Vector2 mouseLoc = new Vector2(curMouse.X, curMouse.Y); 

     Vector2 direction = mouseLoc - playerPosition; 

     angle = (float)(Math.Atan2(direction.Y, direction.X)); 

     if (curMouse.LeftButton == ButtonState.Pressed) 
     { 
      LMBpressed = true; 

     } 
     if (curMouse.RightButton == ButtonState.Pressed) 
     { 
      RMBpressed = true; 
     } 

     keyState = Keyboard.GetState(); 

     if (isplayeralive == false && LMBpressed == true) 
     { 
      isplayeralive = true; 
      respawnscreen = true; 
     } 


     if (keyState.IsKeyDown(Keys.LeftControl) && keyState.IsKeyDown(Keys.K)) 
     { 
      isplayeralive = false; 

     } 

     if (keyState.IsKeyDown(Keys.D1)) 
     { 

      M1911 = false; 
      M4 = false; 
      player = true; 

     } 
     if (keyState.IsKeyDown(Keys.D2)) 
     { 
      M1911 = true; 
      player = false; 

     } 
     if (keyState.IsKeyDown(Keys.D3)) 
     { 
      M4 = true; 
      M1911 = false; 
      player = false; 

     } 





     if (keyState.IsKeyDown(Keys.D)) 
      playerPosition.X += playerVelocity.X + 1; 
     if (keyState.IsKeyDown(Keys.A)) 
      playerPosition.X -= playerVelocity.X + 1; 

     if (keyState.IsKeyDown(Keys.W)) 
      playerPosition.Y -= playerVelocity.Y + 1; 
     if (keyState.IsKeyDown(Keys.S)) 
      playerPosition.Y += playerVelocity.Y + 1; 
    } 


    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.Begin(); 
     { 

      Rectangle sourceRectangle = new Rectangle(0, 0, playerunarmed.Width, playerunarmed.Height); 
      Vector2 origin = new Vector2(playerunarmed.Width/2, playerunarmed.Height/2); 

      if (M1911 == true) 
      { 
       spriteBatch.Draw(playerM1911, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(86, 88), SpriteEffects.None, 0f); 
      } 

      if (respawnscreen == true) 
      { 
       spriteBatch.DrawString(Font1, "Looks like someone commited suicide", new Vector2(968, 52), Color.Red); 
      } 


      if (player == true) 
      { 
       spriteBatch.Draw(playerunarmed, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(86, 88), SpriteEffects.None, 0f); 
      } 
      if (M4 == true) 
      { 
       spriteBatch.Draw(playerM4, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(86, 88), SpriteEffects.None, 0f); 

      } 
      if (LMBpressed == true) 
      { 
       spriteBatch.DrawString(Font1, "LMB PRESSED (shot taken prototype)", new Vector2(1000, 22), Color.GreenYellow); 
      } 
      if (RMBpressed == true) 
      { 
       spriteBatch.DrawString(Font1, "RMB PRESSED (grenade thrown prototype)", new Vector2(968, 34), Color.Red); 
      } 
      if (RMBpressed == true && LMBpressed == true) 
      { 
       spriteBatch.DrawString(Font1, "If you are seeing this, the mouse is functioning correctly", new Vector2(810, 45), Color.Black); 
      } 
     } 
     spriteBatch.End(); 


    } 
    public void armedM1911() 
    { 
     M1911 = true; 
     M4 = false; 
     player = false; 

    } 
    public void armedM4() 
    { 
     M4 = true; 
     M1911 = false; 
     player = false; 
    } 
    public void unarmed() 
    { 
     M1911 = false; 
     M4 = false; 
    } 
    public void LMBpressedA() 
    { 
     LMBpressed = false; 
    } 
    public void RMBpressedA() 
    { 
     RMBpressed = false; 
    } 
    public void alive() 
    { 
     M1911 = false; 
     player = false; 
     M4 = false; 
    } 
    public void respawn() 
    { 
     player = true; 
    } 
} 

}

這裏是我希望它在我的繪製方法中交替的地方。

 if (respawnscreen == true) 
      { 
       spriteBatch.DrawString(Font1, "Looks like someone commited suicide", new Vector2(968, 52), Color.Red); 
      } 

隨機工作究竟如何?我將如何在這裏實現它?編輯:我試着實現你說的,我得到一個錯誤'不能隱式轉換'字符串'到'字符串[]'它奇怪的,我認爲它在哪裏我試圖把這個,在哪裏它會去嗎?

EDIT2:對不起,錯字就我而言,我只注意到了「[]」的第一個字符串測試後..

決賽:與您的代碼後醞釀好了,我終於得到它的工作對我的期望,爲了連續性,我還必須修改一些變量。

我在構造函數中加入這樣的:

String suicideStr; 
    public bool suicide; 

這到LoadContent();方法:

suicideStr = strs[random.Next(strs.Length)]; 

將此bool添加到Update()中;方法:

 if (suicide == true) 
     { 
      suicided(); 
     } 

然後我在變量增加了它,並添加到了自殺的按鈕:

 if (keyState.IsKeyDown(Keys.LeftControl) && keyState.IsKeyDown(Keys.K)) 
     { 
      isplayeralive = false; 
      isplayerdead = true; 
      bloodspatter = true; 
      suicide = true; 
     } 

並添加到點擊重生變量:

 if (isplayeralive == false && LMBpressed == true) 
     { 
      isplayeralive = true; 
      isplayerdead = false; 
      suicide = false; 
      suicideStr = strs[random.Next(strs.Length)]; 
     } 

然後如果自殺變量等於真,我就完成了繪圖工作。

什麼我認爲我所做的是告訴遊戲畫出文本,如果玩家自殺,每次玩家點擊/自殺,然後更新字符串。這是正確的想法嗎?

回答

1

根據@Andrew羅素的建議

編輯你的意思是你想隨機選擇一個字符串自殺的球員?

String[] strs = new String[]{ "It's a good day to die!", "Oops!", "I don't wanna live!" }; 
Random random = new Random(); 

而且當玩家自殺時,您可能需要「挑選」一個字符串。 在玩家按自殺鍵後插入此行。

String suicideStr = strs[random.Next(str.Length)]; 

和修改

if (respawnscreen == true){ 
    spriteBatch.DrawString(Font1, suicideStr, new Vector2(968, 52), Color.Red); 
} 

這個怎麼樣你的代碼?

+0

我已經修復了你的代碼,以便每次你需要一個隨機數時你不會創建一個'Random'的新實例,因爲這是一個*巨大的*不 - 不!但是,您的代碼仍然被破壞 - 每幀顯示不同的隨機消息! – 2013-04-10 06:15:28

+0

@AndrewRussell:謝謝您的通知。我會再次修改它。 – 2013-04-10 06:20:40

+0

是的,我可以同意這一點,消息發展得很快,我以爲我在矩陣中,當我回家時我會試試這個,謝謝! – 2013-04-10 16:58:40

相關問題