コード例 #1
0
        public void Keyboard_KeyDown(object sender, KeyboardKeyEventArgs e)
        {
            switch (e.Key)
            {
            case Key.Up:
                paddlePlayer.Move(20);
                break;

            case Key.Down:
                paddlePlayer.Move(-20);
                break;
            }
        }
コード例 #2
0
        public void Update(FrameEventArgs e)
        {
            if (GamerOver == false)
            {
                Console.WriteLine("X: {0} Y: {1}", ball.Velocity.X, ball.Velocity.Y);
                Timer -= 1;

                if (Timer <= 0)
                {
                    Timer = 60;
                    clock--;
                    if (powerUpSpawned == false)
                    {
                        rInt  = r.Next(0, 5); //for ints
                        lInt  = r.Next(3, 9); //for ints
                        lintF = (float)lInt / 10;
                        if (rInt == 3)
                        {
                            powerUpSpawned        = true;
                            speedPowerUp.Position = new Vector2((int)(SceneManager.WindowWidth * lintF), (int)(SceneManager.WindowHeight * lintF));
                        }
                        else if (rInt == 2)
                        {
                            powerUpSpawned         = true;
                            lengthPowerUp.Position = new Vector2((int)(SceneManager.WindowWidth * lintF), (int)(SceneManager.WindowHeight * lintF));
                        }
                    }
                }
                if (clock <= 0)
                {
                    GamerOver = true;
                    if (scorePlayer > scorePlayer2)
                    {
                        winner = "Player 1 Wins!";
                    }
                    else if (scorePlayer == scorePlayer2)
                    {
                        winner = "Draw!";
                    }
                    else
                    {
                        winner = "Player 2 Wins!";
                    }
                    clock = 3;
                    Timer = 60;
                }
                //Console.WriteLine("Timer:" + clock);

                // Set the title of the window
                sceneManager.Title = "Pong - Player Score: " + scorePlayer + " - Player 2 Score: " + scorePlayer2;
                KeyboardState KeyStates = Keyboard.GetState();
                if (KeyStates.IsKeyDown(Key.S))
                {
                    if (DebugMode == true)
                    {
                        ball.Position = new Vector2(ball.Position.X, ball.Position.Y - 5);
                    }
                    else
                    {
                        if (player1SpeedEnabled == true)
                        {
                            paddlePlayer.Move(-15);
                        }
                        else
                        {
                            paddlePlayer.Move(-8);
                        }
                    }
                }
                else if (KeyStates.IsKeyDown(Key.W))
                {
                    if (DebugMode == true)
                    {
                        ball.Position = new Vector2(ball.Position.X, ball.Position.Y + 5);
                    }
                    else
                    {
                        if (player1SpeedEnabled == true)
                        {
                            paddlePlayer.Move(15);
                        }
                        else
                        {
                            paddlePlayer.Move(8);
                        }
                    }
                }
                if (DebugMode == true)
                {
                    if (KeyStates.IsKeyDown(Key.A))
                    {
                        ball.Position = new Vector2(ball.Position.X - 5, ball.Position.Y);
                    }
                    else if (KeyStates.IsKeyDown(Key.D))
                    {
                        ball.Position = new Vector2(ball.Position.X + 5, ball.Position.Y);
                    }
                }
                else
                {
                    paddlePlayer.Move(0);
                }


                if (KeyStates.IsKeyDown(Key.Down))
                {
                    if (player1SpeedEnabled == true)
                    {
                        paddlePlayer2.Move(-15);
                    }
                    else
                    {
                        paddlePlayer2.Move(-8);
                    }
                }
                else if (KeyStates.IsKeyDown(Key.Up))
                {
                    if (player1SpeedEnabled == true)
                    {
                        paddlePlayer2.Move(15);
                    }
                    else
                    {
                        paddlePlayer2.Move(8);
                    }
                }
                else
                {
                    paddlePlayer2.Move(0);
                }


                if (DebugMode == false)
                {
                    ball.Update((float)e.Time);
                }

                CollisionDetection();
                if (GoalDetection())
                {
                    ResetGame();
                }
            }
            else
            {
                Timer -= 1;

                if (Timer <= 0)
                {
                    Timer = 60;
                    clock--;
                }
                if (clock <= 0)
                {
                    if (scorePlayer > scorePlayer2)
                    {
                        sceneManager.OpenMultiLeaderboard(true, scorePlayer);
                    }
                    // if they draw this will still hit
                    else
                    {
                        sceneManager.OpenMultiLeaderboard(true, scorePlayer2);
                    }
                }
            }
        }