/// <summary> /// Calc the next Racket colliding position and move the Racket to this pos /// </summary> /// <param name="g">GameObject</param> /// <param name="b">Ball to check</param> /// <param name="r">Racket to check</param> /// <param name="dir">Racket direction</param> public static void calc(PongGame g, Ball b, Racket r, Racket.Direction dir) { Ball imag = new Ball(new Point(b.Position.X, b.Position.Y), new Size(50, 50)); imag.dx = int.Parse(b.dx.ToString()); imag.dy = int.Parse(b.dy.ToString()); while (imag.isCollidingWallEX(g.gameRenderSize) != dir) { imag.Move(0.2d, 1); } // Console.WriteLine("while ended colliding: " + imag.Position.ToString()); _ii.Add(imag.Position.Y); int i = imag.Position.Y - (int)(imag.Size.Height); ///0.5f // Console.WriteLine("moving to: " + i); r.MoveTo(i, g.gameRenderSize, g.speed); }
/// <summary> /// Checks collisions with Racket /// </summary> /// <param name="b">used ball</param> /// <param name="racket">Racket to check</param> /// <param name="dir">RacketDirection</param> /// <param name="calc">Auto play second racket</param> public void checkColision(Ball b, Racket racket, Racket.Direction dir, bool calc = false) { int rackTop = racket.Position.Y - b.Size.Height / 2; int rackBot = racket.Position.Y + racket.Size.Height + b.Size.Height / 2; //Console.WriteLine("{0}/{1}", rackTop, rackBot); if (dir == Racket.Direction.Right) { if (b.Position.X >= (racket.Position.X - b.Size.Width)) { // Console.WriteLine("LOOG {0}>={1}", b.Position.X, racket.Position.X - (b.Size.Width / 2) - racket.Size.Width); if (b.Position.Y >= rackTop && b.Position.Y <= rackBot) //evntl. toleranzzone berechnen { b.dx *= -1; b.Position.X = b.Position.X - 5; Console.WriteLine("Collided right:" + b.Position.ToString()); if (calc) { var dispatcherTimer = new Timer { Interval = 500 }; dispatcherTimer.Tick += (sender, args) => { var timer = sender as Timer; if (timer != null) { timer.Stop(); } Console.WriteLine("Calc pos"); Ball.calc(this, ball.Clone(), leftRacket, Racket.Direction.Left); }; dispatcherTimer.Start(); } } } } else if (dir == Racket.Direction.Left) { if (b.Position.X <= (racket.Position.X + b.Size.Width / 12)) { if (b.Position.Y >= rackTop && b.Position.Y <= rackBot) { b.dx *= -1; b.Position.X = b.Position.X + 5; Console.WriteLine("Collided left:" + b.Position.ToString()); this.calc = false; if (calc) { var dispatcherTimer = new Timer { Interval = 500 }; dispatcherTimer.Tick += (sender, args) => { var timer = sender as Timer; if (timer != null) { timer.Stop(); } Console.WriteLine("Calc pos"); Ball.calc(this, ball.Clone(), rightRacket, Racket.Direction.Right); }; dispatcherTimer.Start(); } } } } if (racket.Position.Y <= 0) { racket.Position.Y = 0; } if (racket.Position.Y >= (gameRenderSize.Height - racket.Size.Height)) { racket.Position.Y = gameRenderSize.Height - racket.Size.Height; } }