Beispiel #1
0
        public void CheckCollisions()
        {
            foreach (Snake snake in Snakes)
            {
                foreach (Piece food in FoodPieces.ToArray())
                {
                    if (snake.CanEat(food.X, food.Y))
                    {
                        FoodPieces.Remove(food);
                        GenerateFoodPiece();
                        snake.Grow();
                        Score += 1;
                    }
                }

                if (snake.HitsBorder(Width, Height))
                {
                    Running = false;
                }

                if (snake.EatsItself())
                {
                    Running = false;
                }

                for (int i = 0; i < Walls.Count - 1; i++)
                {
                    if (snake.HitsWall(Walls[i].X, Walls[i].Y))
                    {
                        Running = false;
                    }
                }
            }
        }
Beispiel #2
0
        private void GenerateFoodPiece()
        {
            while (FoodPieces.Count <= Snakes.Count)
            {
                int x = rng.Next((Width - 1));
                int y = rng.Next((Height - 1));

                foreach (Snake snake in Snakes)
                {
                    if (snake.IsAtPos(x, y))
                    {
                        GenerateFoodPiece();
                        return;
                    }
                }

                for (int i = 0; i < Walls.Count - 1; i++)
                {
                    if (Walls[i].X == x && Walls[i].Y == y)
                    {
                        GenerateFoodPiece();
                        return;
                    }
                }

                FoodPieces.Add(new Piece(x, y, PieceType.Food, Color.Red));
            }
        }
Beispiel #3
0
 private void AssembleMap()
 {
     Map = new List <List <Piece> >();
     for (int y = 0; y < Height; y++)
     {
         Map.Add(new List <Piece>());
         for (int x = 0; x < Width; x++)
         {
             if (Walls.Any(p => p.X == x && p.Y == y))
             {
                 Map[y].Add(new Piece(x, y, PieceType.Wall, Color.Blue));
             }
             else if (Snakes.Any(s => s.snakePieces.Any(p => p.X == x && p.Y == y && p.Type == PieceType.SnakePiece)))
             {
                 Map[y].Add(new Piece(x, y, PieceType.SnakePiece, Snakes.Where(s => s.IsAtPos(x, y)).FirstOrDefault().snakePieces.Where(p => p.X == x && p.Y == y).FirstOrDefault().Color));
             }
             else if (Snakes.Any(s => s.snakePieces.Any(p => p.X == x && p.Y == y && p.Type == PieceType.HeadPiece)))
             {
                 Map[y].Add(new Piece(x, y, PieceType.HeadPiece, Snakes.Where(s => s.IsAtPos(x, y)).FirstOrDefault().snakePieces.Where(p => p.X == x && p.Y == y).FirstOrDefault().Color));
             }
             else if (FoodPieces.Any(p => p.X == x && p.Y == y))
             {
                 Piece piece = FoodPieces.Where(p => p.X == x && p.Y == y).FirstOrDefault();
                 Map[y].Add(piece);
             }
             else
             {
                 Map[y].Add(new Piece(x, y, PieceType.Empty));
             }
         }
     }
 }