Advertisement
Guest User

Untitled

a guest
Feb 12th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4. using System;
  5. using System.Collections.Generic;
  6.  
  7. namespace pong__
  8. {
  9.  
  10. public class Game1 : Game
  11. {
  12. GraphicsDeviceManager graphics;
  13. SpriteBatch sb;
  14.  
  15. Player player = new Player();
  16. Ball ball = new Ball();
  17. Texture2D playerT;
  18.  
  19. int points;
  20.  
  21. Random random = new Random();
  22.  
  23. SpriteFont font;
  24.  
  25.  
  26. List<Blocks> lista = new List<Blocks>();
  27.  
  28. public Game1()
  29. {
  30. graphics = new GraphicsDeviceManager(this);
  31. Content.RootDirectory = "Content";
  32. }
  33.  
  34.  
  35. protected override void Initialize()
  36. {
  37. graphics.PreferredBackBufferHeight = 600;
  38. graphics.PreferredBackBufferWidth = 800;
  39. graphics.ApplyChanges();
  40.  
  41. base.Initialize();
  42. }
  43.  
  44. protected override void LoadContent()
  45. {
  46. sb = new SpriteBatch(GraphicsDevice);
  47.  
  48.  
  49. playerT = Content.Load<Texture2D>("player");
  50.  
  51. font = Content.Load<SpriteFont>("font");
  52.  
  53. dodaj(random.Next(4, 9), random.Next(1, 100), 50);
  54. dodaj(random.Next(4, 9), random.Next(1, 100), 100);
  55.  
  56.  
  57.  
  58.  
  59. }
  60.  
  61. protected override void UnloadContent()
  62. {
  63.  
  64. }
  65.  
  66.  
  67. void dodaj(int i, int x, int y)
  68. {
  69. int x_ = x;
  70. for (int i_ = 0; i_ < i; i_++)
  71. {
  72.  
  73. Blocks x1 = new Blocks(new Rectangle(x_, y, 32, 32));
  74. x_ += random.Next(50, 150);
  75. lista.Add(x1);
  76. }
  77. }
  78.  
  79. protected override void Update(GameTime gameTime)
  80. {
  81. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  82. Exit();
  83.  
  84.  
  85. player.Update();
  86. ball.Update();
  87.  
  88.  
  89.  
  90. //logika
  91.  
  92. foreach(var x in lista)
  93. {
  94. if (ball.rect.Intersects(x.rect))
  95. {
  96.  
  97. points++;
  98.  
  99.  
  100.  
  101. if (ball.rect.Top <= x.rect.Bottom)
  102. ball.velocity.Y = ball.speed;
  103. else if (ball.rect.Bottom >= x.rect.Top)
  104. ball.velocity.Y = -ball.speed;
  105.  
  106. if (ball.rect.Right >= x.rect.Left)
  107. ball.velocity.X = -ball.speed;
  108. else if (ball.rect.Left <= x.rect.Right)
  109. ball.velocity.X = ball.speed;
  110. }
  111.  
  112. };
  113.  
  114.  
  115. if (ball.rect.Intersects(player.rect))
  116. {
  117. ball.velocity.Y = -ball.speed;
  118. ball.speed += (float)random.NextDouble();
  119. }
  120.  
  121. // Tutaj występuje problem
  122. lista.ForEach(x =>
  123. {
  124. if (ball.rect.Intersects(x.rect))
  125. {
  126. lista.Remove(x);
  127. }
  128. });
  129. // Koniec problemu
  130.  
  131.  
  132. //////////
  133.  
  134.  
  135.  
  136.  
  137. base.Update(gameTime);
  138. }
  139.  
  140.  
  141. protected override void Draw(GameTime gameTime)
  142. {
  143. GraphicsDevice.Clear(Color.CornflowerBlue);
  144.  
  145.  
  146. sb.Begin();
  147.  
  148. sb.Draw(playerT, player.rect, Color.White);
  149. sb.Draw(playerT, ball.rect, Color.White);
  150.  
  151. sb.DrawString(font, string.Format("Points {0}", points), new Vector2(0, 0), Color.Black);
  152.  
  153. foreach (var x in lista)
  154. {
  155. sb.Draw(playerT, x.rect, Color.White);
  156. }
  157.  
  158. sb.End();
  159.  
  160.  
  161.  
  162. base.Draw(gameTime);
  163. }
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement