Last active
January 6, 2020 07:21
-
-
Save JerryNixon/1629810710a3301aefdafb25f0a70619 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Drawing; | |
| namespace SnakePrep | |
| { | |
| internal class Program | |
| { | |
| private static ConsoleKey _direction = ConsoleKey.Escape; | |
| private static bool _gameOver = false; | |
| private static Point _current; | |
| private static Point _previous; | |
| private static Size _size = new Size(40, 20); | |
| private static Point _fruit; | |
| private static int _score; | |
| private static void Main(string[] args) | |
| { | |
| Start(); | |
| while (_gameOver == false) | |
| { | |
| Input(); | |
| Logic(); | |
| Draw(); | |
| System.Threading.Thread.Sleep(100); | |
| } | |
| Console.Read(); | |
| } | |
| private static void Logic() | |
| { | |
| var next = _current; | |
| if (_direction == ConsoleKey.UpArrow) { next.Y = next.Y - 1; } | |
| if (_direction == ConsoleKey.DownArrow) { next.Y = next.Y + 1; } | |
| if (_direction == ConsoleKey.LeftArrow) { next.X = next.X - 1; } | |
| if (_direction == ConsoleKey.RightArrow) { next.X = next.X + 1; } | |
| if (next != _current) | |
| { | |
| _previous = _current; | |
| _current = next; | |
| } | |
| if (_current.X == 0 | |
| || _current.X == _size.Width - 1 | |
| || _current.Y == 0 | |
| || _current.Y == _size.Height) | |
| { | |
| _gameOver = true; | |
| Draw(); | |
| } | |
| if (_current == _fruit) | |
| { | |
| _score++; | |
| _fruit = GetRandomPoint(); | |
| } | |
| } | |
| private static void Draw() | |
| { | |
| Write(_previous.X, _previous.Y, " "); | |
| Write(_current.X, _current.Y, "8"); | |
| Write(_fruit.X, _fruit.Y, "q"); | |
| Write(0, 21, "Score: " + _score.ToString().PadRight(5)); | |
| } | |
| private static void Input() | |
| { | |
| if (Console.KeyAvailable) | |
| { | |
| var key = Console.ReadKey(true).Key; | |
| if (key == ConsoleKey.UpArrow | |
| || key == ConsoleKey.DownArrow | |
| || key == ConsoleKey.LeftArrow | |
| || key == ConsoleKey.RightArrow) | |
| { | |
| _direction = key; | |
| } | |
| } | |
| } | |
| private static void Start() | |
| { | |
| Console.CursorVisible = false; | |
| Console.Clear(); | |
| _current = GetRandomPoint(); | |
| _previous = new Point(10, 9); | |
| _fruit = GetRandomPoint(); | |
| var line = $"{new string('+', 40)}"; | |
| Write(0, 0, line); | |
| Write(0, 20, line); | |
| line = "+" + new string(' ', 38) + "+"; | |
| for (var i = 1; i < 20; i++) | |
| { | |
| Write(0, i, line); | |
| } | |
| } | |
| public static void Write(int x, int y, string line) | |
| { | |
| Console.SetCursorPosition(x, y); | |
| Console.WriteLine(line); | |
| } | |
| private static readonly Random _random = new Random(); | |
| public static Point GetRandomPoint() | |
| { | |
| var point = new Point | |
| { | |
| X = _random.Next(1, _size.Width - 2), | |
| Y = _random.Next(1, _size.Height - 2) | |
| }; | |
| return point; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment