Example #1
0
        void DoServerStuff()
        {
            // Setup as server here
            IPAddress   ipAd   = IPAddress.Parse(NetBreakoutPongServer.Constants.SERVER_IP);
            TcpListener server = new TcpListener(ipAd, NetBreakoutPongServer.Constants.SERVER_PORT);

            server.Start();

            NetBreakoutPongServer.ClassicPongGameData data = new NetBreakoutPongServer.ClassicPongGameData();
            data.gameBall.Position  = new NetBreakoutPongServer.Location(250, 300);
            data.gameBall.Radius    = 50;
            data.myPaddle.Height    = 40;
            data.myPaddle.Width     = 150;
            data.myPaddle.Position  = new NetBreakoutPongServer.Location(500, 580);
            data.oppPaddle.Height   = 40;
            data.oppPaddle.Width    = 150;
            data.oppPaddle.Position = new NetBreakoutPongServer.Location(300, 20);

            NetBreakoutPongServer.GameClient client = new NetBreakoutPongServer.GameClient(server);

            //Thread.Sleep(5000);

            client.SendData(data);

            client.Dispose();
        }
Example #2
0
        public ClassicPongGameData GetDataForP1()
        {
            ClassicPongGameData P1Data = new ClassicPongGameData();

            // store gameBall data
            P1Data.gameBall.Position = new Location(gameBall.Position.X / 1000, (Constants.PLAYFIELD_HEIGHT - gameBall.Position.Y) / 1000);
            P1Data.gameBall.Radius   = (gameBall.Radius / 1000);

            // store POne data
            P1Data.myPaddle.Height   = playerOne.Height / 1000;
            P1Data.myPaddle.Width    = playerOne.Width / 1000;
            P1Data.myPaddle.Position = new Location(playerOne.Position.X / 1000, (Constants.PLAYFIELD_HEIGHT - playerOne.Position.Y) / 1000);

            // store PTwo data
            P1Data.oppPaddle.Height   = playerTwo.Height / 1000;
            P1Data.oppPaddle.Width    = playerTwo.Width / 1000;
            P1Data.oppPaddle.Position = new Location(playerTwo.Position.X / 1000, (Constants.PLAYFIELD_HEIGHT - playerTwo.Position.Y) / 1000);

            // Set game not over, driver will take care of this
            P1Data.OppLost = P1Data.ILost = false;

            return(P1Data);
        }
Example #3
0
        public ClassicPongGameData GetDataForP2()
        {
            ClassicPongGameData P2Data = new ClassicPongGameData();

            // store gameBall data
            P2Data.gameBall.Position = new Location((Constants.PLAYFIELD_WIDTH - gameBall.Position.X) / 1000, gameBall.Position.Y / 1000);
            P2Data.gameBall.Radius   = (gameBall.Radius / 1000);

            // store PTwo data
            P2Data.myPaddle.Height   = playerTwo.Height / 1000;
            P2Data.myPaddle.Width    = playerTwo.Width / 1000;
            P2Data.myPaddle.Position = new Location((Constants.PLAYFIELD_WIDTH - playerTwo.Position.X) / 1000, playerTwo.Position.Y / 1000);

            // store POne data
            P2Data.oppPaddle.Height   = playerOne.Height / 1000;
            P2Data.oppPaddle.Width    = playerOne.Width / 1000;
            P2Data.oppPaddle.Position = new Location((Constants.PLAYFIELD_WIDTH - playerOne.Position.X) / 1000, playerOne.Position.Y / 1000);

            // Store game over info
            P2Data.OppLost = P2Data.ILost = false;

            return(P2Data);
        }
Example #4
0
 public void SendData(ClassicPongGameData gameData)
 {
     stream   = client.GetStream();
     encoding = JsonConvert.SerializeObject(gameData);
     stream.Write(Encoding.ASCII.GetBytes(encoding), 0, encoding.Length);
 }
Example #5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Pong Server is starting up...");
            // Setup as server here
            IPAddress   ipAd   = IPAddress.Parse(Constants.SERVER_IP);
            TcpListener server = new TcpListener(ipAd, Constants.SERVER_PORT);

            server.Start();

            while (true)
            {
                // Connect Two Clients
                Console.WriteLine("Waiting for Player One to connect...");
                PlayerOne = new GameClient(server);
                Console.WriteLine("Waiting for Player Two to connect...");
                PlayerTwo = new GameClient(server);

                try
                {
                    do
                    {
                        Console.WriteLine("Generating a Classic Pong Game...");
                        game = new ClassicPongGame();

                        // Send initial locations
                        Console.WriteLine("Ready...");
                        PlayerOne.SendData(game.GetDataForP1());
                        PlayerTwo.SendData(game.GetDataForP2());


                        // wait for a sec before starting
                        Console.WriteLine("Set...");
                        Thread.Sleep(1000);

                        // As long as the game is not over, keep playing.
                        Console.WriteLine("Go!");
                        while (!game.PlayerOneLost() && !game.PlayerTwoLost())
                        {
                            // Move and update everything for player one
                            POneKP = PlayerOne.GetKeypress();
                            game.POneUpdate(POneKP);
                            PlayerOne.SendData(game.GetDataForP1());
                            Thread.Sleep(Constants.SLEEP_DELAY);

                            // Move and update everything for player two
                            PTwoKP = PlayerTwo.GetKeypress();
                            game.PTwoUpdate(PTwoKP);
                            PlayerTwo.SendData(game.GetDataForP2());
                            Thread.Sleep(Constants.SLEEP_DELAY);
                        }

                        // Throw away these keypresses. Nobody loves them.
                        PlayerOne.GetKeypress();
                        PlayerTwo.GetKeypress();

                        // Send final positions with gameOverInfo
                        // PlayerOne
                        ClassicPongGameData temp = game.GetDataForP1();
                        temp.ILost   = game.PlayerOneLost();
                        temp.OppLost = game.PlayerTwoLost();
                        PlayerOne.SendData(temp);
                        // PlayerTwo
                        temp         = game.GetDataForP2();
                        temp.ILost   = game.PlayerTwoLost();
                        temp.OppLost = game.PlayerOneLost();
                        PlayerTwo.SendData(temp);

                        pOneContinue = PlayerOne.RequestsContinue();
                        pTwoContinue = PlayerTwo.RequestsContinue();

                        PlayerOne.ApproveContinue(pOneContinue && pTwoContinue);
                        PlayerTwo.ApproveContinue(pOneContinue && pTwoContinue);
                    } while (pOneContinue && pTwoContinue);
                }
                catch (IOException e)
                {
                    Console.WriteLine("Connection with client(s) was interrupted unexpectedly, restarting game.");
                }

                Thread.Sleep(100);

                if (PlayerOne != null)
                {
                    PlayerOne.Dispose();
                }

                if (PlayerTwo != null)
                {
                    PlayerTwo.Dispose();
                }
            }
        }