如何绘制一个随机字符串在XNA时,它的调用
本文关键字:XNA 调用 字符串 一个 何绘制 绘制 随机 | 更新日期: 2023-09-27 18:08:27
我简直不敢相信我被困在这上面,目前我的播放器代码允许玩家四处走动,甚至允许玩家在按下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);
}
随机是如何工作的?我怎么在这里实现它呢?
编辑:我已经尝试实现你所说的,我得到一个错误'不能隐式转换'字符串'到'字符串[]'这很奇怪,我认为这是我想把这个,它去哪里?
编辑2:对不起,我打错了,我只是在第一个字符串测试后注意到'[]' .
最后:在玩弄了你的代码之后,我终于让它按照我的期望工作了,为了连续性,我还必须修改几个变量。
我在构造函数中添加了这个:
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)];
}
如果自杀变量等于true,我就画好了
我认为我所做的是告诉游戏在玩家自杀时绘制文本,每次玩家点击/自杀时,它都会更新字符串。这是正确的想法吗?
根据@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);
}
这个怎么样?