MANY changes

This commit is contained in:
JFronny 2020-03-31 13:52:09 +02:00
parent 4d28d1abf0
commit 2cb56cf434
24 changed files with 555 additions and 198 deletions

View File

@ -1,2 +0,0 @@
# Default ignored files
/workspace.xml

View File

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="true" />
</component>
<component name="ProjectNotificationSettings">
<option name="askShowProject" value="false" />
</component>
</project>

View File

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ContentModelUserStore">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/.idea.SimpleConsoleGame/riderModule.iml" filepath="$PROJECT_DIR$/.idea/.idea.SimpleConsoleGame/riderModule.iml" />
</modules>
</component>
</project>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RiderProjectSettingsUpdater">
<option name="vcsConfiguration" value="1" />
</component>
</project>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="RIDER_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$/../.." />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -1 +0,0 @@
Snakity

View File

@ -0,0 +1,49 @@
using System;
using System.Linq;
using Snakity.Graphics;
namespace Snakity
{
public static class CharArrayLoader
{
public static Tuple<char[,], bool[,]> LoadLevel(string level)
{
char[,] content = Load(level);
int width = content.GetLength(0);
int height = content.GetLength(1);
bool[,] spawn = new bool[width, height];
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
switch (content[x, y])
{
case ' ':
content[x, y] = SpecialChars.Space;
break;
case '.':
content[x, y] = SpecialChars.Space;
spawn[x, y] = true;
break;
}
return new Tuple<char[,], bool[,]>(content, spawn);
}
public static char[,] Load(string text)
{
string[] levelArr = text.Split('\n');
int width = levelArr.Select(s => s.Length).OrderBy(s => s).Last();
int height = levelArr.Length;
char[,] tmp = new char[width, height];
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
tmp[x, y] = SpecialChars.Space;
for (int i = 0; i < levelArr.Length; i++)
{
string s = levelArr[i];
for (int j = 0; j < s.Length; j++) tmp[j, i] = s[j];
}
return tmp;
}
}
}

View File

@ -1,18 +1,38 @@
using System;
using System.Linq;
namespace Snakity.Graphics
{
static class ColorSelector
internal static class ColorSelector
{
private static readonly char[] WallChars =
{
SpecialChars.Wall.DownLeft, SpecialChars.Wall.DownRight, SpecialChars.Wall.LeftRight,
SpecialChars.Wall.UpDown, SpecialChars.Wall.UpLeft, SpecialChars.Wall.UpRight,
SpecialChars.Wall.DownLeftRight, SpecialChars.Wall.UpDownLeft, SpecialChars.Wall.UpDownRight,
SpecialChars.Wall.UpLeftRight, SpecialChars.Wall.UpDownLeftRight
};
private static readonly char[] PlayerChars =
{
SpecialChars.Player.Up, SpecialChars.Player.Down, SpecialChars.Player.Left,
SpecialChars.Player.Right, SpecialChars.Player.DownLeft, SpecialChars.Player.DownRight,
SpecialChars.Player.LeftRight, SpecialChars.Player.UpDown, SpecialChars.Player.UpLeft,
SpecialChars.Player.UpRight, SpecialChars.Player.DownLeftRight, SpecialChars.Player.UpDownLeft,
SpecialChars.Player.UpDownRight, SpecialChars.Player.UpLeftRight, SpecialChars.Player.UpDownLeftRight
};
public static ConsoleColor Get(char c)
{
if (PlayerChars.Contains(c))
return ConsoleColor.Green;
if (WallChars.Contains(c))
return ConsoleColor.Gray;
return c switch
{
SpecialChars.Player => ConsoleColor.Green,
SpecialChars.Wall => ConsoleColor.Gray,
SpecialChars.Enemy => ConsoleColor.Yellow,
_ => ConsoleColor.White
};
}
}
}
}

View File

@ -0,0 +1,10 @@
namespace Snakity.Graphics
{
public enum Direction
{
Up,
Down,
Left,
Right
}
}

View File

