Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System; // ez
  5.  
  6. namespace SpaceShooter
  7. {
  8. /// <summary>
  9. /// This is the main type for your game.
  10. /// </summary>
  11. public class Game1 : Game
  12. {
  13. GraphicsDeviceManager graphics;
  14. SpriteBatch SpriteBatch;
  15. Texture2D Ship;
  16. Vector2 ShipPos;
  17. Vector2 ShipPosAi;
  18. bool ShipColor = false;
  19. Random rng = new Random();
  20. public Game1()
  21. {
  22. graphics = new GraphicsDeviceManager(this);
  23. Content.RootDirectory = "Content";
  24. }
  25.  
  26. /// <summary>
  27. /// Allows the game to perform any initialization it needs to before starting to run.
  28. /// This is where it can query for any required services and load any non-graphic
  29. /// related content. Calling base.Initialize will enumerate through any components
  30. /// and initialize them as well.
  31. /// </summary>
  32. protected override void Initialize()
  33. {
  34. // TODO: Add your initialization logic here
  35.  
  36. ShipPos = new Vector2(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2);
  37. ShipPosAi = new Vector2(Window.ClientBounds.Width, Window.ClientBounds.Height);
  38. base.Initialize();
  39.  
  40. }
  41.  
  42. /// <summary>
  43. /// LoadContent will be called once per game and is the place to load
  44. /// all of your content.
  45. /// </summary>
  46. protected override void LoadContent()
  47. {
  48. // Create a new SpriteBatch, which can be used to draw textures.
  49. SpriteBatch = new SpriteBatch(GraphicsDevice);
  50. Ship = Content.Load<Texture2D>("images/ship");
  51.  
  52. // TODO: use this.Content to load your game content here
  53. }
  54.  
  55. /// <summary>
  56. /// UnloadContent will be called once per game and is the place to unload
  57. /// game-specific content.
  58. /// </summary>
  59. protected override void UnloadContent()
  60. {
  61. // TODO: Unload any non ContentManager content here
  62. }
  63.  
  64. /// <summary>
  65. /// Allows the game to run logic such as updating the world,
  66. /// checking for collisions, gathering input, and playing audio.
  67. /// </summary>
  68. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  69. protected override void Update(GameTime gameTime)
  70. {
  71. // Spelare
  72. KeyboardState KeyboardState = Keyboard.GetState();
  73. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  74. Exit();
  75.  
  76. if (KeyboardState.IsKeyDown(Keys.W) && !(ShipPos.Y <= 0))
  77. {
  78. ShipPos.Y -= 3f;
  79. }
  80. if (KeyboardState.IsKeyDown(Keys.S) && !(ShipPos.Y >= Window.ClientBounds.Height - (int)Ship.Height))
  81. {
  82. ShipPos.Y += 3f;
  83. }
  84. if (KeyboardState.IsKeyDown(Keys.A) && !(ShipPos.X <= 0))
  85. {
  86. ShipPos.X -= 3f;
  87. }
  88. if (KeyboardState.IsKeyDown(Keys.D) && !(ShipPos.X >= Window.ClientBounds.Width - (int)Ship.Width))
  89. {
  90. ShipPos.X += 3f;
  91. }
  92.  
  93. if (KeyboardState.IsKeyDown(Keys.E))
  94. {
  95. if (ShipColor == false)
  96. {
  97. ShipColor = true;
  98. }
  99. else if (ShipColor == true)
  100. {
  101. ShipColor = false;
  102. }
  103. // AI
  104. int dir = rng.Next(1,5);
  105. if (dir == 1)
  106. {
  107. ShipPosAi.Y -= 2f;
  108. }
  109. if (dir == 2)
  110. {
  111. ShipPosAi.Y += 2f;
  112. }
  113. if (dir == 3)
  114. {
  115. ShipPosAi.X -= 2f;
  116. }
  117. if (dir == 4)
  118. {
  119. ShipPosAi.Y += 2f;
  120. }
  121. }
  122. // TODO: Add your update logic here
  123.  
  124. base.Update(gameTime);
  125. }
  126.  
  127. /// <summary>
  128. /// This is called when the game should draw itself.
  129. /// </summary>
  130. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  131. protected override void Draw(GameTime gameTime)
  132. {
  133. GraphicsDevice.Clear(Color.HotPink);
  134. SpriteBatch.Begin();
  135. SpriteBatch.Draw(Ship, ShipPosAi, Color.White);
  136. if (ShipColor == false)
  137. {
  138. SpriteBatch.Draw(Ship, ShipPos, Color.Green);
  139. }
  140. else
  141. {
  142. SpriteBatch.Draw(Ship, ShipPos, Color.Red);
  143. }
  144.  
  145.  
  146. SpriteBatch.End();
  147. // TODO: Add your drawing code here
  148.  
  149. base.Draw(gameTime);
  150. }
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement