コード例 #1
0
ファイル: Pong.cs プロジェクト: BennXYZ/MyPong
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            KeyboardState keyState = Keyboard.GetState();

            if (state == GameState.None)
            {
                if (keyState.IsKeyDown(Keys.Escape) && !prevKeyState.IsKeyDown(Keys.Escape))
                {
                    listener.Close();
                    listener.Dispose();
                    Exit();
                }
            }
            else
            {
                if (keyState.IsKeyDown(Keys.Escape) && !prevKeyState.IsKeyDown(Keys.Escape))
                {
                    if (recievingThread.IsAlive)
                    {
                        listener.Close();
                        listener.Dispose();
                    }

                    state = GameState.None;
                    if (isConnected)
                    {
                        isConnected = false;
                        connection.Shutdown(SocketShutdown.Both);
                        connection.Close();
                    }

                    countdown = new Countdown(new Vector2(graphics.PreferredBackBufferWidth / 2, 100), boldFont, 3000);
                    countdown.OnCountdownElapsed += Countdown_OnCountdownElapsed;

                    drawText = startText;
                }
            }

            prevKeyState = keyState;

            if (state != GameState.Started)
            {
                if (state == GameState.GameOver)
                {
                    if (keyState.IsKeyDown(Keys.Space))
                    {
                        drawText    = startText;
                        state       = GameState.None;
                        isConnected = false;
                        if (connection != null)
                        {
                            if (connection.Connected)
                            {
                                connection.Shutdown(SocketShutdown.Both);
                                connection.Close();
                            }
                        }

                        countdown = new Countdown(new Vector2(graphics.PreferredBackBufferWidth / 2, 100), boldFont, 3000);
                        countdown.OnCountdownElapsed += Countdown_OnCountdownElapsed;

                        if (connectionType == ConnectionType.Server)
                        {
                            listener = new Socket(AddressFamily.InterNetwork,
                                                  SocketType.Stream, ProtocolType.Tcp);
                        }
                    }
                }
                else if (state == GameState.CountDown)
                {
                    countdown.Update(gameTime);
                }
                else if (state != GameState.Searching)
                {
                    if (keyState.IsKeyDown(Keys.D1))
                    {
                        connectionType = ConnectionType.Server;
                        state          = GameState.Searching;
                        drawText       = "Searching for other Players";

                        recievingThread = new Thread(new ParameterizedThreadStart(CreateServer));
                        recievingThread.IsBackground = true;
                        recievingThread.Start(port);
                    }
                    else if (keyState.IsKeyDown(Keys.D2))
                    {
                        connectionType = ConnectionType.Client;
                        state          = GameState.Searching;
                        drawText       = "Searching for other Players";

                        recievingThread = new Thread(new ParameterizedThreadStart(FindServer));
                        recievingThread.IsBackground = true;
                        recievingThread.Start(port);
                    }
                    else if (keyState.IsKeyDown(Keys.D3))
                    {
                        listener.Close();
                        listener.Dispose();
                    }
                    //if (Keyboard.GetState().IsKeyDown(Keys.D1))
                    //{
                    //    state = GameState.Started;
                    //    SetupGame(ballTexture, sliderTexture);
                    //}
                }
                else if (state == GameState.Searching)
                {
                    if (isConnected)
                    {
                        state = GameState.Started;
                        SetupGame(ballTexture, sliderTexture);
                    }
                }
            }
            else
            {
                GameUpdate(gameTime);
            }

            base.Update(gameTime);
        }