0

我不敢相信我被困在这个问题上,目前我的播放器代码允许玩家四处走动,它甚至允许玩家在按下 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'转换为'string []'这很奇怪,我认为它是我试图把它放在哪里,它去哪里了?

EDIT2:对不起,我的错字,我只是在第一次字符串测试后注意到'[]'..

Final:在玩弄了你的代码之后,我终于让它达到了我的预期,为了保持连续性,我还不得不修改几个变量。

我在构造函数中添加了这个:

    String suicideStr;
    public bool suicide;

并将其放入 LoadContent(); 方法:

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

将此布尔值添加到 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)];
        }

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

我所做的是告诉游戏在玩家自杀时绘制文本,并且每次玩家点击/自杀时它都会更新字符串。这是正确的想法吗?

4

1 回答 1

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

这个怎么样?

于 2013-04-10T03:33:08.930 回答