@ -17,14 +17,12 @@ namespace Snakity.Graphics
{
string[] lines = Text.Split('\n').Select(s => s.EndsWith('\r') ? s.Remove(s.Length - 1) : s).ToArray();
for (int i = 0; i < lines.Length; i++)
for (int j = 0; j < lines[i].Length; j++)
{
for (int j = 0; j < lines[i].Length; j++)
{
int tmpX = Position.X + j;
int tmpY = Position.Y + i;
if (tmpX < DiffDraw.Width && tmpY < DiffDraw.Height)
DiffDraw.Set(tmpX, tmpY, lines[i][j]);
}
int tmpX = Position.X + j;
int tmpY = Position.Y + i;
if (tmpX < DiffDraw.Width && tmpY < DiffDraw.Height)
DiffDraw.Set(tmpX, tmpY, lines[i][j]);
}
}
}

View File

@ -0,0 +1,44 @@
using System;
namespace Snakity.Graphics
{
public class DiffDraw
{
private static char[,] Screen { get; set; } = new char[0, 0];
public static int Width => Screen.GetLength(0);
public static int Height => Screen.GetLength(1);
public static void Draw()
{
Console.CursorTop = 0;
Console.CursorLeft = 0;
int width = Width;
int height = Height;
for (int y = 0; y < height; y++)
{
string line = "";
for (int x = 0; x < width; x++)
{
char tmp1 = Screen[x, y];
line += tmp1 == '\0' ? ' ' : tmp1;
}
Console.WriteLine(line);
Console.CursorLeft = 0;
}
}
public static char Get(Point p) => Get(p.X, p.Y);
public static char Get(int x, int y) => Screen[x, y];
public static void Set(Point p, char c) => Set(p.X, p.Y, c);
public static void Set(int x, int y, char c) => Screen[x, y] = c;
public static void Clear() => Clear(Width, Height);
public static void Clear(int width, int height) => Screen = new char[width, height];
public static void Clear(char[,] content) => Screen = content;
}
}

View File

