private void Window_Idle(object sender, EventArgs e) { //update game data gameData.updateInfo(camera.LastFrame); //check for goals for (int i = 0; i < balls.Count; i++) { Ball ball = balls[i]; if (Helper.isGoal(ball, goals)) { //find the player who scored, increase his score int index = players.IndexOf(ball.LastHit); if (index != -1) { //make sure someone hit the ball players[index].Score++; if (Helper.gameIsOver(players[index], setup.MaxScore)) { //if the game is over, display a label Label label = new Label { Content = setup.PlayerNames[index] + " wins the game!", FontSize = 40, FontFamily = new System.Windows.Media.FontFamily("Tahoma") }; Canvas.SetLeft(label, (World.ActualWidth / 4) - label.ActualWidth); Canvas.SetTop(label, (World.ActualHeight / 2) - label.ActualHeight); World.Children.Add(label); } } // Set ball in center of world and reinitialize the speeds. if there were multiple balls, remove the ball that scored instead if (balls.Count == 1) { ResetBall(ball); gameData.updateInfo(camera.LastFrame); PauseResume(); } else { World.Children.Remove(ball.Shape); balls.Remove(ball); } } } //hand detection using (Bitmap bmp = camera.LastFrame) { if (bmp != null) { List <System.Drawing.Point> hands = Handdetection.GetHandLocations(Handdetection.imageBinarization(bmp, setup.Threshold, setup.Mirror), setup.Rectangles); System.Drawing.Size s = bmp.Size; Dispatcher.BeginInvoke(new Action(() => { for (int i = 0; i < players.Count; i++) { Player player = players[i]; //determine which coordinate should be altered if (player.Direction == 'Y') { if (hands[i].Y != 0) { double relativeValue = (double)hands[i].Y / s.Height; //if the player is affected by a reverse powerup, invert position if (player.ReverseTime > 0) { player.Beam.Y = World.ActualHeight - (relativeValue * World.ActualHeight); } else { player.Beam.Y = relativeValue * World.ActualHeight; } } } else { if (hands[i].X != 0) { double relativeValue = (double)hands[i].X / s.Width; //if the player is affected by a reverse powerup, invert position if (player.ReverseTime > 0) { player.Beam.X = World.ActualWidth - (relativeValue * World.ActualWidth); } else { player.Beam.X = relativeValue * World.ActualWidth; } } } } })); } } //ball collision Physics p = new Physics(World, setup); Ball b; for (int i = 0; i < balls.Count; i++) { b = balls[i]; System.Windows.Point[] intersection = new System.Windows.Point[3]; try { Dispatcher.Invoke(new Action(() => { intersection = p.Intersection(b); })); if (!(intersection[0].X < 0)) { b = p.BallCollisionBeam(b, intersection); } b = p.HandleSideCollisions(b); } catch (Exception) { } //ball movement try { Dispatcher.Invoke(new Action(() => { b.X += b.SpeedX; })); } catch (Exception) { } try { Dispatcher.Invoke(new Action(() => { b.Y += b.SpeedY; })); } catch (Exception) { } //Handle powerups try { Dispatcher.Invoke(new Action(() => { p.HandlePowerups(b, players, powerups, balls); })); } catch (Exception) { } } }