Nero-One
Nero-One

Reputation: 127

How to draw a random string in XNA when its called?

I can't believe I'm stuck on this, currently my player code allows the player to walk around ecetera, it even allows the player to commit suicide when the buttons Ctrl and K are pressed. Now heres my problem, When the player commits suicide, it will draw a string of text in the corner with a funny statement. But I don't want this to be the only one, I want it to alternate between 3 different statements based on you committing suicide and I don't know how exactly to put this into motion, heres my player code:

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

}

Here is where I want it to alternate in my draw method.

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

how exactly does random work? and how would I implement it here?

EDIT: I've tried implementing what you said, I'm getting an error 'Cannot implicitly convert 'string' to 'string[]' Its odd, I think its where I'm trying to put this, where does it go?

EDIT2: Sorry, typo on my part, I just noticed the '[]' after the first string testing..

Final: Well after toying with your code, I finally got it to work to my expectations, I also had to modify a couple variables for continuity's sake.

I added this in the constructor:

    String suicideStr;
    public bool suicide;

And this into the LoadContent(); method:

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

adding this bool into the Update(); method:

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

I then added in the variable for it and added this to the suicide buttons:

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

And added this to the click to respawn variable:

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

Then I just finished with drawing it if the suicide variable equalled true.

What I think I did was tell the game to draw the text if the player suicided and each time that the player clicks/suicides then it updates the string. Is this right thinking?

Upvotes: 0

Views: 254

Answers (1)

J.C
J.C

Reputation: 633

Edited according @Andrew Russell's suggestion

Do you mean you want to select a string randomly for suicide player?

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

And you might need to "pick" a string when a player suicide. Insert this line after a player press suicide keys.

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

And modify your code in

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

How about this?

Upvotes: 1

Related Questions