Пример #1
0
        public void Update()
        {
            if (isEnded)
            {
                return;
            }
            if (hits / levelUpCondition > level)
            {
                UpLevel();
            }

            // обновление ракеток
            if (upPlayer.Direction > 0 && upPlayer.FigureXPos + upPlayer.Width < width)
            {
                upPlayer.FigureXPos += upPlayer.Speed;
            }
            if (upPlayer.Direction < 0 && upPlayer.FigureXPos > 0)
            {
                upPlayer.FigureXPos -= upPlayer.Speed;
            }
            if (downPlayer.Direction > 0 && downPlayer.FigureXPos + downPlayer.Width < width)
            {
                downPlayer.FigureXPos += downPlayer.Speed;
            }
            if (downPlayer.Direction < 0 && downPlayer.FigureXPos > 0)
            {
                downPlayer.FigureXPos -= downPlayer.Speed;
            }

            // обработка завершения игры
            if (points[0] >= winPoints || points[1] >= winPoints)
            {
                if (points[0] == points[1])
                {
                    controller.GameOver(0);
                }
                else if (points[0] > points[1])
                {
                    controller.GameOver(1);
                }
                else if (points[0] < points[1])
                {
                    controller.GameOver(2);
                }
            }

            // обновление шаров
            List <Task> allBallsUpdater = new List <Task>();

            foreach (Ball ball in balls)
            {
                Task task = new Task(new Action <object>(BallUpdate), ball);
                allBallsUpdater.Add(task);
                task.Start();
            }

            Task allDone = Task.WhenAll(allBallsUpdater);

            allDone.Wait();
            foreach (Task t in allBallsUpdater)
            {
                t.Dispose();
            }
            allDone.Dispose();

            // обновление счёта игроков
            controller.DownPointsSet(points[1]);
            controller.UpPointsSet(points[0]);
        }