Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Timers;
- namespace SnakeHome
- {
- public class Game
- {
- private Case[,] grid;
- private Snake snake;
- private int heightGrid = 20;
- private int widthGrid = 40;
- private static Timer aTimer;
- public Game()
- {
- Console.WindowHeight = heightGrid + 1;
- Console.WindowWidth = widthGrid + 1;
- GenerateGrid();
- snake = new Snake();
- snake.SetCurrentCase(new Case(widthGrid / 2, heightGrid / 2));
- SetTimer();
- }
- private void SetTimer()
- {
- aTimer = new Timer(250);
- aTimer.Elapsed += OnTimedEvent;
- aTimer.Enabled = true;
- }
- private void OnTimedEvent(Object source, ElapsedEventArgs e)
- {
- Console.Clear();
- DisplayGrid();
- snake.MoveSnake();
- snake.DisplaySnake();
- }
- private void GenerateGrid()
- {
- grid = new Case[heightGrid, widthGrid];
- for (int i = 0; i < grid.GetLength(0); i++)
- {
- for (int j = 0; j < grid.GetLength(1); j++)
- {
- grid[i, j] = new Case(i, j);
- }
- }
- }
- private void DisplayGrid()
- {
- for (int i = 0; i < grid.GetLength(1); i++)
- {
- Console.Write("#");
- }
- for (int i = 0; i < grid.GetLength(0); i++)
- {
- Console.WriteLine("#");
- }
- for (int i = 0; i < grid.GetLength(0); i++)
- {
- Console.SetCursorPosition(widthGrid, i);
- Console.WriteLine("#");
- }
- for (int i = 0; i < grid.GetLength(1) + 1; i++)
- {
- Console.Write("#");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement