robs0271
Why targetRadius should be "43"?
public class Game1 : Game
{
Texture2D target;
Vector2 targetPosition;
MouseState mouseState;
bool mouseReleased = true;
int score = 0;
const int targetRadius = 43;
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
target = Content.Load<Texture2D>("target");
}
protected override void Update(GameTime gameTime)
{
mouseState = Mouse.GetState();
Vector2 mousePosition = mouseState.Position.ToVector2();
float mouseTargetDist = Vector2.Distance(targetPosition, mousePosition);
if (mouseTargetDist < targetRadius && mouseState.LeftButton == ButtonState.Pressed && mouseReleased == true)
{
score++;
mouseReleased = false;
}
if (mouseState.LeftButton == ButtonState.Released)
{
mouseReleased = true;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
Vector2 targetPosition = new Vector2(100, 100);
_spriteBatch.Draw(target, new Vector2(targetPosition.X - targetRadius, targetPosition.Y - targetRadius), Color.Aquamarine);
_spriteBatch.End();
base.Draw(gameTime);
}
}
}
13 replies