@ -1,11 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Snakity.Graphics
{
public static class Renderer
{
public static readonly List<Tuple<Point,Point>> Player = new List<Tuple<Point,Point>>();
public static readonly List<Tuple<Point, Point>> Player = new List<Tuple<Point, Point>>();
public static readonly List<Point> Enemies = new List<Point>();
public static readonly List<Label> Labels = new List<Label>();
@ -13,7 +14,139 @@ namespace Snakity.Graphics
{
foreach (Label label in Labels) label.Render();
foreach (Point point in Enemies) DiffDraw.Set(point, SpecialChars.Enemy);
foreach (Tuple<Point,Point> point in Player) DiffDraw.Set(point.Item1, SpecialChars.Player);
for (int i = 0; i < Player.Count; i++)
{
(Point position, Point direction) = Player[i];
char selected;
if (i == 0)
{
if (Player.Count == 1)
{
if (direction == new Point(0, 1))
selected = SpecialChars.Player.Up;
else if (direction == new Point(0, -1))
selected = SpecialChars.Player.Down;
else if (direction == new Point(1, 0))
selected = SpecialChars.Player.Left;
else if (direction == new Point(-1, 0))
selected = SpecialChars.Player.Right;
else
selected = SpecialChars.Player.Down;
}
else
{
Point prevPosition = Player[i + 1].Item1;
Point tmp = new Point(prevPosition.X - position.X, prevPosition.Y - position.Y);
if (tmp == new Point(0, 1))
selected = SpecialChars.Player.Down;
else if (tmp == new Point(0, -1))
selected = SpecialChars.Player.Up;
else if (tmp == new Point(1, 0))
selected = SpecialChars.Player.Right;
else if (tmp == new Point(-1, 0))
selected = SpecialChars.Player.Left;
else
throw new ArgumentException($"Unexpected: {{X={tmp.X};Y={tmp.Y}}}");
}
}
else if (i == Player.Count - 1)
{
Point prevPosition = Player[i - 1].Item1;
Point tmp = new Point(prevPosition.X - position.X, prevPosition.Y - position.Y);
if (tmp == new Point(0, 1))
selected = SpecialChars.Player.Down;
else if (tmp == new Point(0, -1))
selected = SpecialChars.Player.Up;
else if (tmp == new Point(1, 0))
selected = SpecialChars.Player.Right;
else if (tmp == new Point(-1, 0))
selected = SpecialChars.Player.Left;
else
throw new ArgumentException($"Unexpected: {{X={tmp.X};Y={tmp.Y}}}");
}
else
{
Point prevPosition = Player[i - 1].Item1;
Point nextPosition = Player[i + 1].Item1;
prevPosition = new Point(prevPosition.X - position.X, position.Y - prevPosition.Y);
nextPosition = new Point(nextPosition.X - position.X, position.Y - nextPosition.Y);
Direction[] directions = new Direction[2];
if (prevPosition == new Point(0, 1))
directions[0] = Direction.Up;
else if (prevPosition == new Point(0, -1))
directions[0] = Direction.Down;
else if (prevPosition == new Point(1, 0))
directions[0] = Direction.Right;
else if (prevPosition == new Point(-1, 0))
directions[0] = Direction.Left;
else
throw new ArgumentException("Unexpected previous position delta");
if (nextPosition == new Point(0, 1))
directions[1] = Direction.Up;
else if (nextPosition == new Point(0, -1))
directions[1] = Direction.Down;
else if (nextPosition == new Point(1, 0))
directions[1] = Direction.Right;
else if (nextPosition == new Point(-1, 0))
directions[1] = Direction.Left;
else
throw new ArgumentException("Unexpected next position delta");
directions = directions.OrderBy(s => (int) s).ToArray();
switch (directions[0])
{
case Direction.Up:
switch (directions[1])
{
case Direction.Down:
selected = SpecialChars.Player.UpDown;
break;
case Direction.Left:
selected = SpecialChars.Player.UpLeft;
break;
case Direction.Right:
selected = SpecialChars.Player.UpRight;
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case Direction.Down:
switch (directions[1])
{
case Direction.Left:
selected = SpecialChars.Player.DownLeft;
break;
case Direction.Right:
selected = SpecialChars.Player.DownRight;
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case Direction.Left:
switch (directions[1])
{
case Direction.Right:
selected = SpecialChars.Player.LeftRight;
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
default:
throw new ArgumentOutOfRangeException();
}
}
DiffDraw.Set(position, selected);
}
}
}
}

View File

@ -2,9 +2,58 @@
{
public static class SpecialChars
{
public const char Player = '▮';
public const char Wall = '#';
public const char Enemy = '@';
public const char Space = '\0';
public static class Wall
{
// 2 connectors
public const char UpDown = '║';
public const char LeftRight = '═';
public const char DownRight = '╔';
public const char UpRight = '╚';
public const char DownLeft = '╗';
public const char UpLeft = '╝';
// 3 connectors
public const char UpDownLeft = '╣';
public const char UpDownRight = '╠';
public const char UpLeftRight = '╩';
public const char DownLeftRight = '╦';
// 4 connectors
public const char UpDownLeftRight = '╬';
}
public static class Player
{
// 1 connectors
public const char Up = '╵';
public const char Down = '╷';
public const char Left = '╴';
public const char Right = '╶';
// 2 connectors
public const char UpDown = '│';
public const char LeftRight = '─';
public const char DownRight = '┌';
public const char UpRight = '└';
public const char DownLeft = '┐';
public const char UpLeft = '┘';
// 3 connectors
public const char UpDownLeft = '┤';
public const char UpDownRight = '├';
public const char UpLeftRight = '┴';
public const char DownLeftRight = '┬';
// 4 connectors
public const char UpDownLeftRight = '┼';
}
}
}

View File

@ -1,56 +1,56 @@
using System;
namespace Snakity.Graphics
{
public static class DiffDraw
{
public static char[,] Screen { get; private set; } = new char[0, 0];
private static char[,] _last = new char[0,0];
public static int Width => Screen.GetLength(0);
public static int Height => Screen.GetLength(1);
public static void Draw()
{
Console.CursorTop = 0;
Console.CursorLeft = 0;
int width = Width;
int height = Height;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
char tmp1 = Screen[x, y];
if (tmp1 == _last[x, y]) continue;
Console.ForegroundColor = ColorSelector.Get(tmp1);
Console.CursorLeft = x;
Console.Write(tmp1);
}
Console.WriteLine();
Console.CursorLeft = 0;
}
_last = Screen;
}
public static char Get(Point p) => Get(p.X, p.Y);
public static char Get(int x, int y) => Screen[x, y];
public static void Set(Point p, char c) => Set(p.X, p.Y, c);
public static void Set(int x, int y, char c) => Screen[x, y] = c;
public static void Clear() => Clear(Width, Height);
public static void Clear(int width, int height)
{
Screen = new char[width, height];
_last = _last.Resize(width, height);
}
public static void Clear(char[,] content)
{
Screen = content;
_last = _last.Resize(Width, Height);
}
}
}
using System;
namespace Snakity.Graphics
{
public static class DiffDraw
{
private static char[,] Screen { get; set; } = new char[0, 0];
private static char[,] _last = new char[0,0];
public static int Width => Screen.GetLength(0);
public static int Height => Screen.GetLength(1);
public static void Draw()
{
Console.CursorTop = 0;
Console.CursorLeft = 0;
int width = Width;
int height = Height;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
char tmp1 = Screen[x, y];
if (tmp1 == _last[x, y]) continue;
Console.ForegroundColor = ColorSelector.Get(tmp1);
Console.CursorLeft = x;
Console.Write(tmp1);
}
Console.WriteLine();
Console.CursorLeft = 0;
}
_last = Screen;
}
public static char Get(Point p) => Get(p.X, p.Y);
public static char Get(int x, int y) => Screen[x, y];
public static void Set(Point p, char c) => Set(p.X, p.Y, c);
public static void Set(int x, int y, char c) => Screen[x, y] = c;
public static void Clear() => Clear(Width, Height);
public static void Clear(int width, int height)
{
Screen = new char[width, height];
_last = _last.Resize(width, height);
}
public static void Clear(char[,] content)
{
Screen = content;
_last = _last.Resize(Width, Height);
}
}
}

