Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var score           = 0;
            var speed           = 100;
            var foodPosition    = Point.Empty;
            var screenSize      = new Size(60, 20);
            var snake           = new Queue <Point>();
            var snakeLength     = 3;
            var currentPosition = new Point(0, 9);

            snake.Enqueue(currentPosition);
            var direction = Direction.Right;

            DrawScreen(screenSize);
            ShowScore(score);

            while (MoveSnake(snake, currentPosition, snakeLength, screenSize))
            {
                Thread.Sleep(speed);
                direction       = GetDirection(direction);
                currentPosition = GetNextPosition(direction, currentPosition);

                if (currentPosition.Equals(foodPosition))
                {
                    foodPosition = Point.Empty;
                    snakeLength++;
                    score += 10;
                    ShowScore(score);
                }

                if (foodPosition == Point.Empty)
                {
                    foodPosition = ShowFood(screenSize, snake);
                }
            }

            Console.ResetColor();
            Console.SetCursorPosition(screenSize.Width / 2 - 4, screenSize.Height / 2);
            Console.Write("Perdiste :( ");
            Thread.Sleep(2000);
            Console.ReadKey();
        }
Exemplo n.º 2
0
        public void DetectColision(List <SnakeObject> snakeList, List <Apple> apples)
        {
            //Solid walls
            if (CurrentSnakeBlocks[0].ActualPosition.X > Settings.Default.GameWidth ||
                CurrentSnakeBlocks[0].ActualPosition.X <0 ||
                                                        CurrentSnakeBlocks[0].ActualPosition.Y> Settings.Default.GameHeight ||
                CurrentSnakeBlocks[0].ActualPosition.Y < 0)
            {
                KillSnake(apples);
            }


            foreach (SnakeObject s in snakeList)
            {
                if (!s.Equals(this) && s.Active)
                {
                    foreach (var snakeBlock in s.CurrentSnakeBlocks)
                    {
                        if (Point.Equals(this.CurrentSnakeBlocks[0].ActualPosition, snakeBlock.ActualPosition))
                        {
                            KillSnake(apples);
                        }
                    }
                }
                if (s.Equals(this))
                {
                    for (int i = 1; i < s.CurrentSnakeBlocks.Count; i++)
                    {
                        if (Point.Equals(this.CurrentSnakeBlocks[0].ActualPosition, s.CurrentSnakeBlocks[i].ActualPosition))
                        {
                            KillSnake(apples);
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void PointEqualityTest1()
        {
            Point p = new Point(1, 1);

            Assert.IsTrue(p.Equals(p));
        }
Exemplo n.º 4
0
        public void Update()
        {
            Point  newPosition     = Snake.CalculateNewPosition(Width, Height);
            Entity collisionEntity = EntityOccupyingPosition(newPosition);

            ClearedPositions.Clear();

            if (collisionEntity == null)
            {
                ClearedPositions.Add(Snake.Elements[Snake.Elements.Count - 1].Position);
                Snake.Move(newPosition);
            }
            else if (!newPosition.Equals(Snake.GetPosition()))
            {
                if (collisionEntity is Fruit)
                {
                    Snake.EatFruit(collisionEntity.GetPosition());
                    Entities.Remove(collisionEntity);
                    GenerateFruit();
                }
                else if (collisionEntity is Mouse)
                {
                    Snake.EatMouse(collisionEntity.GetPosition());
                    Entities.Remove(collisionEntity);
                }
                else if (collisionEntity is Obstacle)
                {
                    ClearedPositions.Add(Snake.Elements[Snake.Elements.Count - 1].Position);
                    Snake.EatObstacle(collisionEntity.GetPosition());
                    if (Snake.Lives <= 0)
                    {
                        GameOver = true;
                        return;
                    }
                }
                else if (collisionEntity is Snake)
                {
                    ClearedPositions.Add(Snake.Elements[Snake.Elements.Count - 1].Position);
                    List <Point> removedPositions = Snake.EatSelf(newPosition);
                    if (removedPositions != null)
                    {
                        foreach (Point position in removedPositions)
                        {
                            ClearedPositions.Add(position);
                        }
                    }
                }

                else if (collisionEntity is Powerup)
                {
                    Powerup powerup = (Powerup)collisionEntity;
                    ClearedPositions.Add(Snake.Elements[Snake.Elements.Count - 1].Position);
                    if (powerup.Effect.Variant == Effect.EffectVariant.Shrink)
                    {
                        List <Element> removedElements = Snake.Elements.GetRange(Snake.Size() / 2, Snake.Size() / 2 + Snake.Size() % 2);
                        foreach (Element element in removedElements)
                        {
                            ClearedPositions.Add(element.Position);
                        }
                    }
                    Snake.EatPowerup(collisionEntity.GetPosition(), ((Powerup)collisionEntity).Effect);
                    Entities.Remove(collisionEntity);
                }
            }
            List <Entity> removedEntities = new List <Entity>();
            bool          powerupExists = false, mouseExists = false;

            for (int i = 0; i < Entities.Count; i++)
            {
                if (Entities[i] is Powerup)
                {
                    Powerup powerup = (Powerup)Entities[i];
                    if (powerup.SpawnDuration > 0)
                    {
                        powerup.LowerDuration();
                        powerupExists = true;
                    }
                    else
                    {
                        ClearedPositions.Add(powerup.GetPosition());
                        removedEntities.Add(powerup);
                        powerupExists = false;
                    }
                }

                if (Entities[i] is Mouse)
                {
                    Mouse mouse            = (Mouse)Entities[i];
                    Point newMousePosition = mouse.CalculateNewPosition();
                    ClearedPositions.Add(mouse.GetPosition());
                    if (newMousePosition.X >= Height || newMousePosition.Y >= Width || newMousePosition.X < 0 || newMousePosition.Y < 0)
                    {
                        removedEntities.Add(mouse);
                    }
                    else
                    {
                        mouse.Move(newMousePosition);
                        mouseExists = true;
                    }
                }
            }

            foreach (Entity entity in removedEntities)
            {
                Entities.Remove(entity);
            }

            if (!powerupExists && !Snake.IsEffectActive() && IsDrawn(1))
            {
                GeneratePowerup();
            }

            if (Snake.IsEffectActive() && Snake.Effect.Duration > 0)
            {
                Snake.Effect.LowerDuration();
            }
            else
            {
                Snake.EndEffect();
            }

            if (!mouseExists && IsDrawn(1))
            {
                GenerateMouse();
            }

            if (Snake.Score >= nextStage)
            {
                GenerateObstacle();
                nextStage += Stage;
            }
        }