Advertisement
saluxx

Pong game15

Jan 13th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.17 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 Game15
  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 menyGFX;
  19. Texture2D spelareGFX;
  20. Rectangle spelarposition;
  21. Texture2D enemyGFX;
  22. Rectangle enemyposition;
  23. Texture2D bollGFX;
  24. Rectangle bollposition;
  25. Texture2D bakgrundGFX;
  26. Rectangle bakgrundposition;
  27. KeyboardState tangentbord;
  28. Vector2 bollriktning;
  29. Texture2D win1gfx;
  30. Texture2D win2gfx;
  31. SpriteFont rubrik;
  32. SpriteFont vanligtext;
  33. SpriteFont score;
  34. Vector2 scorepos;
  35. Vector2 scorepos2;
  36.  
  37.  
  38.  
  39. float bollhastighet;
  40. int spelarepoäng1;
  41. int spelarepoäng2;
  42. string ingamez = "ingamez";
  43. string menu = "menu";
  44. string gameover = "gameover";
  45. string gameover2 = "gameover2";
  46. public string gamestate;
  47.  
  48.  
  49. public Game1()
  50. {
  51. graphics = new GraphicsDeviceManager(this);
  52. Content.RootDirectory = "Content";
  53. }
  54.  
  55. public void Restart()
  56. {
  57. bollriktning.X = 1;
  58. bollriktning.Y = 1;
  59. bollhastighet = 1;
  60. bollposition = new Rectangle(400 - bollGFX.Width / 2, 191, bollGFX.Width, bollGFX.Height);
  61. }
  62.  
  63. public void ingamestate()
  64. {
  65. tangentbord = Keyboard.GetState();
  66. if (tangentbord.IsKeyDown(Keys.W))
  67. spelarposition.Y = spelarposition.Y - 4;
  68. if (tangentbord.IsKeyDown(Keys.S))
  69. spelarposition.Y = spelarposition.Y + 4;
  70. if (tangentbord.IsKeyDown(Keys.Up))
  71. enemyposition.Y = enemyposition.Y - 4;
  72. if (tangentbord.IsKeyDown(Keys.Down))
  73. enemyposition.Y = enemyposition.Y + 4;
  74.  
  75. bollhastighet = bollhastighet + 0.01f;
  76. bollposition.X = bollposition.X + (int)(bollriktning.X * bollhastighet);
  77. bollposition.Y = bollposition.Y + (int)(bollriktning.Y * bollhastighet);
  78.  
  79. if (spelarposition.Intersects(bollposition) == true)
  80. bollriktning.X = 1;
  81. if (enemyposition.Intersects(bollposition) == true)
  82. bollriktning.X = -1;
  83.  
  84. if (spelarposition.Y < 24)
  85. spelarposition.Y = 24;
  86. if (spelarposition.Y > 359)
  87. spelarposition.Y = 359;
  88. if (enemyposition.Y < 24)
  89. enemyposition.Y = 24;
  90. if (enemyposition.Y > 359)
  91. enemyposition.Y = 359;
  92.  
  93. if (bollposition.Y < 24)
  94. bollriktning.Y = 1;
  95. if (bollposition.Y > 456 - bollGFX.Height)
  96. bollriktning.Y = -1;
  97.  
  98. if (bollposition.X < 0)
  99. {
  100. spelarepoäng2 = spelarepoäng2 + 1;
  101. Restart();
  102. }
  103.  
  104. if (bollposition.X > 800)
  105. {
  106. spelarepoäng1 = spelarepoäng1 + 1;
  107. Restart();
  108. }
  109.  
  110.  
  111.  
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118. /// <summary>
  119. /// Allows the game to perform any initialization it needs to before starting to run.
  120. /// This is where it can query for any required services and load any non-graphic
  121. /// related content. Calling base.Initialize will enumerate through any components
  122. /// and initialize them as well.
  123. /// </summary>
  124. protected override void Initialize()
  125. {
  126. // TODO: Add your initialization logic here
  127.  
  128. gamestate = menu;
  129.  
  130.  
  131. base.Initialize();
  132. }
  133.  
  134. /// <summary>
  135. /// LoadContent will be called once per game and is the place to load
  136. /// all of your content.
  137. /// </summary>
  138. protected override void LoadContent()
  139. {
  140. // Create a new SpriteBatch, which can be used to draw textures.
  141. spriteBatch = new SpriteBatch(GraphicsDevice);
  142.  
  143. // TODO: use this.Content to load your game content here
  144.  
  145. ramGFX = Content.Load<Texture2D>("ram2");
  146. spelareGFX = Content.Load<Texture2D>("legendfixed");
  147. enemyGFX = Content.Load<Texture2D>("legendfixed");
  148. bakgrundGFX = Content.Load<Texture2D>("bluetexture");
  149. bollGFX = Content.Load<Texture2D>("heartstone");
  150. menyGFX = Content.Load<Texture2D>("spacegfx");
  151. win1gfx = Content.Load<Texture2D>("hny1");
  152. win2gfx = Content.Load<Texture2D>("hny2");
  153.  
  154. rubrik = Content.Load<SpriteFont>("Menyfont");
  155. vanligtext = Content.Load<SpriteFont>("normaltextfont");
  156. score = Content.Load<SpriteFont>("scorefont");
  157. scorepos = new Vector2(50, 50);
  158. scorepos2 = new Vector2(713, 50);
  159.  
  160.  
  161.  
  162.  
  163. spelarposition = new Rectangle(8, 191, spelareGFX.Width, spelareGFX.Height);
  164. enemyposition = new Rectangle(762, 191, enemyGFX.Width, enemyGFX.Height);
  165. bollposition = new Rectangle(400 - bollGFX.Width / 2, 191, bollGFX.Width, bollGFX.Height);
  166. bakgrundposition = new Rectangle(0, 0, 800, 480);
  167. ramposition = new Rectangle(8, 4, 786, 20);
  168. ramposition2 = new Rectangle(8, 456, 786, 20);
  169.  
  170.  
  171. bollriktning.Y = bollriktning.Y = 1;
  172. bollriktning.X = bollriktning.X = 1;
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. }
  180.  
  181. /// <summary>
  182. /// UnloadContent will be called once per game and is the place to unload
  183. /// game-specific content.
  184. /// </summary>
  185. protected override void UnloadContent()
  186. {
  187. // TODO: Unload any non ContentManager content here
  188. }
  189.  
  190. /// <summary>
  191. /// Allows the game to run logic such as updating the world,
  192. /// checking for collisions, gathering input, and playing audio.
  193. /// </summary>
  194. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  195.  
  196.  
  197. protected override void Update(GameTime gameTime)
  198. {
  199.  
  200.  
  201. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  202. Exit();
  203.  
  204. // TODO: Add your update logic here
  205.  
  206. if (gamestate == ingamez)
  207. {
  208. ingamestate();
  209. }
  210.  
  211. if (spelarepoäng1 == 3)
  212. {
  213. gamestate = gameover;
  214. }
  215. if (spelarepoäng2 == 3)
  216. {
  217. gamestate = gameover2;
  218. }
  219.  
  220.  
  221.  
  222.  
  223. tangentbord = Keyboard.GetState();
  224. if (tangentbord.IsKeyDown(Keys.Enter))
  225. gamestate = ingamez;
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232. base.Update(gameTime);
  233. }
  234.  
  235.  
  236. /// <summary>
  237. /// This is called when the game should draw itself.
  238. /// </summary>
  239. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  240.  
  241.  
  242.  
  243.  
  244.  
  245. protected override void Draw(GameTime gameTime)
  246. {
  247. GraphicsDevice.Clear(Color.CornflowerBlue);
  248.  
  249. // TODO: Add your drawing code here
  250.  
  251. string poäng1 = spelarepoäng1.ToString();
  252. string poäng2 = spelarepoäng2.ToString();
  253.  
  254. if (gamestate == ingamez)
  255. {
  256. spriteBatch.Begin();
  257. spriteBatch.Draw(bakgrundGFX, bakgrundposition, Color.White);
  258. spriteBatch.Draw(ramGFX, ramposition, Color.White);
  259. spriteBatch.Draw(ramGFX, ramposition2, Color.White);
  260. spriteBatch.Draw(spelareGFX, spelarposition, Color.White);
  261. spriteBatch.Draw(enemyGFX, enemyposition, Color.White);
  262. spriteBatch.Draw(bollGFX, bollposition, Color.White);
  263. spriteBatch.DrawString(score, (poäng1), scorepos, Color.White);
  264. spriteBatch.DrawString(score, (poäng2), scorepos2, Color.White);
  265. spriteBatch.End();
  266. }
  267.  
  268. if (gamestate == menu)
  269. {
  270. spriteBatch.Begin();
  271. spriteBatch.Draw(menyGFX, bakgrundposition, Color.White);
  272. spriteBatch.DrawString(rubrik, "Welcome to Pong", new Vector2(273, 30), Color.Orange);
  273. spriteBatch.DrawString(vanligtext, "Player 1 controls: W = up S = down", new Vector2(197, 130), Color.Orange);
  274. spriteBatch.DrawString(vanligtext, "Player 2 controls: Upperarrow = up Downarrow = down", new Vector2(93, 180), Color.Orange);
  275. spriteBatch.DrawString(vanligtext, "Press enter to start the game", new Vector2(236, 400), Color.White);
  276. spriteBatch.End();
  277. }
  278.  
  279. if (gamestate == gameover)
  280. {
  281. spriteBatch.Begin();
  282. spriteBatch.Draw(win1gfx, bakgrundposition, Color.White);
  283. spriteBatch.DrawString(vanligtext, "Press enter to start the game", new Vector2(236, 400), Color.White);
  284. spelarepoäng1 = 0;
  285. spelarepoäng2 = 0;
  286. spelarposition = new Rectangle(8, 191, spelareGFX.Width, spelareGFX.Height);
  287. enemyposition = new Rectangle(762, 191, enemyGFX.Width, enemyGFX.Height);
  288. spriteBatch.End();
  289. }
  290. if (gamestate == gameover2)
  291. {
  292. spriteBatch.Begin();
  293. spriteBatch.Draw(win2gfx, bakgrundposition, Color.White);
  294. spriteBatch.DrawString(vanligtext, "Press enter to start the game", new Vector2(236, 400), Color.White);
  295. spelarepoäng1 = 0;
  296. spelarepoäng2 = 0;
  297. spelarposition = new Rectangle(8, 191, spelareGFX.Width, spelareGFX.Height);
  298. enemyposition = new Rectangle(762, 191, enemyGFX.Width, enemyGFX.Height);
  299. spriteBatch.End();
  300. }
  301.  
  302.  
  303. base.Draw(gameTime);
  304. }
  305. }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement