Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Timers;
  7.  
  8. namespace SnakeHome
  9. {
  10.     public class Game
  11.     {
  12.         private Case[,] grid;
  13.         private Snake snake;
  14.         private int heightGrid = 20;
  15.         private int widthGrid = 40;
  16.         private static Timer aTimer;
  17.  
  18.         public Game()
  19.         {
  20.             Console.WindowHeight = heightGrid + 1;
  21.             Console.WindowWidth = widthGrid + 1;
  22.             GenerateGrid();
  23.             snake = new Snake();
  24.             snake.SetCurrentCase(new Case(widthGrid / 2, heightGrid / 2));
  25.             SetTimer();
  26.         }
  27.  
  28.         private void SetTimer()
  29.         {
  30.             aTimer = new Timer(250);
  31.             aTimer.Elapsed += OnTimedEvent;
  32.             aTimer.Enabled = true;
  33.         }
  34.  
  35.         private void OnTimedEvent(Object source, ElapsedEventArgs e)
  36.         {
  37.             Console.Clear();
  38.             DisplayGrid();
  39.             snake.MoveSnake();
  40.             snake.DisplaySnake();
  41.         }
  42.  
  43.         private void GenerateGrid()
  44.         {
  45.             grid = new Case[heightGrid, widthGrid];
  46.  
  47.             for (int i = 0; i < grid.GetLength(0); i++)
  48.             {
  49.                 for (int j = 0; j < grid.GetLength(1); j++)
  50.                 {
  51.                     grid[i, j] = new Case(i, j);
  52.                 }
  53.             }
  54.         }
  55.  
  56.         private void DisplayGrid()
  57.         {
  58.             for (int i = 0; i < grid.GetLength(1); i++)
  59.             {
  60.                 Console.Write("#");
  61.             }
  62.  
  63.             for (int i = 0; i < grid.GetLength(0); i++)
  64.             {
  65.                 Console.WriteLine("#");
  66.             }
  67.  
  68.             for (int i = 0; i < grid.GetLength(0); i++)
  69.             {
  70.                 Console.SetCursorPosition(widthGrid, i);
  71.                 Console.WriteLine("#");
  72.             }
  73.  
  74.             for (int i = 0; i < grid.GetLength(1) + 1; i++)
  75.             {
  76.                 Console.Write("#");
  77.             }
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement