Exemplo n.º 1
0
        public void Load(string sFileName) // Загрузка поля
        {
            string[] lines = File.ReadAllLines(sFileName);
            for (byte yy = 0; yy < lines.Length; yy++)
            {
                string[] arRows = lines[yy].Split(
                    new[] { ';' }, StringSplitOptions.None);
                for (byte xx = 0; xx < arRows.Length; xx++)
                {
                    string sValue = arRows[xx];
                    switch (sValue)
                    {
                    case "0":   //Ячейка травы
                    {
                        cCellField pCell = new cCellField(xx, yy);
                        pCell.Type = false;
                        FieldCellList.Add(pCell);
                        break;
                    }

                    case "1":   // Ячейка стены
                    {
                        cCellField pCell = new cCellField(xx, yy);
                        pCell.Type = true;
                        FieldCellList.Add(pCell);
                        break;
                    }

                    case "2":     //Змея
                    {
                        cCellSnake pCellSnake = new cCellSnake(xx, yy);
                        Snake.SnakeCellList.Add(pCellSnake);
                        cCellField pCell = new cCellField(xx, yy);
                        pCell.Type = false;
                        FieldCellList.Add(pCell);
                        break;
                    }

                    case "3":     // Яблоко
                    {
                        Apple      = new cApple(xx, yy);
                        Apple.Type = type.Point;
                        cCellField pCell = new cCellField(xx, yy);
                        pCell.Type = false;
                        FieldCellList.Add(pCell);

                        break;
                    }

                    default:
                        throw new Exception("Неверный формат файла, некорректное значение " + sValue);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void h_Tick(object state)
        {
            if (Life != 0)
            {
                cCellSnake CellSnake = new cCellSnake(Field.Snake.SnakeCellList.First().CoordX, Field.Snake.SnakeCellList.First().CoordY);

                switch (m_enDirection)
                {
                case EDirection.Left:
                    CellSnake.CoordX--;
                    break;

                case EDirection.Right:
                    CellSnake.CoordX++;
                    break;

                case EDirection.Up:
                    CellSnake.CoordY--;
                    break;

                case EDirection.Down:
                    CellSnake.CoordY++;
                    break;
                }

                if (Check(CellSnake) == true)
                {
                    //Проверка на яблоко
                    if ((CellSnake.CoordX == Field.Apple.CoordX) && (CellSnake.CoordY == Field.Apple.CoordY))
                    {
                        AppleWork(CellSnake);
                    }

                    else //"Движение" змеи
                    {
                        Field.Snake.SnakeCellList.Insert(0, CellSnake);
                        Field.Snake.SnakeCellList.RemoveAt(Field.Snake.SnakeCellList.Count - 1);
                    }
                }

                else //Если бортик
                {
                    EndGame();
                    Life--;
                    CreateField(FieldName); //Возвращаем первоначальный вид змеи
                    Start();
                }
            }
            else
            {
                EndGame();    //Конец игры
            }
        }
Exemplo n.º 3
0
        private bool Check(cCellSnake CellSnake)
        {
            cCellField CellField = Field.FieldCellList.FirstOrDefault(p => (p.CoordX == CellSnake.CoordX) && (p.CoordY == CellSnake.CoordY));

            if (CellField.Type == true)
            {
                return(false);//Проверка на бортик
            }
            else
            {
                return(true);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Новое яблоко
        /// </summary>
        private void NewApple()
        {
            Random     rand   = new Random();
            int        coord  = rand.Next(0, Field.FieldCellList.Count);
            cCellField pField = Field.FieldCellList[coord];
            cCellSnake pSnake = Field.Snake.SnakeCellList.FirstOrDefault(p => (p.CoordX == pField.CoordX) && (p.CoordY == pField.CoordY));

            do
            {
                coord  = rand.Next(0, Field.FieldCellList.Count);                                                                   //Берем рандомный элемент листа
                pField = Field.FieldCellList[coord];
                pSnake = Field.Snake.SnakeCellList.FirstOrDefault(p => (p.CoordX == pField.CoordX) && (p.CoordY == pField.CoordY)); //Проверяем координаты
            }while ((pField.Type == true) || (pSnake != null));                                                                     //Проверка на бортик и змею

            Field.Apple.CoordX = pField.CoordX;
            Field.Apple.CoordY = pField.CoordY;
            AppleType(Field.Apple);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Работа яблока
        /// </summary>
        /// <param name="CellSnake"></param>
        private void AppleWork(cCellSnake CellSnake)
        {
            Field.Snake.SnakeCellList.Insert(0, CellSnake);
            switch (Field.Apple.Type)
            {
            case type.Point:     //Очки +10
                Points = Points + 10;
                break;

            case type.BigPoint:     //Очки +50
                Points = Points + 50;
                break;

            case type.Life:     //Очки +10 жизнь +1
                Points = Points + 10;
                if (Life < 4)
                {
                    Life++;
                }
                break;

            case type.Surprise:     //Яблоко сюрприз
            {
                Random rand = new Random();
                int    N    = rand.Next(0, 100);

                if (N < 60)                          //Очки +50
                {
                    Points = Points + 50;
                }
                else if ((N > 60) & (N < 70))        //Очки -10
                {
                    Points = Points - 10;
                }

                else if ((N > 70) & (N < 80))        //Очки +10 жизнь +1
                {
                    Points = Points + 10;
                    if (Life < 4)
                    {
                        Life++;
                    }
                }

                else if ((N > 80) & (N < 96))                // Отпадает хвост
                {
                    if (Field.Snake.SnakeCellList.Count > 5) // Для длинной змеи
                    {
                        Random r = new Random();
                        int    m = r.Next(1, 5);
                        for (int i = 0; i <= m; i++)
                        {
                            Field.Snake.SnakeCellList.RemoveAt(Field.Snake.SnakeCellList.Count - 1);
                        }
                    }
                    else if (Field.Snake.SnakeCellList.Count >= 3)         // Для короткой змеи
                    {
                        Random r = new Random();
                        int    m = r.Next(1, 3);
                        for (int i = 0; i <= m; i++)
                        {
                            Field.Snake.SnakeCellList.RemoveAt(Field.Snake.SnakeCellList.Count - 1);
                        }
                    }
                }
                else
                {
                    Life--;          // Жизнь -1
                }
                break;
            }
            }
            NewApple();
        }