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;
- using System.Threading.Tasks;
- namespace ConsoleApp11
- {
- enum Direction
- {
- North,
- East,
- South,
- West
- }
- class Variables
- {
- public static int BoardWidth { get; set; }
- public static int BoardHeight { get; set; }
- public static int TrailSpeed { get; set; }
- public static Direction direction { get; set; }
- public static int GameScore { get; set; }
- public static bool IsDead { get; set; }
- public static string[,] GameBoard { get; set; }
- public Variables()
- {
- // Settings
- BoardWidth = 25;
- BoardHeight = 25;
- TrailSpeed = 100;
- direction = Direction.South;
- GameScore = 0;
- IsDead = false;
- GameBoard = new string[Variables.BoardHeight, Variables.BoardWidth];
- }
- }
- class Body
- {
- public int X { get; set; }
- public int Y { get; set; }
- }
- class Program
- {
- public static List<Body> Trail = new List<Body>();
- public static void GetDirection()
- {
- if (Console.KeyAvailable == true)
- {
- var command = Console.ReadKey().Key;
- // Assign direction with arrow key pressed
- switch (command)
- {
- case ConsoleKey.UpArrow:
- Variables.direction = Direction.North;
- break;
- case ConsoleKey.RightArrow:
- Variables.direction = Direction.East;
- break;
- case ConsoleKey.DownArrow:
- Variables.direction = Direction.South;
- break;
- case ConsoleKey.LeftArrow:
- Variables.direction = Direction.West;
- break;
- case ConsoleKey.Spacebar:
- Body body = new Body { X = Trail[Trail.Count - 1].X, Y = Trail[Trail.Count - 1].Y };
- Trail.Add(body);
- break;
- }
- }
- }
- public static void UpdateGame()
- {
- for (int i = Trail.Count - 1; i >= 0; i--)
- {
- // If the head
- if (i == 0)
- {
- switch (Variables.direction)
- {
- case Direction.North:
- Trail[i].Y--;
- break;
- case Direction.East:
- Trail[i].X++;
- break;
- case Direction.South:
- Trail[i].Y++;
- break;
- case Direction.West:
- Trail[i].X--;
- break;
- }
- }
- // If the body
- else
- {
- Trail[i].X = Trail[i - 1].X;
- Trail[i].Y = Trail[i - 1].Y;
- }
- // Reset Board
- for (int k = 0; k < Variables.BoardHeight; k++)
- {
- for (int j = 0; j < Variables.BoardWidth; j++)
- {
- Variables.GameBoard[k, j] = " ";
- }
- }
- // Make Trail
- for (int j = Trail.Count - 1; j >= 0; j--)
- {
- Variables.GameBoard[Trail[j].Y, Trail[j].X] = "#";
- }
- // Output Trail
- string output = "";
- for (int k = 0; k < Variables.BoardWidth; k++)
- {
- for (int j = 0; j < Variables.BoardHeight; j++)
- {
- output += (Variables.GameBoard[k, j]);
- }
- output += (Environment.NewLine);
- }
- Console.Clear();
- Console.WriteLine(output);
- }
- }
- public static void Main(string[] args)
- {
- new Variables();
- Console.CursorVisible = false;
- Body head = new Body { X = 2, Y = 2 };
- Trail.Add(head);
- Console.SetWindowSize(Variables.BoardWidth, Variables.BoardHeight);
- Thread t1 = new Thread(() =>
- {
- while (true)
- {
- GetDirection();
- }
- });
- Thread t2 = new Thread(() =>
- {
- while (true)
- {
- Thread.Sleep(100);
- UpdateGame();
- }
- });
- t1.Start();
- t2.Start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement