XNA実践マウス編--クリック座標に基づいて画像を移動(4)下
5113 ワード
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
/// <summary>
///
/// </summary>
private Vector2 picPosition;
/// <summary>
///
/// </summary>
private Vector2 CurrentPosition;
/// <summary>
///
/// </summary>
private Vector2 MovePix;
/// <summary>
/// 2D
/// </summary>
private Texture2D texture;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;//
//
picPosition = new Vector2();
MovePix = new Vector2(1.0f, 1.0f);//
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
texture = Texture2D.FromFile(graphics.GraphicsDevice, "test.jpg");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
MouseState mouseState = Mouse.GetState();
if (mouseState.LeftButton == ButtonState.Pressed)
{
Viewport vp = graphics.GraphicsDevice.Viewport;//
if (mouseState.X < vp.X || mouseState.X > vp.X + vp.Width || mouseState.Y < vp.Y || mouseState.Y > vp.Y + vp.Height)
{
//
}
else
{
if (CurrentPosition.X != mouseState.X || CurrentPosition.Y != mouseState.Y)
{
CurrentPosition.X = mouseState.X;
CurrentPosition.Y = mouseState.Y;
}
}
}
Mover();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(texture, picPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
/// <summary>
///
/// </summary>
void Mover()
{
Vector2 v = picPosition - CurrentPosition;//
if (v.X > 0)
picPosition.X -= MovePix.X;//
else if (v.X + texture.Width < 0)
picPosition.X += MovePix.X;//
if (v.Y > 0)
picPosition.Y -= MovePix.Y;//
else if (v.Y + texture.Height < 0)
picPosition.Y += MovePix.Y;//
}
}
画像はやはり前回の2次元の画像を使って、今非画像のカバー区でマウスの左ボタンを押してマウスを動かして見て、画像はマウスの方向についてゆっくりと移動したのではないでしょうか.ここを見て、皆さんは何を思い浮かべますか.
ところで、ゲーム中のキャラクターが怪の位置を追跡していますが、この図にはF(G+H)しか現れていません.フォームには障害物がないからです.