Advertisement
saluxx

hej

Dec 17th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4.  
  5.  
  6. namespace Game14
  7. {
  8. /// <summary>
  9. /// This is the main type for your game.
  10. /// </summary>
  11. public class Game1 : Game
  12. {
  13. GraphicsDeviceManager graphics;
  14. Texture2D ramGFX;
  15. Rectangle ramposition;
  16. Rectangle ramposition2;
  17. SpriteBatch spriteBatch;
  18. Texture2D spelareGFX;
  19. Rectangle spelarposition;
  20. Texture2D enemyGFX;
  21. Rectangle enemyposition;
  22. Texture2D bollGFX;
  23. Rectangle bollposition;
  24. Texture2D bakgrundGFX;
  25. Rectangle bakgrundposition;
  26. KeyboardState tangentbord;
  27. Vector2 bollriktning;
  28. SpriteFont score;
  29. Vector2 scorepos;
  30. Vector2 scorepos2;
  31.  
  32.  
  33. float bollhastighet;
  34. int spelarepoäng1;
  35. int spelarepoäng2;
  36. int ingame = 1;
  37. int huvudmeny = 2;
  38. string ingamez = "ingame";
  39. string menu = "huvudmeny";
  40. public string gamestate;
  41.  
  42.  
  43. public Game1()
  44. {
  45.  
  46. graphics = new GraphicsDeviceManager(this);
  47. Content.RootDirectory = "Content";
  48.  
  49. }
  50.  
  51. public void Restart()
  52. {
  53. bollriktning.X = 1;
  54. bollriktning.Y = 1;
  55. bollhastighet = 1;
  56. bollposition = new Rectangle(400 - bollGFX.Width / 2, 191, bollGFX.Width, bollGFX.Height);
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63. /// <summary>
  64. /// Allows the game to perform any initialization it needs to before starting to run.
  65. /// This is where it can query for any required services and load any non-graphic
  66. /// related content. Calling base.Initialize will enumerate through any components
  67. /// and initialize them as well.
  68. /// </summary>
  69. protected override void Initialize()
  70. {
  71. // TODO: Add your initialization logic here
  72.  
  73. gamestate = "meny";
  74.  
  75.  
  76. base.Initialize();
  77. }
  78.  
  79. /// <summary>
  80. /// LoadContent will be called once per game and is the place to load
  81. /// all of your content.
  82. /// </summary>
  83. protected override void LoadContent()
  84. {
  85. // Create a new SpriteBatch, which can be used to draw textures.
  86. spriteBatch = new SpriteBatch(GraphicsDevice);
  87.  
  88. // TODO: use this.Content to load your game content here
  89.  
  90. ramGFX = Content.Load<Texture2D>("ram2");
  91. spelareGFX = Content.Load<Texture2D>("legendfixed");
  92. enemyGFX = Content.Load<Texture2D>("legendfixed");
  93. bakgrundGFX = Content.Load<Texture2D>("petfixed");
  94. bollGFX = Content.Load<Texture2D>("heartstone");
  95.  
  96. score = Content.Load<SpriteFont>("scorefont");
  97. scorepos = new Vector2(50, 50);
  98. scorepos2 = new Vector2(713, 50);
  99.  
  100.  
  101.  
  102.  
  103. spelarposition = new Rectangle(8, 191, spelareGFX.Width, spelareGFX.Height);
  104. enemyposition = new Rectangle(762, 191, enemyGFX.Width, enemyGFX.Height);
  105. bollposition = new Rectangle(400 - bollGFX.Width / 2, 191, bollGFX.Width, bollGFX.Height);
  106. bakgrundposition = new Rectangle(0, 0, 800, 480);
  107. ramposition = new Rectangle(8, 4, 786, 20);
  108. ramposition2 = new Rectangle(8, 456, 786, 20);
  109.  
  110. bollriktning.Y = bollriktning.Y = 1;
  111. bollriktning.X = bollriktning.X = 1;
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. }
  119.  
  120. /// <summary>
  121. /// UnloadContent will be called once per game and is the place to unload
  122. /// game-specific content.
  123. /// </summary>
  124. protected override void UnloadContent()
  125. {
  126. // TODO: Unload any non ContentManager content here
  127. }
  128.  
  129. /// <summary>
  130. /// Allows the game to run logic such as updating the world,
  131. /// checking for collisions, gathering input, and playing audio.
  132. /// </summary>
  133. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  134.  
  135.  
  136. protected override void Update(GameTime gameTime)
  137. {
  138.  
  139.  
  140. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  141. Exit();
  142.  
  143. // TODO: Add your update logic here
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151. tangentbord = Keyboard.GetState();
  152. if (tangentbord.IsKeyDown(Keys.W))
  153. spelarposition.Y = spelarposition.Y - 4;
  154. if (tangentbord.IsKeyDown(Keys.S))
  155. spelarposition.Y = spelarposition.Y + 4;
  156. if (tangentbord.IsKeyDown(Keys.Up))
  157. enemyposition.Y = enemyposition.Y - 4;
  158. if (tangentbord.IsKeyDown(Keys.Down))
  159. enemyposition.Y = enemyposition.Y + 4;
  160.  
  161.  
  162.  
  163.  
  164. bollhastighet = bollhastighet + 0.01f;
  165. bollposition.X = bollposition.X + (int)(bollriktning.X * bollhastighet);
  166. bollposition.Y = bollposition.Y + (int)(bollriktning.Y * bollhastighet);
  167.  
  168.  
  169.  
  170.  
  171. if (spelarposition.Intersects(bollposition) == true)
  172. bollriktning.X = 1;
  173.  
  174.  
  175.  
  176. if (enemyposition.Intersects(bollposition) == true)
  177. bollriktning.X = -1;
  178.  
  179.  
  180.  
  181. if (spelarposition.Y < 24)
  182. spelarposition.Y = 24;
  183. if (spelarposition.Y > 359)
  184. spelarposition.Y = 359;
  185. if (enemyposition.Y < 24)
  186. enemyposition.Y = 24;
  187. if (enemyposition.Y > 359)
  188. enemyposition.Y = 359;
  189.  
  190.  
  191. if (bollposition.Y < 24)
  192. bollriktning.Y = 1;
  193. if (bollposition.Y > 456 - bollGFX.Height)
  194. bollriktning.Y = -1;
  195.  
  196. if (bollposition.X < 0)
  197. {
  198. spelarepoäng2 = spelarepoäng2 + 1;
  199. Restart();
  200. }
  201.  
  202. if (bollposition.X > 800)
  203. {
  204. spelarepoäng1 = spelarepoäng1 + 1;
  205. Restart();
  206. }
  207.  
  208. if (spelarepoäng1 == 3 || spelarepoäng2 == 3)
  209. {
  210. Restart();
  211. spelarepoäng1 = 0;
  212. spelarepoäng2 = 0;
  213. }
  214.  
  215.  
  216.  
  217. base.Update(gameTime);
  218. }
  219.  
  220.  
  221. /// <summary>
  222. /// This is called when the game should draw itself.
  223. /// </summary>
  224. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  225.  
  226.  
  227.  
  228.  
  229.  
  230. protected override void Draw(GameTime gameTime)
  231. {
  232. GraphicsDevice.Clear(Color.CornflowerBlue);
  233.  
  234. // TODO: Add your drawing code here
  235.  
  236. string poäng1 = spelarepoäng1.ToString();
  237. string poäng2 = spelarepoäng2.ToString();
  238.  
  239. spriteBatch.Begin();
  240. spriteBatch.Draw(bakgrundGFX, bakgrundposition, Color.White);
  241. spriteBatch.Draw(ramGFX, ramposition, Color.White);
  242. spriteBatch.Draw(ramGFX, ramposition2, Color.White);
  243. spriteBatch.Draw(spelareGFX, spelarposition, Color.White);
  244. spriteBatch.Draw(enemyGFX, enemyposition, Color.White);
  245. spriteBatch.Draw(bollGFX, bollposition, Color.White);
  246. spriteBatch.DrawString(score, (poäng1), scorepos, Color.White);
  247. spriteBatch.DrawString(score, (poäng2), scorepos2, Color.White);
  248. spriteBatch.End();
  249.  
  250.  
  251. base.Draw(gameTime);
  252. }
  253. }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement