Esempio n. 1
0
File: Pong.cs Progetto: crone66/Pong
        /// <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)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (state != GameState.Started)
            {
                if (state == GameState.CountDown)
                {
                    countdown.Update(gameTime);
                }
                else
                {
                    if (Keyboard.GetState().IsKeyDown(Keys.Space))
                    {
                        state = GameState.Started;
                        SetupGame(ballTexture, sliderTexture);
                    }
                }
            }
            else
            {
                GameUpdate(gameTime);
            }

            base.Update(gameTime);
        }
Esempio n. 2
0
        /// <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)
        {
            newState = Keyboard.GetState();
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (state != GameState.Started)
            {
                if (newState.IsKeyDown(Keys.P) && !oldState.IsKeyDown(Keys.P))
                {
                    Thread sThread = new Thread(new ParameterizedThreadStart(StartServer));
                    sThread.Start(shareObject);
                    isServer = true;
                    drawText = "now running as Server. Waiting for Client to connect";
                }

                else if (newState.IsKeyDown(Keys.U) && !oldState.IsKeyDown(Keys.U))
                {
                    Thread cThread = new Thread(new ParameterizedThreadStart(StartClient));
                    cThread.Start(shareObject);
                    isServer = false;
                    drawText = "now running as Client...connecting to server";
                }
                if (state == GameState.CountDown)
                {
                    countdown.Update(gameTime);
                }
                else
                {
                    if (Keyboard.GetState().IsKeyDown(Keys.Space) || shareObject.PwdAccepted)
                    {
                        state = GameState.Started;
                        SetupGame(ballTexture, sliderTexture);
                    }
                }
            }
            else
            {
                GameUpdate(gameTime);
            }

            base.Update(gameTime);
            oldState = newState;
        }
Esempio n. 3
0
        /// <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);
        }