public string loadLevel() { OpenFileDialog loadLevelDialog = new OpenFileDialog(); loadLevelDialog.Filter = "json Файл (*.json)|*.json|txt Файл (*.txt)|*.txt"; loadLevelDialog.RestoreDirectory = true; string json; if (loadLevelDialog.ShowDialog() == DialogResult.OK) { string fname = loadLevelDialog.FileName; json = File.ReadAllText(fname); try { game = JsonConvert.DeserializeObject <structLevelGame>(json); MessageBox.Show("Уровень загружен!\nЧтобы пройти уровень нам нужно набрать " + game.scoreWin + " очков!\n" + (game.timeLeft > 0 ? ("Время на прохождения уровня: " + (game.timeLeft / 60) + " мин " + (game.timeLeft - (((game.timeLeft / 60) * 60))) + " сек") : ""), "Файл был успешно прочитан", MessageBoxButtons.OK, MessageBoxIcon.Information); return(json); } catch { MessageBox.Show("Уровень не был загружен.\nФайл не корректный.", "Ошибка чтения файла", MessageBoxButtons.OK, MessageBoxIcon.Error); } } return(null); }
private void testLoadLevel(object sender, EventArgs e) { snakeLevelGame level = new snakeLevelGame(); string json = level.loadLevel(); if (json != null) { structLevelGame levelEdit = new structLevelGame(); levelEdit = JsonConvert.DeserializeObject <structLevelGame>(json); blocks = levelEdit.table; tableXSize.Value = levelEdit.tableSize[0]; tableYSize.Value = levelEdit.tableSize[1]; sizeCellTable.Value = levelEdit.cellSize; barrierGame.Checked = levelEdit.barrier; snakeSpeed.Value = levelEdit.speedSnake; foodGenTime.Value = levelEdit.timeFood; timeLevel.Value = levelEdit.timeLeft; scoreWin.Value = levelEdit.scoreWin; tabLevel_Selected(null, null); clearTable(); } }
public void foodMap(structLevelGame levelGame) { int[,] tableFill = new int[numCells[0], numCells[1]]; List <int[]> foodPos = new List <int[]>(); foreach (int[] block in levelGame.table) { if (block[2] == 0) { tableFill[block[0], block[1]] = 1; } else if (block[2] == 1) { tableFill[block[0], block[1]] = block[3]; } if (block[2] == 2) { foodPos.Add(new int[] { block[0], block[1] }); } } for (int i = 0; i < foodPos.Count; i++) { int[] point = foodPos[i]; if (point[0] - 1 >= 0 && (tableFill[point[0] - 1, point[1]] == 0 || (tableFill[point[0] - 1, point[1]] > 1 && stata.score > tableFill[point[0] - 1, point[1]]))) { foodPos.Add(new int[] { point[0] - 1, point[1] }); tableFill[point[0] - 1, point[1]] = -1; } if (point[0] + 1 < numCells[0] && (tableFill[point[0] + 1, point[1]] == 0 || (tableFill[point[0] + 1, point[1]] > 1 && stata.score > tableFill[point[0] + 1, point[1]]))) { foodPos.Add(new int[] { point[0] + 1, point[1] }); tableFill[point[0] + 1, point[1]] = -1; } if (point[1] - 1 >= 0 && (tableFill[point[0], point[1] - 1] == 0 || (tableFill[point[0], point[1] - 1] > 1 && stata.score > tableFill[point[0], point[1] - 1]))) { foodPos.Add(new int[] { point[0], point[1] - 1 }); tableFill[point[0], point[1] - 1] = -1; } if (point[1] + 1 < numCells[1] && (tableFill[point[0], point[1] + 1] == 0 || (tableFill[point[0], point[1] + 1] > 1 && stata.score > tableFill[point[0], point[1] + 1]))) { foodPos.Add(new int[] { point[0], point[1] + 1 }); tableFill[point[0], point[1] + 1] = -1; } } //foreach (int[] point in foodPos) //{ // snakeTable.FillRectangle(Brushes.Red, 1 + point[0] * cellSize, 1 + point[1] * cellSize, cellSize - 1, cellSize - 1); //} foodTable = foodPos; }
private void testJson(object sender, EventArgs e) { structLevelGame level = new structLevelGame(); level.barrier = barrierGame.Checked; level.speedSnake = snakeSpeed.Value; level.cellSize = sizeCellTable.Value; level.tableSize = new int[] { tableXSize.Value, tableYSize.Value }; level.timeFood = Convert.ToInt32(foodGenTime.Value); level.timeLeft = Convert.ToInt32(timeLevel.Value); level.scoreWin = Convert.ToInt32(scoreWin.Value); level.table = blocks; MessageBox.Show(JsonConvert.SerializeObject(level)); Clipboard.SetText(JsonConvert.SerializeObject(level)); }
private void saveLevel(object sender, EventArgs e) { int[] sysBlocks = new int[2] { 0, 0 }; foreach (int[] point in blocks) { if (point[2] == 2) { sysBlocks[0] += 1; } if (point[2] == 3) { sysBlocks[1] += 1; } } if (sysBlocks[0] == 1 && sysBlocks[1] == 1) { structLevelGame level = new structLevelGame(); level.barrier = barrierGame.Checked; level.speedSnake = snakeSpeed.Value; level.cellSize = sizeCellTable.Value; level.tableSize = new int[] { tableXSize.Value, tableYSize.Value }; level.timeFood = Convert.ToInt32(foodGenTime.Value); level.timeLeft = Convert.ToInt32(timeLevel.Value); level.scoreWin = Convert.ToInt32(scoreWin.Value); level.table = blocks; snakeLevelGame levelGame = new snakeLevelGame(); levelGame.saveLevel(JsonConvert.SerializeObject(level)); } else { MessageBox.Show("Проверьте кол-во блоков спавна или завершения уровня", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }