Пример #1
0
        /**
         * Inits random snake position and fruit
         **/
        public void initGame()
        {
            currentScore = 0;
            Random rand = new Random();

            // Creates empty map
            for (int i = 0; i < Space.W; i++)
            {
                for (int j = 0; j < Space.H; j++)
                {
                    Map[j, i] = new Cell(CellType.EMPTY);
                }
            }
            // Generates snake (head + 2 body)
            //Snake.AddFirst(new Coord(5 + rand.Next(Space.H - 10), 5 + rand.Next(Space.W - 10)));
            Snake.AddFirst(new Coord(10, 10));
            //currentOrientation = (Space.Orientation)rand.Next(4);
            currentOrientation = Space.Orientation.NORTH;
            //Snake.AddLast(Snake.Last.Value + Space.Vec[(int)currentOrientation+2%4]);
            //Snake.AddLast(Snake.Last.Value + Space.Vec[(int)currentOrientation+2%4]);
            Snake.AddLast(Snake.Last.Value + Space.Vec[(int)Space.Orientation.SOUTH]);
            Snake.AddLast(Snake.Last.Value + Space.Vec[(int)Space.Orientation.SOUTH]);
            Console.WriteLine("Vec = " + Space.Vec[(int)currentOrientation].y + " ; " + Space.Vec[(int)currentOrientation].x);
            Console.WriteLine("Snake head = " + Snake.First.Value.y + " ; " + Snake.First.Value.y);
            foreach (Coord c in Snake)
            {
                Console.WriteLine(c.y + " ; " + c.x);
                Map[c.y, c.x] = new Cell(CellType.SNAKE);
            }
            Map[Snake.First.Value.x, Snake.First.Value.y] = new Cell(CellType.HEAD);
            // Adds a fruit on the map
            bool fruitOk = false;

            do
            {
                int y = rand.Next(Space.H);
                int x = rand.Next(Space.W);
                if (Map[y, x].type != CellType.SNAKE && Map[y, x].type != CellType.HEAD)
                {
                    Map[y, x] = new Cell(CellType.FRUIT);
                    fruitOk   = true;
                }
            } while (!fruitOk);
        }