Example #1
0
        private bool[] PlayGame()
        {
            this.canExit = false;
            string opponentName = server.ReciveName();

            SetInfoText($"Playing against {opponentName}");
            for (int i = 0; i < 3; i++)
            {
                int startIn = server.ReciveInt();
                SetInfoText($"Starting in {startIn.ToString()}");
            }
            Thread.Sleep(1000);
            SetInfoText("");
            this.canExit = true;
            ShowBall();
            Thread SetRacketWithCursor = new Thread(SetRacketPosition);

            SetRacketWithCursor.Start();

            Tuple <bool, WhoWins, int, int, int> results;

            while (true)
            {
                if (!this.server.IsConnected())
                {
                    SetRacketWithCursor.Abort();
                    SetInfoText("Lost connection...");
                    return(new bool[2] {
                        false, true
                    });
                }
                results = this.server.ReciveServer();
                if (results.Item1)
                {
                    SetRacketWithCursor.Abort();
                    SetInfoText("Opponent disconnected, you won");
                    return(new bool[2] {
                        true, true
                    });
                }
                ChangeLeft(this.ball, results.Item3);
                ChangeTop(this.ball, results.Item4);
                ChangeTop(this.enemyRacket, results.Item5);
                this.Update();
                this.Invalidate();

                switch (results.Item2)
                {
                case WhoWins.you:
                    SetRacketWithCursor.Abort();
                    SetInfoText("you won!");
                    return(new bool[2] {
                        true, false
                    });

                case WhoWins.enemy:
                    SetRacketWithCursor.Abort();
                    SetInfoText("You lost");
                    return(new bool[2] {
                        false, false
                    });
                }
                server.SendMyRacket(this.myRacket.Top);
            }
        }