Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Mouse Goto Location Game

By: a guest on Mar 12th, 2013  |  syntax: C#  |  size: 3.45 KB  |  views: 206  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. ///
  13. /// A simple example to make object move where the mouse is clicked
  14. ///
  15. namespace GotoLocation
  16. {
  17. #if WINDOWS || XBOX
  18.     static class Program
  19.     {
  20.         /// <summary>
  21.         /// The main entry point for the application.
  22.         /// </summary>
  23.         static void Main(string[] args)
  24.         {
  25.             using (Game1 game = new Game1())
  26.             {
  27.                 game.Run();
  28.             }
  29.         }
  30.     }
  31. #endif
  32.  
  33.     public class Game1 : Microsoft.Xna.Framework.Game
  34.     {
  35.         GraphicsDeviceManager graphics;
  36.         SpriteBatch spriteBatch;
  37.  
  38.         Vector2 direction = Vector2.Zero;
  39.         Vector2 position = Vector2.Zero;
  40.         float speed = 2f;
  41.  
  42.         public Game1()
  43.         {
  44.             graphics = new GraphicsDeviceManager(this);
  45.             Content.RootDirectory = "Content";
  46.  
  47.             this.IsMouseVisible = true;
  48.         }
  49.  
  50.         protected override void Initialize()
  51.         {
  52.             base.Initialize();
  53.         }
  54.  
  55.         protected override void LoadContent()
  56.         {
  57.             spriteBatch = new SpriteBatch(GraphicsDevice);
  58.  
  59.             position.X = (graphics.PreferredBackBufferWidth / 2) - (Content.Load<Texture2D>("dot").Width / 2);
  60.             position.Y = (graphics.PreferredBackBufferHeight / 2) - (Content.Load<Texture2D>("dot").Height / 2);
  61.  
  62.             direction = position;
  63.         }
  64.  
  65.         protected override void UnloadContent()
  66.         {
  67.         }
  68.  
  69.         protected override void Update(GameTime gameTime)
  70.         {
  71.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  72.                 this.Exit();
  73.  
  74.  
  75.             if (Mouse.GetState().LeftButton == ButtonState.Pressed)
  76.             {
  77.                 direction = new Vector2(Mouse.GetState().X-(Content.Load<Texture2D>("dot").Width / 2), Mouse.GetState().Y-(Content.Load<Texture2D>("dot").Height / 2));
  78.             }
  79.  
  80.             Vector2 move = Helper_Direction.MoveTowards(position, direction, speed);
  81.  
  82.             position.X += move.X;
  83.             position.Y += move.Y;
  84.  
  85.             base.Update(gameTime);
  86.         }
  87.  
  88.         protected override void Draw(GameTime gameTime)
  89.         {
  90.             GraphicsDevice.Clear(Color.CornflowerBlue);
  91.  
  92.             spriteBatch.Begin();
  93.  
  94.             spriteBatch.Draw(Content.Load<Texture2D>("dot"), position, Color.White);
  95.  
  96.             spriteBatch.End();
  97.  
  98.             base.Draw(gameTime);
  99.         }
  100.     }
  101.  
  102.     public static class Helper_Direction
  103.     {
  104.         public static double FaceObject(Vector2 position, Vector2 target)
  105.         {
  106.             return (Math.Atan2(position.Y - target.Y, position.X - target.X) * (180 / Math.PI));
  107.         }
  108.  
  109.         public static Vector2 MoveTowards(Vector2 position, Vector2 target, float speed)
  110.         {
  111.             double direction = (float)(Math.Atan2(target.Y - position.Y, target.X - position.X) * 180 / Math.PI);
  112.  
  113.             Vector2 move = new Vector2(0, 0);
  114.  
  115.             move.X = (float)Math.Cos(direction * Math.PI / 180) * speed;
  116.             move.Y = (float)Math.Sin(direction * Math.PI / 180) * speed;
  117.  
  118.             return move;
  119.         }
  120.     }
  121. }