View File

@ -1,31 +0,0 @@
using System;
using System.Linq;
using Snakity.Graphics;
namespace Snakity
{
public static class LevelLoader
{
public static Tuple<char[,], bool[,]> Load(string level)
{
string[] levelArr = level.Replace(' ', SpecialChars.Space).Split('\n');
int width = levelArr.Select(s => s.Length).OrderBy(s => s).Last();
int height = levelArr.Length;
char[,] tmp = new char[width, height];
bool[,] spawnable = new bool[width, height];
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
tmp[x, y] = SpecialChars.Space;
for (int i = 0; i < levelArr.Length; i++)
{
string s = levelArr[i];
for (int j = 0; j < s.Length; j++)
{
tmp[j, i] = s[j] == '.' ? SpecialChars.Space : s[j];
spawnable[j, i] = s[j] == '.';
}
}
return new Tuple<char[,], bool[,]>(tmp, spawnable);
}
}
}

View File

@ -8,14 +8,17 @@ namespace Snakity.Loop
{
private static DateTime _lastCheck = DateTime.Now;
private static readonly TimeSpan Delay = new TimeSpan(1000000);
public static bool Compute(bool[,] spawnMap)
{
bool increase = false;
foreach (Point point in Renderer.Enemies.Where(enemy => Renderer.Player.Any(s => s.Item1 == enemy)).ToList())
foreach (Point point in Renderer.Enemies.Where(enemy => Renderer.Player.Any(s => s.Item1 == enemy))
.ToList())
{
Renderer.Enemies.Remove(point);
increase = true;
}
DateTime tmp = DateTime.Now;
while (tmp - _lastCheck > Delay)
{
@ -29,9 +32,11 @@ namespace Snakity.Loop
tmp1.Y = Program.Rnd.Next(DiffDraw.Height);
attempts++;
}
if (attempts < 5)
Renderer.Enemies.Add(tmp1);
}
return increase;
}
}

View File

@ -1,6 +1,4 @@
using System;
using System.Windows.Forms;
using CC_Functions.W32;
using Snakity.Graphics;
namespace Snakity.Loop
@ -8,20 +6,67 @@ namespace Snakity.Loop
public static class Input
{
private static DateTime _lastCheck = DateTime.Now;
public static TimeSpan Delay = new TimeSpan(3000000);
private static bool _shouldIncrease = false;
private static TimeSpan _delay = new TimeSpan(3000000);
private static bool _shouldIncrease;
public static bool R;
public static bool Esc;
public static void Get()
{
Point headDelta = new Point(0, 0);
if ((KeyboardReader.IsKeyDown(Keys.W) || KeyboardReader.IsKeyDown(Keys.Up)))
headDelta.Y--;
if ((KeyboardReader.IsKeyDown(Keys.A) || KeyboardReader.IsKeyDown(Keys.Left)))
headDelta.X--;
if ((KeyboardReader.IsKeyDown(Keys.S) || KeyboardReader.IsKeyDown(Keys.Down)))
headDelta.Y++;
if ((KeyboardReader.IsKeyDown(Keys.D) || KeyboardReader.IsKeyDown(Keys.Right)))
headDelta.X++;
if (headDelta != new Point(0, 0) && (Renderer.Player.Count < 2 || headDelta != Renderer.Player[1].Item1 - Renderer.Player[0].Item1))
bool pause = false;
while (Console.KeyAvailable)
switch (Console.ReadKey().Key)
{
case ConsoleKey.W:
case ConsoleKey.UpArrow:
if (headDelta.Y == 0)
headDelta.Y = -1;
else
headDelta.Y = 0;
break;
case ConsoleKey.A:
case ConsoleKey.LeftArrow:
headDelta.X = headDelta.X switch
{
0 => -1,
_ => 0
};
break;
case ConsoleKey.S:
case ConsoleKey.DownArrow:
headDelta.Y = headDelta.Y switch
{
0 => 1,
_ => 0
};
break;
case ConsoleKey.D:
case ConsoleKey.RightArrow:
headDelta.X = headDelta.X switch
{
0 => 1,
_ => 0
};
break;
case ConsoleKey.R:
R = true;
break;
case ConsoleKey.Escape:
Esc = true;
break;
case ConsoleKey.P:
pause = true;
break;
}
if (pause)
Console.ReadKey();
if (headDelta != new Point(0, 0) && (Renderer.Player.Count < 2 ||
headDelta != Renderer.Player[1].Item1 - Renderer.Player[0].Item1))
Renderer.Player[0] = new Tuple<Point, Point>(Renderer.Player[0].Item1, headDelta);
}
@ -29,9 +74,9 @@ namespace Snakity.Loop
{
DateTime tmp = DateTime.Now;
if (increaseScore) _shouldIncrease = true;
while (tmp - _lastCheck > Delay)
while (tmp - _lastCheck > _delay)
{
_lastCheck += Delay;
_lastCheck += _delay;
Point head = Renderer.Player[0].Item1;
Point headDelta = Renderer.Player[0].Item2;
if (headDelta.Y == -1 && (head.Y == 0 || !CheckPoint(head + new Point(0, -1))))
@ -46,17 +91,21 @@ namespace Snakity.Loop
if (_shouldIncrease)
{
_shouldIncrease = false;
Delay = Delay.Subtract(new TimeSpan((int) (800 * Delay.TotalMilliseconds)));
_delay = _delay.Subtract(new TimeSpan((int) (500 * _delay.TotalMilliseconds)));
}
else
{
Renderer.Player.RemoveAt(Renderer.Player.Count - 1);
}
}
return false;
}
public static void ResetSpeed() => Delay = new TimeSpan(3000000);
public static void ResetSpeed()
{
_delay = new TimeSpan(3000000);
}
private static bool CheckPoint(Point point) =>
DiffDraw.Get(point) == SpecialChars.Space || DiffDraw.Get(point) == SpecialChars.Enemy;

View File

@ -1,54 +1,132 @@
using System;
using System.Threading;
using System.Windows.Forms;
using CC_Functions.W32;
using System.Linq;
using Snakity.Graphics;
using Label = Snakity.Graphics.Label;
namespace Snakity.Loop
{
static class Program
internal static class Program
{
private static int _score = 0;
private static int _score;
public static Random Rnd = new Random();
public static void Main(string[] args)
{
if (args.Contains("DrawTest"))
{
Console.Clear();
(char[,] level, _) =
CharArrayLoader.LoadLevel(@"
#####################
# #
# #
# #
# #
# #
#####################");
DiffDraw.Clear(level);
Renderer.Player.Add(new Tuple<Point, Point>(new Point(1, 2), new Point(0, 0)));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(2, 2), new Point(0, 0)));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(2, 3), new Point(0, 0)));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(1, 3), new Point(0, 0)));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(1, 4), new Point(0, 0)));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(1, 5), new Point(0, 0)));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(1, 6), new Point(0, 0)));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(2, 6), new Point(0, 0)));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(2, 5), new Point(0, 0)));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(2, 4), new Point(0, 0)));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(3, 4), new Point(0, 0)));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(3, 3), new Point(0, 0)));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(3, 2), new Point(0, 0)));
Renderer.Player.Add(new Tuple<Point, Point>(new Point(4, 2), new Point(0, 0)));
Renderer.Render();
DiffDraw.Draw();
Console.ReadKey();
return;
}
bool playing = true;
while (playing)
{
Console.Clear();
//Main menu
Console.WriteLine(@"
~ Snakity ~
s - Start
x - Exit");
switch (Console.ReadKey().Key)
{
case ConsoleKey.S:
PlayRound();
while (GameOver())
PlayRound();
break;
case ConsoleKey.X:
playing = false;
break;
}
}
Console.Clear();
Console.WriteLine("Bye");
}
private static bool GameOver()
{
Console.Clear();
Console.WriteLine($@"
GAME OVER
Score: {_score}
Play again? (y/n)");
return Console.ReadKey().KeyChar == 'y';
}
private static void PlayRound()
{
bool playing = true;
DiffDraw.Clear(5, 5);
Label scoreLabel = new Label(new Point(0, 0), "");
Renderer.Labels.Add(scoreLabel);
(char[,] level, bool[,] spawnable) =
LevelLoader.Load(Levels.levels[Rnd.Next(Levels.levels.Length)]);
Renderer.Player.Add(new Tuple<Point, Point>(new Point(2, 2), new Point(0, 1)));
(char[,] level, bool[,] spawnable) =
CharArrayLoader.LoadLevel(Levels.levels[Rnd.Next(Levels.levels.Length)]);
bool hasIncreased = false;
while (true)
Renderer.Player.Clear();
Renderer.Player.Add(new Tuple<Point, Point>(new Point(1, 1), new Point(0, 1)));
Renderer.Enemies.Clear();
Input.ResetSpeed();
while (playing)
{
DiffDraw.Clear((char[,])level.Clone());
scoreLabel.Text = $"Score: {_score}; d={Input.Delay.TotalMilliseconds}";
DiffDraw.Clear((char[,]) level.Clone());
scoreLabel.Text = $"Score: {_score}";
Renderer.Render();
Input.Get();
bool down = KeyboardReader.IsKeyDown(Keys.R);
if (Input.Move(hasIncreased) || down)
if (Input.Move(hasIncreased) || Input.Esc)
{
playing = false;
Input.Esc = false;
continue;
}
if (Input.R)
{
DiffDraw.Clear();
DiffDraw.Draw();
(level, spawnable) = LevelLoader.Load(Levels.levels[Rnd.Next(Levels.levels.Length)]);
(level, spawnable) = CharArrayLoader.LoadLevel(Levels.levels[Rnd.Next(Levels.levels.Length)]);
Renderer.Player.Clear();
Renderer.Enemies.Clear();
Input.ResetSpeed();
Renderer.Player.Add(new Tuple<Point, Point>(new Point(2, 2), new Point(0, 1)));
DiffDraw.Clear((char[,])level.Clone());
if (down)
while (KeyboardReader.IsKeyDown(Keys.R))
Thread.Sleep(100);
DiffDraw.Clear((char[,]) level.Clone());
Input.R = false;
}
Renderer.Render();
hasIncreased = Enemy.Compute(spawnable);
if (hasIncreased)
_score++;
DiffDraw.Draw();
if (KeyboardReader.IsKeyDown(Keys.Escape))
return;
}
}
}
}
}

View File

@ -12,8 +12,11 @@
}
public static bool operator ==(Point left, Point right) => left.X == right.X && left.Y == right.Y;
public static bool operator !=(Point left, Point right) => left.X != right.X || left.Y != right.Y;
public static Point operator +(Point left, Point right) => new Point(left.X + right.X, left.Y + right.Y);
public static Point operator -(Point left, Point right) => new Point(left.X - right.X, left.Y - right.Y);
}
}

View File

@ -6,11 +6,11 @@ namespace Snakity
{
public static T[,] Resize<T>(this T[,] original, int rows, int cols)
{
T[,] newArray = new T[rows,cols];
T[,] newArray = new T[rows, cols];
int minRows = Math.Min(rows, original.GetLength(0));
int minCols = Math.Min(cols, original.GetLength(1));
for(int i = 0; i < minRows; i++)
for(int j = 0; j < minCols; j++)
for (int i = 0; i < minRows; i++)
for (int j = 0; j < minCols; j++)
newArray[i, j] = original[i, j];
return newArray;
}

View File

@ -4,9 +4,10 @@
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CC-Functions.W32" Version="1.1.7388.28729" />
<Compile Remove="Graphics/LinuxDiffDraw.cs" Condition="'$(OS)' == 'Windows_NT'" />
<Compile Remove="Graphics/WindowsDiffDraw.cs" Condition="'$(OS)' != 'Windows_NT'" />
</ItemGroup>
</Project>