Game1.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Content;
  5. using Microsoft.Xna.Framework.GamerServices;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8. using Microsoft.Xna.Framework.Storage;
  9. namespace Microsoft.Xna.Samples.VirtualGamePad
  10. {
  11. /// <summary>
  12. /// This is the main type for your game
  13. /// </summary>
  14. public class Game1 : Microsoft.Xna.Framework.Game
  15. {
  16. GraphicsDeviceManager graphics;
  17. SpriteBatch spriteBatch;
  18. Texture2D texture, caracter;
  19. Vector2 position = new Vector2();
  20. Color caracterColor = Color.White;
  21. SpriteFont font;
  22. public Game1()
  23. {
  24. graphics = new GraphicsDeviceManager(this);
  25. Content.RootDirectory = "Content";
  26. graphics.IsFullScreen = true;
  27. }
  28. /// <summary>
  29. /// Allows the game to perform any initialization it needs to before starting to run.
  30. /// This is where it can query for any required services and load any non-graphic
  31. /// related content. Calling base.Initialize will enumerate through any components
  32. /// and initialize them as well.
  33. /// </summary>
  34. protected override void Initialize()
  35. {
  36. // TODO: Add your initialization logic here
  37. // Set the initial mouse position
  38. //Mouse.SetPosition(160,240);
  39. base.Initialize();
  40. }
  41. /// <summary>
  42. /// LoadContent will be called once per game and is the place to load
  43. /// all of your content.
  44. /// </summary>
  45. protected override void LoadContent()
  46. {
  47. // Create a new SpriteBatch, which can be used to draw textures.
  48. spriteBatch = new SpriteBatch(GraphicsDevice);
  49. // TODO: use this.Content to load your game content here
  50. texture = Content.Load<Texture2D>("gamepad.png");
  51. caracter = Content.Load<Texture2D>("monogameicon");
  52. font = Content.Load<SpriteFont>("font");
  53. // Set the virtual GamePad
  54. ButtonDefinition BButton = new ButtonDefinition();
  55. BButton.Texture = texture;
  56. BButton.Position = new Vector2(200,150);
  57. BButton.Type = Buttons.B;
  58. BButton.TextureRect = new Rectangle(72,77,36,36);
  59. ButtonDefinition AButton = new ButtonDefinition();
  60. AButton.Texture = texture;
  61. AButton.Position = new Vector2(150,150);
  62. AButton.Type = Buttons.A;
  63. AButton.TextureRect = new Rectangle(73,114,36,36);
  64. GamePad.ButtonsDefinitions.Add(BButton);
  65. GamePad.ButtonsDefinitions.Add(AButton);
  66. ThumbStickDefinition thumbStick = new ThumbStickDefinition();
  67. thumbStick.Position = new Vector2(200,200);
  68. thumbStick.Texture = texture;
  69. thumbStick.TextureRect = new Rectangle(2,2,68,68);
  70. GamePad.LeftThumbStickDefinition = thumbStick;
  71. }
  72. /// <summary>
  73. /// Allows the game to run logic such as updating the world,
  74. /// checking for collisions, gathering input, and playing audio.
  75. /// </summary>
  76. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  77. protected override void Update(GameTime gameTime)
  78. {
  79. // Allows the game to exit
  80. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  81. this.Exit();
  82. // TODO: Add your update logic here
  83. if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
  84. caracterColor = Color.Green;
  85. if (GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed)
  86. caracterColor = Color.Red;
  87. // change the caracter position and center in touch
  88. //MouseState mouse = Mouse.GetState();
  89. //position.X = mouse.X - (caracter.Width/2);
  90. //position.Y = mouse.Y - (caracter.Height/2);
  91. // change the caracter position using thumbstick
  92. GamePadState gamepastatus = GamePad.GetState(PlayerIndex.One);
  93. position.Y += (int) (gamepastatus.ThumbSticks.Left.Y * -4);
  94. position.X += (int) (gamepastatus.ThumbSticks.Left.X * 4);
  95. // change the caracter position using accelerometer
  96. //position.Y += (int)(Accelerometer.GetState().Acceleration.Y * -4);
  97. //position.X += (int)(Accelerometer.GetState().Acceleration.X * 4);
  98. // Keep inside the screen
  99. // right
  100. if(position.X + caracter.Width > Window.ClientBounds.Width)
  101. {
  102. position.X = Window.ClientBounds.Width - caracter.Width;
  103. }
  104. // bottom
  105. if (position.Y + caracter.Height > Window.ClientBounds.Height)
  106. {
  107. position.Y = Window.ClientBounds.Height - caracter.Height;
  108. }
  109. // left
  110. if (position.X < 0)
  111. {
  112. position.X = 0;
  113. }
  114. // top
  115. if (position.Y < 0)
  116. {
  117. position.Y = 0;
  118. }
  119. base.Update(gameTime);
  120. }
  121. /// <summary>
  122. /// This is called when the game should draw itself.
  123. /// </summary>
  124. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  125. protected override void Draw(GameTime gameTime)
  126. {
  127. graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
  128. spriteBatch.Begin();
  129. spriteBatch.Draw(caracter,position,caracterColor);
  130. spriteBatch.DrawString(font,GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.ToString(),Vector2.One,Color.Black);
  131. spriteBatch.DrawString(font,Accelerometer.GetState().Acceleration.ToString(),new Vector2(1,40),Color.Black);
  132. // Draw the virtual GamePad
  133. GamePad.Draw(gameTime,spriteBatch);
  134. spriteBatch.End();
  135. base.Draw(gameTime);
  136. }
  137. }
  138. }