Ejemplo n.º 1
0
        private static void Program33()
        {
            Game.ShowScene();

            var direction = Directions.Right;

            void UpdateDirection()
            {
                if (Game.IsKeyDown(KeyboardKey.Up) && direction != Directions.Down)
                {
                    direction = Directions.Up;
                }
                if (Game.IsKeyDown(KeyboardKey.Down) && direction != Directions.Up)
                {
                    direction = Directions.Down;
                }
                if (Game.IsKeyDown(KeyboardKey.Left) && direction != Directions.Right)
                {
                    direction = Directions.Left;
                }
                if (Game.IsKeyDown(KeyboardKey.Right) && direction != Directions.Left)
                {
                    direction = Directions.Right;
                }
            }

            var snakeHead = Game.AddPlayer(PlayerData.Turtle);
            var tailPart  = PlayerData
                            .Create(SvgImage.CreateCircle(RGBAColors.Black.WithAlpha(0x80), 10));
            var snakeTail = Enumerable.Range(1, 3)
                            .Select(i => Game.AddPlayer(tailPart.WithPosition(snakeHead.Position.X - i * 10, snakeHead.Position.Y))
                                    )
                            .ToList();

            var food = Game.AddPlayer(PlayerData.Ant);

            food.MoveToRandomPosition();

            void MoveSnakeHead(double distance)
            {
                snakeHead.SetDirection(direction);
                if (direction == Directions.Up)
                {
                    snakeHead.MoveTo(snakeHead.Position.X, snakeHead.Position.Y + distance);
                }
                else if (direction == Directions.Down)
                {
                    snakeHead.MoveTo(snakeHead.Position.X, snakeHead.Position.Y - distance);
                }
                else if (direction == Directions.Left)
                {
                    snakeHead.MoveTo(snakeHead.Position.X - distance, snakeHead.Position.Y);
                }
                else if (direction == Directions.Right)
                {
                    snakeHead.MoveTo(snakeHead.Position.X + distance, snakeHead.Position.Y);
                }
            }

            void MoveSnake(double distance)
            {
                var position = snakeHead.Position;

                foreach (var part in snakeTail)
                {
                    var nextPosition = part.Position;
                    part.MoveTo(position);
                    position = nextPosition;
                }
                MoveSnakeHead(distance);
            }

            void CheckIfSnakeEatsFood()
            {
                if (snakeHead.TouchesPlayer(food))
                {
                    food.MoveToRandomPosition();
                    var tailPosition = snakeTail.Last().Position;
                    var tail         = Game.AddPlayer(tailPart.WithPosition(tailPosition));
                    snakeTail.Add(tail);
                }
            }

            bool CheckIfSnakeHitsWall()
            {
                return(snakeHead.TouchesEdge());
            }

            while (true)
            {
                UpdateDirection();
                MoveSnake(10);
                CheckIfSnakeEatsFood();
                if (CheckIfSnakeHitsWall())
                {
                    break;
                }

                Game.Sleep(50);
            }

            snakeHead.Say("Game over.");
        }
Ejemplo n.º 2
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);
        }