예제 #1
0
        private static void Program13()
        {
            Game.ShowScene();

            Game.Sleep(1000);

            var isGameOver = false;

            void controlLeftPlayer(Player player)
            {
                using (player.OnKeyDown(KeyboardKey.W, p => p.MoveUp(10)))
                    using (player.OnKeyDown(KeyboardKey.S, p => p.MoveDown(10)))
                    {
                        while (!isGameOver)
                        {
                            player.Sleep(50);
                        }
                    }
            }

            var leftPlayer = Game.AddPlayer(
                PlayerData
                .Create(
                    SvgImage.CreateRectangle(
                        RGBAColors.DarkMagenta,
                        new Size(20, 150)))
                .WithPosition(Game.SceneBounds.Left + 20, 0),
                controlLeftPlayer);

            void controlRightPlayer(Player player)
            {
                using (player.OnKeyDown(KeyboardKey.Up, p => p.MoveUp(10)))
                    using (player.OnKeyDown(KeyboardKey.Down, p => p.MoveDown(10)))
                    {
                        while (!isGameOver)
                        {
                            player.Sleep(50);
                        }
                    }
            }

            var rightPlayer = Game.AddPlayer(
                PlayerData
                .Create(
                    SvgImage.CreateRectangle(
                        RGBAColors.Magenta,
                        new Size(20, 150)))
                .WithPosition(Game.SceneBounds.Right - 20, 0),
                controlRightPlayer);

            var rand = new Random();

            void controlBall(Player player)
            {
                player.SetDirection(rand.Next(360));
                while (true)
                {
                    player.MoveInDirection(10);
                    player.BounceOffWall();
                    if (player.Bounds.Left <= Game.SceneBounds.Left ||
                        player.Bounds.Right >= Game.SceneBounds.Right)
                    {
                        isGameOver = true;
                        break;
                    }
                    if (player.Bounds.Left <= leftPlayer.Bounds.Right &&
                        player.Position.Y <= leftPlayer.Bounds.Top &&
                        player.Position.Y >= leftPlayer.Bounds.Bottom)
                    {
                        player.SetDirection(180 - player.Direction);
                    }
                    else if (player.Bounds.Right >= rightPlayer.Bounds.Left &&
                             player.Position.Y <= rightPlayer.Bounds.Top &&
                             player.Position.Y >= rightPlayer.Bounds.Bottom)
                    {
                        player.SetDirection(180 - player.Direction);
                    }
                    player.Sleep(50);
                }
            }

            Game.AddPlayer(
                PlayerData.Create(
                    SvgImage.CreateCircle(RGBAColors.Black.WithAlpha(128), 10)),
                controlBall);
        }