static void Main(string[] args) { Console.SetWindowSize(FrameBuffer.WIDTH, FrameBuffer.HEIGHT); Console.SetBufferSize(FrameBuffer.WIDTH, FrameBuffer.HEIGHT); Console.Title = "Snake"; Console.CursorVisible = false; FrameBuffer fb = new FrameBuffer(); while (true) { Game g = new Game((uint)Environment.TickCount64); Result result = g.Run(ref fb); string message = result == Result.Win ? "You Win" : "You lose"; int position = (FrameBuffer.WIDTH - message.Length) / 2; for (int i = 0; i < message.Length; i++) { fb.SetPixel(position + i, FrameBuffer.HEIGHT / 2, message[i]); } fb.Render(); Console.ReadKey(intercept: true); } }
public Result Run(ref FrameBuffer fb) { Snake s = new Snake( (byte)(_random.Next() % FrameBuffer.WIDTH), (byte)(_random.Next() % FrameBuffer.HEIGHT), (Snake.Direction)(_random.Next() % 4) ); MakeFood(s, out byte foodX, out byte foodY); long gameTime = Environment.TickCount64; while (true) { fb.Clear(); if (!s.Update()) { s.Draw(ref fb); return(Result.Loss); } s.Draw(ref fb); if (Console.KeyAvailable) { ConsoleKeyInfo ki = Console.ReadKey(intercept: true); switch (ki.Key) { case ConsoleKey.UpArrow: s.Course = Snake.Direction.Up; break; case ConsoleKey.DownArrow: s.Course = Snake.Direction.Down; break; case ConsoleKey.LeftArrow: s.Course = Snake.Direction.Left; break; case ConsoleKey.RightArrow: s.Course = Snake.Direction.Right; break; } } if (s.HitTest(foodX, foodY)) { if (s.Extend()) { MakeFood(s, out foodX, out foodY); } else { return(Result.Win); } } fb.SetPixel(foodX, foodY, '*'); fb.Render(); gameTime += 100; long delay = gameTime - Environment.TickCount64; if (delay >= 0) { Thread.Sleep((int)delay); } else { gameTime = Environment.TickCount64; } } }