如何在XNA中调用随机字符串?

时间:2013-04-10 03:27:57

标签: c# xna

我无法相信我坚持这一点,目前我的播放器代码允许玩家绕过ecetera,它甚至允许玩家在按下Ctrl和K按钮时自杀。现在是我的问题,当玩家自杀时,它会在角落里画出一串带有趣语句的文字。但是我不希望这是唯一的一个,我希望它基于你自杀而在3个不同的陈述之间交替,我不知道究竟如何将其付诸实践,继承我的播放器代码:

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

随机工作究竟如何?我将如何在这里实施?

编辑:我已经尝试实现你所说的,我收到错误'无法隐式转换'字符串'为'string []'奇怪,我认为它在哪里我试图把它放在哪里,哪里做它去了?

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

并将其添加到click to respawn变量:

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

然后,如果自杀变量等于真,我就完成绘制它。

想法我做的是告诉游戏如果玩家自杀而且每次玩家点击/自杀时都会绘制文本然后更新字符串。这是正确的想法吗?

1 个答案:

答案 0 :(得分:1)

根据@Andrew Russell的建议编辑

你的意思是你想为自杀球员随机选择一个字符串吗?

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

这个怎么样?