public void start_new() { //Used to instantiate variables and to set up pre-defined variables //The random object Random random = new Random(); //The apple object Apple apple = new Apple(random.Next(0, Console.WindowWidth - 1), random.Next(0, Console.WindowHeight - 1), "@", random); //The player object SnakeHead player = new SnakeHead(Console.WindowWidth / 2, Console.WindowHeight / 2, "X", apple); //Used to setup walls this.walls(); while (true) { player.update(); apple.draw(); //Checks if player is in wall if (player.x == 0 || player.x == Console.WindowWidth - 1 || player.y == 0 || player.y == Console.WindowHeight - 2) { this.gameover(); return; } System.Threading.Thread.Sleep(50); } }
public SnakeTail(int x, int y, string graphic, SnakeTail[] tail, int position, string direction, SnakeHead player) : base(x, y, graphic) { this.direction = direction; this.tail = tail; this.position = position; this.next_direction = direction; this.cur_direction = direction; this.player = player; }
private void game_over(SnakeHead player) { Console.Clear(); Console.WriteLine("GAME OVER"); Console.WriteLine("Your Score was: " + player.cur_pos); Console.WriteLine("Press any key to continue"); while (true) { if (Console.KeyAvailable == true) { Console.Clear(); break; } } }
public void start_new() { //Instantiate objects and set up predefined definitions Console.CursorVisible = false; //Set up random object Random random = new Random(); //Creates array for snaketails SnakeTail[] tail = new SnakeTail[(Console.WindowWidth - 2) * (Console.WindowHeight - 2)]; //Set up the apple object Apple apple = new Apple(random.Next(1, Console.WindowWidth - 2), random.Next(1, Console.WindowHeight - 2), "@", random); //Creates player object SnakeHead player = new SnakeHead((Console.WindowWidth / 2), (Console.WindowHeight / 2), "X", apple, tail); //Draws walls this.walls(); //Draws apple apple.draw(); //Draws player player.draw(); while (true) { player.update(); if (player.cur_pos != 0) { tail[0].update(player.direction); } if (player.direction == "Down" || player.direction == "Up") { System.Threading.Thread.Sleep(100); } else { System.Threading.Thread.Sleep(55); } //Checks if snake is in wall if (player.x == 0 || player.x == Console.WindowWidth - 1 || player.y == 0 || player.y == Console.WindowHeight - 1) { this.game_over(player); break; } //Checks if player is in snake tail for (int i = 0; i <= player.cur_pos - 1; i++) { if (player.x == tail[i].x && player.y == tail[i].y) { this.game_over(player); return; } } } }