Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using System;
- using System.Collections.Generic;
- namespace pong__
- {
- public class Game1 : Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch sb;
- Player player = new Player();
- Ball ball = new Ball();
- Texture2D playerT;
- int points;
- Random random = new Random();
- SpriteFont font;
- List<Blocks> lista = new List<Blocks>();
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- protected override void Initialize()
- {
- graphics.PreferredBackBufferHeight = 600;
- graphics.PreferredBackBufferWidth = 800;
- graphics.ApplyChanges();
- base.Initialize();
- }
- protected override void LoadContent()
- {
- sb = new SpriteBatch(GraphicsDevice);
- playerT = Content.Load<Texture2D>("player");
- font = Content.Load<SpriteFont>("font");
- dodaj(random.Next(4, 9), random.Next(1, 100), 50);
- dodaj(random.Next(4, 9), random.Next(1, 100), 100);
- }
- protected override void UnloadContent()
- {
- }
- void dodaj(int i, int x, int y)
- {
- int x_ = x;
- for (int i_ = 0; i_ < i; i_++)
- {
- Blocks x1 = new Blocks(new Rectangle(x_, y, 32, 32));
- x_ += random.Next(50, 150);
- lista.Add(x1);
- }
- }
- protected override void Update(GameTime gameTime)
- {
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
- Exit();
- player.Update();
- ball.Update();
- //logika
- foreach(var x in lista)
- {
- if (ball.rect.Intersects(x.rect))
- {
- points++;
- if (ball.rect.Top <= x.rect.Bottom)
- ball.velocity.Y = ball.speed;
- else if (ball.rect.Bottom >= x.rect.Top)
- ball.velocity.Y = -ball.speed;
- if (ball.rect.Right >= x.rect.Left)
- ball.velocity.X = -ball.speed;
- else if (ball.rect.Left <= x.rect.Right)
- ball.velocity.X = ball.speed;
- }
- };
- if (ball.rect.Intersects(player.rect))
- {
- ball.velocity.Y = -ball.speed;
- ball.speed += (float)random.NextDouble();
- }
- // Tutaj występuje problem
- lista.ForEach(x =>
- {
- if (ball.rect.Intersects(x.rect))
- {
- lista.Remove(x);
- }
- });
- // Koniec problemu
- //////////
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- sb.Begin();
- sb.Draw(playerT, player.rect, Color.White);
- sb.Draw(playerT, ball.rect, Color.White);
- sb.DrawString(font, string.Format("Points {0}", points), new Vector2(0, 0), Color.Black);
- foreach (var x in lista)
- {
- sb.Draw(playerT, x.rect, Color.White);
- }
- sb.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement