Custom levels, fix

This commit is contained in:
JFronny 2020-05-25 19:27:03 +02:00
parent 2f228205d5
commit 5cf9712792
3 changed files with 28 additions and 6 deletions

View File

@ -1,8 +1,13 @@
namespace Snakity
using System;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Snakity
{
public static class Levels
{
public static readonly string[] levels =
private static readonly string[] DefaultLevels =
{
@"
######################
@ -39,5 +44,18 @@
#....................#
######################"
};
public static string[] levels { get; private set; }
private const string LevelsDir = "levels";
public static void Load()
{
Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (!Directory.Exists(LevelsDir)) Directory.CreateDirectory(LevelsDir);
if (!Directory.GetFiles(LevelsDir).Any())
for (int i = 0; i < DefaultLevels.Length; i++) File.WriteAllText(Path.Combine(LevelsDir, $"lvl_{i + 1}.txt"), DefaultLevels[i]);
levels = Directory.GetFiles(LevelsDir).Select(File.ReadAllText).ToArray();
}
}
}

View File

@ -6,7 +6,7 @@ namespace Snakity.Loop
public static class Input
{
private static DateTime _lastCheck = DateTime.Now;
private static TimeSpan _delay = new TimeSpan(3000000);
private static TimeSpan _delay = new TimeSpan(4000000);
private static bool _shouldIncrease;
public static bool R;
@ -91,7 +91,7 @@ namespace Snakity.Loop
if (_shouldIncrease)
{
_shouldIncrease = false;
_delay = _delay.Subtract(new TimeSpan((int) (500 * _delay.TotalMilliseconds)));
_delay = _delay.Subtract(new TimeSpan((int) (200 * _delay.TotalMilliseconds)));
}
else
{

View File

@ -68,8 +68,11 @@ Score: {_score}
Play again? (y/n)");
_score = 0;
ConsoleKey tmp = Console.ReadKey().Key;
return tmp == ConsoleKey.Y || tmp == ConsoleKey.Enter;
ConsoleKey tmp;
do
tmp = Console.ReadKey().Key;
while (tmp != ConsoleKey.Y && tmp != ConsoleKey.N);
return tmp == ConsoleKey.Y;
}
private static void PlayRound()
@ -81,6 +84,7 @@ Play again? (y/n)");
Label scoreLabel = new Label(new Point(0, 0), "");
Renderer.Labels.Clear();
Renderer.Labels.Add(scoreLabel);
Levels.Load();
(char[,] level, bool[,] spawnable) =
CharArrayLoader.LoadLevel(Levels.levels[Rnd.Next(Levels.levels.Length)], SettingsMan.SmoothTerrain);
bool hasIncreased = false;